Results 1 to 10 of 10

Thread: VB.Net - Print Paperspace to PDF

  1. #1
    Member
    Join Date
    2013-11
    Posts
    6
    Login to Give a bone
    0

    Default VB.Net - Print Paperspace to PDF

    Dear All,



    I can not figure out how to print my autocad drawings - all located in a same folder - to pdf from a Visual Studio App.

    So far, I can open a Autocad Drawing, but after that, can for activated the particular PaperSpace (called "A3 - ENGLISH").

    The PC3 file is Herve.pc3 and Herve.pmp.


    Here below the code I have but not working. I look on google but still can not figure out how to di it.
    Thank you
    Herve

    Code:
    Imports System
    Imports System.IO
    Imports System.Text
    Imports Autodesk.AutoCAD.Interop
    Imports Autodesk.AutoCAD.Interop.Common
    Imports System.Data.SqlClient
    Imports System.Data.OleDb
    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    Imports Excel = Microsoft.Office.Interop.Excel
    
    
    Module Mdl_Print
        Dim AcadApp As AcadApplication
        Dim AcadDoc As AcadDocument
    
        Public Sub PDF_Print()
    
            Dim dirInfo As New DirectoryInfo(Frm_Main.LblFolder1.Text)
            Dim fileInfo As FileInfo
    
            Dim AcadApp As AcadApplication = New AcadApplication
            AcadApp.Visible = True
            AcadApp.WindowState = AcWindowState.acMax
            Dim success As Boolean = False
    
    
            For Each fileInfo In dirInfo.GetFiles("*.dwg") 'dwg for Autocad
                AcadDoc = AcadApp.Documents.Open(fileInfo.FullName)
    
    
                Dim oSS As AcadSelectionSet
                oSS = AcadApp.ActiveDocument.SelectionSets.Add("oSS")
                oSS.Clear()
    
    
                Try
                    Dim ftype(0) As Int16
                    Dim fdata(0) As Object
                    ftype(0) = 410  'layout
                    fdata(0) = "A3 - ENGLISH"
    
                    oSS.Select(AcSelect.acSelectionSetAll, , , ftype, fdata)
    
                    Dim objPrefFiles As AcadPreferencesFiles
                    Dim PC3PathOld As String
                    Dim PC3PathNew As String
                    Dim PMPPathOld As String
                    Dim PMPPathNew As String
    
                    Dim PtConfigs As AcadPlotConfigurations
                    Dim PlotConfig As AcadPlotConfiguration
                    Dim PtObj As AcadPlot
                    Dim BackPlot As Object
    
                    'Set the preferences object
                    objPrefFiles = AcadDoc.Application.Preferences.Files
    
                    'Get the current Printer Config Path (pc3)
                    PC3PathOld = objPrefFiles.PrinterConfigPath
                    MessageBox.Show(PC3PathOld, "old Printer Config path (pc3)")
                    objPrefFiles.PrinterConfigPath = "C:\Users\ROBERT\Documents\Deb + Herve + Will\B - Herve\Technip\Autocad\1st project\OpenExcel\PC3 Files"
                    PC3PathNew = objPrefFiles.PrinterConfigPath
                    MessageBox.Show(PC3PathNew, "new Printer Config path (pc3)")
    
                    'Get the current Printer Desc Path (pmp)
                    PMPPathOld = objPrefFiles.PrinterDescPath
                    MessageBox.Show(PMPPathOld, "Printer Desc Path (pmp)")
                    objPrefFiles.PrinterDescPath = "C:\Users\ROBERT\Documents\Deb + Herve + Will\B - Herve\Technip\Autocad\1st project\OpenExcel\PMP Files"
                    PMPPathNew = objPrefFiles.PrinterDescPath
                    MessageBox.Show(PMPPathNew, "Printer Desc Path (pmp)")
    
    
    
                    PtObj = AcadDoc.Plot
                    AcadApp.ActiveDocument.AcPlotType.acExtents()
    
                    PtConfigs = AcadDoc.PlotConfigurations
                    ''Add a new plot configuration
                    PtConfigs.Add("PDF", False)
                    '
                    ''The plot config you created become active
                    PlotConfig = PtConfigs.Item("PDF")
                    
                    ''Use this method to set the scale
                    PlotConfig.StandardScale = AcPlotScale.acScaleToFit
                    ''Updates the plot
                    PlotConfig.RefreshPlotDeviceInfo()
    
                    'Here you specify the pc3 file you want to use
                    PlotConfig.ConfigName = "Herve.pc3"
    
                    'You can select the plot style table here
                    PlotConfig.StyleSheet = "monochrome.ctb"
    
                    'You can select the Scale Line Weight
                    PlotConfig.ScaleLineweights = True
    
                    'Specifies Paper Size
                    PlotConfig.CanonicalMediaName = "ISO_A3_(420.00_x_297.00_MM)"
    
                    'Specifies whether or not to plot using the plot styles
                    PlotConfig.PlotWithPlotStyles = True
    
                    BackPlot = AcadDoc.GetVariable("BACKGROUNDPLOT")
                    AcadDoc.SetVariable("BACKGROUNDPLOT", 0)
    
                    'Updates the plot
                    PlotConfig.RefreshPlotDeviceInfo()
                    MessageBox.Show("scale" & PlotConfig.StandardScale)
                    MessageBox.Show("canonical media name" & PlotConfig.CanonicalMediaName)
                    MessageBox.Show("config name" & PlotConfig.ConfigName)
                    MessageBox.Show("plot type" & PlotConfig.PlotType)
                    MessageBox.Show("scale line weight" & PlotConfig.ScaleLineweights)
    
                    'Now you can use the PlotTofile method
                    If PtObj.PlotToFile(Replace(AcadDoc.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then
                        AcadDoc.Utility.Prompt(vbLf + "PDF Was Created")
                    Else
                        AcadDoc.Utility.Prompt(vbLf + "PDF Creation Unsuccessful")
                    End If
    
                    'If you wish you can delete the plot configuration you created programmatically, and set the 'BACKGROUNDPLOT' system variable to its original status.
                    PtConfigs.Item("PDF").Delete()
                    PlotConfig = Nothing
                    AcadDoc.SetVariable("BACKGROUNDPLOT", BackPlot)
    
                    objPrefFiles.PrinterConfigPath = "C:\Users\ROBERT\AppData\Roaming\Autodesk\AutoCAD 2014\R19.1\enu\Plotters"
                    objPrefFiles.PrinterDescPath = "C:\Users\ROBERT\AppData\Roaming\Autodesk\AutoCAD 2014\R19.1\enu\Plotters\PMP File
    
                    oSS.Clear()
    
                    If AcadDoc.Saved Then
                        AcadDoc.Close(False)
                    End If
                    AcadDoc = Nothing
                    AcadApp.Quit()
    
                    AcadApp = Nothing
                    success = True
                Catch ex As Exception
                    MsgBox(ex.Message + vbLf + ex.StackTrace)
                    success = False
                End Try
                '            AcadDoc.Close()
                AcadDoc = Nothing
                '            AcadApp.Quit()
            Next
            AcadApp.Quit()
        End Sub

  2. #2
    Member
    Join Date
    2025-01
    Posts
    21
    Login to Give a bone
    0

    Default Re: VB.Net - Print Paperspace to PDF

    Did you ever get this sorted man?

    Looks like you're using the exact code I've finally managed to come across.

    I have it printing, if PlotConfig already there, just looking to create PlotConfig if not already in the drawing.

    Understand it's been some years, but I looked at automating AutoCAD 12 years ago, and only now have I got round to looking again and actually making progress. Although it seems difficult to get documentation on it, everything seems to .NET API which needs the drawing to open and then load a .dll and then run the command, COM/ObjectARX seems a lot nicer, and less code also. Hopefully speak soon.

  3. #3
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,576
    Login to Give a bone
    0

    Default Re: VB.Net - Print Paperspace to PDF

    Hi. Welcome to AUGI.
    It's not clear what you are trying to do. Are you trying to print without opening the file in the editor?
    C:> ED WORKING....


    LinkedIn

  4. #4
    Member
    Join Date
    2025-01
    Posts
    21
    Login to Give a bone
    0

    Default Re: VB.Net - Print Paperspace to PDF

    Hi Ed Jobe,

    Thanks for your response.

    I was just wondering if the posted got sorted because I've finally got it working, so I might be able to help.

    I also stated that my understanding for .NET API is that the drawing files opens, a .dll is loading and then commands ran. The COM/ObjectARX seems to suit my plan better that my application can load/open the drawing file and read/write data and also print. The code seems simpler too. But I touched on I've struggled with finding good working examples and maybe I've misunderstood.

    I would love it if the AutoCAD application could load in the background (hidden) and do the work, but acApp.visible = false doesn't seem to do that.

    I also want to take a look at RealDWG for computers that only have AutoCAD LT, but the TechSoft3D website doesn't seem to load properly.

  5. #5
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,771
    Login to Give a bone
    0

    Default Re: VB.Net - Print Paperspace to PDF

    Quote Originally Posted by atomicpetrie823619 View Post
    <snip>
    I also stated that my understanding for .NET API is that the drawing files opens, a .dll is loading and then commands ran. The COM/ObjectARX seems to suit my plan better that my application can load/open the drawing file and read/write data and also print. The code seems simpler too. But I touched on I've struggled with finding good working examples and maybe I've misunderstood.

    I would love it if the AutoCAD application could load in the background (hidden) and do the work, but acApp.visible = false doesn't seem to do that.

    I also want to take a look at RealDWG for computers that only have AutoCAD LT, but the TechSoft3D website doesn't seem to load properly.
    Consider Core Console.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 5860, Xeon W7-2495X, 128GB RAM, Dual PCIe 4.0 M.2 SSD (RAID 0), 20GB NVIDIA RTX 4000 ADA

  6. #6
    Member
    Join Date
    2025-01
    Posts
    21
    Login to Give a bone
    0

    Default Re: VB.Net - Print Paperspace to PDF

    BlackBox,

    Thank you for your response.

    I've had a quick search for AutoCAD Core Console and vb.net. As with all my searches for .net and AutoCAD programming it seems difficult to get clear documentation or examples.

    Does Core Console only work by targeting the accoreconsole.exe with subsequent switches to either run an AutoCAD built in command, a created .dll or LISP script?

    Can it be used how I currently use my ObjectARX/COM/Interop?

    For some reason the forum isn't letting me post code.

    Thank you.

    - - - Updated - - -

    This is my current code, can coreconsole be used with code like this?

    Code:
            'AutoCAD app
            Dim acadApp As AutoCAD.AcadApplication = GetObject(, "AutoCAD.Application")
            Try
                acadApp = GetObject(, "AutoCAD.Application")
            Catch ex As Exception
                acadApp = CreateObject("AutoCAD.Application")
            End Try
            'Dim acadApp As AutoCAD.AcadApplication = CreateObject("AutoCAD.Application")
            acadApp.Visible = True
    
            Dim acDoc = acadApp.Documents.Open("C:\Users\user\Desktop\Drawing.dwg", False)
    
            'Active doc
            'Dim acDoc As AutoCAD.AcadDocument = acadApp.ActiveDocument
    
            ' Create a selectionset to select only "INSERT" with attributes
            Dim ss As AutoCAD.AcadSelectionSet = acDoc.SelectionSets.Add("test")
            ss.Select(AutoCAD.AcSelect.acSelectionSetAll)
    
            For Each el In ss
                Try
                    Dim blk As AutoCAD.AcadBlockReference = el
                    'Iterate through the block attribute collection
                    For Each att As AutoCAD.AcadAttributeReference In blk.GetAttributes()
                        If att.TagString = "DRAWING_NO" Then
                            att.TextString = "DOC-001"
                        End If
                    Next
                Catch ex As Exception
    
                End Try
            Next
    
            'Delete the selection set
            ss.Delete()

  7. #7
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,771
    Login to Give a bone
    0

    Default Re: VB.Net - Print Paperspace to PDF

    Here's an example of how simple it is to leverage Core Console from Windows Shell menu:

    https://forums.augi.com/showthread.p...=1#post1357300


    And here's another example using LISP from within CAD, using the active session's current Profile:

    https://forums.augi.com/showthread.p...=1#post1339054


    Most native AutoCAD Commands will work OOTB, occasionally you'll need to take an extra step to say iterate LISP's (layoutlist) for plotting multi-layout drawings:

    https://www.theswamp.org/index.php?t...9440#msg609440


    HTH
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 5860, Xeon W7-2495X, 128GB RAM, Dual PCIe 4.0 M.2 SSD (RAID 0), 20GB NVIDIA RTX 4000 ADA

  8. #8
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,576
    Login to Give a bone
    0

    Default Re: VB.Net - Print Paperspace to PDF

    To post code in a reply, click on the Go Advanced button at the lower right. Then surround your code with code tags. The toolbar button looks like the pound sign, hashtag.

    I made a post at the swamp that shows how you can run accoreconsole.exe from a bat file. All you have to do is open Windows Explorer and drag/drop a bunch of files onto it.
    Last edited by Ed Jobe; 2025-02-05 at 10:50 PM.
    C:> ED WORKING....


    LinkedIn

  9. #9
    Member
    Join Date
    2025-01
    Posts
    21
    Login to Give a bone
    0

    Default Re: VB.Net - Print Paperspace to PDF

    Thank you both for your responses.

    So I think I was right, there isn't an ObjectARX/COM/Interop for AcCoreConsole itself; meaning I can't have the code in my Application doing the work.

    I would have to write a LISP script/routing that does the work, and load AcCoreConsole from my Application and tell it to run the LISP script/routine.

    If I've got that correct, in your opinion is LISP my best solution as I need to pass variable/information from my Application to AcCoreConsole?

    Thank you.

  10. #10
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,771
    Login to Give a bone
    0

    Default Re: VB.Net - Print Paperspace to PDF

    Quote Originally Posted by atomicpetrie823619 View Post
    Thank you both for your responses.

    So I think I was right, there isn't an ObjectARX/COM/Interop for AcCoreConsole itself; meaning I can't have the code in my Application doing the work.

    I would have to write a LISP script/routing that does the work, and load AcCoreConsole from my Application and tell it to run the LISP script/routine.

    If I've got that correct, in your opinion is LISP my best solution as I need to pass variable/information from my Application to AcCoreConsole?

    Thank you.
    You can ARX/NETLOAD a plugin's assembly into Core Console (as shown in my 3rd link above) - you just have to use ARX/.NET API (internal) in lieu of COM (external).

    For a simple 'plot' app, save yourself some overhead and use the native (likely ARX) Commands for that, if possible - that said, if you need to delete/import/reload layer states first, import named page setups, etc, then ARX/.NET API will be much faster, and increase the speedup gained with using Core Console to process multiple drawings in parallel (where PUBLISH processes drawings in series).

    Just use the right tool for the job.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 5860, Xeon W7-2495X, 128GB RAM, Dual PCIe 4.0 M.2 SSD (RAID 0), 20GB NVIDIA RTX 4000 ADA

Similar Threads

  1. Autolisp Code To Print Multiple Paperspace layout
    By mill3929974348 in forum AutoLISP
    Replies: 2
    Last Post: 2012-04-27, 09:10 PM
  2. Measure PaperSpace in PaperSpace
    By Avatart in forum AutoCAD General
    Replies: 16
    Last Post: 2008-10-16, 07:19 PM
  3. Text will not print or view in paperspace
    By stephen.coff in forum AutoCAD Annotation
    Replies: 2
    Last Post: 2007-09-05, 01:30 PM
  4. PRINT WINDOW & PRINT SETTINGS PER SHEET
    By neb1998 in forum Revit Architecture - Wish List
    Replies: 1
    Last Post: 2005-07-01, 07:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •