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
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 -
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'~
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
Hi Hallex.
Looks great. Is there any ways to export to pdf without open the drawing (using ReadDWGfile(...
) method.
Thanks,
Hi andre,
I still have the same problem of converting the dwg file to pdf.
Could you please kindly show your code?
Thank you very much!
Oh my mistake...
I found my pdf files
They were in "C:\Users\Administrator\AppData\Local\Temp"
Thank Hallex and RBell!
I just copy the Hallex's code. It works but I thought it could not work before because I cannot find my pdf files.
But Hallex's code actually works!
The pdf files are hidden in somewhere in the computer. You may check it out with "CreatePDF" and see his code the line of "If PtObj.PlotToFile(Replace(acDoc.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then".
The variable of "FullName" is the path of the pdf files saved.
I think the tip was about using AutoCAD as a Windows service, not the PDF generation.
If anybody has a trip to use AutoCAD as a windows service, please, share
Hi !
this may be the right way for my problem. i want to use the exportpdf-function of autocad via vb.net.
but what means the .....
Imports System.IO ...................... <CommandMethod("pdfing")> _ Public Sub TestPDF() SaveToPDF("C:\Test\blah.dwg") End Sub
so current i did know how to find the defintion of
regards Jan
Can't find what you're looking for? Ask the community or share your knowledge.