Plot.PlotToDevice() random failure

Plot.PlotToDevice() random failure

Markeckert
Enthusiast Enthusiast
1,445 Views
17 Replies
Message 1 of 18

Plot.PlotToDevice() random failure

Markeckert
Enthusiast
Enthusiast

I'm using the Plot.PlotToDevice() function in a VB application in conjunction with AutoCAD 2021. My results are inconsistent. It randomly drops pages from the output PDF file and cuts off sections of other pages. Does anyone know how to resolve this?

 

Thanks

0 Likes
1,446 Views
17 Replies
Replies (17)
Message 2 of 18

Ed__Jobe
Mentor
Mentor

You need to show your code if you want help with it. My first thought is that there are problems with your page setup for the problem sheets. You might also check this forum. I recently helped a couple others with publishing to pdf.

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 3 of 18

Markeckert
Enthusiast
Enthusiast

Here's a little more detail. The VB app uses this line of code to write a DWG file into a PDF file - Plotresult = bb.Plot.PlotToDevice(). Using the same set of DWG files I sometimes get a complete PDF file and other times the file will be blank or partially blank. I've checked the Plotresult integer I set up as a return value, and it always shows a -1 after execution of the statement regardless of whether it was successful or not.

Here is the routine. I highlighted the statement in Yellow.

 


Private Function Export(ByVal strOldFile As String) As Boolean

Dim strFile As String
Dim strFile2 As String
Dim strFile3 As String

Dim i As Integer

Dim dd As Autodesk.AutoCAD.Interop.Common.AcadPlotConfiguration
Dim aa As Autodesk.AutoCAD.Interop.AcadDocument

Try

Export = False

strFile = strNewFolder & "\tmp_Stage1_" & VB.Left(objAutocad.ActiveDocument.Name, Len(objAutocad.ActiveDocument.Name) - 4)

'objAutocad.Preferences.OpenSave.CreateBackup = False

System.Windows.Forms.Application.DoEvents()

Dim bb As Autodesk.AutoCAD.Interop.AcadDocument
bb = objAutocad.ActiveDocument

aa = objAutocad.Documents.Open(Label4.Text)
dd = aa.PlotConfigurations.Item("PDF")
'dd = objAutocad.ActiveDocument.PlotConfigurations.Item("PDF")

bb.Layouts.Item("Model").CopyFrom(dd)

strFile = bb.Name
Application.DoEvents()
Application.DoEvents()
Application.DoEvents()
Application.DoEvents()

Dim di As Date

di = Now

Do While DateDiff(DateInterval.Second, di, Now) < 2
Application.DoEvents()
Loop

Try
Plotresult = bb.Plot.PlotToDevice()

Catch ex As Exception
CreateLog(strLogFile, "Custom Error: " & ex.Message)
End Try


System.Windows.Forms.Application.DoEvents()

 



*Moderator edit* post your code using a code window.

0 Likes
Message 4 of 18

Ed__Jobe
Mentor
Mentor

@Markeckert wrote:

I've checked the Plotresult integer I set up as a return value, and it always shows a -1 after execution of the statement regardless of whether it was successful or not.


You have to include the logic to return the result. In C# you need to use the Return statement. I'm not sure if its the same in VB. I don't remember. In VBA you use the Set statement and the name of the function.

 

Set Export = true

 

The best place for this logic would be in your try statement.

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 5 of 18

Markeckert
Enthusiast
Enthusiast
Any thoughts on why the Plot.PlotToDevice() statement would be successful only randomly? I changed the VB code to execute the statement two times in a succession. It will sometimes produce the populated PDF on the first iteration and fail on the second or it will fail on the first iteration but be successful on the second one.


0 Likes
Message 6 of 18

Ed__Jobe
Mentor
Mentor

Without seeing all your code and having sample files to test on, I won't be able to reproduce your results. So my best guess is what I stated in post 2.

 

This looks like you want to plot from an out-of-process application. Am I correct? It might help if you stated what your goal is. Rather than plotting using AutoCAD, you might want to use AcCoreConsole.

 

