AutoCAD 2016 Multipage plot crashing at BeginGenerateGraphics()

AutoCAD 2016 Multipage plot crashing at BeginGenerateGraphics()

Anonymous
Not applicable
2,321 Views
12 Replies
Message 1 of 13

AutoCAD 2016 Multipage plot crashing at BeginGenerateGraphics()

Anonymous
Not applicable

I have used below function to create Multipage PDF of all layouts of dwg file. But AutoCAD 2016 crashes when for-loop execute for second layout at BeginGenerateGraphics() line. Same code works perfectly in AutoCAD 2013 

My machine configuration is Windows 7 SP1 64 bit, AutoCAD 2016 64 bit .NET framework 4.5

Please help.

 

[CommandMethod("mplot")]
static public void MultiSheetPlot()
{
Document doc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;

Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt =
(BlockTable)tr.GetObject(
db.BlockTableId,
OpenMode.ForRead
);

// A PlotEngine does the actual plotting
// (can also create one for Preview)

if (PlotFactory.ProcessPlotState ==
ProcessPlotState.NotPlotting)
{
PlotEngine pe =
PlotFactory.CreatePublishEngine();

using (pe)
{
// Create a Progress Dialog to provide info
// and allow thej user to cancel

ObjectIdCollection layoutsToPlot =
new ObjectIdCollection();

foreach (ObjectId btrId in bt)
{

BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
btrId,
OpenMode.ForRead
);
if (btr.IsLayout &&
btr.Name.ToUpper() !=
BlockTableRecord.ModelSpace.ToUpper())
{
layoutsToPlot.Add(btrId);
}
}

PlotProgressDialog ppd =
new PlotProgressDialog(false, layoutsToPlot.Count, true);
using (ppd)
{

int numSheet = 1;

foreach (ObjectId btrId in layoutsToPlot)
{

PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy =
MatchingPolicy.MatchEnabled;

BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
btrId,
OpenMode.ForRead
);
Layout lo =
(Layout)tr.GetObject(
btr.LayoutId,
OpenMode.ForRead
);
PlotInfo pi = new PlotInfo() { Layout = lo.ObjectId }; ;

// We need a PlotSettings object
// based on the layout settings
// which we then customize

LayoutManager.Current.CurrentLayout = lo.LayoutName;

PlotSettings ps =
new PlotSettings(lo.ModelType);
ps.CopyFrom(lo);


// The PlotSettingsValidator helps
// create a valid PlotSettings object

PlotSettingsValidator psv =
PlotSettingsValidator.Current;

// We'll plot the extents, centered and
// scaled to fit

psv.SetPlotType(
ps,
Autodesk.AutoCAD.DatabaseServices.PlotType.Extents
);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
psv.SetPlotCentered(ps, true);

// We'll use the standard DWFx PC3, as
// this supports multiple sheets

psv.SetPlotConfigurationName(
ps,
//"DWFx ePlot (XPS Compatible).pc3",
"DWG To PDF.pc3",
"ANSI_A_(8.50_x_11.00_Inches)"
);

// We need a PlotInfo object
// linked to the layout

pi.Layout = btr.LayoutId;

// Make the layout we're plotting current

LayoutManager.Current.CurrentLayout =
lo.LayoutName;

LayoutManager.Current.SetCurrentLayoutId(btr.LayoutId);

// We need to link the PlotInfo to the
// PlotSettings and then validate it

pi.OverrideSettings = ps;
piv.Validate(pi);

if (numSheet == 1)
{
ppd.set_PlotMsgString(
PlotMessageIndex.DialogTitle,
"Custom Plot Progress"
);
ppd.set_PlotMsgString(
PlotMessageIndex.CancelJobButtonMessage,
"Cancel Job"
);
ppd.set_PlotMsgString(
PlotMessageIndex.CancelSheetButtonMessage,
"Cancel Sheet"
);
ppd.set_PlotMsgString(
PlotMessageIndex.SheetSetProgressCaption,
"Sheet Set Progress"
);
ppd.set_PlotMsgString(
PlotMessageIndex.SheetProgressCaption,
"Sheet Progress"
);
ppd.LowerPlotProgressRange = 0;
ppd.UpperPlotProgressRange = 100;
ppd.PlotProgressPos = 0;

// Let's start the plot, at last

ppd.OnBeginPlot();
ppd.IsVisible = true;
pe.BeginPlot(ppd, null);

// We'll be plotting a single document

pe.BeginDocument(
pi,
doc.Name,
null,
1,
true, // Let's plot to file
"E:\\test-multi-sheet"
);
}

// Which may contain multiple sheets

ppd.StatusMsgString =
"Plotting " +
doc.Name.Substring(
doc.Name.LastIndexOf("\\") + 1
) +
" - sheet " + numSheet.ToString() +
" of " + layoutsToPlot.Count.ToString();

ppd.OnBeginSheet();

ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = 100;
ppd.SheetProgressPos = 0;

PlotPageInfo ppi = new PlotPageInfo();
pe.BeginPage(
ppi,
pi,
(numSheet == layoutsToPlot.Count),
null
);
pe.BeginGenerateGraphics(null);
ppd.SheetProgressPos = 50;
pe.EndGenerateGraphics(null);

// Finish the sheet
pe.EndPage(null);

ppd.SheetProgressPos = 100;
ppd.OnEndSheet();

bool ok = pe.IsDisposed;

numSheet++;
}

// Finish the document

pe.EndDocument(null);

// And finish the plot

ppd.PlotProgressPos = 100;
ppd.OnEndPlot();
pe.EndPlot(null);

pe.Dispose();
}
}
}
else
{
ed.WriteMessage(
"\nAnother plot is in progress."
);
}
}
}
}

