明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: _wtf

[打印] 在调用cad的.net api打印pdf文件效率很低

[复制链接]
发表于 2017-10-20 14:41 | 显示全部楼层
本帖最后由 mkhsj928 于 2017-10-20 15:13 编辑
  1. /// <summary>
  2.         /// 多页打印/预览函数
  3.         /// </summary>
  4.         /// <param name="pe"></param>
  5.         /// <param name="isPreview"></param>
  6.         /// <param name="plotList"></param>
  7.         /// <param name="sheetNumIfPreview"></param>
  8.         /// <param name="layoutCount"></param>
  9.         /// <param name="plotDevice"></param>
  10.         /// <param name="plotCanonicalMeida"></param>
  11.         /// <param name="plotStyle"></param>
  12.         /// <param name="saveFileName"></param>
  13.         /// <returns></returns>
  14.         private static PreviewEndPlotStatus MultiPlotOrPreview(
  15.         PlotEngine pe,
  16.         bool isPreview,
  17.         Dictionary<Extents2d, ObjectId> plotList,
  18.         int sheetNumIfPreview,
  19.         int layoutCount,//布局总数
  20.         string plotDevice,
  21.         string plotCanonicalMeida,
  22.         string plotStyle,
  23.         string saveFileName
  24.         )
  25.         {
  26.             PreviewEndPlotStatus ret = PreviewEndPlotStatus.Cancel;

  27.             string DocName = mCommands.Doc.Name;
  28.             DocName = DocName.Substring(DocName.LastIndexOf("\") + 1);
  29.             DocName = DocName.Substring(0, DocName.LastIndexOf("."));

  30.             #region 准备打印区域列表plotAreaList
  31.             Dictionary<Extents2d, ObjectId> plotAreaList = new Dictionary<Extents2d, ObjectId>();
  32.             if (isPreview && sheetNumIfPreview >= 0)
  33.             {
  34.                 KeyValuePair<Extents2d, ObjectId> kv = plotList.ElementAt(sheetNumIfPreview);
  35.                 plotAreaList.Add(kv.Key, kv.Value);//预览只能一个区域
  36.             }
  37.             else
  38.             {
  39.                 plotAreaList = plotList;//打印全部区域
  40.             }
  41.             #endregion

  42.             using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
  43.             {
  44.                 try
  45.                 {
  46.                     using (PlotProgressDialog ppd = new PlotProgressDialog(isPreview, plotAreaList.Count, false))
  47.                     {
  48.                         #region 设置进度条显示信息                                    
  49.                         ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "转换进度");
  50.                         ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "本布局转换进度:__/__");
  51.                         ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "总转换进度: __/__");

  52.                         ppd.LowerPlotProgressRange = 0;
  53.                         ppd.UpperPlotProgressRange = plotAreaList.Count;

  54.                         //显示进度条对话框
  55.                         ppd.OnBeginPlot();
  56.                         ppd.IsVisible = true;
  57.                         #endregion

  58.                         int pageNum = 0;//布局打印页计数
  59.                         int layoutPageNum = 0;//当前布局总打印页数(区域数)
  60.                         int sheetNum = 0;//所有打印页计数(打印总区域数)
  61.                         ObjectId layoutId = ObjectId.Null;//当前布局Id
  62.                         
  63.                         Layout lo = null;
  64.                         foreach (KeyValuePair<Extents2d, ObjectId> kv in plotAreaList)
  65.                         {
  66.                             if (kv.Value != layoutId)
  67.                             {
  68.                                 layoutId = kv.Value;

  69.                                 lo = mFun.m_OpenEntity(layoutId) as Layout;
  70.                                 LayoutManager.Current.CurrentLayout = lo.LayoutName;//切换为当前布局,是否必须?!!

  71.                                 pageNum=0;//重置布局页计数

  72.                                 layoutPageNum = plotAreaList.Count(a => a.Value == layoutId);

  73.                                 ppd.LowerSheetProgressRange = 0;
  74.                                 ppd.UpperSheetProgressRange = layoutPageNum;
  75.                             }

  76.                             pageNum++;//布局页计数+1
  77.                             sheetNum++;//总打印区域计数+1                    

  78.                             ppd.set_PlotMsgString(PlotMessageIndex.SheetName, $"{DocName}-{lo.LayoutName}");//打印文件名-布局名

  79.                             //设置打印配置参数
  80.                             PlotInfo pi = SetPlotInfo(lo, kv.Key, plotDevice, plotCanonicalMeida, plotStyle,isPreview);

  81.                             #region 启动打印
  82.                             if (sheetNum == 1)
  83.                             {
  84.                                 pe.BeginPlot(ppd, null);

  85.                                 pe.BeginDocument(
  86.                                     pi,                                     //打印信息
  87.                                     mCommands.Doc.Name,                     //当前图纸名
  88.                                     null,
  89.                                     1,                                      //打印份数
  90.                                     !isPreview,                             //是否打印至文件
  91.                                     isPreview ? "" : saveFileName           //保存文件名
  92.                                     );
  93.                             }
  94.                             #endregion

  95.                             #region 开始打印
  96.                             ppd.OnBeginSheet();                           

  97.                             ppd.SheetProgressPos = pageNum;
  98.                             ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, $"本布局转换进度:{pageNum}/{layoutPageNum}");

  99.                             ppd.PlotProgressPos = sheetNum;
  100.                             ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, $"总转换进度:sheetNum}/{plotAreaList.Count}");

  101.                             pe.BeginPage(
  102.                                 new PlotPageInfo(),
  103.                                 pi,                                 //打印信息
  104.                                 sheetNum == plotAreaList.Count,     //是否最后一页
  105.                                 null
  106.                                 );

  107.                             pe.BeginGenerateGraphics(null);
  108.                             pe.EndGenerateGraphics(null);

  109.                             PreviewEndPlotInfo pepi = new PreviewEndPlotInfo();
  110.                             pe.EndPage(pepi);//结束本页打印,返回预览状态
  111.                             ret = pepi.Status;
  112.                             #endregion
  113.                         }

  114.                         #region 结束打印
  115.                         ppd.OnEndSheet();
  116.                         pe.EndDocument(null);

  117.                         ppd.OnEndPlot();
  118.                         pe.EndPlot(null);
  119.                         #endregion
  120.                     }
  121.                 }
  122.                 catch (Autodesk.AutoCAD.Runtime.Exception e)
  123.                 {
  124.                     MessageBox.Show($"转换为单个PDF文档出错: {e.Message}!\n请选择【单幅分别保存】进行转换。", "转换出错", MessageBoxButtons.OK, MessageBoxIcon.Error);

  125.                     ret = PreviewEndPlotStatus.Cancel;
  126.                 }
  127.             }

  128.             return ret;
  129.         }
