VBA AutoCAD Electrical 2024 Plot dwg to pdf

VBA AutoCAD Electrical 2024 Plot dwg to pdf

sonchai_n
Contributor Contributor
684 Views
6 Replies
Message 1 of 7

VBA AutoCAD Electrical 2024 Plot dwg to pdf

sonchai_n
Contributor
Contributor

I write code for plot all dwg of project to pdf. But code con't create all pdf of project .Received an incomplete pdf file. Plase help me. 

 

This my code . Sub ReadFileForPlotToPdf for Read dwg file of project and Sub PlotThisFileToPdf for plot pdf

 

Sub ReadFileForPlotToPdf()

Dim configName As String
Dim mediaName As String
Dim plotRotation As Integer
Dim plotType As Integer
Dim scaleSheet As Integer
Dim styleSheet As String
Dim plotWithStyles As Boolean
Dim plotHidden As Boolean
Dim plotViewportsFirst As Boolean

With ThisDrawing.ActiveLayout
configName = .configName
mediaName = .CanonicalMediaName
plotRotation = .plotRotation
plotType = .plotType
scaleSheet = .StandardScale
styleSheet = .styleSheet
plotWithStyles = .PlotWithPlotStyles
plotHidden = .plotHidden
plotViewportsFirst = .plotViewportsFirst
End With
Dim folderPath As String
folderPath = ThisDrawing.Path
Dim File As String
File = Dir(folderPath & "\*.dwg")
Dim FullFilename As String

Do While File <> ""
FullFilename = folderPath & "\" & File
If FullFilename <> ThisDrawing.FullName Then
ThisDrawing.Application.Documents.Open (FullFilename)
With ThisDrawing.ActiveLayout
.configName = configName
.CanonicalMediaName = mediaName
.CenterPlot = True
.plotRotation = plotRotation
.plotType = plotType
.StandardScale = scaleSheet
.styleSheet = styleSheet
.PlotWithPlotStyles = plotWithStyles
.plotHidden = plotHidden
.plotViewportsFirst = plotViewportsFirst
End With
'Save plot style
ThisDrawing.Save
'Plot thisdrawing to pdf
PlotThisFileToPdf
ThisDrawing.Close False
Else
'Plot file type to pdf
PlotThisFileToPdf
End If

File = Dir
If Emergency Then
File = ""
End If
Loop

End Sub

 

Sub PlotThisFileToPdf()
Dim File As String
File = ThisDrawing.FullName
Dim plotFileName As String
plotFileName = Left(File, Len(File) - 4) & ".pdf"
Dim result As Boolean
On Error Resume Next
result = ThisDrawing.plot.PlotToFile(plotFileName)
On Error GoTo 0
If result Then
Debug.Print Format(Now, "hh-nn-ss") & " PDF generated successfully for: " & GetFileName(plotFileName)
Else
Debug.Print Format(Now, "hh-nn-ss") & " Plot failed for: " & GetFileName(plotFileName)
End If
End Sub

 

Thank you.

0 Likes
Accepted solutions (1)
685 Views
6 Replies
Replies (6)
Message 2 of 7

norman.yuan
Mentor
Mentor

Firstly, please post code by using the button "</>", so the code would be easier to read.

 

You may want to explain what "...But code con't create all pdf of project .Received an incomplete pdf file. ..." mean. Do you mean some drawing files opened and plotted correctly, and some do not, or the PDF does not present the desired plotting area?

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 7

sonchai_n
Contributor
Contributor

I mean some DWG files can be plotted to PDF, but some cannot. When plotting the entire project to PDF, not all files are included in the output, even though the DWG files themselves can be opened normally without any issues. The problem only occurs during the plot-to-PDF process, resulting in incomplete PDFs.

0 Likes
Message 4 of 7

sonchai_n
Contributor
Contributor
Sub Button1_Click()
    Call ReadFileForPlotToPdf
End Sub

