• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Member
    Posts: 4
    Registered: ‎05-24-2012

    Export pdf from VB.net

    1043 Views, 3 Replies
    05-24-2012 06:32 PM

    Hi

     

      I'm trying to make an application that would take a drawing(dwg) and convert it to pdf.  Since I can do it directly in autocad 2013, i presume that there should be a function in the API, that i could use to do the same.  So far, i tried with AcadDocument.export(filename, FileExtension, AcadSelectionSet) with no luck. I cannot put PDF as a valid file extension.

     

    Here's my code

     

    Public sub SaveToPDF(ByVal psfilename As String)

    Dim oAutocad As AutoCAD.AcadApplication = New AutoCAD.AcadApplication()

    Dim oAcadDoc As AutoCAD.AcadDocument = oAutocad.Documents.Open(psfilename, True)

    Dim sFolder As String = psfilename.Substring(0, psfilename.LastIndexOf("\") + 1)

    Dim sFilename As String = psfilename.Substring(psfilename.LastIndexOf("\") + 1, (psfilename.Length - psfilename.LastIndexOf("\")) - (psfilename.Length - psfilename.LastIndexOf(".") + 1))

    Dim oSS As AcadSelectionSet = Nothing

    Try  oSS = oAutocad.ActiveDocument.SelectionSets.Add("MySet")

         oSS.Select(AcSelect.acSelectionSetAll)

         oAutocad.ActiveDocument.Export(sFolder & sFilename & ".pdf", ".pdf", oSS)

         oSS.Delete() Catch ex As Exception  success = False  oSS.Delete()  

         oSS = Nothing

    End Try

     

    For Each Doc In oAutocad.Documents

      Doc.Close(False)

    Next

    oAutocad.Quit()

    oAutocad = Nothing

    end sub

     

    Does anyone have an idea?

     

    Thanks

    Please use plain text.
    *Expert Elite*
    Posts: 6,645
    Registered: ‎06-29-2007

    Re: Export pdf from VB.net

    05-24-2012 11:32 PM in reply to: andre.fortin

    Hi,

     

    you will have to go through the plot-functionality using the "DWG to PDF.pc3"

    That makes also more sense for setting the page-size you like to have as result.

     

    - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,371
    Registered: ‎10-08-2008

    Re: Export pdf from VB.net

    05-25-2012 04:55 AM in reply to: andre.fortin

    See if this helps

        Imports System.IO
         ......................
        <CommandMethod("pdfing")> _
        Public Sub TestPDF()
            SaveToPDF("C:\Test\blah.dwg")
        End Sub
        Public Sub SaveToPDF(ByVal psfilename As String)
    
            Dim oAutocad As AcadApplication = New AcadApplication
    
            oAutocad.Visible = True
    
            oAutocad.WindowState = AcWindowState.acMax
    
            Dim success As Boolean = False
    
            Dim oAcadDoc As AcadDocument = oAutocad.Documents.Open(psfilename, False, "")
    
            'Dim sFolder As String = Path.GetDirectoryName(psfilename) 'psfilename.Substring(0, psfilename.LastIndexOf("\") + 1)
    
            'Dim sFilename As String = Path.GetFileNameWithoutExtension(psfilename) 'psfilename.Substring(psfilename.LastIndexOf("\") + 1, (psfilename.Length - psfilename.LastIndexOf("\")) - (psfilename.Length - psfilename.LastIndexOf(".") + 1))
    
            Dim oSS As AcadSelectionSet = Nothing
    
            Try
               
                Dim ftype(0) As Short
                ftype(0) = 410
                Dim fdata(0) As Object
                fdata(0) = "Model"
                oAutocad.ZoomExtents()
    
                oSS = oAcadDoc.SelectionSets.Add("MySet")
    
                oSS.Select(AcSelect.acSelectionSetAll, Nothing, Nothing, ftype, fdata)
    
                CreatePDF(oAcadDoc)
         
                If oAcadDoc.Saved Then
                    oAcadDoc.Close(False)
                End If
                oAcadDoc = Nothing
             
                oAutocad.Quit()
    
                oAutocad = Nothing
                success = True
            Catch ex As Exception
                MsgBox(ex.Message + vbLf + ex.StackTrace)
                success = False
            Finally
    
                MsgBox(vbLf + "Program ended up with result of: " + success.ToString)
    
            End Try
    
        End Sub
        ''-------------------------------------------------------------''
        Sub CreatePDF(acDoc As AcadDocument) 'by RBell
    
            Dim PtConfigs As AcadPlotConfigurations
            Dim PlotConfig As AcadPlotConfiguration
            Dim PtObj As AcadPlot
            Dim BackPlot As Object
    
            'Create a new plot configutrarion with all needed parameters
            PtObj = acDoc.Plot
            PtConfigs = acDoc.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 = "DWG To PDF.pc3"
            'You can select the plot style table here
            PlotConfig.StyleSheet = "acad.ctb"
            'Specifies whether or not to plot using the plot styles
            PlotConfig.PlotWithPlotStyles = True
    
            'If you are going to create pdf files in a batch mode,
            'I would recommend to turn off the BACKGROUNDPLOT system variable,
            'so autocad will not continue to do anything until finishes
            'the pdf creation
            BackPlot = acDoc.GetVariable("BACKGROUNDPLOT")
            acDoc.SetVariable("BACKGROUNDPLOT", 0)
            'Updates the plot
            PlotConfig.RefreshPlotDeviceInfo()
            ''Acad.ActiveDocument.ActiveLayout.CopyFrom(plotconfig)
            'Now you can use the PlotTofile method
            If PtObj.PlotToFile(Replace(acDoc.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then
                acDoc.Utility.Prompt(vbLf + "PDF Was Created")
            Else
                acDoc.Utility.Prompt(vbLf + "PDF Creation Unsuccessful")
            End If
            'If you wish you can delete th plot configuration you created
            'programmatically, and set the 'BACKGROUNDPLOT' system variable
            'to its original status.
            PtConfigs.Item("PDF").Delete()
            PlotConfig = Nothing
            acDoc.SetVariable("BACKGROUNDPLOT", BackPlot)
    
        End Sub

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Member
    Posts: 4
    Registered: ‎05-24-2012

    Re: Export pdf from VB.net

    05-25-2012 10:29 AM in reply to: andre.fortin

    Thanks guys

     

      I've been able to convert my drawing in a pdf.  The next step is turning my application into a windows service.  i'm not able to start autocad.  I see the process comming up in task manager but the next line ain't executed.  Is there a way to open autocad in a windows service application?

     

    Thanks

     

    Andre

    Please use plain text.