VBA AUTOCAD BATCH PRINTING OK WITH BULLZIP ver 9.8.1599
Posted: Sun Jul 07, 2013 8:03 am
Folks,
I'm sharing this piece of VBA code I wrote for batch printing from autocad to pdf using Bullzip ver 9.8.1599.
As I've tested (OS Win7 x64, Core I7), Bullzip is able to process 138 ISO A3 pages in around 4 minutes, autocad don't even blink ('ThisDrawing.Plot.PlotToDevice' returned true in each one of the 138 plot actions, nice!)
I hope you find it useful.
Cheers.
-----------------------------------------------------------------------------------------------------------------
Attribute VB_Name = "PRINT_PDF_ACAD"
-----------------------------------------------------------------------------------------------------------------
Option Explicit
-----------------------------------------------------------------------------------------------------------------
Sub aCAD_VBA_PRINT()
'FILE I/O VARS
Dim fso As New FileSystemObject
Dim iniFile As Variant
Dim iniFileDate As Date
'GENERIC PROCESSING VARs
Dim Flag As Boolean
Dim lastPage As Integer
Dim eachPage As Integer
Dim cntPage As Integer
Dim blnPlotSuccessful As Boolean
Dim PageSS As AcadSelectionSet
Dim Page As AcadBlockReference
Dim Page_att As Variant
'QUERY CONDITION VARs
Dim grpCode() As Integer
Dim dataVal() As Variant
Dim ptArr As Variant
Dim dblPoints(11) As Double
'PRINT VARS
Dim PlotConfigurationsCollection As AcadPlotConfigurations
Dim PlotConfigurationObject As AcadPlotConfiguration
Dim dblLowerLeftCorner(1) As Double
Dim dblUpperRightCorner(1) As Double
Dim BackPlot As Variant
'BULLZIP SPECIFICS
Dim Output As String
Dim printerName As String
Dim Obj As Variant
Dim runonce As Variant
'COMMENTS-------------------------------------------------------------------------------------------------------------
'VBA BATCH PRINTING IN ACAD IS A COMPLEX TASK PER-SE, SO LETS KEEP IT SIMPLE BY MAKING SURE THAT:
'a) BULLZIP PRINTER IS PROPERLY INSTALLED AND SET IT AS DEFAULT PRINTER
'b) BEFORE RUNNING THIS CODE, MAKE SURE THE FOLLOWING LIBRARIES ARE SELECTED (ON VBA EDITOR, TOOLS > REFERENCES)
'Bullzip
'Bullzip 1.0 Type Library
'Bullzip PDF Writer Automation
'Bullzip Dictionary 1.0 Type Library
'Bullzip PdfWriter Lib
'Microsoft Scripting Runtime
'****THESE CANNOT BE LOADED BY CODE??****
'c) I RECOMMEND TO DISABLE aCAD AUTOSAVE WHILE RUNNING THIS CODE (DO IT THROUGH aCAD GUI)
'---------------------------------------------------------------------------------------------------------------------
'ACAD SPECIFICS-------------------------------------------------------------------------------------------------------
ThisDrawing.ActiveSpace = acModelSpace
BackPlot = ThisDrawing.GetVariable("BACKGROUNDPLOT")
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
Set PlotConfigurationsCollection = ThisDrawing.PlotConfigurations
On Error Resume Next
'BULLZIP PDF PRINTER PRELIMINARS--------------------------------------------------------------------------------------
Set Obj = CreateObject("Bullzip.PDFPrinterSettings")
printerName = Obj.GetPrinterName
runonce = Obj.GetSettingsFileName(True)
Set iniFile = Nothing
Set iniFile = fso.GetFile(runonce)
If Not iniFile Is Nothing Then
iniFile.Delete
End If
Obj.Init
Obj.SetValue "ShowSettings", "never"
Obj.SetValue "ShowPDF", "no"
Obj.SetValue "WatermarkText", ""
Obj.SetValue "ShowProgress", "no"
Obj.SetValue "ShowProgressFinished", "no"
Obj.SetValue "SuppressErrors", "yes"
Obj.SetValue "ConfirmOverwrite", "no"
Obj.WriteSettings True 'WRITE CONFIG TO PDF SPOOL. TARGET FILE IS DEFINED AND SET LATER ON
'---------------------------------------------------------------------------------------------------------------------
'BELOW, MY APPLICATION SPECIFICS, THIS SHOULD BE ADAPTED TO EACH CASE, MEANING THAT:
'a) SETUP BY CODE (LIKE THISONE) THE 'PLOT CONFIGURATION' FOR EACH PAGE (ISO A3, 1:1, MODELSPACE IN THIS EXAMPLE), OR
'b) SETUP THE 'PLOT CONFIGURATION' FOR EACH PAGE MANUALLY THROUGH THE aCAD GUI AND THEN RUN THIS SCRIPT.
'---------------------------------------------------------------------------------------------------------------------
'DETECT A SPECIFIC BLOCK REF THAT HOLDS ALL ISO A3 FRAME--------------------------------------------------------------
ReDim grpCode(1): ReDim dataVal(1)
grpCode(0) = 410
grpCode(1) = 2
dataVal(0) = "Model"
dataVal(1) = "PSC_ROT_MA3" 'AN SPECIFIC BLOCK REF HOLDS EACH PAGE
ThisDrawing.SelectionSets("pageSS").Delete: Err.Clear
Set PageSS = ThisDrawing.SelectionSets.Add("pageSS")
PageSS.Select acSelectionSetAll, , , grpCode, dataVal 'GET ALL BLOCK REFS THAT MATCHES (>ALL PAGES)
'MAIN LOOP (PAGExPAGE-------------------------------------------------------------------------------------------------
cntPage = 0
For Each Page In PageSS
'THE BLOCK WAS DEFINED WITH A SINGLE ATTRIBUTE USED TO STORE THE PAGE NUMBER
Page_att = Page.GetAttributes
eachPage = Val(Page_att(0).TextString)
'HEART BIT
cntPage = cntPage + 1
ThisDrawing.Utility.Prompt vbCrLf & "Processing Page '" & eachPage & "' (" & cntPage & " of " & PageSS.Count & ")"
'NOTE THAT I'AM USING 1:1 SCALE
dblLowerLeftCorner(0) = Page.InsertionPoint(0) + 0 'X OFFSET FROM THE BLOCKREF INSERTION POINT
dblLowerLeftCorner(1) = Page.InsertionPoint(1) - 53 'Y OFFSET FROM THE BLOCKREF INSERTION POINT
dblUpperRightCorner(0) = Page.InsertionPoint(0) + 420 '420 WIDTH |
dblUpperRightCorner(1) = Page.InsertionPoint(1) - 350 '350-53=297 HEIGHT | THAT'S ISO A3 1:1 SCALE
'CREATE or (UPDATE IF EXISTS) THE PLOT CONFIG 'H000' (FROM 001 TO 999 PAGES)
PlotConfigurationsCollection("H" & Format(eachPage, "000")).Delete: Err.Clear
Set PlotConfigurationObject = PlotConfigurationsCollection.Add("H" & Format(eachPage, "000"), True)
With PlotConfigurationObject 'STORE THE PLOT CONFIG, THE USER MIGHT WANT TO PRINT MANUALLY
.ConfigName = "Bullzip PDF Printer"
.CanonicalMediaName = "A3"
.PlotHidden = False
.PlotRotation = ac90degrees
.PlotViewportBorders = False
.PlotViewportsFirst = True
.PlotWithLineweights = True
.PlotWithPlotStyles = True
.ShowPlotStyles = False
.StandardScale = ac1_1
.StyleSheet = "TGS.ctb"
.UseStandardScale = True
.CenterPlot = True
.PaperUnits = acMillimeters
.SetWindowToPlot dblLowerLeftCorner, dblUpperRightCorner
.PlotType = acWindow
End With
With ThisDrawing.ActiveLayout 'SET CURRENT 'PLOT CONFIG' AS ACTIVE LAYOUT AND PLOT IT
.ConfigName = "Bullzip PDF Printer"
.CanonicalMediaName = "A3"
.PlotHidden = False
.PlotRotation = ac90degrees
.PlotViewportBorders = False
.PlotViewportsFirst = True
.PlotWithLineweights = True
.PlotWithPlotStyles = True
.ShowPlotStyles = False
.StandardScale = ac1_1
.StyleSheet = "TGS.ctb"
.UseStandardScale = True
.CenterPlot = True
.PaperUnits = acMillimeters
.SetWindowToPlot dblLowerLeftCorner, dblUpperRightCorner
.PlotType = acWindow
End With
'HEART BIT
ThisDrawing.Utility.Prompt vbCrLf
ThisDrawing.Utility.Prompt vbCrLf & "Plotting Page '" & PlotConfigurationObject.Name & "' (of " & PlotConfigurationsCollection.Count & ")"
'TARGET FILE NAME
Output = Replace(ThisDrawing.FullName, ".dwg", "_" & PlotConfigurationObject.Name) & ".pdf"
'BULLZIP PDF PRINTER SPECIFICS------------------------------------------------------------------------------------
Set iniFile = Nothing
Set iniFile = fso.GetFile(runonce)
If Not iniFile Is Nothing Then
Flag = WaitAndCheck("0:00:10") 'MAKE SURE LAST PRINT ACTION WAS COMPLETED (runonce.ini NOT PRESENT)
iniFile.Delete
Err.Clear
End If
Obj.SetValue "Output", Output 'SET OUTPUT FILE NAME
Obj.WriteSettings True 'WRITE CONFIG TO PDF SPOOL
Set iniFile = Nothing 'EACH (BULLZIP) WRITE SETTING ACTION CREATES runonce.ini FILE
Set iniFile = fso.GetFile(runonce) 'EACH PRINT ACTION DELETES runonce.ini FILE
If iniFile Is Nothing Then
Flag = WaitAndCheck("0:00:05")
Set iniFile = fso.GetFile(runonce)
If iniFile Is Nothing Then
MsgBox ("Cannot create <runonce.ini> for '" & PlotConfigurationObject.Name & "'. Abort.")
ThisDrawing.SetVariable "BACKGROUNDPLOT", BackPlot
On Error GoTo 0
Exit Sub
End If
End If
'PRINT------------------------------------------------------------------------------------------------------------
blnPlotSuccessful = False '(aCAD) RETURNS TRUE/FALSE UPON PLOT RESULT
blnPlotSuccessful = ThisDrawing.Plot.PlotToDevice
If Not blnPlotSuccessful Then
Flag = WaitAndCheck("0:00:20") 'WAIT 20 secs & TRY AGAIN
blnPlotSuccessful = ThisDrawing.Plot.PlotToDevice
If Not blnPlotSuccessful Then
ThisDrawing.Regen acAllViewports 'REGEN AND WAIT 50 secs & TRY AGAIN
Flag = WaitAndCheck("0:00:50")
blnPlotSuccessful = ThisDrawing.Plot.PlotToDevice
If Not blnPlotSuccessful Then
MsgBox ("Cannot print page '" & PlotConfigurationObject.Name & "'. Abort.")
ThisDrawing.SetVariable "BACKGROUNDPLOT", BackPlot
On Error GoTo 0
Exit Sub 'GIVE UP!
End If
End If
End If
Next Page
'EXIT-----------------------------------------------------------------------------------------------------------------
ThisDrawing.SetVariable "BACKGROUNDPLOT", BackPlot
On Error GoTo 0
MsgBox ("Done!")
End Sub
-----------------------------------------------------------------------------------------------------------------
Function WaitAndCheck(xDelay As String) As Boolean
Dim startTime As Date
Dim stopTime As Date
startTime = Now
stopTime = startTime + TimeValue(xDelay)
Do While Now < stopTime
DoEvents
Loop
WaitAndCheck = True
End Function
I'm sharing this piece of VBA code I wrote for batch printing from autocad to pdf using Bullzip ver 9.8.1599.
As I've tested (OS Win7 x64, Core I7), Bullzip is able to process 138 ISO A3 pages in around 4 minutes, autocad don't even blink ('ThisDrawing.Plot.PlotToDevice' returned true in each one of the 138 plot actions, nice!)
I hope you find it useful.
Cheers.
-----------------------------------------------------------------------------------------------------------------
Attribute VB_Name = "PRINT_PDF_ACAD"
-----------------------------------------------------------------------------------------------------------------
Option Explicit
-----------------------------------------------------------------------------------------------------------------
Sub aCAD_VBA_PRINT()
'FILE I/O VARS
Dim fso As New FileSystemObject
Dim iniFile As Variant
Dim iniFileDate As Date
'GENERIC PROCESSING VARs
Dim Flag As Boolean
Dim lastPage As Integer
Dim eachPage As Integer
Dim cntPage As Integer
Dim blnPlotSuccessful As Boolean
Dim PageSS As AcadSelectionSet
Dim Page As AcadBlockReference
Dim Page_att As Variant
'QUERY CONDITION VARs
Dim grpCode() As Integer
Dim dataVal() As Variant
Dim ptArr As Variant
Dim dblPoints(11) As Double
'PRINT VARS
Dim PlotConfigurationsCollection As AcadPlotConfigurations
Dim PlotConfigurationObject As AcadPlotConfiguration
Dim dblLowerLeftCorner(1) As Double
Dim dblUpperRightCorner(1) As Double
Dim BackPlot As Variant
'BULLZIP SPECIFICS
Dim Output As String
Dim printerName As String
Dim Obj As Variant
Dim runonce As Variant
'COMMENTS-------------------------------------------------------------------------------------------------------------
'VBA BATCH PRINTING IN ACAD IS A COMPLEX TASK PER-SE, SO LETS KEEP IT SIMPLE BY MAKING SURE THAT:
'a) BULLZIP PRINTER IS PROPERLY INSTALLED AND SET IT AS DEFAULT PRINTER
'b) BEFORE RUNNING THIS CODE, MAKE SURE THE FOLLOWING LIBRARIES ARE SELECTED (ON VBA EDITOR, TOOLS > REFERENCES)
'Bullzip
'Bullzip 1.0 Type Library
'Bullzip PDF Writer Automation
'Bullzip Dictionary 1.0 Type Library
'Bullzip PdfWriter Lib
'Microsoft Scripting Runtime
'****THESE CANNOT BE LOADED BY CODE??****
'c) I RECOMMEND TO DISABLE aCAD AUTOSAVE WHILE RUNNING THIS CODE (DO IT THROUGH aCAD GUI)
'---------------------------------------------------------------------------------------------------------------------
'ACAD SPECIFICS-------------------------------------------------------------------------------------------------------
ThisDrawing.ActiveSpace = acModelSpace
BackPlot = ThisDrawing.GetVariable("BACKGROUNDPLOT")
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
Set PlotConfigurationsCollection = ThisDrawing.PlotConfigurations
On Error Resume Next
'BULLZIP PDF PRINTER PRELIMINARS--------------------------------------------------------------------------------------
Set Obj = CreateObject("Bullzip.PDFPrinterSettings")
printerName = Obj.GetPrinterName
runonce = Obj.GetSettingsFileName(True)
Set iniFile = Nothing
Set iniFile = fso.GetFile(runonce)
If Not iniFile Is Nothing Then
iniFile.Delete
End If
Obj.Init
Obj.SetValue "ShowSettings", "never"
Obj.SetValue "ShowPDF", "no"
Obj.SetValue "WatermarkText", ""
Obj.SetValue "ShowProgress", "no"
Obj.SetValue "ShowProgressFinished", "no"
Obj.SetValue "SuppressErrors", "yes"
Obj.SetValue "ConfirmOverwrite", "no"
Obj.WriteSettings True 'WRITE CONFIG TO PDF SPOOL. TARGET FILE IS DEFINED AND SET LATER ON
'---------------------------------------------------------------------------------------------------------------------
'BELOW, MY APPLICATION SPECIFICS, THIS SHOULD BE ADAPTED TO EACH CASE, MEANING THAT:
'a) SETUP BY CODE (LIKE THISONE) THE 'PLOT CONFIGURATION' FOR EACH PAGE (ISO A3, 1:1, MODELSPACE IN THIS EXAMPLE), OR
'b) SETUP THE 'PLOT CONFIGURATION' FOR EACH PAGE MANUALLY THROUGH THE aCAD GUI AND THEN RUN THIS SCRIPT.
'---------------------------------------------------------------------------------------------------------------------
'DETECT A SPECIFIC BLOCK REF THAT HOLDS ALL ISO A3 FRAME--------------------------------------------------------------
ReDim grpCode(1): ReDim dataVal(1)
grpCode(0) = 410
grpCode(1) = 2
dataVal(0) = "Model"
dataVal(1) = "PSC_ROT_MA3" 'AN SPECIFIC BLOCK REF HOLDS EACH PAGE
ThisDrawing.SelectionSets("pageSS").Delete: Err.Clear
Set PageSS = ThisDrawing.SelectionSets.Add("pageSS")
PageSS.Select acSelectionSetAll, , , grpCode, dataVal 'GET ALL BLOCK REFS THAT MATCHES (>ALL PAGES)
'MAIN LOOP (PAGExPAGE-------------------------------------------------------------------------------------------------
cntPage = 0
For Each Page In PageSS
'THE BLOCK WAS DEFINED WITH A SINGLE ATTRIBUTE USED TO STORE THE PAGE NUMBER
Page_att = Page.GetAttributes
eachPage = Val(Page_att(0).TextString)
'HEART BIT
cntPage = cntPage + 1
ThisDrawing.Utility.Prompt vbCrLf & "Processing Page '" & eachPage & "' (" & cntPage & " of " & PageSS.Count & ")"
'NOTE THAT I'AM USING 1:1 SCALE
dblLowerLeftCorner(0) = Page.InsertionPoint(0) + 0 'X OFFSET FROM THE BLOCKREF INSERTION POINT
dblLowerLeftCorner(1) = Page.InsertionPoint(1) - 53 'Y OFFSET FROM THE BLOCKREF INSERTION POINT
dblUpperRightCorner(0) = Page.InsertionPoint(0) + 420 '420 WIDTH |
dblUpperRightCorner(1) = Page.InsertionPoint(1) - 350 '350-53=297 HEIGHT | THAT'S ISO A3 1:1 SCALE
'CREATE or (UPDATE IF EXISTS) THE PLOT CONFIG 'H000' (FROM 001 TO 999 PAGES)
PlotConfigurationsCollection("H" & Format(eachPage, "000")).Delete: Err.Clear
Set PlotConfigurationObject = PlotConfigurationsCollection.Add("H" & Format(eachPage, "000"), True)
With PlotConfigurationObject 'STORE THE PLOT CONFIG, THE USER MIGHT WANT TO PRINT MANUALLY
.ConfigName = "Bullzip PDF Printer"
.CanonicalMediaName = "A3"
.PlotHidden = False
.PlotRotation = ac90degrees
.PlotViewportBorders = False
.PlotViewportsFirst = True
.PlotWithLineweights = True
.PlotWithPlotStyles = True
.ShowPlotStyles = False
.StandardScale = ac1_1
.StyleSheet = "TGS.ctb"
.UseStandardScale = True
.CenterPlot = True
.PaperUnits = acMillimeters
.SetWindowToPlot dblLowerLeftCorner, dblUpperRightCorner
.PlotType = acWindow
End With
With ThisDrawing.ActiveLayout 'SET CURRENT 'PLOT CONFIG' AS ACTIVE LAYOUT AND PLOT IT
.ConfigName = "Bullzip PDF Printer"
.CanonicalMediaName = "A3"
.PlotHidden = False
.PlotRotation = ac90degrees
.PlotViewportBorders = False
.PlotViewportsFirst = True
.PlotWithLineweights = True
.PlotWithPlotStyles = True
.ShowPlotStyles = False
.StandardScale = ac1_1
.StyleSheet = "TGS.ctb"
.UseStandardScale = True
.CenterPlot = True
.PaperUnits = acMillimeters
.SetWindowToPlot dblLowerLeftCorner, dblUpperRightCorner
.PlotType = acWindow
End With
'HEART BIT
ThisDrawing.Utility.Prompt vbCrLf
ThisDrawing.Utility.Prompt vbCrLf & "Plotting Page '" & PlotConfigurationObject.Name & "' (of " & PlotConfigurationsCollection.Count & ")"
'TARGET FILE NAME
Output = Replace(ThisDrawing.FullName, ".dwg", "_" & PlotConfigurationObject.Name) & ".pdf"
'BULLZIP PDF PRINTER SPECIFICS------------------------------------------------------------------------------------
Set iniFile = Nothing
Set iniFile = fso.GetFile(runonce)
If Not iniFile Is Nothing Then
Flag = WaitAndCheck("0:00:10") 'MAKE SURE LAST PRINT ACTION WAS COMPLETED (runonce.ini NOT PRESENT)
iniFile.Delete
Err.Clear
End If
Obj.SetValue "Output", Output 'SET OUTPUT FILE NAME
Obj.WriteSettings True 'WRITE CONFIG TO PDF SPOOL
Set iniFile = Nothing 'EACH (BULLZIP) WRITE SETTING ACTION CREATES runonce.ini FILE
Set iniFile = fso.GetFile(runonce) 'EACH PRINT ACTION DELETES runonce.ini FILE
If iniFile Is Nothing Then
Flag = WaitAndCheck("0:00:05")
Set iniFile = fso.GetFile(runonce)
If iniFile Is Nothing Then
MsgBox ("Cannot create <runonce.ini> for '" & PlotConfigurationObject.Name & "'. Abort.")
ThisDrawing.SetVariable "BACKGROUNDPLOT", BackPlot
On Error GoTo 0
Exit Sub
End If
End If
'PRINT------------------------------------------------------------------------------------------------------------
blnPlotSuccessful = False '(aCAD) RETURNS TRUE/FALSE UPON PLOT RESULT
blnPlotSuccessful = ThisDrawing.Plot.PlotToDevice
If Not blnPlotSuccessful Then
Flag = WaitAndCheck("0:00:20") 'WAIT 20 secs & TRY AGAIN
blnPlotSuccessful = ThisDrawing.Plot.PlotToDevice
If Not blnPlotSuccessful Then
ThisDrawing.Regen acAllViewports 'REGEN AND WAIT 50 secs & TRY AGAIN
Flag = WaitAndCheck("0:00:50")
blnPlotSuccessful = ThisDrawing.Plot.PlotToDevice
If Not blnPlotSuccessful Then
MsgBox ("Cannot print page '" & PlotConfigurationObject.Name & "'. Abort.")
ThisDrawing.SetVariable "BACKGROUNDPLOT", BackPlot
On Error GoTo 0
Exit Sub 'GIVE UP!
End If
End If
End If
Next Page
'EXIT-----------------------------------------------------------------------------------------------------------------
ThisDrawing.SetVariable "BACKGROUNDPLOT", BackPlot
On Error GoTo 0
MsgBox ("Done!")
End Sub
-----------------------------------------------------------------------------------------------------------------
Function WaitAndCheck(xDelay As String) As Boolean
Dim startTime As Date
Dim stopTime As Date
startTime = Now
stopTime = startTime + TimeValue(xDelay)
Do While Now < stopTime
DoEvents
Loop
WaitAndCheck = True
End Function