static void PlotBlockColToPDF()
{
String PrinterName = "DWG To PDF.pc3";
String OutPath = "f:\\333.pdf";
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
Object SysVarBackPlot = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("BACKGROUNDPLOT");
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("BACKGROUNDPLOT", 0);
using (tr)
{
List<Entity> sortedPolylines = PrintAll();
int polylineCount = sortedPolylines.Count;
if (polylineCount < 1) return;
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
PlotEngine pe = PlotFactory.CreatePublishEngine();
using (pe)
{
PlotProgressDialog ppd = new PlotProgressDialog(false, sortedPolylines.Count, true);
using (ppd)
{
int numSheet = 1;
// Setting up the PlotProgress dialog
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;
ppd.OnBeginPlot();
ppd.IsVisible = true;
pe.BeginPlot(ppd, null);
foreach (Entity ent in sortedPolylines)
{
// Starting new page
ppd.StatusMsgString = "Plotting block " + numSheet.ToString() + " of " + sortedPolylines.Count.ToString();
ppd.OnBeginSheet();
ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = 100;
ppd.SheetProgressPos = 0;
PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
PlotPageInfo ppi = new PlotPageInfo();
PlotInfo pi = new PlotInfo();
// We'll be plotting the current layout
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
Layout lo = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead);
Autodesk.AutoCAD.DatabaseServices.Layout acadLayout = (Autodesk.AutoCAD.DatabaseServices.Layout)lo;
// получаем координаты для печати
Polyline polyline = (Polyline)ent;
// Get the bounding box of the polyline
Extents3d extents = polyline.GeometricExtents;
Point3d minPoint = extents.MinPoint;
Point3d maxPoint = extents.MaxPoint;
//подставляем полученные точки превращая их в 2д
Point2d first = new Point2d(minPoint.X, minPoint.Y);
Point2d second = new Point2d(maxPoint.X, maxPoint.Y);
Extents2d window = new Extents2d(first, second);
//получаем название листа из его размеров
string Format_lista = GetFormatList(minPoint, maxPoint);
string canonocalName = GetCanonicalName(Format_lista);
String PaperSize = canonocalName;
// Get a copy of the PlotSettings from the layout
PlotSettings ps = new PlotSettings(lo.ModelType);
pi.Layout = btr.LayoutId;
ps.CopyFrom(lo);
// The PlotSettingsValidator helps create a valid PlotSettings object
PlotSettingsValidator psv = PlotSettingsValidator.Current;
psv.SetPlotWindowArea(ps, window);
psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
psv.SetPlotPaperUnits(ps, PlotPaperUnit.Millimeters);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.StdScale1To1);
psv.SetPlotCentered(ps, true);
psv.SetPlotConfigurationName(ps, PrinterName, PaperSize);
// поворачиваем лист при печати в альбомном формате
if (Format_lista.Contains("_goriz"))
{
psv.SetPlotRotation(ps, PlotRotation.Degrees090);
}
else if (Format_lista.Contains("_vert"))
{
psv.SetPlotRotation(ps, PlotRotation.Degrees000);
}
pi.OverrideSettings = ps;
piv.Validate(pi);
if (numSheet == 1) pe.BeginDocument(pi, doc.Name, null, 1, true, OutPath); // Create document for the first page
// Plot the window
pe.BeginPage(ppi, pi, numSheet == sortedPolylines.Count, null);
pe.BeginGenerateGraphics(null);
ppd.SheetProgressPos = 50;
pe.EndGenerateGraphics(null);
// Finish the sheet
pe.EndPage(null);
ppd.SheetProgressPos = 100;
ppd.PlotProgressPos += Convert.ToInt32(100 / sortedPolylines.Count);
ppd.OnEndSheet();
numSheet++;
}
// Finish the document and finish the plot
pe.EndDocument(null);
ppd.PlotProgressPos = 100;
ppd.OnEndPlot();
pe.EndPlot(null);
ed.WriteMessage("\nPlot completed successfully!\n\n");
}
}
}
else
{
ed.WriteMessage("\nAnother plot is in progress.\n\n");
}
tr.Commit();
}
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("BACKGROUNDPLOT", SysVarBackPlot);
}