- 积分
- 2848
- 明经币
- 个
- 注册时间
- 2004-7-28
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2017-10-20 14:41:37
|
显示全部楼层
本帖最后由 mkhsj928 于 2017-10-20 15:13 编辑
- /// <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 (Autodesk.AutoCAD.ApplicationServices.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;
- }
|
|