Batch plot By Excel VBA code

Batch plot By Excel VBA code

Anonymous
Not applicable
3,585 Views
3 Replies
Message 1 of 4

Batch plot By Excel VBA code

Anonymous
Not applicable

Dear all,

 

I recently started to get accustomed with Autocad, so not all references are familiar.

 

I have an excel sheet which contains PLC Controller Address Data that needs to get in a Autocad Drawing.

I am using OLE objects so once I open my drawing this information is automatically updated when parameters in my Excel File are adjusted. Then I press "Plot" to plot this drawing to PDF. 

So far all is working as expected.

 

Unfortunate I have between 500 and 1200 drawings I need to plot this way for each project, so I want to automate this process using VBA. I am using Excel as my master VBA project, because I can easily update the parameters for the next drawing within this document.

 

I am able to open Autocad, open the first drawing, make it plot. And this works.

 

The problem comes when I update my parameters and open the second drawing (which can be the same source file or a different one) and make it plot again, nothing happens.

 

I noticed this is probably due to, that I can only plot 1 drawing at a time and my VBA script does not wait on this.

Because when I step through the code and wait for the plot job to finish it does work.

I attached my testing code with 4 files as a reference. (Once I get these 4 working I can go to bigger projects)

 

What would be the correct way to do this.

Or should I look in a different direction. (VBA in Autocad/AutoLISP)

 

- I already tried "Tables" but these do not update if I do not save my excel sheet,

  and this still does not solved the 1 drawing per plot isseu.

 

 

Thank you for any advise that may come.

 

Regard Peter

0 Likes
Accepted solutions (1)
3,586 Views
3 Replies
Replies (3)
Message 2 of 4

maratovich
Advisor
Advisor

Give us an example of your .dwg file

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 3 of 4

Anonymous
Not applicable

Hi @maratovich 

 

I added 1 of the 50 source drawings. All the white square objects are OLE references to my main Excel file.

As mentioned, depending on the project, I need to create various drawings based on the original 50 source drawings.

I adjust some parameters in the excel file and open 1 of the drawings which has the updated information and then plot it to PDF.  Then adjust some parameters in the excel again, open the same or different drawings and plot it.

 

Now i need to get this working for 500+ PDFs.

 

Thanks for the support

0 Likes
Message 4 of 4

Anonymous
Not applicable
Accepted solution

I just found the answer in a different Autodesk Knowledge page.

I had to set the background plot to False

 

 

    BackPlot = AcadApp.ActiveDocument.GetVariable("BackGroundPlot")
    'Set Background plotting to false
    AcadApp.ActiveDocument.SetVariable "BACKGROUNDPLOT", 0

 

 

This will prevent the "Success" flag from the "DWG to PDF" maker from coming in at the beginning, but will come in the end after the plot is finished.

 

My Plot for 4 files is now 

 

 

    Dim AcadApp As AcadApplication
    Dim MyDWG1 As String
    Dim MyDWG2 As String
    Dim MyDWG3 As String
    Dim MyDWG4 As String
    
    Dim AcadDoc1 As AcadDocument
    Dim AcadDoc2 As AcadDocument
    Dim AcadDoc3 As AcadDocument
    Dim AcadDoc4 As AcadDocument
    
    Dim MyPDF1 As String
    Dim MyPDF2 As String
    Dim MyPDF3 As String
    Dim MyPDF4 As String

    MyDWG1 = strLocalFilePath & "\AutocadDrawings\xxx1.dwg"
    MyDWG2 = strLocalFilePath & "\AutocadDrawings\xxx2.dwg"
    MyDWG3 = strLocalFilePath & "\AutocadDrawings\xxx3.dwg"
    MyDWG4 = strLocalFilePath & "\AutocadDrawings\xxx4.dwg"
    
    MyPDF1 = strLocalFilePath & "\AutocadDrawings\xxx1.pdf"
    MyPDF2 = strLocalFilePath & "\AutocadDrawings\xxx2.pdf"
    MyPDF3 = strLocalFilePath & "\AutocadDrawings\xxx3.pdf"
    MyPDF4 = strLocalFilePath & "\AutocadDrawings\xxx4.pdf"

    strLocalFilePath = Application.ActiveWorkbook.Path

    On Error Resume Next
    'try if AutoCAD already is started
    Set AcadApp = GetObject(, "AutoCAD.Application")
    If AcadApp Is Nothing Then
       'AutoCAD is not running or not reacting, so we create a new process
       Set AcadApp = CreateObject("AutoCAD.Application")
    End If
   
    If AcadApp Is Nothing Then
        'if that failed too something not correct
    Else
        AcadApp.Visible = True
    End If
        
    Dim BackPlot As Integer

    Set AcadDoc1 = AcadApp.Documents.Open(MyDWG1)
    BackPlot = AcadApp.ActiveDocument.GetVariable("BackGroundPlot")
    AcadApp.ActiveDocument.SetVariable "BACKGROUNDPLOT", 0
        
    DoPdf AcadDoc1, MyPDF1
    AcadDoc1.Close False
   
    Set AcadDoc2 = AcadApp.Documents.Open(MyDWG2)
    DoPdf AcadDoc2, MyPDF2
    AcadDoc2.Close False
    
    Set AcadDoc3 = AcadApp.Documents.Open(MyDWG3)
    DoPdf AcadDoc3, MyPDF3
    AcadDoc3.Close False
    
    Set AcadDoc4 = AcadApp.Documents.Open(MyDWG4)
    DoPdf AcadDoc4, MyPDF4
    AcadDoc4.Close False

    AcadApp.ActiveDocument.SetVariable "BACKGROUNDPLOT", BackPlot
    AcadApp.Quit

 

 

With the last statement closing autocad.

 

I am now testing the same procedure for 9 x 17 files (using multiple For loops) and all seems in good working condition.

 

Thanks for all support @maratovich 

 

 

0 Likes