autocad.net-圖片列印合成

来源:http://www.cnblogs.com/he-xiang/archive/2017/11/24/7890751.html
-Advertisement-
Play Games

調用列印程式“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         }
坐標轉換

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 虛方法: 1、在父類方法的返回值前加 virtual 關鍵字,標記為虛方法,表示這個方法可以被子類重寫。 2、虛方法必須有方法體,方法體中可以沒有任何內容。 3、子類可以根據需求選擇性的是否重寫虛方法。如果需要重寫,在子類方法的返回值前加 override 關鍵字。 4、子類在重寫虛方法時,可以根據 ...
  • .net core的跨平臺有了Linux,不能沒有docker……網上的系列文章一大推,特別是docker還有了中文官網:https://www.docker-cn.com/ 。上面說的很清楚了,這裡只是操作記錄。操作的環境是:Ubuntu 16.04、docker。 一、參照官網安裝docker, ...
  • 恢復內容開始 Session共用是分散式架構設計中的一大難點,儘管session共用的解決方案不少,但是.net 下的解決方案還是比較少,而且說明文檔也很少。 之前嘗試用memcached緩存session,以解決session共用問題,後來發現實在是沒有解決方案,github上有一個Memcach ...
  • 在博客園也很多年了,一直未曾分享過什麼東西,也沒有寫過博客,但自己也是汲取著博客園的知識成長的; 這兩天想著不能這麼無私,最近.NET CORE貌似挺流行的,閑來無事也自己搞了個asp.net core signalr 博客園裡面也有人在.net core 2.0下麵集成了signalr,但是是集成 ...
  • PS: 第一次用Repeater控制項 記錄一下 請忽略我的命名不規範 請忽略我的最終效果圖(太醜了) 需要用到的朋友可以自行調整的漂亮點 最終效果圖 HTML 後臺代碼 批註:分頁主要實現在於sql語句上 ...
  • 在net framework 4.0中微軟又提供了一個新的非同步操作的功能,叫做任務並行庫(TPL)。任務並行庫的核心是任務(task)。一個任務代表了一個非同步操作,譔操作可以通過多種方式運行,可以使用或不使用獨立的線程。 一個任務(Task)可以通過多種方式和其他任務組合起來使用。... ...
  • 背水一戰 Windows 10 之 控制項(控制項基類 - FrameworkElement): 基礎知識, 相關事件, HorizontalAlignment 和 VerticalAlignment ...
  • case Acad::eOk:lstrcpy(Glb_AcadErrorInfo,_T("正確"));break;case Acad::eNotImplementedYet:lstrcpy(Glb_AcadErrorInfo,_T("尚未實現"));break;case Acad::eNotAppl ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...