Ejemplo n.º 1
0
        public void PlotTitleBlock(Document acDoc, Transaction acTrans, BlockTableRecord acBlkTblRecSpc, Extents2d PlotArea, string path, string PlotDevice, string PlotStyle, string CanonicalMediaName)
        {
            Database acCurDb = acDoc.Database;

            //string folder = DirectoryFolder(ac.Database.OriginalFileName);
            //string filename = DocumentShortName(ac.Database.OriginalFileName) + ".pdf";
            //Application.ShowAlertDialog(path);
            Application.SetSystemVariable("BACKGROUNDPLOT", 0);

            Layout acLayout = (Layout)acTrans.GetObject(acBlkTblRecSpc.LayoutId, OpenMode.ForRead);

            PlotInfo acPtInfo = new PlotInfo();

            acPtInfo.Layout = acLayout.ObjectId;

            PlotSettings acPtSet = new PlotSettings(acLayout.ModelType);

            acPtSet.CopyFrom(acLayout);
            if (!acPtSet.PlotPlotStyles)
            {
                acPtSet.PlotPlotStyles = true;
            }
            //if (!acPtSet.ScaleLineweights) acPtSet.ScaleLineweights = true;
            if (acPtSet.ShadePlotResLevel != ShadePlotResLevel.Maximum)
            {
                acPtSet.ShadePlotResLevel = ShadePlotResLevel.Maximum;
            }

            PlotSettingsValidator acPtSetVlr = PlotSettingsValidator.Current;

            //acPtSetVlr.SetPlotType(acPtSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
            //Application.ShowAlertDialog(acPlDev);
            acPtSetVlr.SetPlotConfigurationName(acPtSet, PlotDevice, null);
            acPtSetVlr.RefreshLists(acPtSet);
            acPtSetVlr.SetPlotWindowArea(acPtSet, PlotArea);
            acPtSetVlr.SetPlotType(acPtSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
            acPtSetVlr.SetPlotWindowArea(acPtSet, PlotArea);
            acPtSetVlr.SetUseStandardScale(acPtSet, true);
            acPtSetVlr.SetStdScaleType(acPtSet, StdScaleType.ScaleToFit);
            acPtSetVlr.SetPlotCentered(acPtSet, true);
            acPtSetVlr.SetCurrentStyleSheet(acPtSet, PlotStyle);
            //acPtSetVlr.SetCurrentStyleSheet(acPtSet, "monochrome.ctb");

            //acPtSetVlr.SetPlotConfigurationName(acPtSet, "DWG To PDF.pc3", "ISO_full_bleed_A1_(594.00_x_841.00_MM)");
            //acPtSetVlr.SetPlotConfigurationName(acPtSet, "DWG To PDF.pc3", "ISO_A1_(594.00_x_841.00_MM)");
            acPtSetVlr.SetPlotConfigurationName(acPtSet, PlotDevice, CanonicalMediaName);
            Point2d acPSize = acPtSet.PlotPaperSize;

            if (acPSize.X > acPSize.Y)
            {
                acPtSetVlr.SetPlotRotation(acPtSet, PlotRotation.Degrees000);
            }
            else
            {
                acPtSetVlr.SetPlotRotation(acPtSet, PlotRotation.Degrees090);
            }

            //acPtSetVlr.SetPlotConfigurationName(acPtSet, "DWF6 ePlot.pc3", "ANSI_A_(8.50_x_11.00_Inches)");
            acPtInfo.OverrideSettings           = acPtSet;
            LayoutManager.Current.CurrentLayout = acLayout.LayoutName;

            //IAcSmEnumComponent

            PlotInfoValidator acPtInfoVlr = new PlotInfoValidator();

            acPtInfoVlr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
            acPtInfoVlr.Validate(acPtInfo);



            //Check if plot in process
            if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
            {
                using (PlotEngine acPtEng = PlotFactory.CreatePublishEngine())
                {
                    PlotProgressDialog acPtProgDlg = new PlotProgressDialog(false, 1, true);
                    using (acPtProgDlg)
                    {
                        //Define message when plot start
                        acPtProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot Process");
                        acPtProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
                        acPtProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
                        acPtProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
                        acPtProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Process");
                        //acPtProgDlg.set_PlotMsgString(PlotMessageIndex.SheetName, "abc");

                        //Set the process range
                        acPtProgDlg.LowerPlotProgressRange = 0;
                        acPtProgDlg.UpperPlotProgressRange = 100;
                        acPtProgDlg.PlotProgressPos        = 0;

                        //Display the process dialog
                        acPtProgDlg.OnBeginPlot();
                        acPtProgDlg.IsVisible = true;

                        //Start the layout plot
                        acPtEng.BeginPlot(acPtProgDlg, null);

                        //Define the plot output
                        acPtEng.BeginDocument(acPtInfo, acDoc.Name, null, 1, true, @path);
                        //Display the process message
                        acPtProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting " + acDoc.Name + " - " + acLayout.LayoutName);

                        //Set the sheet process range
                        acPtProgDlg.OnBeginSheet();
                        acPtProgDlg.LowerSheetProgressRange = 0;
                        acPtProgDlg.UpperPlotProgressRange  = 100;
                        acPtProgDlg.SheetProgressPos        = 0;

                        //Plot the first sheet
                        PlotPageInfo acPtPageInfo = new PlotPageInfo();
                        acPtEng.BeginPage(acPtPageInfo, acPtInfo, true, null);
                        //acPtEng.BeginPage(
                        acPtEng.BeginGenerateGraphics(null);
                        acPtEng.EndGenerateGraphics(null);

                        //End plot sheet
                        acPtEng.EndPage(null);
                        acPtProgDlg.SheetProgressPos = 100;
                        acPtProgDlg.OnEndSheet();

                        //End document
                        acPtEng.EndDocument(null);

                        //End plot
                        acPtProgDlg.PlotProgressPos = 100;
                        acPtProgDlg.OnEndPlot();
                        acPtEng.EndPlot(null);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// plotting method
        /// </summary>
        /// <param name="pdfFileName"> name</param>
        /// <param name="printModel">print param model</param>
        public static void PlotCurrentLayout(string pdfFileName, PrintModel printModel)
        {
            // Get the current document and database, and start a transaction
            Document acDoc   = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            //short bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT");
            Application.SetSystemVariable("BACKGROUNDPLOT", 0);
            try
            {
                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    // Reference the Layout Manager
                    LayoutManager acLayoutMgr;
                    acLayoutMgr = LayoutManager.Current;
                    // Get the current layout and output its name in the Command Line window
                    Layout acLayout;
                    acLayout =
                        acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout),
                                          OpenMode.ForRead) as Layout;

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

                    // Get a copy of the PlotSettings from the layout
                    PlotSettings acPlSet = new PlotSettings(acLayout.ModelType);

                    acPlSet.CopyFrom(acLayout);
                    // Update the PlotSettings object
                    PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
                    var sheetList = acPlSetVdr.GetPlotStyleSheetList();
                    //acPlSetVdr.SetCurrentStyleSheet(acPlSet, "monochrome.ctb");

                    Extents2d points = new Extents2d(printModel.BlockPosition, printModel.BlockDimensions);

                    bool isHor = printModel.IsFormatHorizontal();
                    //pdfCreator.GetBlockDimensions();
                    string canonName = printModel.GetCanonNameByWidthAndHeight();

                    //acDoc.Utility.TranslateCoordinates(point1, acWorld, acDisplayDCS, False);
                    acPlSetVdr.SetPlotWindowArea(acPlSet, points);
                    acPlSetVdr.SetPlotType(acPlSet, Db.PlotType.Window);
                    if (!isHor)
                    {
                        acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees090);
                    }
                    //else if(canonName =="ISO_full_bleed_A4_(297.00_x_210.00_MM)")
                    //    acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees090);
                    else
                    {
                        acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees000);
                    }

                    // Set the plot scale
                    acPlSetVdr.SetUseStandardScale(acPlSet, false);
                    acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);
                    // Center the plot
                    acPlSetVdr.SetPlotCentered(acPlSet, true);
                    //acPlSetVdr.SetClosestMediaName(acPlSet,printModel.width,printModel.height,PlotPaperUnit.Millimeters,true);
                    //string curCanonName = PdfCreator.GetLocalNameByAtrrValue(formatValue);
                    acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWG_To_PDF_Uzle.pc3", canonName);
                    //acPlSetVdr.SetCanonicalMediaName(acPlSet, curCanonName);

                    // Set the plot device to use

                    // Set the plot info as an override since it will
                    // not be saved back to the layout
                    acPlInfo.OverrideSettings = acPlSet;
                    // Validate the plot info
                    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
                            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
                                string filename = Path.Combine(Path.GetDirectoryName(acDoc.Name), pdfFileName);
                                Active.Editor.WriteMessage(filename);

                                acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, filename);
                                // 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
                                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 (Exception e)
            {
                Application.ShowAlertDialog(e.Message);
            }
        }
