Automated print from VBA using a saved page setup

Automated print from VBA using a saved page setup

Anonymous
Not applicable
13,071 Views
9 Replies
Message 1 of 10

Automated print from VBA using a saved page setup

Anonymous
Not applicable
Hi all..

i have a small program in VBA where i run some procedures .

The program reads in the files i select and then work from that.

I want to add an automatic plot to PDF function, but i'm struggling a bit to get the syntaxes right. I have tried it with some send commands and it works but i'd want to do it properly using VBA.

Anyways, this is roughly what i'd want to do:



(my program already opens every drawing automatic in sequence)

-( drawing is opened)

-copy page setup from one file i have saved/manually enter the plot styles. ( i want to use the DWGToPDF.pc3 config)

-print file



Anyone have any code/ideas for this? any help would be appreciated...

-(close drawing) and then loop
0 Likes
13,072 Views
9 Replies
Replies (9)
Message 2 of 10

Mario-Villada
Advocate
Advocate
Hi Takstein.

I believe what you need to do is to use the Plot Object of autocad, and add a new plot configuration which uses DWGToPDF.pc3, and then use the PlotToFile method to create the pdf.

here is some code I did in Delphi, but I hope you can adapt it to your VBA code.


Procedure CreatePDF(Dwg : String );
Var
PtConfigs : AcadplotConfigurations;
PlotConfig : AcadPlotConfiguration;
PtObj : AcadPlot;
BackPlot : olevariant;

begin
//Create a new plot configutrarion with all needed parameters
PtObj:= ThisDrawing.Plot; '
PtConfigs:= ThisDrawing.PlotConfigurations;
PtConfigs.Add('PDF',false);{Here is where you add a new plot configuration}

plotConfig:= PtConfigs.Item('PDF');{the plot config you created become active}
plotConfig.StandardScale := acScaleToFit;{use this method to set the scale}

plotconfig.RefreshPlotDeviceInfo;

plotconfig.ConfigName:='DWG To PDF.pc3';{here you specify the pc3 file you want to use}


PlotConfig.StyleSheet:='Acad.ctb';{you can select the plot style table here}
PlotConfig.PlotWithPlotStyles:=True;

{If you are ging 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 craetion}
BackPlot:= ThisDrawing.GetVariable('BACKGROUNDPLOT');
Acad.ActiveDocument.SetVariable('BACKGROUNDPLOT',0);
plotconfig.RefreshPlotDeviceInfo;
{Now you can use the PlotTofile method}
if PtObj.PlotToFile('DwgName.pdf' ,plotConfig.ConfigName) then
showmessage('PDF Was Created')
else
showmessage('PDF Creation Unsuccessful');

{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;
Acad.ActiveDocument.SetVariable('BACKGROUNDPLOT',BackPlot);


It is important to remember putting the extension".pdf" to the name of the file you create using the plot to file method.

Hope this helped.

Regards.

Mario
0 Likes
Message 3 of 10

Anonymous
Not applicable
Thanks, Mario
Worked like a charm for me (A2008)

Here is VBA version:

Option Explicit

Sub CreatePDF()

Dim PtConfigs As AcadPlotConfigurations
Dim PlotConfig As AcadPlotConfiguration
Dim PtObj As AcadPlot
Dim BackPlot As Variant

'Create a new plot configutrarion with all needed parameters
Set PtObj = ThisDrawing.Plot
Set PtConfigs = ThisDrawing.PlotConfigurations
'Add a new plot configuration
PtConfigs.Add "PDF", False
'The plot config you created become active
Set PlotConfig = PtConfigs.Item("PDF")
'Use this method to set the scale
PlotConfig.StandardScale = 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 = ThisDrawing.GetVariable("BACKGROUNDPLOT")
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
'Updates the plot
PlotConfig.RefreshPlotDeviceInfo
'Now you can use the PlotTofile method
If PtObj.PlotToFile(Replace(ThisDrawing.FullName, "dwg", "pdf"), PlotConfig.ConfigName) Then
MsgBox "PDF Was Created"
Else
MsgBox "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
Set PlotConfig = Nothing
ThisDrawing.SetVariable "BACKGROUNDPLOT", BackPlot

End Sub

~'J'~
Message 4 of 10

Anonymous
Not applicable
Hi all, I have made a program following the codes above using C#. But in my code, the plotconfiguration does not work. The program do created a new pagesetup, but it's not selected when plotting. is there any method to set a plotconfiguration to be active when plotting?

Thanks,Eric
0 Likes
Message 5 of 10

Anonymous
Not applicable
HI Fatty
what's the BACKGROUNDPLOT system variable in your code?.
i copied your code and trying in my my application it gives error.
0 Likes
Message 6 of 10

Anonymous
Not applicable
Thats exactly why i'm here today!
AcadPlotConfigurations dosen't seem to have a method that 'set current' the selected PlotConfig.
if u managed to find a way to do it, please let me know.
0 Likes
Message 7 of 10

Mario-Villada
Advocate
Advocate
I had that problem too and I solve it by using copyfrom method.
just insert this line before calling the PlotToFile Method.
{code}
Acad.ActiveDocument.ActiveLayout.CopyFrom(plotconfig)
{code}

Sorry guys for the late response but I did not get any warning from autodesk that someone wrote a reply.

I hope at least this help someone else in the future

regards,

Mario.
0 Likes
Message 8 of 10

Anonymous
Not applicable
I have one problem. Looks like this method plots to plt file, but with .pdf extension. I'm unable to open it in Adobe Acrobat.
Also, the file is approx. 700 kB.
The same drawing, with the same settings (set programatically), but plotted manually, using PLOT command, is OK and weights approx. 170 kB.
Any suggestions?
0 Likes
Message 9 of 10

Mario-Villada
Advocate
Advocate
Are you using DWG To PDF.pc3 as your plotter? (plotconfig.ConfigName). If you use a different printer it migh not work, unless that printer has a driver capable of making pdfs.

And please don't forget to add this line:
{code}
Acad.ActiveDocument.ActiveLayout.CopyFrom(plotconfig)
{code}

just before calling the PlotToFile method. otherwise you woud be using the last plot configuration saved in the drawing.
hope this helps.
0 Likes
Message 10 of 10

Anonymous
Not applicable

If I add the code below:

Acad.ActiveDocument.ActiveLayout.CopyFrom(plotconfig)

 

it gives an error on that code, if i dont add that code it gives an error on the "Plotfile method"

 

"Object doesnt support this property or method"

 

any idea what teh reason can be?

0 Likes