明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 337|回复: 4

【转载】CAD.NET 批量打印、导出PDF

[复制链接]
发表于 3 天前 | 显示全部楼层 |阅读模式
  1. public static void PrintModelExtents(string dwg文件地址, string s_打印机名称, string s_打印机纸张类型, PlotRotation v_旋转角度, int i_打印份数 = 1, bool b_打印PDF = false, string pdf保存文件夹 = "")
  2.   {
  3.       // 读取DWG文件
  4.       Document acDoc = Application.DocumentManager.Add(dwg文件地址);

  5.       Application.DocumentManager.CurrentDocument = acDoc;
  6.       using (DocumentLock docLock = acDoc.LockDocument())
  7.       {
  8.           Database acExDb = acDoc.Database;
  9.           // 因为我只打印模型,所以这里只写了Model
  10.           string layoutName = "Model";
  11.           //layoutName = "Layout1";
  12.           // Create a transaction for the external drawing
  13.           using Transaction acTransEx = acExDb.TransactionManager.StartTransaction();
  14.           // Get the layouts dictionary
  15.           DBDictionary layoutsEx = acTransEx.GetObject(acExDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
  16.           if (layoutsEx.Contains(layoutName) != true)
  17.           {
  18.               // Display a message if the layout could not be found in the specified drawing
  19.               MessageBox.Show($"\nLayout '{layoutName}' could not be imported from '{dwg文件地址}'.");
  20.               return;
  21.           }

  22.           // Get the layout and block objects from the external drawing
  23.           Layout acLayout = layoutsEx.GetAt(layoutName).GetObject(OpenMode.ForRead) as Layout;
  24.           LayoutManager.Current.SetCurrentLayoutId(acLayout.Id);

  25.           // 这一句不写的话
  26.           // 在CAD 2018与2020上批量打印的时候会出现打印不出来的情况(测试过)
  27.           // 具体表现在下面判断 PlotFactory.ProcessPlotState 的时候永远不为 NotPlotting
  28.           // 在CAD 2021上是可以正常使用的
  29.           // 所以还是建议写上
  30.           Application.SetSystemVariable("BACKGROUNDPLOT", 0);

  31.           using PlotInfo acPlInfo = new();

  32.           acPlInfo.Layout = acLayout.ObjectId;

  33.           // Get a copy of the PlotSettings from the layout
  34.           using PlotSettings acPlSet = new(acLayout.ModelType);
  35.           acPlSet.CopyFrom(acLayout);

  36.           // Update the PlotSettings object
  37.           PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;

  38.           // Set the plot type
  39.           acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);

  40.           // Set the plot scale
  41.           acPlSetVdr.SetUseStandardScale(acPlSet, true);
  42.           acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);

  43.           // Center the plot
  44.           acPlSetVdr.SetPlotCentered(acPlSet, true);

  45.           // 部分打印机竖纸打横图的时候需要旋转90°
  46.           // 比如 Microsoft Print to PDF
  47.           acPlSetVdr.SetPlotRotation(acPlSet, v_旋转角度);

  48.           // Set the plot device to use           
  49.           acPlSetVdr.SetPlotConfigurationName(acPlSet, s_打印机名称, s_打印机纸张类型);

  50.           // Set the plot info as an override since it will
  51.           // not be saved back to the layout
  52.           acPlInfo.OverrideSettings = acPlSet;

  53.           // Validate the plot info
  54.           using PlotInfoValidator acPlInfoVdr = new() { MediaMatchingPolicy = MatchingPolicy.MatchEnabled };
  55.           acPlInfoVdr.Validate(acPlInfo);
  56.           //check:
  57.           // Check to see if a plot is already in progress
  58.           if (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
  59.           {
  60.               MessageBox.Show("ProcessPlotState is not NotPlotting");
  61.               return;
  62.           }


  63.           using PlotEngine acPlEng = PlotFactory.CreatePublishEngine();
  64.           // Track the plot progress with a Progress dialog
  65.           using PlotProgressDialog acPlProgDlg = new(false, 1, true);

  66.           // Define the status messages to display
  67.           // when plotting starts
  68.           // 这里的信息可以按需修改
  69.           acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "打印中,请稍候……");
  70.           acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
  71.           acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
  72.           acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
  73.           acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");

  74.           // Set the plot progress range
  75.           acPlProgDlg.LowerPlotProgressRange = 0;
  76.           acPlProgDlg.UpperPlotProgressRange = 100;
  77.           acPlProgDlg.PlotProgressPos = 0;

  78.           // Display the Progress dialog
  79.           acPlProgDlg.OnBeginPlot();
  80.           acPlProgDlg.IsVisible = true;

  81.           // Start to plot the layout
  82.           acPlEng.BeginPlot(acPlProgDlg, null);

  83.           // 核心关键语句
  84.           acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, i_打印份数, b_打印PDF, b_打印PDF ? pdf保存文件夹 : "");

  85.           // Display information about the current plot
  86.           acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, $"Plotting: {Path.GetFileNameWithoutExtension(dwg文件地址)}");

  87.           // Set the sheet progress range
  88.           acPlProgDlg.OnBeginSheet();
  89.           acPlProgDlg.LowerSheetProgressRange = 0;
  90.           acPlProgDlg.UpperSheetProgressRange = 100;
  91.           acPlProgDlg.SheetProgressPos = 0;

  92.           // Plot the first sheet/layout
  93.           using PlotPageInfo acPlPageInfo = new();
  94.           acPlEng.BeginPage(acPlPageInfo, acPlInfo, true, null);


  95.           acPlEng.BeginGenerateGraphics(null);
  96.           acPlEng.EndGenerateGraphics(null);

  97.           // Finish plotting the sheet/layout
  98.           acPlEng.EndPage(null);

  99.           // Finish plotting the document
  100.           acPlEng.EndDocument(null);

  101.           // Finish the plot
  102.           acPlEng.EndPlot(null);
  103.       }
  104.       acDoc.CloseAndDiscard();
  105.   }
原文链接:CAD.NET 批量打印、导出DPF - 敲键听响 - 博客园 (cnblogs.com)
发表于 前天 01:00 来自手机 | 显示全部楼层
谢谢分享 谢谢
发表于 前天 09:12 | 显示全部楼层

学习学习,谢谢楼主的资料
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-9-28 02:13 , Processed in 0.150078 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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