Ejemplo n.º 3
0
        public static ResultDTO plotTest(string id, string file, string fileID, bool isSnap = false)
        {
            try
            {
                Document doc = Application.DocumentManager.Open(file, false);
                Application.DocumentManager.MdiActiveDocument = doc;

                PlotSettings ps     = null; //声明增强型打印设置对象
                Layout       layout = null; //当前布局对象
                bool         isTile = AppConfig.GetAppSettings("ViewMode").ToLower().Trim() == "tilepicviewer";
                using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
                {
                    LayoutManager lm = LayoutManager.Current;//获取当前布局管理器
                    layout = (Layout)GetLayoutId(doc.Database, lm.CurrentLayout).GetObject(OpenMode.ForRead);

                    ps = new PlotSettings(layout.ModelType);
                    ps.CopyFrom(layout);//从已有打印设置中获取打印设置

                    //0.更新打印设备、图纸尺寸和打印样式表信息
                    PlotSettingsValidator validator = PlotSettingsValidator.Current;
                    validator.RefreshLists(ps);
                    //1.设置打印驱动
                    string plotConfigurationName = AppConfig.GetAppSettings("Printer");
                    if (isSnap)
                    {
                        plotConfigurationName = "PublishToWeb JPG.pc3";
                    }
                    validator.SetPlotConfigurationName(ps, plotConfigurationName, null);

                    //2.设置打印纸张
                    StringCollection cMNameLst = validator.GetCanonicalMediaNameList(ps);
                    string           mediaName = AppConfig.GetAppSettings("CanonicalMediaName");
                    if (isSnap)
                    {
                        mediaName = "VGA (480.00 x 640.00 像素)";
                    }
                    bool             isHas = false;
                    StringCollection canonicalMediaNames = validator.GetCanonicalMediaNameList(ps);
                    foreach (string canonicalMediaName in canonicalMediaNames)
                    {
                        string localeMediaName = validator.GetLocaleMediaName(ps, canonicalMediaName);
                        if (localeMediaName == mediaName)
                        {
                            validator.SetCanonicalMediaName(ps, canonicalMediaName);
                            isHas = true;
                            break;
                        }
                    }
                    if (!isHas)
                    {
                        throw new System.Exception("纸张:" + mediaName + "不存在!");
                    }
                    //3.设置打印样式表
                    string plotStyleSheet = AppConfig.GetAppSettings("PlotStyleSheet");
                    validator.SetCurrentStyleSheet(ps, plotStyleSheet);

                    validator.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
                    validator.SetPlotCentered(ps, true);
                    validator.SetUseStandardScale(ps, true);
                    validator.SetStdScaleType(ps, StdScaleType.ScaleToFit);//设置打印时布满图纸

                    validator.SetPlotRotation(ps, PlotRotation.Degrees000);
                    trans.Commit();
                }

                PlotConfig config = PlotConfigManager.CurrentConfig;
                doc = AcadApp.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                //获取去除扩展名后的文件名(不含路径)
                string fileName = SymbolUtilityServices.GetSymbolNameFromPathName(doc.Name, "dwg");
                //存放在同目录下
                var basePath = AppConfig.GetAppSettings("CacheViewFilePath");
                if (!basePath.EndsWith("\\"))
                {
                    basePath += "\\";
                }

                int    num  = Convert.ToInt32(fileID) / 1000 + 1;
                string root = basePath + "Dwg\\" + string.Format("{0}", num.ToString("D8")) + "\\";
                if (!isTile)
                {
                    root = Path.Combine(basePath, string.Format("{0}", num.ToString("D8")) + "\\");
                }
                if (!Directory.Exists(@root))
                {
                    Directory.CreateDirectory(@root);
                }

                fileName = root + id + config.DefaultFileExtension;
                if (!isTile || isSnap)
                {
                    fileName = root + fileID + config.DefaultFileExtension;
                }

                //为了防止后台打印问题,必须在调用打印API时设置BACKGROUNDPLOT系统变量为0
                short backPlot = (short)AcadApp.GetSystemVariable("BACKGROUNDPLOT");
                AcadApp.SetSystemVariable("BACKGROUNDPLOT", 0);

                PlotEngine plotEngine = PlotFactory.CreatePublishEngine(); //创建一个打印引擎
                PlotInfo   pi         = new PlotInfo();                    //创建打印信息
                pi.Layout           = layout.ObjectId;                     //要打印的布局
                pi.OverrideSettings = ps;                                  //使用ps中的打印设置

                //验证打印信息是否有效
                PlotInfoValidator piv = new PlotInfoValidator();
                piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
                piv.Validate(pi);

                PlotPageInfo ppi = new PlotPageInfo();
                plotEngine.BeginPlot(null, null);
                plotEngine.BeginDocument(pi, doc.Name, null, 1, true, fileName);
                plotEngine.BeginPage(ppi, pi, true, null);
                plotEngine.BeginGenerateGraphics(null);
                plotEngine.EndGenerateGraphics(null);
                plotEngine.EndPage(null);
                plotEngine.EndDocument(null);
                plotEngine.EndPlot(null);

                plotEngine.Destroy();//销毁打印引擎

                //恢复BACKGROUNDPLOT系统变量的值
                AcadApp.SetSystemVariable("BACKGROUNDPLOT", backPlot);

                doc.CloseAndDiscard();

                if (!File.Exists(fileName))
                {
                    throw new System.Exception("打印文件生成失败!");
                }

                ResultDTO reVal = new ResultDTO();
                if (isTile && !isSnap)//如果是图片就切图
                {
                    var img = new ZoomifyImage(fileName, 1024);
                    img.Zoomify(root + id + "\\");

                    reVal.status        = true;
                    reVal.DirectoryPath = "/" + id + "/";
                    reVal.ZoomLevel     = img.ZoomLevels - 1;
                }
                else
                {
                    reVal.status = true;
                }

                try
                {
                    //强制删除过程打印文件
                    if (File.Exists(file) && !isSnap)
                    {
                        File.Delete(file);
                    }
                }
                catch { }

                return(reVal);
            }
            catch (System.Exception e)
            {
                return(new ResultDTO
                {
                    status = false,
                    ErrInfo = e.Message,
                    FileName = file
                });
            }
        }
