調用列印程式“PublishToWeb JPG.pc3”進行圖片列印,該列印驅動程式中內置了很多的列印方案,在同尺寸的列印方案下,數據範圍越大列印出來的清晰度就越差,內置的尺寸不一定都滿足,在又要通過我們的插件去完成列印任務,又不能讓客戶總是做配置的情況下,我總結了一個不是很完美的解決方案,實現思路 ...
調用列印程式“PublishToWeb JPG.pc3”進行圖片列印,該列印驅動程式中內置了很多的列印方案,在同尺寸的列印方案下,數據範圍越大列印出來的清晰度就越差,內置的尺寸不一定都滿足,在又要通過我們的插件去完成列印任務,又不能讓客戶總是做配置的情況下,我總結了一個不是很完美的解決方案,實現思路如下:
1、選定基礎列印尺寸方案(本demo選定“UserDefinedRaster (1600.00 x 1200.00Pixels)”),一定是系統自帶的,不然就需要人工配置,暫時沒有找到通過代碼去修改列印方案
2、在一定的精度比例尺下(本demo是一個像素點代表10個坐標點的跨度),根據用戶選擇的範圍計算出要列印的圖片數量。
3、將單張列印的圖片存儲到臨時文件夾下,等全部列印完畢,按照順序合併成一張圖圖片
主方法,列印合成的入口
1 public bool CutImgAndPutFromLeftUpperCAD(Document doc) 2 { 3 Point3d maxPt = Point3d.Origin; 4 Point3d minPt = Point3d.Origin; 5 PromptIntegerOptions prInt = new PromptIntegerOptions("\n請選擇導出方式:選擇方式(1:全圖導出,2:拉框導出):"); 6 prInt.DefaultValue = 1; 7 prInt.AllowArbitraryInput = false; 8 prInt.AllowNegative = false; 9 prInt.AllowNone = false; 10 PromptIntegerResult ptrInt = Tools.MdiAcEditor.GetInteger(prInt); 11 if (ptrInt.Status != PromptStatus.OK) 12 return false; 13 switch (ptrInt.Value) 14 { 15 case 1: 16 maxPt = doc.Database.Extmax; 17 minPt = doc.Database.Extmin; 18 break; 19 case 2: 20 PromptPointOptions StartPoint = new PromptPointOptions("\n請選擇第一個角點"); 21 PromptPointResult StartPointResult = Tools.MdiAcEditor.GetPoint(StartPoint); 22 PromptCornerOptions PromptCornerOptions = new PromptCornerOptions("\n請選中第二個角點", StartPointResult.Value); 23 PromptPointResult EndPointResult = Tools.MdiAcEditor.GetCorner(PromptCornerOptions); 24 double maxX = StartPointResult.Value.X > EndPointResult.Value.X ? StartPointResult.Value.X : EndPointResult.Value.X; 25 double minX = StartPointResult.Value.X > EndPointResult.Value.X ? EndPointResult.Value.X : StartPointResult.Value.X; 26 double maxY = StartPointResult.Value.Y > EndPointResult.Value.Y ? StartPointResult.Value.Y : EndPointResult.Value.Y; 27 double minY = StartPointResult.Value.Y > EndPointResult.Value.Y ? EndPointResult.Value.Y : StartPointResult.Value.Y; 28 29 maxPt = new Point3d(maxX, maxY, 0); 30 minPt = new Point3d(minX, minY, 0); 31 break; 32 } 33 34 string JPGSavePath = Const.systemPath + "\\Save"; 35 string dataSavePath = Const.systemPath + "\\Temp"; 36 if (Directory.Exists(dataSavePath)) 37 { 38 FileOperate.DelectDir(dataSavePath); 39 } 40 else 41 { 42 Directory.CreateDirectory(dataSavePath); 43 } 44 if (Directory.Exists(JPGSavePath)) 45 { 46 FileOperate.DelectDir(JPGSavePath); 47 } 48 else 49 { 50 Directory.CreateDirectory(JPGSavePath); 51 } 52 Point3d LeftUpP = new Point3d(minPt.X, maxPt.Y, 0); 53 Point3d RightDownP = new Point3d(maxPt.X, minPt.Y, 0); 54 Point3d LeftDownP = new Point3d(minPt.X, minPt.Y, 0); 55 Point3d RightUpP = new Point3d(maxPt.X, maxPt.Y, 0); 56 //根據坐標範圍計算圖片的數量,多於100張不執行列印任務 57 int rows = Convert.ToInt32(Math.Floor(LeftUpP.Y - RightDownP.Y) / 12000.0) + 1; 58 int cels = Convert.ToInt32(Math.Floor(RightUpP.X - LeftUpP.X) / 16000.0) + 1; 59 if (rows *cels > 100) 60 { 61 return false; 62 } 63 int cutTrueCount = 0; 64 int itemImgCount = 0; 65 itemImgCount = 0; 66 int imgCount = 0; 67 string imgPathAndName = string.Empty; 68 CircularProgress cirProgress = new CircularProgress(0, rows * cels, "正在導出圖片"); 69 cirProgress.StartProgress(); 70 71 try 72 { 73 74 for (int row = 0; row < rows; row++) 75 { 76 for (int cel = 0; cel < cels; cel++) 77 { 78 try 79 { 80 imgCount++; 81 cirProgress.SendMessageChangeValue("正在切圖 ", (imgCount*100)/ (rows * cels)); 82 double nowLeftX = LeftUpP.X + (double)(cel * 16000); 83 double nowLeftY = LeftUpP.Y - (double)(row * 12000); 84 Point3d leftPoint = new Point3d(nowLeftX, nowLeftY - 12000, 0); 85 Point3d rightPoint = new Point3d(nowLeftX + 16000, nowLeftY, 0); 86 string m_ImgName = string.Concat(new object[] 87 { 88 row, 89 "_", 90 cel, 91 ".jpg" 92 }); 93 imgPathAndName = dataSavePath + "\\"; 94 object obj = imgPathAndName; 95 imgPathAndName = string.Concat(new object[] 96 { 97 obj, 98 row, 99 "_", 100 cel, 101 ".jpg" 102 }); 103 //單張圖片列印 104 ExportMapToFileCAD(leftPoint, rightPoint, imgPathAndName, doc); 105 itemImgCount++; 106 cutTrueCount++; 107 } 108 catch 109 { 110 break; 111 } 112 } 113 } 114 string JPGName = doc.Window.Text; 115 JPGName = JPGName.Split(new char[] { '.' })[0]; 116 //開始合成圖片 117 CombinImage(rows, cels, 1600, 1200, dataSavePath, JPGSavePath + "\\" + JPGName + ".jpg", cirProgress); 118 Tools.MdiAcEditor.WriteMessageWithReturn("圖片導出成功:" + JPGSavePath + "\\" + JPGName + ".jpg"); 119 cirProgress.StopProgress(); 120 return true; 121 122 } 123 catch (Exception ex) 124 { 125 cirProgress.StopProgress(); 126 return false; 127 } 128 }
1 private bool ExportMapToFileCAD(Point3d leftPoint, Point3d rigthPoint, string fileName, Document doc) 2 { 3 bool result; 4 try 5 { 6 Editor ed = doc.Editor; 7 Database db = doc.Database; 8 if (fileName.Trim().Equals("")) 9 { 10 result = false; 11 } 12 else 13 { 14 using (doc.LockDocument()) 15 { 16 using (Transaction tr = db.TransactionManager.StartTransaction()) 17 { 18 //一定要記得設置,否則列印將非常之慢 19 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("BackGroundPlot", 0); 20 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 21 Layout lo = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForWrite); 22 PlotInfo pi = new PlotInfo(); 23 pi.Layout = btr.LayoutId; 24 PlotSettings ps = new PlotSettings(lo.ModelType); 25 ps.CopyFrom(lo); 26 PlotSettingsValidator psv = PlotSettingsValidator.Current; 27 //很重要,坐標處理,針對自定義坐標系的,像天正的圖紙,不做處理可能會列印出空白的 28 Extents2d ext2d = Ucs2Dcs(leftPoint, rigthPoint); 29 psv.SetPlotWindowArea(ps, ext2d); 30 psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Window); 31 psv.SetPlotRotation(ps, 0); 32 psv.SetStdScaleType(ps, 0); 33 psv.SetPlotCentered(ps, true); 34 35 psv.GetCanonicalMediaNameList(ps); 36 psv.SetPlotConfigurationName(ps, "PublishToWeb JPG.pc3", "UserDefinedRaster (1600.00 x 1200.00Pixels)"); 37 pi.OverrideSettings = ps; 38 PlotInfoValidator piv = new PlotInfoValidator(); 39 piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled; 40 piv.Validate(pi); 41 while ((PlotFactory.ProcessPlotState == ProcessPlotState.BackgroundPlotting) || (PlotFactory.ProcessPlotState == ProcessPlotState.ForegroundPlotting)) 42 { 43 System.Threading.Thread.Sleep(1); 44 } 45 if (PlotFactory.ProcessPlotState == 0) 46 { 47 PlotEngine pe = PlotFactory.CreatePublishEngine(); 48 using (pe) 49 { 50 PlotProgressDialog ppd = new PlotProgressDialog(false, 1, true); 51 using (ppd) 52 { 53 ppd.set_PlotMsgString(0, "CAD切圖"); 54 ppd.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "取消切圖"); 55 ppd.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "取消切圖"); 56 ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "切圖"); 57 ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "正在切圖"); 58 ppd.LowerPlotProgressRange = 0; 59 ppd.UpperPlotProgressRange = 100; 60 ppd.PlotProgressPos = 0; 61 ppd.OnBeginPlot(); 62 ppd.IsVisible = false; 63 pe.BeginPlot(ppd, null); 64 pe.BeginDocument(pi, fileName, null, 1, true, fileName); 65 ppd.OnBeginSheet(); 66 ppd.LowerSheetProgressRange = 0; 67 ppd.UpperSheetProgressRange = 100; 68 ppd.SheetProgressPos = 0; 69 PlotPageInfo ppi = new PlotPageInfo(); 70 pe.BeginPage(ppi, pi, true, null); 71 pe.BeginGenerateGraphics(null); 72 pe.EndGenerateGraphics(null); 73 pe.EndPage(null); 74 ppd.SheetProgressPos = 100; 75 ppd.OnEndSheet(); 76 pe.EndDocument(null); 77 ppd.PlotProgressPos = 100; 78 ppd.OnEndPlot(); 79 pe.EndPlot(null); 80 } 81 } 82 } 83 else 84 { 85 ed.WriteMessage("\n另外一個程式正在運行中。。。"); 86 } 87 } 88 } 89 result = true; 90 } 91 } 92 catch (System.Exception ex) 93 { 94 result = false; 95 } 96 return result; 97 }根據範圍列印單張圖片
1 private void CombinImage(int rows, int cels, int width, int height,string url,string outPath , CircularProgress cirProgress) 2 { 3 using (Bitmap bg = new Bitmap(cels * width, rows * height)) 4 { 5 //構建畫布 6 Graphics g = Graphics.FromImage(bg); 7 //清除畫布,背景透明 8 g.Clear(Color.Transparent); 9 int leftUpX = 0; 10 int leftUpY = 0; 11 int count = 0; 12 cirProgress.SendMasterChangeMessage("正在進行圖片合成", rows * cels); 13 for (int row = 0; row < rows; row++) 14 { 15 for (int cel = 0; cel < cels; cel++) 16 { 17 count++; 18 cirProgress.SendMessageChangeValue("正在進行圖片合成",(count*100)/(rows*cels)); 19 //載入小圖片 20 System.Drawing.Image img = System.Drawing.Image.FromFile(url + "\\" + row + "_" + cel + ".jpg"); 21 //確定插入位置 22 leftUpX = (cel * width); 23 leftUpY = (row * height); 24 //插入圖片到畫布中 25 g.DrawImage(img, new System.Drawing.Point(leftUpX, leftUpY)); 26 img.Dispose(); 27 } 28 } 29 g.Dispose(); 30 bg.Save(outPath); 31 cirProgress.SendMessageChangeValue("正在進行圖片反色", (count * 100) / (rows * cels)); 32 // 反色處理並且保存圖片 33 reversePic(bg); 34 //Bitmap tempBitmap = reversePic(bg); 35 //tempBitmap.Save(Const.systemPath + "\\Save\\test.jpg"); 36 //tempBitmap.Dispose(); 37 } 38 }圖片合成
1 Extents2d Ucs2Dcs(Point3d objStart, Point3d objEnd) 2 { 3 ResultBuffer rbFrom = 4 new ResultBuffer(new TypedValue(5003, 1)), 5 rbTo = 6 new ResultBuffer(new TypedValue(5003, 2)); 7 8 9 double[] firres = new double[] { 0, 0, 0 }; 10 double[] secres = new double[] { 0, 0, 0 }; 11 12 CommandTools.acedTrans( 13 objStart.ToArray(), 14 rbFrom.UnmanagedObject, 15 rbTo.UnmanagedObject, 16 0, 17 firres 18 ); 19 20 21 CommandTools.acedTrans( 22 objEnd.ToArray(), 23 rbFrom.UnmanagedObject, 24 rbTo.UnmanagedObject, 25 0, 26 secres 27 ); 28 29 30 Extents2d window = 31 new Extents2d( 32 firres[0], 33 firres[1], 34 secres[0], 35 secres[1] 36 ); 37 return window; 38 }坐標轉換