0 Likes
2,322 Views
12 Replies
Replies (12)
Message 2 of 13

FRFR1426
Collaborator
Collaborator

Not sure it's the root of your problem, but you are calling pe.Dispose() twice. You use an using and you call explicitly dispose. 

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 3 of 13

Anonymous
Not applicable

Hello Maxene,

 

Thanks for the reply. The same code works perfectly in AutoCAD 2013 64 bit (Windows 7 SP1) but if I use AutoCAD 2016 64 bit (Windows 7 SP1) it crashes. Please see attached image of Fatal Error.

 

Please help.

 

Thanks,

Pushpka

0 Likes
Message 4 of 13

FRFR1426
Collaborator
Collaborator

May be you are plotting in the background in 2016. Try to put this line at the top to plot in the foreground:

 

Application.SetSystemVariable("BACKGROUNDPLOT", 0);
Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 5 of 13

Anonymous
Not applicable

Hello Maxence,

 

I have added this line at top but still same result.. AutoCAD crash.

Application.SetSystemVariable("BACKGROUNDPLOT", 0);

Surprisingly it works perfectly AutoCAD 2013.

 

Please help.. I am really fed up now... 

😞

0 Likes
Message 6 of 13

FRFR1426
Collaborator
Collaborator

pushpakp a écrit :

 

 

Surprisingly it works perfectly AutoCAD 2013.

 

There is nothing surprising in that. You know, code changes between releases... I'm afraid I cannot help you more, I've run out of ideas and I cannot spend more time on this issue, sorry.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 7 of 13

Anonymous
Not applicable

Thanks for the reply.

 

Please see attached image from the forum http://through-the-interface.typepad.com/through_the_interface/2007/09/driving-a-multi.html

 

Someone reports the same problem but Kean Walmsley mentioned that he could not observe the issue.

 

Can you please help on it further.

 

Thanks in advance

0 Likes
Message 8 of 13

Anonymous
Not applicable

I tried same above code in the Autodesk 2016 (64 bit) Windows 7 SP1 Microsoft Visual Studio 2015 (same install as my machine), it works without crash.

 

What could be the reason?

 

Thanks in advance.

 

 

0 Likes
Message 9 of 13

FRFR1426
Collaborator
Collaborator
Sometimes, programs can behave differently when there is a debugger attached.
Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 10 of 13

Anonymous
Not applicable

I tried with debugger but same code works. Don't know the reason.

0 Likes
Message 11 of 13

Anonymous
Not applicable

Did you find the answer? I have the same problem.

0 Likes
Message 12 of 13

John204
Participant
Participant

We had the same problem with the BeginGenerateGraphics method and found that the problem is with Service Pack 1 for AutoCAD 2016.  When we uninstalled SP1 for AutoCAD 2016 our program worked fine.  We were wondering if anyone knows if this is fixed in AutoCAD 2018 or 2019 since we are considering an upgrade soon?

0 Likes
Message 13 of 13

lifanvc
Contributor
Contributor

hi , I have the game question, how to do ? can you help me ?

0 Likes