I have a part of code in VBA for printing files in PDF
I would add a line to acaddoc.lsp to run this routine each time a specific file is opened.
I would add this specific drawing to task scheduler
It works almost perfectly. The drawings are printed, but not taking into account the printing specifications (size, CTB, rotation)
Could someone check my code and see where my error is? I guess it plotConfig the problem
Thank you
Public Sub DoPdfPlot()
Dim backPlot As Integer
Dim fileNames() As String
Dim oPath As String
Dim dPath As String
Dim plotter As String
'Setting background plot variable
backPlot = ThisDrawing.GetVariable("BACKGROUNDPLOT")
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
'Original path -> Where DWG are
oPath = "C:\Temp\architecture\"
'Destination path -> Where PDF go
dPath = "C:\Temp\architecture\pdf\"
fileNames = GetFileNames(oPath)
Dim fName As String
Dim pdfName As String
Dim dwg As AcadDocument
Dim i As Integer
For i = 0 To UBound(fileNames)
fName = fileNames(i)
Dim splitedFileName() As String
Dim brutName As String
''Name de PDF "[DWGName].pdf"
splitedFileName = Split(fName, ".")
brutName = splitedFileName(0)
pdfName = dPath & brutName & ".pdf" ''Determine PDF name based on DWG file name
Set dwg = AcadApplication.Documents.Open(fName)
PlotToPdf dwg, pdfName
dwg.Close False
Next
ThisDrawing.SetVariable "BACKGROUNDPLOT", backPlot
End Sub
Private Function GetFileNames(path As String) As String()
Dim fileNames() As String
Dim i As Integer
Dim file As String
''Get all DWG file from the Original Path
file = Dir(path & "*.dwg")
While file <> ""
ReDim Preserve fileNames(i)
fileNames(i) = file
i = i + 1
file = Dir
Wend
GetFileNames = fileNames
End Function
Private Sub PlotToPdf(dwg As AcadDocument, pdfFile As String)
Dim ptObj As AcadPlot
Dim ptConfigs As AcadPlotConfigurations
Dim plotConfig As AcadPlotConfiguration
Dim canonicalName As String
'Create a new plot configuration with all needed parameters
Set ptObj = dwg.Plot
Set ptConfigs = dwg.PlotConfigurations
'Add a new plot configuration
Set plotConfig = ptConfigs.Add("PDF", True)
plotConfig.ConfigName = "AutoCAD PDF (Web and Mobile).pc3"
plotConfig.UseStandardScale = False
plotConfig.StandardScale = acScaleToFit
plotConfig.PlotRotation = ac0degrees
plotConfig.PlotType = acExtents
plotConfig.CanonicalMediaName = "UserDefinedImperial (48.00 x 36.00Inches)"
plotConfig.CenterPlot = True
plotConfig.StyleSheet = "C:\Users\KI-0052-SI\AppData\Roaming\Autodesk\AutoCAD 2021\R24.0\enu\Plotters\Plot Styles\Acad.ctb"
plotConfig.PlotWithPlotStyles = True
'Updates the plot
plotConfig.RefreshPlotDeviceInfo
Dim success As Boolean
success = ptObj.PlotToFile(pdfFile, plotConfig.ConfigName)
End Sub