VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

1 REPLY 1
SOLVED
Reply
Message 1 of 2
sonchai_n
125 Views, 1 Reply

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

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

 

1 REPLY 1
Message 2 of 2
norman.yuan
in reply to: sonchai_n

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report