- 积分
- 4400
- 明经币
- 个
- 注册时间
- 2015-1-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
- public static void PrintModelExtents(string dwg文件地址, string s_打印机名称, string s_打印机纸张类型, PlotRotation v_旋转角度, int i_打印份数 = 1, bool b_打印PDF = false, string pdf保存文件夹 = "")
- {
- // 读取DWG文件
- Document acDoc = Application.DocumentManager.Add(dwg文件地址);
-
- Application.DocumentManager.CurrentDocument = acDoc;
- using (DocumentLock docLock = acDoc.LockDocument())
- {
- Database acExDb = acDoc.Database;
- // 因为我只打印模型,所以这里只写了Model
- string layoutName = "Model";
- //layoutName = "Layout1";
- // Create a transaction for the external drawing
- using Transaction acTransEx = acExDb.TransactionManager.StartTransaction();
- // Get the layouts dictionary
- DBDictionary layoutsEx = acTransEx.GetObject(acExDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
- if (layoutsEx.Contains(layoutName) != true)
- {
- // Display a message if the layout could not be found in the specified drawing
- MessageBox.Show($"\nLayout '{layoutName}' could not be imported from '{dwg文件地址}'.");
- return;
- }
-
- // Get the layout and block objects from the external drawing
- Layout acLayout = layoutsEx.GetAt(layoutName).GetObject(OpenMode.ForRead) as Layout;
- LayoutManager.Current.SetCurrentLayoutId(acLayout.Id);
-
- // 这一句不写的话
- // 在CAD 2018与2020上批量打印的时候会出现打印不出来的情况(测试过)
- // 具体表现在下面判断 PlotFactory.ProcessPlotState 的时候永远不为 NotPlotting
- // 在CAD 2021上是可以正常使用的
- // 所以还是建议写上
- Application.SetSystemVariable("BACKGROUNDPLOT", 0);
-
- using PlotInfo acPlInfo = new();
-
- acPlInfo.Layout = acLayout.ObjectId;
-
- // Get a copy of the PlotSettings from the layout
- using PlotSettings acPlSet = new(acLayout.ModelType);
- acPlSet.CopyFrom(acLayout);
-
- // Update the PlotSettings object
- PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
-
- // Set the plot type
- acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
-
- // Set the plot scale
- acPlSetVdr.SetUseStandardScale(acPlSet, true);
- acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);
-
- // Center the plot
- acPlSetVdr.SetPlotCentered(acPlSet, true);
-
- // 部分打印机竖纸打横图的时候需要旋转90°
- // 比如 Microsoft Print to PDF
- acPlSetVdr.SetPlotRotation(acPlSet, v_旋转角度);
-
- // Set the plot device to use
- acPlSetVdr.SetPlotConfigurationName(acPlSet, s_打印机名称, s_打印机纸张类型);
-
- // Set the plot info as an override since it will
- // not be saved back to the layout
- acPlInfo.OverrideSettings = acPlSet;
-
- // Validate the plot info
- using PlotInfoValidator acPlInfoVdr = new() { MediaMatchingPolicy = MatchingPolicy.MatchEnabled };
- acPlInfoVdr.Validate(acPlInfo);
- //check:
- // Check to see if a plot is already in progress
- if (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
- {
- MessageBox.Show("ProcessPlotState is not NotPlotting");
- return;
- }
-
-
- using PlotEngine acPlEng = PlotFactory.CreatePublishEngine();
- // Track the plot progress with a Progress dialog
- using PlotProgressDialog acPlProgDlg = new(false, 1, true);
-
- // Define the status messages to display
- // when plotting starts
- // 这里的信息可以按需修改
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "打印中,请稍候……");
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
-
- // Set the plot progress range
- acPlProgDlg.LowerPlotProgressRange = 0;
- acPlProgDlg.UpperPlotProgressRange = 100;
- acPlProgDlg.PlotProgressPos = 0;
-
- // Display the Progress dialog
- acPlProgDlg.OnBeginPlot();
- acPlProgDlg.IsVisible = true;
-
- // Start to plot the layout
- acPlEng.BeginPlot(acPlProgDlg, null);
-
- // 核心关键语句
- acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, i_打印份数, b_打印PDF, b_打印PDF ? pdf保存文件夹 : "");
-
- // Display information about the current plot
- acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, $"Plotting: {Path.GetFileNameWithoutExtension(dwg文件地址)}");
-
- // Set the sheet progress range
- acPlProgDlg.OnBeginSheet();
- acPlProgDlg.LowerSheetProgressRange = 0;
- acPlProgDlg.UpperSheetProgressRange = 100;
- acPlProgDlg.SheetProgressPos = 0;
-
- // Plot the first sheet/layout
- using PlotPageInfo acPlPageInfo = new();
- acPlEng.BeginPage(acPlPageInfo, acPlInfo, true, null);
-
-
- acPlEng.BeginGenerateGraphics(null);
- acPlEng.EndGenerateGraphics(null);
-
- // Finish plotting the sheet/layout
- acPlEng.EndPage(null);
-
- // Finish plotting the document
- acPlEng.EndDocument(null);
-
- // Finish the plot
- acPlEng.EndPlot(null);
- }
- acDoc.CloseAndDiscard();
- }
原文链接:CAD.NET 批量打印、导出DPF - 敲键听响 - 博客园 (cnblogs.com)
|
|