static public void ExportPDF() { var doc = AcApp.DocumentManager.MdiActiveDocument; var ed = doc.Editor; ExportForm exportForm = new ExportForm(); var result = AcApp.ShowModalDialog(exportForm); if (result == DialogResult.Cancel) { ed.WriteMessage($"{Environment.NewLine}The operation has been canceled by the user."); return; } int layoutCount = exportForm.SelectedLayouts.Count; ed.WriteMessage($"{Environment.NewLine}Number of selected layouts: {layoutCount}"); if (layoutCount == 0) { ed.WriteMessage($"{Environment.NewLine}No layouts were detected."); return; } else if (layoutCount == 1) { if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting) { // First we preview... PreviewEndPlotStatus stat; PlotEngine pre = PlotFactory.CreatePreviewEngine((int)PreviewEngineFlags.Plot); using (pre) { stat = PlotOrPreview(pre, true, exportForm.SelectedLayouts[0].BlockTableRecordId, "", ""); } if (stat == PreviewEndPlotStatus.Plot) { // And if the user asks, we plot... var jobNumber = Path.GetFileNameWithoutExtension(doc.Name); if (jobNumber.Length > 9) { jobNumber = jobNumber.Remove(9); } if (!JobNumber.TryParse(jobNumber)) { ed.WriteMessage("Job number could not be determined." + Environment.NewLine); return; } string path = JobNumber.GetPath(jobNumber); if (Directory.Exists(path)) { if (!Directory.Exists(path + "\\Submittals")) { Directory.CreateDirectory(path + "\\Submittals"); } PlotEngine ple = PlotFactory.CreatePublishEngine(); PlotOrPreview(ple, false, exportForm.SelectedLayouts[0].BlockTableRecordId, path + "\\Submittals", jobNumber); ed.WriteMessage($"{Environment.NewLine}Plot created."); } else { ed.WriteMessage("Project folder could not be determined." + Environment.NewLine); } } } else { ed.WriteMessage( "\nAnother plot is in progress." ); } } else { ObjectIdCollection layouts = new ObjectIdCollection(); foreach (Layout layout in exportForm.SelectedLayouts) { layouts.Add(layout.BlockTableRecordId); } if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting) { int layoutNum = 0; bool isFinished = false; bool isReadyForPlot = false; while (!isFinished) { // Create the preview engine with the appropriate // buttons enabled - this depends on which // layout in the list is being previewed PreviewEngineFlags flags = PreviewEngineFlags.Plot; if (layoutNum > 0) { flags |= PreviewEngineFlags.PreviousSheet; } if (layoutNum < layouts.Count - 1) { flags |= PreviewEngineFlags.NextSheet; } using (PlotEngine pre = PlotFactory.CreatePreviewEngine((int)flags)) { PreviewEndPlotStatus stat = MultiplePlotOrPreview( pre, true, layouts, layoutNum, "", "" ); // We're not checking the list bounds for // next/previous as the buttons are only shown // when they can be used if (stat == PreviewEndPlotStatus.Next) { layoutNum++; } else if (stat == PreviewEndPlotStatus.Previous) { layoutNum--; } else if (stat == PreviewEndPlotStatus.Normal || stat == PreviewEndPlotStatus.Cancel) { isFinished = true; } else if (stat == PreviewEndPlotStatus.Plot) { isFinished = true; isReadyForPlot = true; } } } // If the plot button was used to exit the preview... if (isReadyForPlot) { var jobNumber = Path.GetFileNameWithoutExtension(doc.Name); if (jobNumber.Length > 9) { jobNumber = jobNumber.Remove(9); } if (!JobNumber.TryParse(jobNumber)) { ed.WriteMessage("Job number could not be determined." + Environment.NewLine); return; } string path = JobNumber.GetPath(jobNumber); if (Directory.Exists(path)) { if (!Directory.Exists(path + "\\Submittals")) { Directory.CreateDirectory(path + "\\Submittals"); } using (PlotEngine ple = PlotFactory.CreatePublishEngine()) { PreviewEndPlotStatus stat = MultiplePlotOrPreview(ple, false, layouts, -1, path + "\\Submittals", jobNumber); } } } } else { ed.WriteMessage( "\nAnother plot is in progress." ); } } exportForm.Dispose(); }
/// <summary> /// 打印预览 /// </summary> /// <returns></returns> public static bool Preview( Dictionary<ObjectId, List<Extents2d>> plotAreaDict, string plotDevice, string plotCanonicalMeida, string plotStyle, string saveFileName, bool isPlotSingle//是否每页单独保存 ) { Database db = HostApplicationServices.WorkingDatabase; Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; bool ret = false; if (plotAreaDict.Count == 0) return true; #region 准备打印区域列表 PlotList Dictionary<Extents2d, ObjectId> PlotList = new Dictionary<Extents2d, ObjectId>(); foreach (KeyValuePair<ObjectId, List<Extents2d>> kv in plotAreaDict) { foreach (Extents2d plotArea in kv.Value) { PlotList.Add(plotArea, kv.Key); } } #endregion if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting) { #region 设置为前台打印 //保存App的原参数 short bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT"); //设定为前台打印,加快打印速度 Application.SetSystemVariable("BACKGROUNDPLOT", 0); //object BackGroundPlotVar = mFun.m_GetSystemVar("BACKGROUNDPLOT"); //mFun.m_SetSystemVar("BACKGROUNDPLOT", 0);//一定要设置成0(前台打印),否则保存PDF文件中一个布局只会有一张图!!!! #endregion int sheetNum = 0; bool isFinished = false;//预览是否结束 bool isReadyForPlot = false;//是否准备好打印 while (!isFinished) { PreviewEngineFlags flags = PreviewEngineFlags.Plot; if (sheetNum > 0) flags |= PreviewEngineFlags.PreviousSheet; if (sheetNum < PlotList.Count - 1) flags |= PreviewEngineFlags.NextSheet; using (PlotEngine pe = PlotFactory.CreatePreviewEngine((int)flags)) { PreviewEndPlotStatus stat = MultiPlotOrPreview( pe, true, PlotList, sheetNum, plotAreaDict.Count, plotDevice, plotCanonicalMeida, plotStyle, saveFileName); if (stat == PreviewEndPlotStatus.Next) { sheetNum++; } else if (stat == PreviewEndPlotStatus.Previous) { sheetNum--; } else if (stat == PreviewEndPlotStatus.Normal || stat == PreviewEndPlotStatus.Cancel) { isFinished = true; } else if (stat == PreviewEndPlotStatus.Plot) { isFinished = true; isReadyForPlot = true; ret = true;//结束 } } } // If the plot button was used to exit the preview... if (isReadyForPlot) { if (!isPlotSingle) { using (PlotEngine pe = PlotFactory.CreatePublishEngine()) { PreviewEndPlotStatus stat = MultiPlotOrPreview( pe, false, PlotList, -1, plotAreaDict.Count, plotDevice, plotCanonicalMeida, plotStyle, saveFileName ); ret = stat == PreviewEndPlotStatus.Cancel ? false : true; } } else { #region 每页打印成一个PDF文件 foreach (KeyValuePair<ObjectId, List<Extents2d>> kv in plotAreaDict) { int i = 1; foreach (Extents2d plotArea in kv.Value) { PlotSinglePage( kv.Key, plotArea, plotDevice, plotCanonicalMeida, plotStyle, string.Format("{0}-{1}({2})", saveFileName, wmLayout.GetLayoutName(kv.Key), i++)); } } #endregion } } //恢复变量 Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot); } else { ed.WriteMessage("\n其他打印正在进行中!"); } return ret; }