VBA Plotting several planes from the same ModelSpace using Window Plotting.

VBA Plotting several planes from the same ModelSpace using Window Plotting.

Anonymous
Not applicable
2,976 Views
7 Replies
Message 1 of 8

VBA Plotting several planes from the same ModelSpace using Window Plotting.

Anonymous
Not applicable

 

 

Hello all,

 

My name is Alvaro, I have been reading in this forum for a month now, this is the first time I write a post. I've looked through previous topics and despite I have found similar topics I haven't been able to solve my problem. 

 

I need to plot several planes from the same model space. 

 

I am able to do this using: commands, for, and a coordinate sheet that I wrote in excel. However this method doesn’t allow me to automatically name each file with its corresponding name.

 

I think there is a way to use VBA so I can automatically name the files, I have made some progress but I am struggling to set the printer and of course to set the file name. 

 

If you could please tell me how to set the printer and how to set the file name using VBA in the window plotting mode. I think I will be able to manage to write the remaining code.

 

This is the part of the code I would like to rewrite in VBA in order to be able to name each file in each iteration of the for.

 

 

 

 

For i = 0 To c
'Lower Left Corner          p0(0) = ThisWorkbook.Sheets("Coordinates").Cells(7, 3 + 5 * i): p0(1) = ThisWorkbook.Sheets("Coordinates").Cells(7, 4 + 5 * i)         

'Right top corner
p1(0) = ThisWorkbook.Sheets("Coordinates").Cells(7, 5 + 5 * i): p1(1) = ThisWorkbook.Sheets("Coordinates").Cells(7, 6 + 5 * i)           

'Plot window by command.
AutoCAD.Application.ActiveDocument.SendCommand "_-PLOT" & vbCr & "Si" & vbCr & vbCr & "PDFCreator" & vbCr & "A4" & vbCr & vbCr & "Horizontal" & vbCr & vbCr & "Ventana" & vbCr & p0(0) & "," & p0(1) & vbCr & p1(0) & "," & p1(1) & vbCr & vbCr & vbCr & vbCr & vbCr & "PDFCreator" & vbCr & vbCr & vbCr & vbCr          Next i

 

 

Thanks, Alvaro.

0 Likes
2,977 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

Hello,

 

I’ve been working in this code. I am trying to adapt a code that I found in this forum to me needs.

 

To sum up, I am trying to plot several planes from the same model space, I have the planes in my model space " as a matrix”  ( see pic ). Therefor I have a boucle that goes through all planes printing them. 

 

Plane Matrix.png

 

I am able to see the planes I want as a preview but I can’t manage to generate the PDF file.

 

I guess I need to:

-Define printing device

-Configure printing device

-Define Path

-Define file name (this is the only field that I should change in each iteration of the for)

 

This is the code I am using ("for" is not really important at this time, I am using 2 iterations just to be sure it can do it):

 

 

Sub Example_SetWindowToPlot()
    
    ' This example allows the user to define an area in the current layout
    ' and displays a plot preview of the defined area.
    

    AppActivate ThisDrawing.Application.Caption
    
    ' I define Points
    Dim point1(0 To 1) As Double, point2(0 To 1) As Double
    
    ' I start for to plot several planes ( cordinates will be read from a Excel File)
    
    For i = 0 To 1
    ' Get first point in window
    point1(0) = 0 + i * 200: point1(1) = 0 + i * 200
    ' Get second point in window
    point2(0) = 600 + i * 200: point2(1) = 900 + i * 200
    
    ' Send information about window to current layout
    ThisDrawing.ActiveLayout.SetWindowToPlot point1, point2
    
    ' Read back window information
    ThisDrawing.ActiveLayout.GetWindowToPlot point1, point2
    
    ' Configure Centerplot to be true
    ThisDrawing.ActiveLayout.CenterPlot = True
    
    ' Be sure to plot a view, not some other plot style
    ThisDrawing.ActiveLayout.PlotType = acWindow
    
    ' Send Plot To Window.
    'Here is where my problem starts. I would like to plot each window to  a folder with a different name for each file.
        
    
    ThisDrawing.ActiveLayout.ConfigName = "DWG to PDF.pc3"
    ThisDrawing.Plot.PlotToFile ("C:\Print")
   

    ThisDrawing.Plot.DisplayPlotPreview acFullPreview
    
    Next i
    