Finally, you may want to consider moving from VB.NET to C#, since it's what all us AutoCAD developers use and you will find that almost all sample code is written in C#. You can still use the COM api with C#.

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 7 of 18

norman.yuan
Mentor
Mentor
You may want to try to set "BACKGROUNDPLOT" system variable to 0 (i.e. disable back ground plotting) before calling PlotToDevice() (and restore it back afterwards).

But it would not solve the issue you stated in the original post regarding content not plotted corrected, which is determined by layout/PlotConfiguration identified by Item("PDF"), to which your code does nothing to change/adjust it, so I have to assume the layout's plot is not configured correctly.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 8 of 18

Markeckert
Enthusiast
Enthusiast
Are you able to provide the syntax to set the BACKGROUNDPLOT system variable or a link to the method to do so?
0 Likes
Message 9 of 18

Ed__Jobe
Mentor
Mentor

ThisDrawing.SetVariable. ThisDrawing.GetVariable

Dim bgp As Integer
bgp = ThisDrawing.GetVariable("BACKGROUNDPLOT")
ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
'continue process
ThisDrawing.SetVariable "BACKGROUNDPLOT", bgp

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 10 of 18

Markeckert
Enthusiast
Enthusiast
Setting the BACKGROUNDPLOT to 0 didn't change the outcome. I'm still getting random blank pdfs and it doesn't seem to be throwing an error. Is there any other error checking that you can suggest?

0 Likes
Message 11 of 18

norman.yuan
Mentor
Mentor

Getting blank page is because the layout/PlotConfiguration is the plot content (view/window/layout size) larger than the paper size used in plotting.

 

So, basically, you need to open the drawing, manually use the existing plot settings to plot to PDF to verify the layout/PlotConfiguration is set correctly. However, if you are familiar with Layout/Plotting setup, you can easily spot these issues in the plotting dialog box (and fix it there). It is really not a programming/code issue, because your code simply plot whatever the layout/PlotConfiguration was set.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 12 of 18

Ed__Jobe
Mentor
Mentor

@Markeckert wrote:
Setting the BACKGROUNDPLOT to 0 didn't change the outcome. I'm still getting random blank pdfs and it doesn't seem to be throwing an error. Is there any other error checking that you can suggest?


Maybe if you answered my questions in post 6.

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 13 of 18

Markeckert
Enthusiast
Enthusiast
The blank pages are random and are from the same input drawing files. If I run the process several times using the same input drawings files, I'll get different results almost every time. On some runs I get all the pages populated and on other runs page one is blank and on others page 3 is blank. I can't find any errors thrown to indicate what is happening.
0 Likes
Message 14 of 18

Markeckert
Enthusiast
Enthusiast
The VB app that I'm struggling with is not new development. It's been used for several years and it was working with the AutoCAD 2018 product. Its when I switched to AutoCAD 2021 that it began to have this problem of dropping pages randomly for the same input files. Once in a while it will process and combine all of the input files but most of the time it will create one or two blank pages in the output PDF. I have not been able to trap any error codes.

0 Likes
Message 15 of 18

Ed__Jobe
Mentor
Mentor

I'm sorry, but you didn't answer my questions. The code you shown is seems to be lacking. It doesn't matter how long you've been using it if there is a better way of doing it. I would rewrite it but I don't have enough info to make any recommendations.

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 16 of 18

Markeckert
Enthusiast
Enthusiast

I'm attaching some of the code. I didn't include it all since it might be a bit much to sort through especially if it doesn't apply to the issue. The CreatePDF(bb, 1) subroutine is what is failing to consistently write out a full PDF page. 

 

Dim strFile As String
Dim strFile2 As String
Dim strFile3 As String

Dim i As Integer

Dim dd As Autodesk.AutoCAD.Interop.Common.AcadPlotConfiguration
Dim aa As Autodesk.AutoCAD.Interop.AcadDocument

Try

Export = False