Ejemplo n.º 4
0
        //打印模型布局的范围
        //打印到DWF文件

        //[CommandMethod("PlotCurrentLayout")]
        /// <summary>
        /// 打印输出单个文件
        /// </summary>
        /// <param name="LayerName"></param>
        /// <param name="FileName"></param>
        /// <param name="outputFilePath"></param>
        /// <param name="printer"></param>
        /// <param name="paperFormat"></param>
        public static void PlotCurrentLayout(string LayerName, string FileName, string outputFilePath, string printer, string paperFormat)
        {
            // 获取当前文档和数据库,启动事务
            Document acDoc   = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Editor   ed      = acDoc.Editor;

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 获取图层
                LayerTable acLyrTbl;
                acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;

                // 将活动图层修改为第一个,即图层0
                acCurDb.Clayer = acLyrTbl[LayerName];
                LayerTableRecord acLayerTblRec = acTrans.GetObject(acLyrTbl[LayerName], OpenMode.ForWrite) as LayerTableRecord;
                acLayerTblRec.IsOff = false; //显示该图层

                // 引用布局管理器LayoutManager
                LayoutManager acLayoutMgr;
                acLayoutMgr = LayoutManager.Current;

                // 获取当前布局,在命令行窗口显示布局名字
                Layout acLayout;
                acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout),
                                             OpenMode.ForRead) as Layout;

                // 从布局中获取PlotInfo
                PlotInfo acPlInfo = new PlotInfo();
                acPlInfo.Layout = acLayout.ObjectId;

                // 复制布局中的PlotSettings
                PlotSettings acPlSet = new PlotSettings(acLayout.ModelType);
                acPlSet.CopyFrom(acLayout);

                // 更新PlotSettings对象
                PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;

                //// 获取打印设备
                //StringCollection deviceList = acPlSetVdr.GetPlotDeviceList();

                //// 打印设备列表-MessageBox形式
                //foreach (string d in deviceList)
                //{
                //    ed.WriteMessage("打印设备:" + d + "\n");
                //}

                //// 获取纸张列表
                //StringCollection mediaList = acPlSetVdr.GetCanonicalMediaNameList(acPlSet);

                //ed.WriteMessage("图纸种类:" + Convert.ToString(mediaList.Count) + " 个\n");
                //foreach (string m in mediaList)
                //{
                //    ed.WriteMessage("打印图纸:" + m + "\n");
                //}

                // 设置打印区域
                acPlSetVdr.SetPlotType(acPlSet,
                                       Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);

                // 设置打印比例
                acPlSetVdr.SetUseStandardScale(acPlSet, true);
                acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);

                // 居中打印
                acPlSetVdr.SetPlotCentered(acPlSet, true);

                // 设置使用的打印设备
                //acPlSetVdr.SetPlotConfigurationName(acPlSet, "PostScript Level 2.pc3",
                //"ANSI_A_(8.50_x_11.00_Inches)");
                //设置采用的打印设备与纸张格式
                acPlSetVdr.SetPlotConfigurationName(acPlSet, printer,
                                                    paperFormat);


                // 用上述设置信息覆盖PlotInfo对象,
                // 不会将修改保存回布局
                acPlInfo.OverrideSettings = acPlSet;

                // 验证打印信息
                PlotInfoValidator acPlInfoVdr = new PlotInfoValidator();
                acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
                acPlInfoVdr.Validate(acPlInfo);

                // 检查是否有正在处理的打印任务
                if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
                {
                    using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine())
                    {
                        // 使用PlotProgressDialog对话框跟踪打印进度
                        PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false,
                                                                                1,
                                                                                true);

                        using (acPlProgDlg)
                        {
                            // 定义打印开始时显示的状态信息
                            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");

                            // 设置打印进度范围
                            acPlProgDlg.LowerPlotProgressRange = 0;
                            acPlProgDlg.UpperPlotProgressRange = 100;
                            acPlProgDlg.PlotProgressPos        = 0;

                            // 显示打印进度对话框
                            acPlProgDlg.OnBeginPlot();
                            acPlProgDlg.IsVisible = true;

                            // 开始打印
                            acPlEng.BeginPlot(acPlProgDlg, null);

                            string opFile = outputFilePath + LayerName;
                            // 定义打印输出
                            acPlEng.BeginDocument(acPlInfo,
                                                  acDoc.Name,
                                                  null,
                                                  1,
                                                  true,
                                                  opFile);

                            // 显示当前打印任务的有关信息
                            //acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status,
                            //                              "Plotting: " + acDoc.Name + " - " +
                            //                              acLayout.LayoutName);
                            acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status,
                                                          "Plotting: " + acDoc.Name + " - " +
                                                          LayerName);

                            // 设置图纸进度范围
                            acPlProgDlg.OnBeginSheet();
                            acPlProgDlg.LowerSheetProgressRange = 0;
                            acPlProgDlg.UpperSheetProgressRange = 100;
                            acPlProgDlg.SheetProgressPos        = 0;

                            // 打印第一张图/布局
                            PlotPageInfo acPlPageInfo = new PlotPageInfo();
                            acPlEng.BeginPage(acPlPageInfo,
                                              acPlInfo,
                                              true,
                                              null);

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

                            // 结束第一张图/布局的打印
                            acPlEng.EndPage(null);
                            acPlProgDlg.SheetProgressPos = 100;
                            acPlProgDlg.OnEndSheet();

                            // 结束文档局的打印
                            acPlEng.EndDocument(null);

                            // 打印结束
                            acPlProgDlg.PlotProgressPos = 100;
                            acPlProgDlg.OnEndPlot();
                            acPlEng.EndPlot(null);
                        }
                    }
                }
                acLayerTblRec.IsOff = true; //关闭该图层
            }
        }