End Sub

 

 

I've been trying to use Plottofile, plottodevice and plotconfiguration commands but I haven’t made any progress. I have never studied VBA before. If you could help me with this or recommend me a book where I can learn I would really appreciate it.

 

 

 

0 Likes
Message 3 of 8

norman.yuan
Mentor
Mentor

You can certainly write code to locate each plotting window on each sheet for plotting. However, if you are tasted to do this coding work, you need to ask the AutoCAD user (if it is not you) hard as why the drawing is done in such a bad way and it is very bad to build some kind of applicationo based on such bad usage of AutoCAD. With minimum effort the drawing can be done in a proper way as AutoCAD is supposed to be used, then you really do not need to write any code at all.

 

You can ccreate as many layouts as the the sheets showing in the drawing's modelspace, and open viewport on each layout to show only one sheet. Then you can plot any layout as you want. Or you can easily use Publish command to plot all/selected layouts in one shot.

 

You only try to write code against the oddly created drawing if there is very good reasons the drawing CAN ONLY be created in that bad way.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 8

Anonymous
Not applicable

Hello Norman.Yuan,

 

i can see from your activity in this forum that you are a advance user of Autocad. I am aware that the code i have written and the way i am trying to use autocad is not the way is meant to be used, but i have limitations on writing code and this is the first code i ever write. I hope in the future to became a better programmer and be able to write codes that not only work but work in an efficient way. Both in programming time and execution time of the progam.

 

Therefore, in my situation, i would really aprecciate if you gave me the tips, or refer me to a existing code, where i can see how to name a sheet ploted from a window, wich is what i was asking in first place. If you dont want to do that because is not the way autocad should be used i understand it, it must be strange for you what i am triying to do. In that case i beg you to develop a bit more what you exposed in your reply on how my problem should be solved so i can understand it.

 

 

Thank you very much.

 

 

0 Likes
Message 5 of 8

norman.yuan
Mentor
Mentor

Well, please execuse me for not being able to clearly know what does that mean "can't manage to generate the PDF file". Does that mean not PDF generated at all and no error message?

 

Since you are doing plotting in a "For...Next..." loop, in order to make debugging easier, you could write a sine plotting unit to make sure the plotting part works before try the looping.

 

From what I can see, you code should have print at least the first frame. After that, depending whether a PDF viewer (Adode Reader, for example) is automatically launched to open the PDF or not, you may run into issue, bacause you savae each plotting PDF file in the same file name, that means, the the the later PDF would overwrite the previous one, and if the previous one is opened in PDF viewer, the overwrting may fail. Another issue is since you do multiple plottings ini code, you want to disable "BACKGROUNDPLOT" so that the ploting started by code would set to PDF plotter one by one. Also, I assume you should the preview window is just for debugging, but logically, it would be placed before the plotting is done, not after, right?

 

Here is my quick single plotting code that works OK:

 

Sub PlotToFile()

    Dim backP As Integer
    backP = ThisDrawing.GetVariable("BACKGROUNDPLOT")
    ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
    
    ThisDrawing.ActiveLayout.ConfigName = "DWG to PDF.pc3"
    
    Dim plotFileName As String
    plotFileName = "C:\Temp\My PDF Plot"
    
    ThisDrawing.Plot.DisplayPlotPreview acFullPreview
    
    Dim result As Boolean
    result = ThisDrawing.Plot.PlotToFile (plotFileName)
    
    ThisDrawing.SetVariable "BACKGROUNDPLOT", backP

If result then
MsgBox "PDF file generated!"
End if End Sub

You want to try that to make sure you can plot a single layout/window. Notice the code to save current BACKGROUNDPLOT value, and then set it to 0. After the plotting is done, set the value back.

 

In VBA, this line of code

 

ThisDrawing.Plot.PlotToFile ("C:\Print")

 

Should be

 

ThisDrawing.Plot.PlotToFile "C:\Print_" & i

 

Note, you do not use bracket if the call to method PlotToFile() does not assign its result to a variable (even in this particular case the bracket does no harm).  You also want to give each plotted frame a different file name, so you do not end up with only last frame as PDF file.

 

