/// <summary> /// 打印 /// </summary> /// <param name="plotAreaDict"></param> /// <param name="plotDevice"></param> /// <param name="plotCanonicalMeida"></param> /// <param name="plotStyle"></param> /// <param name="saveFileName"></param> /// <returns></returns> public static bool Plot( Dictionary<ObjectId, List<Extents2d>> plotAreaDict, string plotDevice, string plotCanonicalMeida, string plotStyle, string saveFileName ) { bool ret = true; 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 设置为前台打印 object BackGroundPlotVar = mFun.m_GetSystemVar("BACKGROUNDPLOT"); mFun.m_SetSystemVar("BACKGROUNDPLOT", 0);//一定要设置成0(前台打印),否则保存PDF文件中一个布局只会有一张图!!!! #endregion using (PlotEngine pe = PlotFactory.CreatePublishEngine()) { PreviewEndPlotStatus stat = MultiPlotOrPreview( pe, false, PlotList, -1, plotAreaDict.Count, plotDevice, plotCanonicalMeida, plotStyle, saveFileName ); ret = stat == PreviewEndPlotStatus.Cancel ? false : true; } mFun.m_SetSystemVar("BACKGROUNDPLOT", BackGroundPlotVar); } else { mCommands.m_ed.WriteMessage("\n其他打印正在进行中!"); } return ret; }
static PreviewEndPlotStatus MultiplePlotOrPreview(PlotEngine pe, bool isPreview, ObjectIdCollection layoutSet, int layoutNumIfPreview, string dirPath, string jobnumber) { Document doc = AcApp.DocumentManager.MdiActiveDocument; Database db = doc.Database; PreviewEndPlotStatus ret = PreviewEndPlotStatus.Cancel; ObjectIdCollection layoutsToPlot; if (isPreview && layoutNumIfPreview >= 0) { // Preview is really pre-sheet, so we reduce the // sheet collection to contain the one we want layoutsToPlot = new ObjectIdCollection { layoutSet[layoutNumIfPreview] }; } else { // If we're plotting we need all the sheets, // so copy the ObjectIds across ObjectId[] ids = new ObjectId[layoutSet.Count]; layoutSet.CopyTo(ids, 0); layoutsToPlot = new ObjectIdCollection(ids); } using (Transaction tr = db.TransactionManager.StartTransaction()) { // Create a Progress Dialog to provide info // and allow thej user to cancel using (PlotProgressDialog ppd = new PlotProgressDialog(isPreview, layoutsToPlot.Count, true)) { int numSheet = 1; foreach (ObjectId btrId in layoutsToPlot) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead); Layout lo = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead); // We need a PlotSettings object // based on the layout settings // which we then customize PlotSettings ps = new PlotSettings(lo.ModelType); ps.CopyFrom(lo); // The PlotSettingsValidator helps // create a valid PlotSettings object PlotSettingsValidator psv = PlotSettingsValidator.Current; // We need a PlotInfo object // linked to the layout PlotInfo pi = new PlotInfo { Layout = btr.LayoutId }; // 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 pi.OverrideSettings = ps; PlotInfoValidator piv = new PlotInfoValidator { MediaMatchingPolicy = MatchingPolicy.MatchEnabled }; piv.Validate(pi); // We set the sheet name per sheet ppd.set_PlotMsgString(PlotMessageIndex.SheetName, doc.Name.Substring(doc.Name.LastIndexOf("\\") + 1) + " - " + lo.LayoutName); if (numSheet == 1) { // All other messages get set once ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Custom Preview 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; // Let's start the plot/preview, at last ppd.OnBeginPlot(); ppd.IsVisible = true; pe.BeginPlot(ppd, null); // We'll be plotting a single document string file = dirPath + "\\" + jobnumber + " Combined " + DateTime.Now.ToString("MM-dd-yy"); pe.BeginDocument(pi, doc.Name, null, 1, !isPreview, file); } // Which may contains multiple sheets ppd.LowerSheetProgressRange = 0; ppd.UpperSheetProgressRange = 100; ppd.SheetProgressPos = 0; PlotPageInfo ppi = new PlotPageInfo(); pe.BeginPage(ppi, pi, (numSheet == layoutsToPlot.Count), null); ppd.OnBeginSheet(); pe.BeginGenerateGraphics(null); ppd.SheetProgressPos = 50; pe.EndGenerateGraphics(null); // Finish the sheet PreviewEndPlotInfo pepi = new PreviewEndPlotInfo(); pe.EndPage(pepi); ret = pepi.Status; ppd.SheetProgressPos = 100; ppd.OnEndSheet(); numSheet++; // Update the overall progress ppd.PlotProgressPos += (100 / layoutsToPlot.Count); } // Finish the document pe.EndDocument(null); // And finish the plot ppd.PlotProgressPos = 100; ppd.OnEndPlot(); pe.EndPlot(null); } } return(ret); }
static PreviewEndPlotStatus PlotOrPreview(PlotEngine pe, bool isPreview, ObjectId layout, string dirPath, string jobnumber) { Document doc = AcApp.DocumentManager.MdiActiveDocument; Database db = doc.Database; PreviewEndPlotStatus ret = PreviewEndPlotStatus.Cancel; using (Transaction tr = db.TransactionManager.StartTransaction()) { // We'll be plotting the current layout BlockTableRecord btr = (BlockTableRecord)tr.GetObject(layout, OpenMode.ForRead); Layout lo = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead); PlotSettings ps = new PlotSettings(lo.ModelType); ps.CopyFrom(lo); PlotInfo pi = new PlotInfo { Layout = btr.LayoutId }; // 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 pi.OverrideSettings = ps; PlotInfoValidator piv = new PlotInfoValidator { MediaMatchingPolicy = MatchingPolicy.MatchEnabled }; piv.Validate(pi); /* * // We need a PlotInfo object * // linked to the layout * PlotInfo pi = new PlotInfo(); * pi.Layout = lo.BlockTableRecordId; * * // We need a PlotSettings object * // based on the layout settings * // which we then customize * PlotSettings ps = new PlotSettings(lo.ModelType); * ps.CopyFrom(lo); * * // The PlotSettingsValidator helps * // create a valid PlotSettings object * PlotSettingsValidator psv = PlotSettingsValidator.Current; * * // We need to link the PlotInfo to the * // PlotSettings and then validate it * pi.OverrideSettings = ps; * PlotInfoValidator piv = new PlotInfoValidator(); * piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled; * piv.Validate(pi);*/ // Create a Progress Dialog to provide info // and allow thej user to cancel PlotProgressDialog ppd = new PlotProgressDialog(isPreview, 1, true); using (ppd) { ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Custom Preview Progress"); ppd.set_PlotMsgString(PlotMessageIndex.SheetName, doc.Name.Substring(doc.Name.LastIndexOf("\\") + 1)); 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; // Let's start the plot/preview, at last ppd.OnBeginPlot(); ppd.IsVisible = true; pe.BeginPlot(ppd, null); // We'll be plotting/previewing // a single document string file = dirPath + "\\" + jobnumber + " " + lo.LayoutName + " " + DateTime.Now.ToString("MM-dd-yy"); pe.BeginDocument(pi, doc.Name, null, 1, !isPreview, file); // Which contains a single sheet ppd.OnBeginSheet(); ppd.LowerSheetProgressRange = 0; ppd.UpperSheetProgressRange = 100; ppd.SheetProgressPos = 0; PlotPageInfo ppi = new PlotPageInfo(); pe.BeginPage(ppi, pi, true, null); pe.BeginGenerateGraphics(null); ppd.SheetProgressPos = 50; pe.EndGenerateGraphics(null); // Finish the sheet PreviewEndPlotInfo pepi = new PreviewEndPlotInfo(); pe.EndPage(pepi); ret = pepi.Status; ppd.SheetProgressPos = 100; ppd.OnEndSheet(); // Finish the document pe.EndDocument(null); // And finish the plot ppd.PlotProgressPos = 100; ppd.OnEndPlot(); pe.EndPlot(null); } // Committing is cheaper than aborting tr.Commit(); } return(ret); }
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> /// <param name="engine">打印引擎对象</param> /// <param name="layout">要打印的布局</param> /// <param name="ps">打印设置</param> /// <param name="fileName">打印文件名</param> /// <param name="copies">打印份数</param> /// <param name="isPreview">是否预览打印</param> /// <param name="showProgressDialog">是否显示打印进度框</param> /// <param name="plotToFile">是否打印到文件</param> /// <returns>返回打印状态</returns> public static PreviewEndPlotStatus Plot(this PlotEngine engine, Layout layout, PlotSettings ps, string fileName, int copies, bool isPreview, bool showProgressDialog, bool plotToFile) { PlotProgressDialog plotDlg = null; //声明一个打印进度框对象 if (showProgressDialog) //如果需要显示打印进度框,则创建 { plotDlg = new PlotProgressDialog(isPreview, 1, true); //获取去除扩展名后的文件名(不含路径) string plotFileName = SymbolUtilityServices.GetSymbolNameFromPathName(fileName, "dwg"); //在打印进度框中显示的图纸名称:当前打印布局的名称(含文档名) plotDlg.set_PlotMsgString(PlotMessageIndex.SheetName, "正在处理图纸:" + layout.LayoutName + "(" + plotFileName + ".dwg)"); } //设置打印的状态为停止 PreviewEndPlotStatus status = PreviewEndPlotStatus.Cancel; PlotInfo pi = new PlotInfo(); //创建打印信息 pi.Layout = layout.ObjectId; //要打印的布局 pi.OverrideSettings = ps; //使用ps中的打印设置 //验证打印信息是否有效 PlotInfoValidator piv = new PlotInfoValidator(); piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled; piv.Validate(pi); //启动打印进度框 if (showProgressDialog) { plotDlg.StartPlotProgress(isPreview); } engine.BeginPlot(plotDlg, null);//开启打印引擎进行打印任务 //开始打印文档 engine.BeginDocument(pi, layout.Database.GetDocument().Name, null, copies, plotToFile, fileName); //启动打印图纸进程 if (plotDlg != null) { plotDlg.StartSheetProgress(); } //开始打印页面 PlotPageInfo ppi = new PlotPageInfo(); engine.BeginPage(ppi, pi, true, null); engine.BeginGenerateGraphics(null);//开始打印内容 //设置打印进度 if (plotDlg != null) { plotDlg.SheetProgressPos = 50; } engine.EndGenerateGraphics(null);//内容打印结束 //结束页面打印 PreviewEndPlotInfo pepi = new PreviewEndPlotInfo(); engine.EndPage(pepi); status = pepi.Status;//打印预览结束时的状态 //终止显示打印图纸进程 if (plotDlg != null) { plotDlg.EndSheetProgress(); } engine.EndDocument(null);//文档打印结束 //终止显示打印进度框 if (plotDlg != null) { plotDlg.EndPlotProgress(); } engine.EndPlot(null); //结束打印任务 return(status); //打印预览结束时的状态 }
/// <summary> /// 执行多布局打印 /// </summary> /// <param name="engine">打印引擎对象</param> /// <param name="layouts">要打印的布局列表</param> /// <param name="ps">打印设置</param> /// <param name="fileName">打印文件名</param> /// <param name="previewNum">预览的布局号,小于1表示打印</param> /// <param name="copies">打印份数</param> /// <param name="showProgressDialog">是否显示打印进度框</param> /// <param name="plotToFile">是否打印到文件</param> /// <returns>返回打印状态</returns> public static PreviewEndPlotStatus MPlot(this PlotEngine engine, List <Layout> layouts, PlotSettings ps, string fileName, int previewNum, int copies, bool showProgressDialog, bool plotToFile) { int numSheet = 1;//表示当前打印的图纸序号 //设置是否为预览 bool isPreview = previewNum >= 1 ? true : false; Document doc = Application.DocumentManager.MdiActiveDocument; PlotProgressDialog plotDlg = null; //声明一个打印进度框对象 if (showProgressDialog) //如果需要显示打印进度框,则创建 { plotDlg = new PlotProgressDialog(isPreview, layouts.Count, true); } //设置打印的状态为停止 PreviewEndPlotStatus status = PreviewEndPlotStatus.Cancel; //重建一个布局列表,因为打印预览只能是单页的 List <Layout> layoutList = new List <Layout>(); if (isPreview && previewNum >= 1) //如果为打印预览 { layoutList.Add(layouts[previewNum - 1]); //只对预览的布局进行操作 } else { layoutList.AddRange(layouts); //如果为打印,则需对所有的布局进行操作 } foreach (Layout layout in layoutList) //遍历布局 { PlotInfo pi = new PlotInfo(); //创建打印信息 pi.Layout = layout.ObjectId; //要打印的布局 //要多文档打印,必须将要打印的布局设置为当前布局 LayoutManager.Current.CurrentLayout = layout.LayoutName; pi.OverrideSettings = ps;//使用ps中的打印设置 //验证打印信息是否有效 PlotInfoValidator piv = new PlotInfoValidator(); piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled; piv.Validate(pi); if (plotDlg != null)//如果显示打印进度框 { //获取去除扩展名后的文件名(不含路径) string plotFileName = SymbolUtilityServices.GetSymbolNameFromPathName(doc.Name, "dwg"); //在打印进度框中显示的图纸名称:当前打印布局的名称(含文档名) plotDlg.set_PlotMsgString(PlotMessageIndex.SheetName, plotFileName + "-" + layout.LayoutName); } //如果打印的是第一张图纸则启动下面的操作,以后就不再需要再次进行 if (numSheet == 1) { //启动打印进度框 if (showProgressDialog) { plotDlg.StartPlotProgress(isPreview); } engine.BeginPlot(plotDlg, null);//开启打印引擎进行打印任务 //开始打印文档 engine.BeginDocument(pi, doc.Name, null, copies, plotToFile, fileName); } //启动打印图纸进程 if (plotDlg != null) { plotDlg.StartSheetProgress(); } //开始打印页面 PlotPageInfo ppi = new PlotPageInfo(); engine.BeginPage(ppi, pi, (numSheet == layoutList.Count), null); engine.BeginGenerateGraphics(null);//开始打印内容 //设置打印进度 if (plotDlg != null) { plotDlg.SheetProgressPos = 50; } engine.EndGenerateGraphics(null);//内容打印结束 //结束页面打印 PreviewEndPlotInfo pepi = new PreviewEndPlotInfo(); engine.EndPage(pepi); status = pepi.Status;//打印预览结束时的状态 //终止显示打印图纸进程 if (plotDlg != null) { plotDlg.EndSheetProgress(); } numSheet++;//将要打印的图纸序号设置为下一个 //更新打印进程 if (plotDlg != null) { plotDlg.PlotProgressPos += (100 / layouts.Count); } } engine.EndDocument(null);//文档打印结束 if (plotDlg != null) { plotDlg.EndPlotProgress(); //终止显示打印进度框 } engine.EndPlot(null); //结束打印任务 return(status); //返回打印预览结束时的状态 }
/// <summary> /// 多页打印/预览函数 /// </summary> /// <param name="pe"></param> /// <param name="isPreview"></param> /// <param name="plotList"></param> /// <param name="sheetNumIfPreview"></param> /// <param name="layoutCount"></param> /// <param name="plotDevice"></param> /// <param name="plotCanonicalMeida"></param> /// <param name="plotStyle"></param> /// <param name="saveFileName"></param> /// <returns></returns> private static PreviewEndPlotStatus MultiPlotOrPreview( PlotEngine pe, bool isPreview, Dictionary<Extents2d, ObjectId> plotList, int sheetNumIfPreview, int layoutCount,//布局总数 string plotDevice, string plotCanonicalMeida, string plotStyle, string saveFileName ) { PreviewEndPlotStatus ret = PreviewEndPlotStatus.Cancel; string DocName = mCommands.Doc.Name; DocName = DocName.Substring(DocName.LastIndexOf("\") + 1); DocName = DocName.Substring(0, DocName.LastIndexOf(".")); #region 准备打印区域列表plotAreaList Dictionary<Extents2d, ObjectId> plotAreaList = new Dictionary<Extents2d, ObjectId>(); if (isPreview && sheetNumIfPreview >= 0) { KeyValuePair<Extents2d, ObjectId> kv = plotList.ElementAt(sheetNumIfPreview); plotAreaList.Add(kv.Key, kv.Value);//预览只能一个区域 } else { plotAreaList = plotList;//打印全部区域 } #endregion using (Application.DocumentManager.MdiActiveDocument.LockDocument()) { try { using (PlotProgressDialog ppd = new PlotProgressDialog(isPreview, plotAreaList.Count, false)) { #region 设置进度条显示信息 ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "转换进度"); ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "本布局转换进度:__/__"); ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "总转换进度: __/__"); ppd.LowerPlotProgressRange = 0; ppd.UpperPlotProgressRange = plotAreaList.Count; //显示进度条对话框 ppd.OnBeginPlot(); ppd.IsVisible = true; #endregion int pageNum = 0;//布局打印页计数 int layoutPageNum = 0;//当前布局总打印页数(区域数) int sheetNum = 0;//所有打印页计数(打印总区域数) ObjectId layoutId = ObjectId.Null;//当前布局Id Layout lo = null; foreach (KeyValuePair<Extents2d, ObjectId> kv in plotAreaList) { if (kv.Value != layoutId) { layoutId = kv.Value; lo = mFun.m_OpenEntity(layoutId) as Layout; LayoutManager.Current.CurrentLayout = lo.LayoutName;//切换为当前布局,是否必须?!! pageNum = 0;//重置布局页计数 layoutPageNum = plotAreaList.Count(a => a.Value == layoutId); ppd.LowerSheetProgressRange = 0; ppd.UpperSheetProgressRange = layoutPageNum; } pageNum++;//布局页计数+1 sheetNum++;//总打印区域计数+1 ppd.set_PlotMsgString(PlotMessageIndex.SheetName, $"{DocName}-{lo.LayoutName}");//打印文件名-布局名 //设置打印配置参数 PlotInfo pi = SetPlotInfo(lo, kv.Key, plotDevice, plotCanonicalMeida, plotStyle, isPreview); #region 启动打印 if (sheetNum == 1) { pe.BeginPlot(ppd, null); pe.BeginDocument( pi, //打印信息 mCommands.Doc.Name, //当前图纸名 null, 1, //打印份数 !isPreview, //是否打印至文件 isPreview ? "" : saveFileName //保存文件名 ); } #endregion #region 开始打印 ppd.OnBeginSheet(); ppd.SheetProgressPos = pageNum; ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, $"本布局转换进度:{pageNum}/{layoutPageNum}"); ppd.PlotProgressPos = sheetNum; ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, $"总转换进度:sheetNum}/{plotAreaList.Count}"); pe.BeginPage( new PlotPageInfo(), pi, //打印信息 sheetNum == plotAreaList.Count, //是否最后一页 null ); pe.BeginGenerateGraphics(null); pe.EndGenerateGraphics(null); PreviewEndPlotInfo pepi = new PreviewEndPlotInfo(); pe.EndPage(pepi);//结束本页打印,返回预览状态 ret = pepi.Status; #endregion } #region 结束打印 ppd.OnEndSheet(); pe.EndDocument(null); ppd.OnEndPlot(); pe.EndPlot(null); #endregion } } catch (Autodesk.AutoCAD.Runtime.Exception e) { MessageBox.Show($"转换为单个PDF文档出错: {e.Message}!\n请选择【单幅分别保存】进行转换。", "转换出错", MessageBoxButtons.OK, MessageBoxIcon.Error); ret = PreviewEndPlotStatus.Cancel; } } return ret; }
/// <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; }