//if the media size doesn't exist, this will search media list for best match // 8.5 x 11 should be there private static string setClosestMediaName(PlotSettingsValidator psv, PlotSettings ps, double pageWidth, double pageHeight, bool matchPrintableArea) { //get all of the media listed for plotter StringCollection mediaList = psv.GetCanonicalMediaNameList(ps); double smallestOffest = 0.0; string selectedMedia = string.Empty; PlotRotation selectedRot = PlotRotation.Degrees000; foreach (string media in mediaList) { psv.SetCanonicalMediaName(ps, media); double mediaWidth = ps.PlotPaperSize.X; double mediaHeight = ps.PlotPaperSize.Y; if (matchPrintableArea) { mediaWidth -= (ps.PlotPaperMargins.MinPoint.X + ps.PlotPaperMargins.MaxPoint.X); mediaHeight -= (ps.PlotPaperMargins.MinPoint.Y + ps.PlotPaperMargins.MaxPoint.Y); } PlotRotation rot = PlotRotation.Degrees090; //check that we are not outside the media print area if (mediaWidth < pageWidth || mediaHeight < pageHeight) { //Check if turning paper will work if (mediaHeight < pageWidth || mediaWidth >= pageHeight) { //still too small continue; } rot = PlotRotation.Degrees090; } double offset = Math.Abs(mediaWidth * mediaHeight - pageWidth * pageHeight); if (selectedMedia == string.Empty || offset < smallestOffest) { selectedMedia = media; smallestOffest = offset; selectedRot = rot; if (smallestOffest == 0) { break; } } } psv.SetCanonicalMediaName(ps, selectedMedia); psv.SetPlotRotation(ps, selectedRot); return(selectedMedia); }
void comboBoxDevices_SelectedChanged(object sender, EventArgs e) { int i = comboBoxDevices.SelectedIndex; String strDeviceName = comboBoxDevices.SelectedItem.ToString(); String canonicalMediaName = m_plotStg.CanonicalMediaName; setPlotCfgName2Validator(strDeviceName, canonicalMediaName, true); // fill paper sizes combo box if (!FillPaperSizes()) { return /*FALSE*/; } String localeMediaName = m_plotSettingVal.GetLocaleMediaName(m_plotStg, canonicalMediaName); // select active media comboBoxPaperSize.SelectedItem = localeMediaName; if (comboBoxPaperSize.SelectedIndex < 0) { String csLocaleMediaName = comboBoxPaperSize.Items[0].ToString(); if (csLocaleMediaName.Length < 1) { return /*FALSE*/; } canonicalMediaName = getCanonicalByLocaleMediaName(csLocaleMediaName); m_plotSettingVal.SetCanonicalMediaName(m_plotStg, canonicalMediaName); comboBoxPaperSize.SelectedItem = csLocaleMediaName; } FillPlotAreaCombo(false); FillPlotOffset(); FillScaleValues(false); FillPaperOrientation(); FillPlotStyles(); }
public void QueryPaperSize() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; LayoutManager layMgr = LayoutManager.Current; PlotSettingsValidator plSetVdr = PlotSettingsValidator.Current; Dictionary <string, Point2d> customPaperSize = new Dictionary <string, Point2d>(StringComparer.CurrentCultureIgnoreCase); customPaperSize.Add("A4", new Point2d(210.0, 297.0)); customPaperSize.Add("A3", new Point2d(297.0, 420.0)); customPaperSize.Add("A2", new Point2d(420.0, 594.0)); customPaperSize.Add("A1", new Point2d(594.0, 841.0)); customPaperSize.Add("A0", new Point2d(841.0, 1189.0)); customPaperSize.Add("A0Plus", new Point2d(841.0, 1480.0)); customPaperSize.Add("A0+", new Point2d(841.0, 1470.0)); customPaperSize.Add("GBKN", new Point2d(594.0, 1051.0)); customPaperSize.Add("4Z", new Point2d(297.0, 750.0)); customPaperSize.Add("5Z", new Point2d(297.0, 930.0)); customPaperSize.Add("6Z", new Point2d(297.0, 1110.0)); customPaperSize.Add("7Z", new Point2d(297.0, 1290.0)); customPaperSize.Add("Z3", new Point2d(297.0, 594.0)); customPaperSize.Add("Z4", new Point2d(297.0, 841.0)); customPaperSize.Add("Z6", new Point2d(297.0, 1189.0)); customPaperSize.Add("3A4", new Point2d(297.0, 630.0)); customPaperSize.Add("4A4", new Point2d(297.0, 841.0)); customPaperSize.Add("5A4", new Point2d(297.0, 1050.0)); customPaperSize.Add("6A4", new Point2d(297.0, 1260.0)); customPaperSize.Add("7A4", new Point2d(297.0, 1470.0)); db.TileMode = false; //goto Paperspace if not already ed.SwitchToPaperSpace(); //turn PVP off using (Transaction tr = db.TransactionManager.StartTransaction()) { Layout theLayout = (Layout)tr.GetObject(layMgr.GetLayoutId(layMgr.CurrentLayout), OpenMode.ForWrite); // Get the PlotInfo from the layout PlotInfo plInfo = new PlotInfo(); plInfo.Layout = theLayout.ObjectId; PlotSettings np = new PlotSettings(theLayout.ModelType); np.CopyFrom(theLayout); string devName = "DWG To PDF.pc3"; //OceTds600.pc3 plSetVdr.SetPlotConfigurationName(np, devName, null); plSetVdr.RefreshLists(np); StringCollection canMedNames = plSetVdr.GetCanonicalMediaNameList(np); for (int i = 0; i < canMedNames.Count; i++) { plSetVdr.SetCanonicalMediaName(np, canMedNames[i]); plSetVdr.RefreshLists(np); string medName = plSetVdr.GetLocaleMediaName(np, i); //ed.WriteMessage("\n{0}: Can. MediaName={1}/{2}", i, np.CanonicalMediaName, medName); if (medName.Equals("ISO A1 (841.00 x 594.00 MM)")) { ed.WriteMessage("\n{0}: Can. MediaName={1}/{2}", i, np.CanonicalMediaName, medName); Point2d pSize = np.PlotPaperSize; ed.WriteMessage("\n\tPaperSize={0}", pSize.ToString()); PlotRotation pRot = np.PlotRotation; ed.WriteMessage("\n\tPlot Rotation={0}", pRot.ToString()); Extents2d pM = np.PlotPaperMargins; ed.WriteMessage("\n\tPaper Margins={0}, {1}", pM.MinPoint.ToString(), pM.MaxPoint.ToString()); } } } }
//set up plotinfo static public PlotInfo plotSetUp(Extents2d window, Transaction tr, Database db, Editor ed, bool scaleToFit, bool pdfout) { using (tr) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead); // We need a PlotInfo object linked to the layout PlotInfo pi = new PlotInfo(); pi.Layout = btr.LayoutId; //current layout Layout lo = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead); // We need a PlotSettings object based on the layout settings which we then customize PlotSettings ps = new PlotSettings(lo.ModelType); ps.CopyFrom(lo); //The PlotSettingsValidator helps create a valid PlotSettings object PlotSettingsValidator psv = PlotSettingsValidator.Current; //set rotation psv.SetPlotRotation(ps, orientation(window)); //perhaps put orientation after window setting window?? // We'll plot the window, centered, scaled, landscape rotation psv.SetPlotWindowArea(ps, window); psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);//breaks here on some drawings // Set the plot scale psv.SetUseStandardScale(ps, true); if (scaleToFit == true) { psv.SetStdScaleType(ps, StdScaleType.ScaleToFit); } else { psv.SetStdScaleType(ps, StdScaleType.StdScale1To1); } // Center the plot psv.SetPlotCentered(ps, true);//finding best location //get printerName from system settings PrinterSettings settings = new PrinterSettings(); string defaultPrinterName = settings.PrinterName; psv.RefreshLists(ps); // Set Plot device & page size // if PDF set it up for some PDF plotter if (pdfout == true) { psv.SetPlotConfigurationName(ps, "DWG to PDF.pc3", null); var mns = psv.GetCanonicalMediaNameList(ps); if (mns.Contains("ANSI_expand_A_(8.50_x_11.00_Inches)")) { psv.SetCanonicalMediaName(ps, "ANSI_expand_A_(8.50_x_11.00_Inches)"); } else { string mediaName = setClosestMediaName(psv, ps, 8.5, 11, true); } } else { psv.SetPlotConfigurationName(ps, defaultPrinterName, null); var mns = psv.GetCanonicalMediaNameList(ps); if (mns.Contains("Letter")) { psv.SetCanonicalMediaName(ps, "Letter"); } else { string mediaName = setClosestMediaName(psv, ps, 8.5, 11, true); } } //rebuilts plotter, plot style, and canonical media lists //(must be called before setting the plot style) psv.RefreshLists(ps); //ps.ShadePlot = PlotSettingsShadePlotType.AsDisplayed; //ps.ShadePlotResLevel = ShadePlotResLevel.Normal; //plot options //ps.PrintLineweights = true; //ps.PlotTransparency = false; //ps.PlotPlotStyles = true; //ps.DrawViewportsFirst = true; //ps.CurrentStyleSheet // Use only on named layouts - Hide paperspace objects option // ps.PlotHidden = true; //psv.SetPlotRotation(ps, PlotRotation.Degrees180); //plot table needs to be the custom heavy lineweight for the Uphol specs psv.SetCurrentStyleSheet(ps, "monochrome.ctb"); // We need to link the PlotInfo to the PlotSettings and then validate it pi.OverrideSettings = ps; PlotInfoValidator piv = new PlotInfoValidator(); piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled; piv.Validate(pi); return(pi); } }
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 }); } }