Also, you cannot plot all frames into a multi-page single PDF file with this VBA code. If you want single PDF file with muiti-pages, you need to ask your user to use AutoCAD CORRECTLY. That is, to set up multiple layouts, and use AutoCAD built-in publishing feature. No programming is needed.

 

Hope this helps a bit.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 8

Anonymous
Not applicable

I do not have time to work in this code today, but i have read your answer and i would like to clarify the points you ask me.

 

a) I have not PDF generated at all

 

b) I dont want the PDFviewer ( Adobe, Foxit Phantom...) to open.

 

c) I dont really want a preview window. I placed the preview window in the code just to show what i wanted to sustitude with a actual PDF File. I though it would made it more clear to you but maybe it didnt work.

 

d) I want each frame to be ploted into a different PDF File with a different name. For example. Frame 1 ---> Frame 1.PDF, Frame 2 ---> Frame 2.PDF and so on.

 

I'am sure one day ill come back over this code and rewrite it again in a more correct way. And not only this piece of code but all the code; the part that draw the planes, name the elements etc... 

 

Thank you for your time, i am sure it will help. I will work on this.

 

Greetings, Alvaro.

0 Likes
Message 7 of 8

Anonymous
Not applicable

Good Morning,

 

i have work a bit on the code and i now have something  that works as i expected. Thank you.

 

Sub Example_SetWindowToPlot()

    ' This example plots a pdf file for each frame as the for function selects each frame.
    
    
    'Desactivate background plot
    
    Dim backP As Integer
    backP = ThisDrawing.GetVariable("BACKGROUNDPLOT")
    ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
    

    AppActivate ThisDrawing.Application.Caption
    
    'Select printing config
    ThisDrawing.ActiveLayout.ConfigName = "DWG to PDF.pc3"
    
    ' I define Points
    Dim point1(0 To 1) As Double, point2(0 To 1) As Double
    
    ' I start for to plot several planes ( cordinates can be read from a Excel File)
    
    'Start of for function
    For i = 0 To 1
    ' Get first point in window
    point1(0) = 0 + i * 200: point1(1) = 0 + i * 200
    ' Get second point in window
    point2(0) = 600 + i * 200: point2(1) = 900 + i * 200
    
    ' Send information about window to current layout
    ThisDrawing.ActiveLayout.SetWindowToPlot point1, point2
    
    ' Read back window information
    ThisDrawing.ActiveLayout.GetWindowToPlot point1, point2
    
    ' Configure Centerplot to be true
    ThisDrawing.ActiveLayout.CenterPlot = True
    
    ' Be sure to plot a view, not some other plot style
    ThisDrawing.ActiveLayout.PlotType = acWindow
    
    'Name pdf file
    Dim plotFileName As String
    plotFileName = "C:\Temp\My PDF Plot" & i

    'Print pdf
    Dim result As Boolean
        
    result = ThisDrawing.Plot.PlotToFile(plotFileName)
    
    Next i
    
    ThisDrawing.SetVariable "BACKGROUNDPLOT", backP
    
      
End Sub

This code is part of a bigger code wrote in excel. ( i still have to adapt it to excel)

 

 

The excel document allows me to:

 

a) Select product configuration.

 

b) Draw planes for that product configuration: according to the product configuration and its characteristics autocad draw the neccessary planes to define it (this is why i used a matrix). There is two ways in wich the planes are drawn.

 

First, excel goes into autocad and change the visibility property for a dynamic block. This types of draws only apear one time.

 

Second, excel goes into atuocad and copies a base plane wich i have in the model space as times as it is neccesary. There can up to 16 replicas of the same draw.

 

c) I name the elements for all planes. For the dynamic block planes those are allready named. For the remaining plains i have several boucles that go through all sheets naming all elements on them. Autocad reads the names that have to use from the excel file.

 

d) I plot the planes using the code i just wrote above, an adaptation of it.

 

 

Thank you very much for your help, it have been really usefull. By the way i am starting to understand what you told me about the different layers.

 

I guess i can have all the sheets drawn in different layers and just print the ones that i need. In that case i will have to work again in the plane drawing and the plane naming functions.

 

I hope you can understand what i am willing to say, i have some issues with my english.

 

Greetings, Alvaro.

 

 

0 Likes
Message 8 of 8

Anonymous
Not applicable

I just use your VBA

 

can you tell me how can I plott without line strength?

0 Likes