Sub ReadFileForPlotToPdf()
Dim configName As String
Dim mediaName As String
Dim plotRotation As Integer
Dim plotType As Integer
Dim scaleSheet As Integer
Dim styleSheet As String
Dim plotWithStyles As Boolean
Dim plotHidden As Boolean
Dim plotViewportsFirst As Boolean

With ThisDrawing.ActiveLayout
configName = .configName
mediaName = .CanonicalMediaName
plotRotation = .plotRotation
plotType = .plotType
scaleSheet = .StandardScale
styleSheet = .styleSheet
plotWithStyles = .PlotWithPlotStyles
plotHidden = .plotHidden
plotViewportsFirst = .plotViewportsFirst
End With
Dim folderPath As String
folderPath = ThisDrawing.Path
Dim File As String
File = Dir(folderPath & "\*.dwg")
Dim FullFilename As String

Do While File <> ""
FullFilename = folderPath & "\" & File
If FullFilename <> ThisDrawing.FullName Then
ThisDrawing.Application.Documents.Open (FullFilename)
With ThisDrawing.ActiveLayout
.configName = configName
.CanonicalMediaName = mediaName
.CenterPlot = True
.plotRotation = plotRotation
.plotType = plotType
.StandardScale = scaleSheet
.styleSheet = styleSheet
.PlotWithPlotStyles = plotWithStyles
.plotHidden = plotHidden
.plotViewportsFirst = plotViewportsFirst
End With
'Save plot style
ThisDrawing.Save
'Plot thisdrawing to pdf
PlotThisFileToPdf
ThisDrawing.Close False
Else
'Plot file type to pdf
PlotThisFileToPdf
End If

File = Dir
If Emergency Then
File = ""
End If
Loop

End Sub

 

Sub PlotThisFileToPdf()
Dim File As String
File = ThisDrawing.FullName
Dim plotFileName As String
plotFileName = Left(File, Len(File) - 4) & ".pdf"
Dim result As Boolean
On Error Resume Next
result = ThisDrawing.plot.PlotToFile(plotFileName)
On Error GoTo 0
If result Then
Debug.Print Format(Now, "hh-nn-ss") & " PDF generated successfully for: " & GetFileName(plotFileName)
Else
Debug.Print Format(Now, "hh-nn-ss") & " Plot failed for: " & GetFileName(plotFileName)
End If
End Sub

 

0 Likes
Message 5 of 7

norman.yuan
Mentor
Mentor

So, when you say the PDFs are incomplete, you mean, say, you have 10 drawings in the folder, and all 10 drawing have been opened in AutoCAD for plotting, but instead of getting 10 PDFs, you got less PDF files, right?

 

If so, obviously, something is not correct i the PlotThisFileToPdf() method, most likely one error occurred but your code did not catch it, because you use On "Error Resume Next". Have you taken a closer look in the immediate window to see if the "Plot failed for: ..." printed there?

 

Also, you might want to set system variable "BackgroundPlot" to 0 before the loop starts (and restore it to original value after PDF plotting is done).

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 7

sonchai_n
Contributor
Contributor

Yes i got less PDF files. 

I set  "BackgroundPlot" to 0  This solution is Ok

Thank you very must

0 Likes
Message 7 of 7

sonchai_n
Contributor
Contributor
Accepted solution
Sub PlotThisFileToPdf()
    Dim File As String
            File = ThisDrawing.FullName
            Dim plotFileName As String
            plotFileName = Left(File, Len(File) - 4) & ".plt.pdf"
            ThisDrawing.SetVariable "BACKGROUNDPLOT", 0

            Dim result As Boolean
            On Error Resume Next
            result = ThisDrawing.plot.PlotToFile(plotFileName)
            On Error GoTo 0
            
            If result Then
                Debug.Print Format(Now, "hh-nn-ss") & " PDF generated successfully for: " & GetFileName(plotFileName)
            Else
                Debug.Print Format(Now, "hh-nn-ss") & " Plot failed for: " & GetFileName(plotFileName)
            End If
End Sub
0 Likes