Ejemplo n.º 5
0
            /// <summary>
            /// 设置打印信息
            /// </summary>
            /// <param name="layoutId">布局ID</param>
            /// <param name="plotArea">该布局中的一个区域</param>
            /// <param name="plotDevice">打印设备名</param>
            /// <param name="plotCanonicalMeida">标准打印介质名</param>
            /// <param name="plotStyle">打印样式</param>
            /// <param name="isSinglePage">是否只打印单页</param>
            /// <returns></returns>
      private static PlotInfo SetPlotInfo(Layout lo, Extents2d plotArea,string plotDevice, string plotCanonicalMeida, string plotStyle, bool isSinglePage)
        {
            PlotInfo pi = new PlotInfo();
            pi.Layout = lo.Id;

            //获取当前布局的打印信息
            PlotSettings ps = new PlotSettings(lo.ModelType);//是否模型空间
            ps.CopyFrom(lo);

            //着色打印选项,设置按线框进行打印
            ps.ShadePlot = PlotSettingsShadePlotType.Wireframe;

            //获取当前打印设置校验器
            PlotSettingsValidator psv = PlotSettingsValidator.Current;

            #region 以下这些设置请不要改变顺序!!!
            //以下2句顺序不能换!
            psv.SetPlotWindowArea(ps, plotArea);//设置打印区域            
            psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);//设置为窗口打印模式

            //设置布满图纸打印
            psv.SetUseStandardScale(ps, true);//需要?
            psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);//布满图纸

            //设置居中打印
            psv.SetPlotCentered(ps, true);

            //设置打印样式
            try
            {
                psv.SetCurrentStyleSheet(ps, plotStyle);//设置打印样式(笔宽等)(为什么有时会出错?PS:不能与原样式形同?!!!)
            }
            catch (Autodesk.AutoCAD.Runtime.Exception e)
            {
               // MessageBox.Show(string.Format("{0}\n当前打印样式:{1}\n设置打印样式:{2}", e.Message, ps.CurrentStyleSheet, plotStyle), "设置打印样式出错");
            }

            //配置打印机和打印介质
            psv.SetPlotConfigurationName(ps, plotDevice, plotCanonicalMeida);
            psv.RefreshLists(ps);

            //设置打印单位
            try
            {
                psv.SetPlotPaperUnits(ps, PlotPaperUnit.Millimeters);//(为什么有时会出错?)            
            }
            catch (Autodesk.AutoCAD.Runtime.Exception e)
            {
                //MessageBox.Show(string.Format("{0}\n当前尺寸单位:{1}\n设置单位:{2}", e.Message, ps.PlotPaperUnits, PlotPaperUnit.Millimeters), "设置尺寸单位出错");
            }

            //设置旋转角度(打印到同一文档时必须设置为同一旋转角)
            if (isSinglePage)
            {
                if ((plotArea.MaxPoint.X - plotArea.MinPoint.X) > (plotArea.MaxPoint.Y - plotArea.MinPoint.Y))
                {
                    if (ps.PlotPaperSize.X > ps.PlotPaperSize.Y)
                    {
                        psv.SetPlotRotation(ps, PlotRotation.Degrees000);
                    }
                    else
                    {
                        psv.SetPlotRotation(ps, PlotRotation.Degrees090);
                    }
                }
                else
                {
                    if (ps.PlotPaperSize.X > ps.PlotPaperSize.Y)
                    {
                        psv.SetPlotRotation(ps, PlotRotation.Degrees090);
                    }
                    else
                    {
                        psv.SetPlotRotation(ps, PlotRotation.Degrees000);
                    }
                }
            }
            else
            {
                //多页打印必须设置为统一旋转角度(否则打印会出错,出错信息:eValidePlotInfo!特别注意!!!)
                psv.SetPlotRotation(ps, PlotRotation.Degrees000);
            }
            #endregion

            pi.OverrideSettings = ps;//将PlotSetting与PlotInfo关联

            PlotInfoValidator piv = new PlotInfoValidator();
            piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
            piv.Validate(pi);//激活打印设置

            ps.Dispose();

            return pi;
        }