鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > .net技术 > c#编程 > >

autocad.net-图片打印合成

来源:互联网 作者:佚名 时间:2018-02-06 13:39
调用打印程序“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         }
坐标转换

 

网友评论
<