Hi guys, I've a problem. I need to print each layout of my drawing using PlotToFile method of the ActiveDocument.Plot obect, but randomly AutoCAD crash (internal error message popup) with eWasOpenForRead code. This is my code:
AcadDocument acDoc = this.Application.ActiveDocument;
AcadPlot aPlot = acDoc.Plot;
aPlot.QuietErrorMode = false;
this.SetVariable(acDoc, "BACKGROUNDPLOT", 0);
string PlotPc3 = "AUTOMATICO-BN.pc3";
bPrintOk = true;
foreach (AcadLayout layout in acDoc.Layouts)
{
this.WaitApplicationQuiescent();
string sFileLayout = sName + "_" + layout.Name + ".plt";
string[] ls = new string[] {layout.Name};
aPlot.SetLayoutsToPlot(ls);
if (aPlot.PlotToFile(sFileLayout, PlotPc3) &&
System.IO.File.Exists(sFileLayout))
{
//No errors!
}
else
{
//Error!
bPrintOk = false; break;
}
aPlot.SetLayoutsToPlot(new string[0]);
ls = null; GC.Collect();
this.WaitApplicationQuiescent();
}
Any ideas?
Thank you very much
Enrico
Should we try to guess what your WaitApplicationQuiescent() method is doing?
My guess is that that is where the problem is.
I tried with and without that method, but the error always appear. that method is only a loop that check the GetAcadState().IsQuiescent property of the AutoCADApplication class... By the way, this is the method:
private void WaitApplicationQuiescent()
{
if (this.Application.Documents.Count == 0)
return;
while (!this.Application.GetAcadState().IsQuiescent)
{
//Aspetto un po' di tempo
Thread.Sleep(100);
}
}
I already use this method in my code and it never give me any problems, I discover IsQuiescent property reading some post at "http://through-the-interface.typepad.com/".
I need to solve this problem, because I have hundreds of drawing with multi layer to print...
Thanks again
Enrico
Ops.. sorry! That method is called by an C# Excel AddIn with reference to:
- acdmmgd.dll (v.18.0.0.0)
- acmgd.dll (v.18.0.0.0)
- Autodesk.AutoCAD.Interop.dll (v.18.0.0.0)
- Autodesk.AutoCAD.Interop.Common.dll (v.18.0.0.0)
In this AddIn i have a sheet with the list of drawing to print and in the first cell of each row I put the action to execute (P = Print, E = ETransmit, ecc). Pressing a button i Execute the method...
I'm using Visual Studio 2010, Target Framework 4.0, Excel 2007 and Autocad 2010.
To Open AutoCAD:
string progID = "AutoCAD.Application";
AcadApplication acApp = null;
try
{
Type acType = Type.GetTypeFromProgID(progID);
object o = Activator.CreateInstance(acType, true);
if (o == null)
MessageBox.Show("Error 1!");
acApp = (AcadApplication)o;
if (acApp == null)
MessageBox.Show("Error 2!");
}
catch (System.Exception ex)
{
MessageBox.Show(string.Format("Error opening AutoCAD. Error: {0}", ex.ToString()));
}
return acApp;
To Open Drawing:
AcadDocument doc = acApp.Documents.Open(@"MyDocumentPath", false);
Thanks again
Enrico
Just a wild guess here, since you do not say which line of code causes the error (have you debugged by stepping through the code, so that you know which line of code is the offender?):
Try to remove this line:
aPlot.SetLayoutsToPlot (new string[0]);
According to Acad's VBA/COM API document, this parameter should be either an array of string, which are VALID layout names, or NULL, which leads to active layout to be plotted. However, you pass in an array with 1 element, which is different from a NULL, and that element IS NOT a valid layout name at all.
Even it turns out that this is not the cause of your issue, you still do not need this line of code, because you do call SetLayoutToPlot() correctly rignt before doing the PlotToFile().
BTW, according to your reply to Tony below, you have references set to acadmgd.dll and acmgd.dll. Since your code does the Acad automation from external application, you CANNOT use that two managed Acad assemblies, although they may not be the cause of your issue based on the code you showed. However, if it turned out the actual error was from other part of code if you stepped through in debugging and found out it was because of these two assemblies, I wouldn't be supprised.
Norman Yuan
Just a wild guess here, since you do not say which line of code causes the error (have you debugged by stepping through the code, so that you know which line of code is the offender?):
the line that cause the error is: aPlot.SetLayoutToPlot(ls), where ls is the array with the name of the layer to print
Try to remove this line:
aPlot.SetLayoutsToPlot (new string[0]);
According to Acad's VBA/COM API document, this parameter should be either an array of string, which are VALID layout names, or NULL, which leads to active layout to be plotted. However, you pass in an array with 1 element, which is different from a NULL, and that element
IS NOT a valid layout name at all.
I'm using that line only to erase previous assignment, I just remove it and the problem is not solved
Even it turns out that this is not the cause of your issue, you still do not need this line of code, because you do call SetLayoutToPlot() correctly rignt before doing the PlotToFile().
BTW, according to your reply to Tony below, you have references set to acadmgd.dll and acmgd.dll. Since your code does the Acad automation from external application, you CANNOT use that two managed Acad assemblies, although they may not be the cause of your issue based on the code you showed. However, if it turned out the actual error was from other part of code if you stepped through in debugging and found out it was because of these two assemblies, I wouldn't be supprised.
Just remove every reference not necessary and the problem is still here. My drawing contain 42layer, and sometimes it crash on the 7th, other time on the 23th... It is absolutelly random, but it appear every times...
Enrico
In the mean time I try also by changing
acDoc.SetLayoutToPlots() with acDoc.ActiveLayout = layout (variable layout is the item processed by the foreach statement) but I get always the error.
As attachments there's the error...
Thanks
Enrico
Can't find what you're looking for? Ask the community or share your knowledge.