/// <summary>
/// Печать объекта области (region)
/// </summary>
/// <param name="regionId">Идентификатор области, подлежащей публикации.
/// </param>
/// <param name="pcsFileName">Наименование PC3 файла</param>
/// <param name="mediaName">Наименование выбранного формата листа</param>
/// <param name="outputFileName">Имя файла, в котором сохраняется результат
/// печати.</param>
public static void PlotRegion(Db.ObjectId regionId, String pcsFileName,
String mediaName, String outputFileName) {
if(regionId.IsNull)
throw new ArgumentException("regionId.IsNull == true");
if(!regionId.IsValid)
throw new ArgumentException("regionId.IsValid == false");
if(regionId.ObjectClass.Name != "AcDbRegion")
throw new ArgumentException("regionId.ObjectClass.Name != AcDbRegion");
if(pcsFileName == null)
throw new ArgumentNullException("pcsFileName");
if(pcsFileName.Trim() == String.Empty)
throw new ArgumentException("pcsFileName.Trim() == String.Empty");
if(mediaName == null)
throw new ArgumentNullException("mediaName");
if(mediaName.Trim() == String.Empty)
throw new ArgumentException("mediaName.Trim() == String.Empty");
if(outputFileName == null)
throw new ArgumentNullException("outputFileName");
if(outputFileName.Trim() == String.Empty)
throw new ArgumentException("outputFileName.Trim() == String.Empty");
Db.Database previewDb = Hs.WorkingDatabase;
Db.Database db = null;
Ap.Document doc = cad.DocumentManager.MdiActiveDocument;
if(doc == null || doc.IsDisposed)
return;
Ed.Editor ed = doc.Editor;
try {
if(regionId.Database != null && !regionId.Database.IsDisposed) {
Hs.WorkingDatabase = regionId.Database;
db = regionId.Database;
}
else {
db = doc.Database;
}
using(doc.LockDocument()) {
using(Db.Transaction tr = db.TransactionManager.StartTransaction()) {
Db.Region region = tr.GetObject(regionId,
Db.OpenMode.ForRead) as Db.Region;
Db.Extents3d extends = region.GeometricExtents;
Db.ObjectId modelId = Us.GetBlockModelSpaceId(db);
Db.BlockTableRecord model = tr.GetObject(modelId,
Db.OpenMode.ForRead) as Db.BlockTableRecord;
Db.Layout layout = tr.GetObject(model.LayoutId,
Db.OpenMode.ForRead) as Db.Layout;
using(Pt.PlotInfo pi = new Pt.PlotInfo()) {
pi.Layout = model.LayoutId;
using(Db.PlotSettings ps = new Db.PlotSettings(layout.ModelType)
) {
Db.PlotSettingsValidator psv = Db.PlotSettingsValidator
.Current;
Gm.Point2d bottomLeft = Gm.Point2d.Origin;
Gm.Point2d topRight = Gm.Point2d.Origin;
region.GetVisualBoundary(RegionTools.Delta, ref bottomLeft,
ref topRight);
Db.Extents2d extents = new Db.Extents2d(bottomLeft.X,
bottomLeft.Y, topRight.X, topRight.Y);
// Прежде чем методу SetPlotType присвоить в качестве второго
// параметра значение PlotType.Window, необходимо вызвать метод
// SetPlotWindowArea, указав через него границы Window.
// В противном случае возникнет runtime error.
psv.SetPlotWindowArea(ps, extents);
psv.SetPlotType(ps, Db.PlotType.Window);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, Db.StdScaleType.ScaleToFit);
psv.SetPlotCentered(ps, true);
// We'll use the standard DWF PC3, as
// for today we're just plotting to file
psv.SetPlotConfigurationName(ps, pcsFileName, mediaName);
// We need to link the PlotInfo to the
// PlotSettings and then validate it
pi.OverrideSettings = ps;
Pt.PlotInfoValidator piv = new Pt.PlotInfoValidator();
piv.MediaMatchingPolicy = Pt.MatchingPolicy.MatchEnabled;
piv.Validate(pi);
// A PlotEngine does the actual plotting
// (can also create one for Preview)
if(Pt.PlotFactory.ProcessPlotState == Pt.ProcessPlotState
.NotPlotting) {
Pt.PlotEngine pe = Pt.PlotFactory.CreatePublishEngine();
using(pe) {
// Create a Progress Dialog to provide info
// and allow thej user to cancel
using(Pt.PlotProgressDialog ppd =
new Pt.PlotProgressDialog(false, 1, true)) {
ppd.set_PlotMsgString(
Pt.PlotMessageIndex.DialogTitle, "Custom Plot Progress");
ppd.set_PlotMsgString(
Pt.PlotMessageIndex.CancelJobButtonMessage,
"Cancel Job");
ppd.set_PlotMsgString(
Pt.PlotMessageIndex.CancelSheetButtonMessage,
"Cancel Sheet");
ppd.set_PlotMsgString(
Pt.PlotMessageIndex.SheetSetProgressCaption,
"Sheet Set Progress");
ppd.set_PlotMsgString(
Pt.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
outputFileName);
// Which contains a single sheet
ppd.OnBeginSheet();
ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = 100;
ppd.SheetProgressPos = 0;
Pt.PlotPageInfo ppi = new Pt.PlotPageInfo();
pe.BeginPage(ppi, pi, true, null);
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
// Finish the sheet
pe.EndPage(null);
ppd.SheetProgressPos = 100;
ppd.OnEndSheet();
// Finish the document
pe.EndDocument(null);
// And finish the plot
ppd.PlotProgressPos = 100;
ppd.OnEndPlot();
pe.EndPlot(null);
}
}
}
else {
ed.WriteMessage("\nAnother plot is in progress.");
}
}
}
tr.Commit();
}
}
}
finally {
Hs.WorkingDatabase = previewDb;
}
}