发表于 2017-10-20 14:42 | 显示全部楼层
本帖最后由 mkhsj928 于 2017-10-20 15:10 编辑
  1. /// <summary>
  2.         /// 单页预览
  3.         /// </summary>
  4.         /// <param name="layoutId"></param>
  5.         /// <param name="plotArea"></param>
  6.         /// <param name="plotDevice"></param>
  7.         /// <param name="plotCanonicalMeida"></param>
  8.         /// <param name="plotStyle"></param>
  9.         public static void PreviewSinglePage(
  10.             ObjectId layoutId,
  11.             Extents2d plotArea,
  12.             string plotDevice,
  13.             string plotCanonicalMeida,
  14.             string plotStyle
  15.             )
  16.         {
  17.             if (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
  18.             {
  19.                 MessageBox.Show($"当前不能预览,另一个打印任务正在进行中!");
  20.                 return;
  21.             }

  22.             using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
  23.             {
  24.                 Layout lo = mFun.m_OpenEntity(layoutId) as Layout;

  25.                 if (LayoutManager.Current.CurrentLayout != lo.LayoutName)
  26.                 {
  27.                     LayoutManager.Current.CurrentLayout = lo.LayoutName;
  28.                 }

  29.                 using (PlotProgressDialog ppd = new PlotProgressDialog(true, 1, false))
  30.                 {
  31.                     #region 设置进度条信息

  32.                     #endregion

  33.                     using (PlotEngine pe = PlotFactory.CreatePreviewEngine(0))
  34.                     {
  35.                         //设置打印配置参数
  36.                         PlotInfo pi = SetPlotInfo(lo, plotArea, plotDevice, plotCanonicalMeida, plotStyle,true);

  37.                         pe.BeginPlot(ppd, null);

  38.                         pe.BeginDocument(pi, mCommands.Doc.Name, null, 1, false, "");

  39.                         pe.BeginPage(new PlotPageInfo(), pi, true, null);

  40.                         pe.BeginGenerateGraphics(null);

  41.                         pe.EndGenerateGraphics(null);

  42.                         PreviewEndPlotInfo pepi = new PreviewEndPlotInfo();
  43.                         pe.EndPage(pepi);//结束本页打印,返回预览状态

  44.                         pe.EndDocument(null);

  45.                         pe.EndPlot(null);
  46.                     }
  47.                 }
  48.             }
  49.         }
发表于 2017-10-20 14:42 | 显示全部楼层
本帖最后由 mkhsj928 于 2017-10-20 15:09 编辑
  1. /// <summary>
  2.         /// 单页打印
  3.         /// </summary>
  4.         /// <param name="layoutId"></param>
  5.         /// <param name="plotArea"></param>
  6.         /// <param name="plotDevice"></param>
  7.         /// <param name="plotCanonicalMeida"></param>
  8.         /// <param name="plotStyle"></param>
  9.         /// <param name="saveFilName"></param>
  10.         public static void PlotSinglePage(
  11.             ObjectId layoutId,
  12.             Extents2d plotArea,
  13.             string plotDevice,
  14.             string plotCanonicalMeida,
  15.             string plotStyle,
  16.             string saveFilName)
  17.         {
  18.             if (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
  19.             {
  20.                 MessageBox.Show($"当前不能打印,另一个打印任务正在进行中!");
  21.                 return;
  22.             }

  23.             using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
  24.             {
  25.                 Layout lo = mFun.m_OpenEntity(layoutId) as Layout;

  26.                 if (LayoutManager.Current.CurrentLayout != lo.LayoutName)
  27.                 {
  28.                     LayoutManager.Current.CurrentLayout = lo.LayoutName;
  29.                 }

  30.                 using (PlotProgressDialog ppd = new PlotProgressDialog(false, 1, false))
  31.                 {
  32.                     #region 设置进度条信息

  33.                     #endregion

  34.                     using (PlotEngine pe = PlotFactory.CreatePublishEngine())
  35.                     {
  36.                         //设置打印配置参数
  37.                         PlotInfo pi = SetPlotInfo(lo, plotArea, plotDevice, plotCanonicalMeida, plotStyle,true);

  38.                         pe.BeginPlot(ppd, null);

  39.                         pe.BeginDocument(pi, mCommands.Doc.Name, null, 1, true, saveFilName);

  40.                         pe.BeginPage(new PlotPageInfo(), pi, true, null);

  41.                         pe.BeginGenerateGraphics(null);

  42.                         pe.EndGenerateGraphics(null);

  43.                         pe.EndPage(null);

  44.                         pe.EndDocument(null);

  45.                         pe.EndPlot(null);
  46.                     }
  47.                 }
  48.             }
  49.         }
发表于 2017-10-20 14:44 | 显示全部楼层
以上就是打印至PDF的主要函数代码,可以打印成每张一个PDF,也可以打印成一个PDF速度没你说的那么慢
发表于 2017-11-27 17:35 | 显示全部楼层
试试楼上的代码
发表于 2017-12-5 09:17 | 显示全部楼层
我觉得主要是要设置为前台打印
SetSystemVariable("BACKGROUNDPLOT", 0);
发表于 2017-12-6 15:28 | 显示全部楼层

你的代码是一个布局一个图,能不能只在一个布局中打很多张在一个PDF里,我试了很多次,每次都在beginpage中发生eValidePlotInfo异常,把角度设置成一样的也没用
发表于 2017-12-6 19:37 | 显示全部楼层
有用吗大家?我怎么设置了前景还是一分钟一张图
发表于 2017-12-6 19:43 | 显示全部楼层
menethil 发表于 2017-12-6 19:37
有用吗大家?我怎么设置了前景还是一分钟一张图

是我设置错了,设置前景是有用的,一秒一张
发表于 2017-12-17 19:35 | 显示全部楼层
好贴  留名。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-28 20:53 , Processed in 0.235126 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表