Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

How to Plot a Selected Area from Model Space Using VBA?

sonchai_n
Explorer

How to Plot a Selected Area from Model Space Using VBA?

sonchai_n
Explorer
Explorer

Hi,

I’m trying to create a VBA macro to plot a specific area from the Model space in AutoCAD to a PDF file. The plot settings (e.g., plotter, paper size, plot style) are already configured, so I don’t need to set them again in the code. I only want to specify the coordinates of the area to plot and generate the PDF.

Here’s my current code, but I’m getting the error:
Error Number: 438, Description: Object doesn't support this property or method.

 

 

Sub TestPlotFromModel()
    Dim fileName As String
    fileName = "C:\Temp\ModelAreaPlot.pdf" ' Specify the output PDF file name

    ' Coordinates of the plot area (example values)
    Call PlotSelectedAreaFromModel(fileName, 0, 0, 420, 297)
End Sub

Sub PlotSelectedAreaFromModel(plotFileName As String, lowerLeftX As Double, lowerLeftY As Double, upperRightX As Double, upperRightY As Double)
    On Error GoTo ErrorHandler

    ' Disable background plotting
    ThisDrawing.SetVariable "BACKGROUNDPLOT", 0

    ' Switch to Model space layout
    ThisDrawing.ActiveLayout = ThisDrawing.Layouts("Model")

    ' Set the plot window using the coordinates
    ThisDrawing.ActiveLayout.SetWindowToPlot Array(lowerLeftX, lowerLeftY), Array(upperRightX, upperRightY)

    ' Plot directly to the specified file
    Debug.Print "Plotting to: " & plotFileName
    ThisDrawing.Plot.PlotToFile plotFileName

    ' Notify user
    Debug.Print "PDF generated successfully: " & plotFileName
    Exit Sub

ErrorHandler:
    Debug.Print "Error Number: " & Err.Number & ", Description: " & Err.Description
    Err.Clear
    Exit Sub
End Sub

 

0 Likes
Reply
Accepted solutions (1)
159 Views
1 Reply
Reply (1)

norman.yuan
Mentor
Mentor
Accepted solution

You should indicate which line of code that causes the error to save others from having to guess, or actually running your code.

 

Anyway, I did run your code and get error on this line:

ThisDrawing.ActiveLayout.SetWindowToPlot Array(lowerLeftX, lowerLeftY), Array(upperRightX, upperRightY)

However, the error I got is different:

 

Error Number: 5, Description: Invalid procedure call or argument

 

I made changes like this:

Sub PlotSelectedAreaFromModel(plotFileName As String, lowerLeftX As Double, lowerLeftY As Double, upperRightX As Double, upperRightY As Double)
    On Error GoTo ErrorHandler

    ' Disable background plotting
    ThisDrawing.SetVariable "BACKGROUNDPLOT", 0

    ' Switch to Model space layout
    ThisDrawing.ActiveLayout = ThisDrawing.Layouts("Model")

    Dim a(0 To 1) As Double
    Dim b(0 To 1) As Double
    a(0) = lowerLeftX: a(1) = lowerLeftY
    b(0) = upperRightX: b(1) = upperRightY
    
    ' Set the plot window using the coordinates
    ThisDrawing.ActiveLayout.SetWindowToPlot a, b
    
    ' Plot directly to the specified file
    Debug.Print "Plotting to: " & plotFileName
    ThisDrawing.Plot.PlotToFile plotFileName

    ' Notify user
    Debug.Print "PDF generated successfully: " & plotFileName
    Exit Sub

ErrorHandler:
    Debug.Print "Error Number: " & Err.Number & ", Description: " & Err.Description
    Err.Clear
    Exit Sub
End Sub

 

Then, it works as expected.

 

 

Norman Yuan

Drive CAD With Code

EESignature