- public static bool AcDbPlotPlt(Extents2d PlotExtent, string sPlotDeviceName, string sSheetStyleName
- , string sPlotMediaName, string sPlotPath, PlotRotation pRotation, double dPlotScale)
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- doc.LockDocument();
- Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("BACKGROUNDPLOT", 0);
- Editor ed = doc.Editor;
- Database db = doc.Database;
- Transaction tr = db.TransactionManager.StartTransaction();
- using (tr)
- {
- // We'll be plotting the current layout 绘制当前布局
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
- Layout acLayout = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead);
- //lo.PlotPlotStyles = false;
- // We need a PlotInfo object linked to the layout 初始化PlotInfo的对象并与布局关联
- PlotInfo acPlInfo = new PlotInfo();
- acPlInfo.Layout = btr.LayoutId;
- PlotSettings acPlSet = new PlotSettings(acLayout.ModelType); //modeltype 区分布局空间,模型空间
- acPlSet.CopyFrom(acLayout);
- //如果是布局空间,直接默认打印样式为1:1
- if (!acLayout.ModelType)
- dPlotScale = 1;
- // The PlotSettingsValidator helps create a valid PlotSettings object
- PlotSettingsValidator psv = PlotSettingsValidator.Current;
- //设置打印样式
- if (!string.IsNullOrEmpty(sSheetStyleName))
- {
- acPlSet.PlotPlotStyles = true;
- acPlSet.PrintLineweights = true;
- //lo.CurrentStyleSheet = sSheetStyleName;
- psv.SetCurrentStyleSheet(acPlSet, sSheetStyleName);
- }
- //设置打印方向
- psv.SetPlotRotation(acPlSet, pRotation);
- psv.SetPlotConfigurationName(acPlSet, sPlotDeviceName, sPlotMediaName);
- //设置打印尺寸
- //设置图纸单位
- psv.SetPlotPaperUnits(acPlSet, PlotPaperUnit.Millimeters);
- #region>
- psv.SetUseStandardScale(acPlSet, true);
- if (dPlotScale == 1)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To1);
- else if (dPlotScale == 2)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To2);
- else if (dPlotScale == 4)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To4);
- else if (dPlotScale == 8)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To8);
- else if (dPlotScale == 10)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To10);
- else if (dPlotScale == 16)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To16);
- else if (dPlotScale == 20)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To20);
- else if (dPlotScale == 30)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To30);
- else if (dPlotScale == 40)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To40);
- else if (dPlotScale == 50)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To50);
- else if (dPlotScale == 100)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale1To100);
- else if (dPlotScale == 0.5)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale2To1);
- else if (dPlotScale == 0.25)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale4To1);
- else if (dPlotScale == 0.125)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale8To1);
- else if (dPlotScale == 0.1)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale10To1);
- else if (dPlotScale == 0.01)
- psv.SetStdScaleType(acPlSet, StdScaleType.StdScale100To1);
- else if (dPlotScale == 0)
- psv.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);
- else
- {
- psv.SetUseStandardScale(acPlSet, false);
- CustomScale plotScale = new CustomScale(1, dPlotScale);
- psv.SetCustomPrintScale(acPlSet, plotScale);
- }
- #endregion
- //设置打印的范围,中心和比例
- psv.SetPlotWindowArea(acPlSet, PlotExtent);
- // 设置自定义打印偏移
- psv.SetPlotOrigin(acPlSet, new Point2d(0, 0));
- psv.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
- acPlInfo.OverrideSettings = acPlSet;
- // Validate the plot info
- PlotInfoValidator acPlInfoVdr = new PlotInfoValidator();
- acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
- acPlInfoVdr.Validate(acPlInfo);
- // Check to see if a plot is already in progress
- if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
- {
- using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine())
- {
- // Track the plot progress with a Progress dialog
- PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false,
- 1,
- true);
- using (acPlProgDlg)
- {
- // Define the status messages to display when plotting starts
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle,
- "Plot Progress");
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage,
- "Cancel Job");
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage,
- "Cancel Sheet");
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption,
- "Sheet Set Progress");
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption,
- "Sheet Progress");
- // Set the plot progress range
- acPlProgDlg.LowerPlotProgressRange = 0;
- acPlProgDlg.UpperPlotProgressRange = 100;