Why does my VBA function generate a blank PDF for one DWG file but work fine for another?

Why does my VBA function generate a blank PDF for one DWG file but work fine for another?

sonchai_n
Contributor Contributor
539 Views
6 Replies
Message 1 of 7

Why does my VBA function generate a blank PDF for one DWG file but work fine for another?

sonchai_n
Contributor
Contributor

I have a VBA Excel function that plots a selected area to PDF using DWG To PDF.pc3. The function works correctly for one DWG file, generating a properly formatted PDF, but when I run it on another DWG file (with the same settings and details), the resulting PDF is blank.

Here is my function:

 

 

 

Function TestPlotSelectedAreaToPdf() As Boolean
    On Error GoTo ErrorHandler
    acadDoc.SetVariable "BACKGROUNDPLOT", 0
    Dim plotFileName As String
    plotFileName = "D:\Test\TestPlotSelectedArea.pdf"

    Dim lowerLeft(0 To 1) As Double
    Dim upperRight(0 To 1) As Double
    lowerLeft(0) = 0#: lowerLeft(1) = 0#
    upperRight(0) = 297#: upperRight(1) = 210#

    With acadDoc.ActiveLayout
        .configName = "Acade - DWG To PDF.pc3"
        .CanonicalMediaName = "ISO_full_bleed_A4_(297.00_x_210.00_MM)"
        .plotType = acWindow
        .SetWindowToPlot lowerLeft, upperRight
        .centerPlot = True
        .StandardScale = acScaleToFit
        .plotRotation = ac0degrees
        .PlotWithPlotStyles = True
        .RefreshPlotDeviceInfo
    End With

    acadDoc.Plot.PlotToFile plotFileName
    TestPlotSelectedAreaToPdf = True
    Exit Function
ErrorHandler:
    Debug.Print "Error TestPlotSelectedAreaToPdf : " & Err.Description
    TestPlotSelectedAreaToPdf = False
End Function

 

 

Additional Info:

 

  • Both DWG files contain similar objects and layers.
  • The PDF output for one file is correct, while the other file generates a blank PDF.
  • No errors occur during execution.
  • File Test1.dwg can ploted to pdf
  • File Test2.dwg can ploted to pdf but is blank pdf

What could be causing this issue? Are there any settings in the DWG file that might affect the plot result?

0 Likes
540 Views
6 Replies
Replies (6)
Message 2 of 7

Ed__Jobe
Mentor
Mentor

Since you didn’t post the dwg that plots blank, I can only guess that there’s nothing in the area of the window. Since you plot the ActiveLaout, it’s up to the user to make sure that the desired layout is active and that model or paper space is correct.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 3 of 7

Ed__Jobe
Mentor
Mentor

Sorry it took me so long to respond. In Test2, the problem is simply that you put the entities on the Defpoints layer, which is non-plotting. 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 7

sonchai_n
Contributor
Contributor

Thank you for your response!

I have checked the Defpoints layer, and I can confirm that my entities are not on that layer.

However, the issue still persists. When I use the same VBA function to plot Test2.dwg, the selected Window area is not applied correctly, which results in a PDF that does not match the expected output.

What I have verified:

  • Objects are not on the Defpoints layer.
  • The SetWindowToPlot coordinates are correct.
  • The plot configuration is the same for both DWG files.
  • Other DWG files with the same settings plot correctly.

What could be causing this issue? Are there any settings in AutoCAD that might affect how the plot window is applied?

Thanks again for your help! 😊

0 Likes
Message 5 of 7

Ed__Jobe
Mentor
Mentor

I've looked over the code and made some minor changes but Test2 still won't accept setting the plot window. If you start the PLOT command and manually set the window, you will notice that there is no plot area set. The PLOT command is able to set the window, but the code isn't setting it. I would run the VARS2CR command on each file and compare them to see if test2 has something set differently. If not, it may just be a corrupt file. I ran the AUDIT command, but it didn't find any  problems.

Sub TestPlotSelectedAreaToPdf()
    On Error GoTo ErrorHandler
    ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
    Dim plotFileName As String
    plotFileName = Left(ThisDrawing.Name, Len(ThisDrawing.Name) - 4) '"D:\\Test\\TestPlotSelectedArea.pdf"

    Dim lowerLeft(0 To 1) As Double
    Dim upperRight(0 To 1) As Double
    lowerLeft(0) = 0: lowerLeft(1) = 0
    upperRight(0) = 297: upperRight(1) = 210

    With ThisDrawing.ActiveLayout
        .RefreshPlotDeviceInfo
        .ConfigName = "DWG To PDF.pc3"
        .CanonicalMediaName = "ISO_full_bleed_A4_(297.00_x_210.00_MM)"
        .SetWindowToPlot lowerLeft, upperRight
        .PlotType = acWindow
        .CenterPlot = True
        .StandardScale = acScaleToFit
        .PlotRotation = ac0degrees
        .PlotWithPlotStyles = True
    End With
    
    ' Read back window information
    ThisDrawing.ActiveLayout.GetWindowToPlot lowerLeft, upperRight
    
    MsgBox "Press any key to plot the following window:" & vbCrLf & vbCrLf & _
           "Lower Left: " & lowerLeft(0) & ", " & lowerLeft(1) & vbCrLf & _
           "Upper Right: " & upperRight(0) & ", " & upperRight(1)


    ThisDrawing.Plot.DisplayPlotPreview acFullPreview

    'ThisDrawing.Plot.PlotToFile plotFileName & ".pdf"
    'TestPlotSelectedAreaToPdf = True
    Exit Sub
ErrorHandler:
    Debug.Print "Error TestPlotSelectedAreaToPdf : " & Err.Description
    'TestPlotSelectedAreaToPdf = False
End Sub

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 6 of 7

sonchai_n
Contributor
Contributor

Thank you for your response!

I'm using the VBA code above with test2.dwg, but the plotted area in the output PDF does not match the expected coordinates.

 

    Dim lowerLeft(0 To 1) As Double
    Dim upperRight(0 To 1) As Double
    lowerLeft(0) = 0: lowerLeft(1) = 0
    upperRight(0) = 297: upperRight(1) = 210

Expected Plot Area (A4 - 297x210 mm)

at point X= 0.0000 Y= 0.0000 Z= 0.0000
at point X= 297.0000 Y= 0.0000 Z= 0.0000
at point X= 297.0000 Y= 210.0000 Z= 0.0000
at point X= 0.0000 Y= 210.0000 Z= 0.0000

Actual Plot Area in PDF

 

at point X=1933.1250 Y=-346.8750 Z= 0.0000
at point X=2230.1250 Y=-346.8750 Z= 0.0000
at point X=2230.1250 Y=-136.8750 Z= 0.0000
at point X=1933.1250 Y=-136.8750 Z= 0.0000

 

If anyone has encountered this issue or has suggestions, I would appreciate your help!

Thanks in advance.

0 Likes
Message 7 of 7

Ed__Jobe
Mentor
Mentor

That's because the SetWindowToPlot function is not working. The "blank" area is what is already in the dwg. You need to find out if there is a setting in the dwg that is blocking the SetWindowToPlot function from working. Right now, only setting it within the PLOT command works. I told you how to compare drawing variables using the VARS2SCR command.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes