using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.Publishing;
using Autodesk.AutoCAD.Runtime;
[CommandMethod("mplot")]
static public void MultiSheetPlot()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
using (doc.LockDocument())
{
Editor ed = doc.Editor;
Database db = doc.Database;
Transaction trans = db.TransactionManager.StartTransaction();
using (trans)
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// A PlotEngine does the actual plotting
// (can also create one for Preview)
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
PlotEngine plotengine = PlotFactory.CreatePublishEngine();
using (plotengine)
{
ObjectIdCollection layoutsToPlot = new ObjectIdCollection();
List<Layout> listlayout = new List<Layout>();
DBDictionary layoutdic= (DBDictionary)db.LayoutDictionaryId.GetObject(OpenMode.ForRead);
// Create a Progress Dialog to provide info
// and allow thej user to cancel
foreach (DBDictionaryEntry entry in layoutdic)
{
if (entry.Key != "Model")
listlayout.Add((Layout)trans.GetObject(entry.Value, OpenMode.ForRead));
}
listlayout.Sort((l1, l2) => l1.TabOrder.CompareTo(l2.TabOrder));
layoutsToPlot = new ObjectIdCollection(listlayout.Select(l => l.BlockTableRecordId).ToArray());
PlotProgressDialog plotprogressdialog = new PlotProgressDialog(false, layoutsToPlot.Count, true);
using (plotprogressdialog)
{
//ObjectIdCollection layoutsToPlot = new ObjectIdCollection();
int numSheet = 1;
foreach (ObjectId btrId in layoutsToPlot)
{
PlotInfo plotinfo = new PlotInfo();
PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabledCustom;
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForRead);
Layout lo = (Layout)trans.GetObject(btr.LayoutId, OpenMode.ForRead);
// We need a PlotSettings object
// based on the layout settings
// which we then customize
PlotSettings plotset = new PlotSettings(lo.ModelType);
plotset.CopyFrom(lo);
// The PlotSettingsValidator helps
// create a valid PlotSettings object
PlotSettingsValidator plotsetvalid = PlotSettingsValidator.Current;
// We'll plot the extents, centered and
// scaled to fit
plotsetvalid.SetPlotType(plotset, Autodesk.AutoCAD.DatabaseServices.PlotType.Layout);
plotsetvalid.SetUseStandardScale(plotset, true);
plotsetvalid.SetStdScaleType(plotset, StdScaleType.StdScale1To1);
// We'll use the standard DWFx PC3, as
// this supports multiple sheets
try
{
plotsetvalid.SetPlotConfigurationName(plotset, "DWG To PDF.pc3", lo.CanonicalMediaName);
}
catch (System.Exception)
{
System.Windows.MessageBox.Show("Для листа " + lo.LayoutName.ToUpper() + " не удалось определить формат листа и назначить ему настройки печати." +
"\n Принтер должен быть DWG to PDF.pc3" +
"\n " + lo.CanonicalMediaName +
"\n Обратитесь в поддержку.", "ERROR", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
return;
}
// We need a PlotInfo object
// linked to the layout
// Make the layout we're plotting current
LayoutManager.Current.CurrentLayout = lo.LayoutName;
// We need to link the PlotInfo to the
// PlotSettings and then validate it
plotinfo.OverrideSettings = plotset;
plotinfo.Layout = btr.LayoutId;
piv.Validate(plotinfo);
if (numSheet == 1)
{
plotprogressdialog.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Custom Plot Progress");
plotprogressdialog.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
plotprogressdialog.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
plotprogressdialog.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
plotprogressdialog.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
plotprogressdialog.LowerPlotProgressRange = 0;
plotprogressdialog.UpperPlotProgressRange = 100;
plotprogressdialog.PlotProgressPos = 0;
// Let's start the plot, at last
plotprogressdialog.OnBeginPlot();
plotprogressdialog.IsVisible = true;
plotengine.BeginPlot(plotprogressdialog, null);
// We'll be plotting a single document
plotengine.BeginDocument(plotinfo, doc.Name, null, 1, true, doc.Name.Replace("dwg", "PDF"));
}
// Which may contain multiple sheets
plotprogressdialog.StatusMsgString = "Plotting " + doc.Name.Substring(doc.Name.LastIndexOf("\\") + 1) + " - sheet " + numSheet.ToString() +
" of " + layoutsToPlot.Count.ToString();
plotprogressdialog.OnBeginSheet();
plotprogressdialog.LowerSheetProgressRange = 0;
plotprogressdialog.UpperSheetProgressRange = 100;
plotprogressdialog.SheetProgressPos = 0;
PlotPageInfo plotpageinfo = new PlotPageInfo();
try
{
plotengine.BeginPage(plotpageinfo, plotinfo, numSheet == layoutsToPlot.Count, null);
}
catch (System.Exception)
{
System.Windows.MessageBox.Show("Ошибки с настройкой листа " + lo.LayoutName.ToUpper() + " при печати. Обратитесь в поддержку.", "ERROR",
System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
return;
}
plotengine.BeginGenerateGraphics(null);
plotprogressdialog.SheetProgressPos = 50;
plotengine.EndGenerateGraphics(null);
// Finish the sheet
plotengine.EndPage(null);
plotprogressdialog.SheetProgressPos = 100;
plotprogressdialog.OnEndSheet();
numSheet++;
}
// Finish the document
plotengine.EndDocument(null);
// And finish the plot
plotprogressdialog.PlotProgressPos = 100;
plotprogressdialog.OnEndPlot();
plotengine.EndPlot(null);
}
}
}
else
{
ed.WriteMessage("\nAnother plot is in progress.");
}
trans.Commit();
}
}
}