strFile = strNewFolder & "\tmp_Stage1_" & VB.Left(objAutocad.ActiveDocument.Name, Len(objAutocad.ActiveDocument.Name) - 4)

'objAutocad.Preferences.OpenSave.CreateBackup = False

System.Windows.Forms.Application.DoEvents()

 

Dim bb As Autodesk.AutoCAD.Interop.AcadDocument

bb = objAutocad.ActiveDocument

 

aa = objAutocad.Documents.Open(Label4.Text)

dd = aa.PlotConfigurations.Item("PDF")
'dd = objAutocad.ActiveDocument.PlotConfigurations.Item("PDF")


bb.Layouts.Item("Model").CopyFrom(dd)


strFile = bb.Name
Application.DoEvents()
Application.DoEvents()
Application.DoEvents()
Application.DoEvents()

'Dim di As Date

'di = Now

'Do While DateDiff(DateInterval.Second, di, Now) < 2
' Application.DoEvents()
'Loop

 


Dim Plotresult As Integer

' SetAttr(BACKGROUNDPLOT = 1)

CreatePDF(bb, 1)

 

Try

 

Catch ex As Exception
blnHasErrors = True
CreateLog(strLogFile, "Custom Error: " & ex.Message)
End Try


System.Windows.Forms.Application.DoEvents()

 

Do While Dir(Label7.Text & strFile.Substring(0, strFile.Length - 4) & "*.pdf") = ""
Application.DoEvents()
Loop

strFile3 = Dir(Label7.Text & strFile.Substring(0, strFile.Length - 4) & "*.pdf")

Application.DoEvents()
strFile2 = strNewFolder & "\tmp_Stage1_" & strFile.Substring(0, strFile.Length - 4)

Thread.Sleep(3000)

Application.DoEvents()
System.IO.File.Move(Label7.Text & strFile3, strFile2 & ".pdf")
'Rename(Label7.Text & strFile3, strFile2 & ".pdf")
'Rename(Label7.Text & strFile.Substring(0, strFile.Length - 4) & " Model (1).pdf", strFile2 & ".pdf")

Application.DoEvents()
Application.DoEvents()
Application.DoEvents()

UpdatePDF(strFile2 & ".pdf")

Application.DoEvents()
Application.DoEvents()
Application.DoEvents()

Export = True

Try
bb.Close(False)
Catch ex As Exception
End Try

Catch ex As Exception
blnHasErrors = True
lblCurrentFile.ForeColor = Color.Red
CreateLog(strLogFile, "Export: Error Exporting File: (" & strOldFile & ") - " & ex.Message)
End Try

Try
aa.Close(False)
Catch ex As Exception
CreateLog(strLogFile, "Export: Closing ACAD: (" & strOldFile & ") - " & ex.Message)
End Try

Try
dd = Nothing
Catch ex As Exception
CreateLog(strLogFile, "Export: Closing ACAD: (" & strOldFile & ") - " & ex.Message)
End Try

GC.Collect(1)

End Function

Private Sub CreatePDF(ByVal bb As Autodesk.AutoCAD.Interop.AcadDocument, ByVal PlotResult As Integer)
Dim backPlot As Integer
backPlot = objAutocad.ActiveDocument.GetVariable("BACKGROUNDPLOT")
Try
objAutocad.ActiveDocument.SetVariable("BACKGROUNDPLOT", 0)
PlotResult = bb.Plot.PlotToDevice()


Catch ex As Exception
CreateLog(strLogFile, "Custom Error: " & ex.Message)

End Try

End Sub

0 Likes
Message 17 of 18

Markeckert
Enthusiast
Enthusiast

I just noticed that some of the code I pasted was cut off so I have attached a file with the code.

0 Likes
Message 18 of 18

Markeckert
Enthusiast
Enthusiast
I added the code to the posting. Hopefully that will be of some help. This Application worked well and still does with AutoCAD 2018. Does anyone have an idea what might be different in AutoCAD 2021 that would cause the issue that I'm experiencing ? If there was just a method to trap the error I could put a retry in the code.
0 Likes