首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ReadDwgFile在AutoCAD .net端加载dwg,然后打印

使用ReadDwgFile在AutoCAD .net端加载dwg,然后打印
EN

Stack Overflow用户
提问于 2015-10-01 23:45:03
回答 1查看 1.7K关注 0票数 0

我使用示例here 将dwg侧面加载到内存中,并结合使用模型空间中的Plot的示例here。我让一切正常工作,除了绘图不关心图层是否冻结,打印时就好像它们都解冻了一样。从dwg传入的数据是正确的。我可以在调试时迭代层,并验证正确的层是冻结的还是解冻的。另外,如果我只是将dwg另存为一个新名称,它与原始的关于layer state.Any的想法相匹配?

代码语言:javascript
运行
复制
[CommandMethod("PlotLayout")]
    public static void PlotLayout()
    {
        // Get the current document and database, and start a transaction
        Document acDoc = Active.Document;
        Database acCurDb = Active.Database;
        Editor ed = Active.Editor;
        var collection = new List<string>() { "C:\\Test\\440001A.dwg", "C:\\Test\\440001B.dwg", "C:\\Test\\440001C.dwg", "C:\\Test\\440001D.dwg" };
        for (int i = 0; i < collection.Count; i++)
        {
            var dwg = collection[i];
            var dir = Path.GetDirectoryName(dwg);
            var fn = Path.GetFileNameWithoutExtension(dwg);
            var filePath = Path.Combine(dir, fn + "-" + i.ToString() + ".pdf");
            Database oldDb = HostApplicationServices.WorkingDatabase;
            using (Database db = new Database(false, true))
            {
                db.ReadDwgFile(dwg, FileOpenMode.OpenForReadAndAllShare, false, null);
                db.CloseInput(true);


                using (Transaction acTrans = db.TransactionManager.StartTransaction())
                    try
                    {
                        HostApplicationServices.WorkingDatabase = db;

                        LayoutManager acLayoutMgr = LayoutManager.Current;

                        // Get the current layout and output its name in the Command Line window
                        Layout acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout),
                                                            OpenMode.ForRead) as Layout;

                        // Get the PlotInfo from the layout
                        using (PlotInfo acPlInfo = new PlotInfo())
                        {
                            acPlInfo.Layout = acLayout.ObjectId;

                            // Get a copy of the PlotSettings from the layout
                            using (PlotSettings acPlSet = new PlotSettings(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);

                                // Set the plot device to use
                                acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWF6 ePlot.pc3", "ANSI_B_(11.00_x_17.00_Inches)");

                                // 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 PlotInfoValidator())
                                {
                                    acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
                                    acPlInfoVdr.Validate(acPlInfo);

                                    // Check to see if a plot is already in progress
                                    if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
                                    {
                                        using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine())
                                        {
                                            // Track the plot progress with a Progress dialog
                                            using (PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false, 1, true))
                                            {
                                                using ((acPlProgDlg))
                                                {
                                                    // Define the status messages to display 
                                                    // when plotting starts
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot Progress");
                                                    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);

                                                    // Define the plot output
                                                    acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, "c:\\myplot");

                                                    // Display information about the current plot
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + acDoc.Name + " - " + acLayout.LayoutName);

                                                    // 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 PlotPageInfo())
                                                    {
                                                        acPlEng.BeginPage(acPlPageInfo, acPlInfo, true, null);
                                                    }

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

                                                    // Finish plotting the sheet/layout
                                                    acPlEng.EndPage(null);
                                                    acPlProgDlg.SheetProgressPos = 100;
                                                    acPlProgDlg.OnEndSheet();

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

                                                    // Finish the plot
                                                    acPlProgDlg.PlotProgressPos = 100;
                                                    acPlProgDlg.OnEndPlot();
                                                    acPlEng.EndPlot(null);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                    catch (System.Exception ex)
                    {

                        ed.WriteMessage(ex.ToString());
                    }
                HostApplicationServices.WorkingDatabase = oldDb;
            }
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2015-10-02 08:40:42

不是很理想,但我找到了一份工作。如果我迭代LayerTable,检查是否冻结,然后将IsPlottable设为false它将正确打印。如果有更好的办法,请让我知道。谢谢

代码语言:javascript
运行
复制
 LayerTable _LayerTable = acTrans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                         foreach (ObjectId _LayerTableId in _LayerTable)
                         {
                             LayerTableRecord _LayerTableRecord =
                                        (LayerTableRecord)acTrans.GetObject(_LayerTableId,
                                                            OpenMode.ForWrite);
                             if (_LayerTableRecord.IsFrozen)
                             {
                                 _LayerTableRecord.IsPlottable = false;
                             }
                         }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32891720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档