最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

全球关注:CAD.NET 批量打印、导出DPF

来源:博客园


(资料图片仅供参考)

网上好多使用C#开发CAD.Net打印功能的代码都有问题【特别是CSDN上面的

最后还是参考了官方文档才彻底搞明白

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();    }

官方文档里的参考代码段

关键词: 文件地址 纸张类型 打印机名称