MeasureRectangle.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.SettingModel;
  3. using PaintDotNet.Base.CommTool;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Windows.Forms;
  11. namespace PaintDotNet.Annotation.Measure
  12. {
  13. using PointList = List<PointF>;
  14. /// <summary>
  15. /// 测量->多边形测量->矩形
  16. /// </summary>
  17. public class MeasureRectangle : MeasureDrawObject
  18. {
  19. public PointList pointArray;
  20. /// <summary>
  21. /// Graphic objects for hit test
  22. /// </summary>
  23. private GraphicsPath areaPath = null;
  24. private Pen areaPen = null;
  25. private Region areaRegion = null;
  26. /// <summary>
  27. /// 周长
  28. /// </summary>
  29. //private double length = 0.0;
  30. /// <summary>
  31. /// 面积
  32. /// </summary>
  33. public double area = 0.0;
  34. /// <summary>
  35. /// 测量信息矩形定义
  36. /// </summary>
  37. private RectangleF rectangleF1 = new RectangleF();
  38. private RectangleF rectangleF2 = new RectangleF();
  39. private RectangleF rectangleF3 = new RectangleF();
  40. private RectangleF rectangleF4 = new RectangleF();
  41. private RectangleF rectangleF5 = new RectangleF();
  42. private RectangleF rectangleF6 = new RectangleF();
  43. private RectangleF rectangleF7 = new RectangleF();
  44. private RectangleF rectangleF8 = new RectangleF();
  45. private RectangleF rectangleF9 = new RectangleF();
  46. private RectangleF rectangleF10 = new RectangleF();
  47. private RectangleF rectangleF11 = new RectangleF();
  48. /// <summary>
  49. /// 文本上用于拖动的点
  50. /// </summary>
  51. private Point pointL = new Point();
  52. /// <summary>
  53. /// 绘制限制(第一次绘制时)
  54. /// </summary>
  55. public bool pointChange = true;
  56. /// <summary>
  57. /// 区分移动文本
  58. /// </summary>
  59. private int moveKb;
  60. /// <summary>
  61. /// 绘制限制(从配置文件加载)
  62. /// </summary>
  63. public bool configurationFile = false;
  64. /// <summary>
  65. /// 绘制限制(更改属性时)
  66. /// </summary>
  67. private bool SavePointChange;
  68. /// <summary>
  69. /// 绘制属性
  70. /// </summary>
  71. private string[] drawingPropertiesList;
  72. /// <summary>
  73. /// 绘制属性(克隆)
  74. /// </summary>
  75. private string[] drawingPropertiesListClone;
  76. private bool drawing;
  77. /// <summary>
  78. /// 是否移动了鼠标
  79. /// </summary>
  80. public bool moved = false;
  81. /// <summary>
  82. /// 限制绘制(绘制中)
  83. /// </summary>
  84. public bool mouseUpPointChange = false;
  85. /// <summary>
  86. /// 测量样式信息model
  87. /// </summary>
  88. private MeasureStyleModel.MeasureRectangle measureStyleModel;
  89. public MeasureStyleModel.MeasureRectangle MeasureStyleModel
  90. {
  91. set
  92. {
  93. this.measureStyleModel = value;
  94. }
  95. }
  96. public MeasureRectangle(ISurfaceBox surfaceBox, int x, int y, int width, int height) : base()
  97. {
  98. this.objectType = DrawClass.Measure;
  99. this.drawToolType = DrawToolType.MeasureRectangle;
  100. this.ISurfaceBox = surfaceBox;
  101. rectangle.X = x;
  102. rectangle.Y = y;
  103. rectangle.Width = width;
  104. rectangle.Height = height;
  105. Initialize();
  106. }
  107. public MeasureRectangle(ISurfaceBox surfaceBox, Point startPoint, Point endPoint, bool clone) : base(surfaceBox)
  108. {
  109. this.objectType = DrawClass.Measure;
  110. this.drawToolType = DrawToolType.MeasureRectangle;
  111. measureStyleModel = surfaceBox.GetMeasureStyleModel().measureRectangle;
  112. pointArray = new PointList();
  113. if (clone)
  114. {
  115. drawing = true;
  116. pointArray.Add(startPoint);
  117. pointArray.Add(endPoint);
  118. }
  119. this.startPoint = startPoint;
  120. this.endPoint = endPoint;
  121. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  122. this.unitString = surfaceBox.GetPxPerUnit()[1];
  123. this.unit = surfaceBox.GetPxPerUnit()[2];
  124. Initialize();
  125. }
  126. public MeasureRectangle(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  127. {
  128. this.objectType = DrawClass.Measure;
  129. this.drawToolType = DrawToolType.MeasureRectangle;
  130. this.ISurfaceBox = surfaceBox;
  131. measureStyleModel = (MeasureStyleModel.MeasureRectangle)parentStyleModel;
  132. pointArray = DrawRulerHelper.DeepCopyListByReflect(points);
  133. startPoint = points[0];
  134. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  135. this.unitString = surfaceBox.GetPxPerUnit()[1];
  136. this.unit = surfaceBox.GetPxPerUnit()[2];
  137. this.configurationFile = true;
  138. this.moved = true;
  139. }
  140. /// <summary>
  141. /// Clone this instance
  142. /// </summary>
  143. public override DrawObject Clone()
  144. {
  145. MeasureRectangle drawPolygon = new MeasureRectangle(ISurfaceBox, new Point((int)pointArray[0].X, (int)pointArray[0].Y), new Point((int)pointArray[1].X, (int)pointArray[1].Y), false);
  146. drawPolygon.ISurfaceBox = ISurfaceBox;
  147. foreach (PointF p in this.pointArray)
  148. {
  149. drawPolygon.pointArray.Add(p);
  150. }
  151. if (drawingPropertiesList != null)
  152. drawPolygon.drawingPropertiesListClone = drawingPropertiesList;
  153. drawPolygon.moved = true;
  154. FillDrawObjectFields(drawPolygon);
  155. return drawPolygon;
  156. }
  157. public override DrawObject Clone(ISurfaceBox surfaceBox)
  158. {
  159. MeasureRectangle drawPolygon = new MeasureRectangle(surfaceBox, this.GetPoints(), this.measureStyleModel, null);
  160. if (drawingPropertiesList != null)
  161. drawPolygon.drawingPropertiesListClone = drawingPropertiesList;
  162. drawPolygon.moved = true;
  163. FillDrawObjectFields(drawPolygon);
  164. return drawPolygon;
  165. }
  166. /// <summary>
  167. /// 测量属性
  168. /// </summary>
  169. /// <returns></returns>
  170. public override Dictionary<System.Enum, object> GetData()
  171. {
  172. if (data.ContainsKey(MeasureAttributes.MeasureMethod))
  173. data[MeasureAttributes.MeasureMethod] = PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text");
  174. else
  175. data.Add(MeasureAttributes.MeasureMethod, PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"));
  176. if (data.ContainsKey(MeasureAttributes.MeasureUnitCN))
  177. data[MeasureAttributes.MeasureUnitCN] = this.unitString;
  178. else
  179. data.Add(MeasureAttributes.MeasureUnitCN, this.unitString);
  180. if (data.ContainsKey(MeasureAttributes.MeasureUnitEN))
  181. data[MeasureAttributes.MeasureUnitEN] = this.unit;
  182. else
  183. data.Add(MeasureAttributes.MeasureUnitEN, this.unit);
  184. if (data.ContainsKey(MeasureAttributes.PixelStartX))
  185. data[MeasureAttributes.PixelStartX] = startPoint.X;
  186. else
  187. data.Add(MeasureAttributes.PixelStartX, startPoint.X);
  188. if (data.ContainsKey(MeasureAttributes.PixelStartY))
  189. data[MeasureAttributes.PixelStartY] = startPoint.Y;
  190. else
  191. data.Add(MeasureAttributes.PixelStartY, startPoint.Y);
  192. if (data.ContainsKey(MeasureAttributes.PhysicalStartX))
  193. data[MeasureAttributes.PhysicalStartX] = Math.Round(startPoint.X * unitLength, decimalPlaces);
  194. else
  195. data.Add(MeasureAttributes.PhysicalStartX, Math.Round(startPoint.X * unitLength, decimalPlaces));
  196. if (data.ContainsKey(MeasureAttributes.PhysicalStartY))
  197. data[MeasureAttributes.PhysicalStartY] = Math.Round(startPoint.Y * unitLength, decimalPlaces);
  198. else
  199. data.Add(MeasureAttributes.PhysicalStartY, Math.Round(startPoint.Y * unitLength, decimalPlaces));
  200. if (data.ContainsKey(MeasureAttributes.PixelArea))
  201. data[MeasureAttributes.PixelArea] = Math.Round(area, decimalPlaces);
  202. else
  203. data.Add(MeasureAttributes.PixelArea, Math.Round(area, decimalPlaces));
  204. string s = Math.Round(this.area * unitLength, decimalPlaces).ToString();
  205. if (s.IndexOf(".") == -1)
  206. {
  207. for (int i = 0; i < decimalPlaces; i++)
  208. {
  209. if (i == 0)
  210. s += ".";
  211. s += "0";
  212. }
  213. }
  214. else
  215. {
  216. int a = s.Length - s.IndexOf(".") - 1;
  217. if (a < decimalPlaces)
  218. {
  219. for (int i = 0; i < decimalPlaces - a; i++)
  220. {
  221. s += "0";
  222. }
  223. }
  224. }
  225. if (data.ContainsKey(MeasureAttributes.PhysicalArea))
  226. data[MeasureAttributes.PhysicalArea] = s;
  227. else
  228. data.Add(MeasureAttributes.PhysicalArea, s);
  229. if (data.ContainsKey(MeasureAttributes.PixelCircumference))
  230. data[MeasureAttributes.PixelCircumference] = Math.Round(length, decimalPlaces);
  231. else
  232. data.Add(MeasureAttributes.PixelCircumference, Math.Round(length, decimalPlaces));
  233. s = Math.Round(this.length * unitLength, decimalPlaces).ToString();
  234. if (s.IndexOf(".") == -1)
  235. {
  236. for (int i = 0; i < decimalPlaces; i++)
  237. {
  238. if (i == 0)
  239. s += ".";
  240. s += "0";
  241. }
  242. }
  243. else
  244. {
  245. int a = s.Length - s.IndexOf(".") - 1;
  246. if (a < decimalPlaces)
  247. {
  248. for (int i = 0; i < decimalPlaces - a; i++)
  249. {
  250. s += "0";
  251. }
  252. }
  253. }
  254. if (data.ContainsKey(MeasureAttributes.PhysicalCircumference))
  255. data[MeasureAttributes.PhysicalCircumference] = s;
  256. else
  257. data.Add(MeasureAttributes.PhysicalCircumference, s);
  258. return data;
  259. }
  260. public override void Draw(Graphics g)
  261. {
  262. if(this.moved)
  263. {
  264. measureStyleModel = this.ISurfaceBox.GetMeasureStyleModel().measureRectangle;
  265. drawingProperties.TryGetValue(this.drawToolType, out drawingPropertiesList);
  266. if (drawingPropertiesList == null)
  267. {
  268. drawingPropertiesList = drawingPropertiesListClone;
  269. if (!this.drawing)
  270. this.configurationFile = true;
  271. }
  272. pointChangeObject.TryGetValue(this.drawToolType, out SavePointChange);
  273. string frontStr = string.Empty;
  274. if (this.measureStyleModel.showSuffix)
  275. {
  276. frontStr = this.measureStyleModel.suffixName;
  277. }
  278. if (this.measureStyleModel.showSerial)
  279. {
  280. #region [获取序号]
  281. List<DrawObject> newGraphicsList = new List<DrawObject>();
  282. int n = ISurfaceBox.GraphicsList.Count;
  283. int num = 0;
  284. for (int i = n - 1; i >= 0; i--)
  285. {
  286. newGraphicsList.Add(ISurfaceBox.GraphicsList[i].Clone());
  287. }
  288. var newGraphics = newGraphicsList.Where(m => m.ID.Equals(this.ID)).FirstOrDefault();
  289. if (newGraphics != null)
  290. {
  291. num = newGraphicsList.IndexOf(newGraphics) + 1;
  292. }
  293. #endregion
  294. frontStr = $"[{ num }]{frontStr}";
  295. }
  296. if (this.measureStyleModel.showAlias)
  297. {
  298. frontStr = this.measureStyleModel.aliasName + frontStr;
  299. }
  300. if (HandleCount - 1 >= 2)
  301. {
  302. SizeF sizeF = new SizeF();
  303. g.SmoothingMode = SmoothingMode.AntiAlias;
  304. Font textfont = new Font(measureStyleModel.font, measureStyleModel.fontSize);
  305. Brush textbrush = new SolidBrush(Color.FromArgb(measureStyleModel.textColor));
  306. Pen pen = new Pen(Color.FromArgb(measureStyleModel.lineColor), measureStyleModel.lineWidth);
  307. pen.DashStyle = (DashStyle)measureStyleModel.lineStyle;
  308. if (this.Selected)
  309. {
  310. if (measureStyleModel.chooseStyle != null)
  311. {
  312. textfont = new Font(measureStyleModel.chooseStyle.font, measureStyleModel.chooseStyle.fontSize);
  313. textbrush = new SolidBrush(Color.FromArgb(measureStyleModel.chooseStyle.textColor));
  314. pen = new Pen(Color.FromArgb(measureStyleModel.chooseStyle.lineColor), measureStyleModel.chooseStyle.lineWidth);
  315. pen.DashStyle = (DashStyle)measureStyleModel.chooseStyle.lineStyle;
  316. }
  317. }
  318. if (!this.Selected)
  319. {
  320. this.pointChange = true;
  321. rectangleF1 = new RectangleF();
  322. rectangleF2 = new RectangleF();
  323. rectangleF3 = new RectangleF();
  324. rectangleF4 = new RectangleF();
  325. rectangleF5 = new RectangleF();
  326. rectangleF6 = new RectangleF();
  327. rectangleF7 = new RectangleF();
  328. rectangleF8 = new RectangleF();
  329. rectangleF9 = new RectangleF();
  330. rectangleF10 = new RectangleF();
  331. rectangleF11 = new RectangleF();
  332. }
  333. PointF point = new Point();
  334. //判断象限
  335. float x = pointArray[1].X - pointArray[0].X;
  336. if (x == 0)
  337. x = 1;
  338. float y = pointArray[1].Y - pointArray[0].Y;
  339. if (y == 0)
  340. y = 1;
  341. if (x > 0 && y > 0) //第4象限
  342. {
  343. point = pointArray[0];
  344. }
  345. else if (x > 0 && y < 0) //第1象限
  346. {
  347. point = new PointF(pointArray[0].X, pointArray[1].Y);
  348. }
  349. else if (x < 0 && y < 0) //第2象限
  350. {
  351. point = pointArray[1];
  352. }
  353. else if (x < 0 && y > 0) //第3象限
  354. {
  355. point = new PointF(pointArray[1].X, pointArray[0].Y);
  356. }
  357. g.DrawRectangle(pen, point.X, point.Y,
  358. Math.Abs(pointArray[0].X - pointArray[1].X), Math.Abs(pointArray[0].Y - pointArray[1].Y));
  359. length = (Math.Abs(pointArray[0].X - pointArray[1].X) + Math.Abs(pointArray[0].Y - pointArray[1].Y)) * 2;
  360. area = Math.Abs(pointArray[0].X - pointArray[1].X) * Math.Abs(pointArray[0].Y - pointArray[1].Y);
  361. PointF pointF = new PointF();
  362. if (pointArray[1].X > pointArray[0].X && pointArray[1].Y > pointArray[0].Y)
  363. pointF = pointArray[0];
  364. else if (pointArray[1].X > pointArray[0].X && pointArray[1].Y < pointArray[0].Y)
  365. pointF = new PointF(pointArray[0].X, pointArray[1].Y);
  366. else if (pointArray[1].X < pointArray[0].X && pointArray[1].Y > pointArray[0].Y)
  367. pointF = new PointF(pointArray[1].X, pointArray[0].Y);
  368. else if (pointArray[1].X < pointArray[0].X && pointArray[1].Y < pointArray[0].Y)
  369. pointF = pointArray[1];
  370. // 是否绘制
  371. if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute)
  372. {
  373. // 属性文本的间隔定义
  374. int offsetValue = measureStyleModel.fontSize + measureStyleModel.fontSize / 2;
  375. int offsetValue1 = measureStyleModel.lineWidth * 2 / 3;
  376. int offset = offsetValue1;
  377. this.pointL = new Point((int)pointArray[0].X, (int)pointArray[0].Y);
  378. if (drawingPropertiesList != null)
  379. {
  380. // 像素周长
  381. if (drawingPropertiesList.Contains(MeasureAttributes.PixelCircumference.ToString()))
  382. {
  383. sizeF = g.MeasureString(frontStr + "" + Math.Round(this.length, decimalPlaces) + "px", textfont);
  384. rectangleF10.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  385. offset += offsetValue;
  386. }
  387. // 物理周长
  388. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalCircumference.ToString()))
  389. {
  390. string s = Math.Round(this.length * unitLength, decimalPlaces).ToString();
  391. if (s.IndexOf(".") == -1)
  392. {
  393. for (int i = 0; i < decimalPlaces; i++)
  394. {
  395. if (i == 0)
  396. s += ".";
  397. s += "0";
  398. }
  399. }
  400. else
  401. {
  402. int a = s.Length - s.IndexOf(".") - 1;
  403. if (a < decimalPlaces)
  404. {
  405. for (int i = 0; i < decimalPlaces - a; i++)
  406. {
  407. s += "0";
  408. }
  409. }
  410. }
  411. sizeF = g.MeasureString(frontStr + s + this.unit, textfont);
  412. rectangleF11.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  413. offset += offsetValue;
  414. }
  415. // 像素面积
  416. if (drawingPropertiesList.Contains(MeasureAttributes.PixelArea.ToString()))
  417. {
  418. sizeF = g.MeasureString(frontStr + "" + Math.Round(this.area, decimalPlaces) + "px²", textfont);
  419. rectangleF8.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  420. offset += offsetValue;
  421. }
  422. // 物理面积
  423. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalArea.ToString()))
  424. {
  425. string s = Math.Round(this.area * unitLength, decimalPlaces).ToString();
  426. if (s.IndexOf(".") == -1)
  427. {
  428. for (int i = 0; i < decimalPlaces; i++)
  429. {
  430. if (i == 0)
  431. s += ".";
  432. s += "0";
  433. }
  434. }
  435. else
  436. {
  437. int a = s.Length - s.IndexOf(".") - 1;
  438. if (a < decimalPlaces)
  439. {
  440. for (int i = 0; i < decimalPlaces - a; i++)
  441. {
  442. s += "0";
  443. }
  444. }
  445. }
  446. sizeF = g.MeasureString(frontStr + s + this.unit + "²", textfont);
  447. rectangleF9.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  448. offset += offsetValue;
  449. }
  450. // 测量方式
  451. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  452. {
  453. sizeF = g.MeasureString(frontStr + "" + PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"), textfont);
  454. rectangleF1.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  455. offset += offsetValue;
  456. }
  457. // 测量单位(中文)
  458. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  459. {
  460. sizeF = g.MeasureString(frontStr + "" + this.unitString, textfont);
  461. rectangleF2.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  462. offset += offsetValue;
  463. }
  464. // 测量单位(英文)
  465. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  466. {
  467. sizeF = g.MeasureString(frontStr + "" + this.unit, textfont);
  468. rectangleF3.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  469. offset += offsetValue;
  470. }
  471. // 像素起始点X
  472. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  473. {
  474. sizeF = g.MeasureString(frontStr + "" + startPoint.X, textfont);
  475. rectangleF4.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  476. offset += offsetValue;
  477. }
  478. // 像素起始点Y
  479. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  480. {
  481. sizeF = g.MeasureString(frontStr + "" + startPoint.Y, textfont);
  482. rectangleF5.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  483. offset += offsetValue;
  484. }
  485. // 物理起始点X
  486. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  487. {
  488. sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
  489. rectangleF6.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  490. offset += offsetValue;
  491. }
  492. // 物理起始点Y
  493. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  494. {
  495. sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
  496. rectangleF7.Location = new Point((int)pointF.X + offsetValue1 - (int)sizeF.Width / 2, (int)pointF.Y + offset);
  497. offset += offsetValue;
  498. }
  499. }
  500. }
  501. if (drawingPropertiesList != null)
  502. {
  503. // 测量方式
  504. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  505. {
  506. sizeF = g.MeasureString(frontStr + "" + PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"), textfont);
  507. rectangleF1.Width = sizeF.Width;
  508. rectangleF1.Height = sizeF.Height;
  509. g.DrawString(frontStr + "" + PdnResources.GetString("Menu.MeasureAction.PolygonMeasurement.Text"), textfont, textbrush, rectangleF1);
  510. }
  511. else
  512. {
  513. rectangleF1 = new RectangleF();
  514. }
  515. // 测量单位(中文)
  516. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  517. {
  518. sizeF = g.MeasureString(frontStr + "" + this.unitString, textfont);
  519. rectangleF2.Width = sizeF.Width;
  520. rectangleF2.Height = sizeF.Height;
  521. g.DrawString(frontStr + "" + this.unitString, textfont, textbrush, rectangleF2);
  522. }
  523. else
  524. {
  525. rectangleF2 = new RectangleF();
  526. }
  527. // 测量单位(英文)
  528. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  529. {
  530. sizeF = g.MeasureString(frontStr + "" + this.unit, textfont);
  531. rectangleF3.Width = sizeF.Width;
  532. rectangleF3.Height = sizeF.Height;
  533. g.DrawString(frontStr + "" + this.unit, textfont, textbrush, rectangleF3);
  534. }
  535. else
  536. {
  537. rectangleF3 = new RectangleF();
  538. }
  539. // 像素起始点X
  540. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  541. {
  542. sizeF = g.MeasureString(frontStr + "" + startPoint.X, textfont);
  543. rectangleF4.Width = sizeF.Width;
  544. rectangleF4.Height = sizeF.Height;
  545. g.DrawString(frontStr + "" + startPoint.X, textfont, textbrush, rectangleF4);
  546. }
  547. else
  548. {
  549. rectangleF4 = new RectangleF();
  550. }
  551. // 像素起始点Y
  552. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  553. {
  554. sizeF = g.MeasureString(frontStr + "" + startPoint.Y, textfont);
  555. rectangleF5.Width = sizeF.Width;
  556. rectangleF5.Height = sizeF.Height;
  557. g.DrawString(frontStr + "" + startPoint.Y, textfont, textbrush, rectangleF5);
  558. }
  559. else
  560. {
  561. rectangleF5 = new RectangleF();
  562. }
  563. // 物理起始点X
  564. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  565. {
  566. sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont);
  567. rectangleF6.Width = sizeF.Width;
  568. rectangleF6.Height = sizeF.Height;
  569. g.DrawString(frontStr + "" + Math.Round(startPoint.X * unitLength, decimalPlaces), textfont, textbrush, rectangleF6);
  570. }
  571. else
  572. {
  573. rectangleF6 = new RectangleF();
  574. }
  575. // 物理起始点Y
  576. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  577. {
  578. sizeF = g.MeasureString(frontStr + "" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont);
  579. rectangleF7.Width = sizeF.Width;
  580. rectangleF7.Height = sizeF.Height;
  581. g.DrawString(frontStr + "" + Math.Round(startPoint.Y * unitLength, decimalPlaces), textfont, textbrush, rectangleF7);
  582. }
  583. else
  584. {
  585. rectangleF7 = new RectangleF();
  586. }
  587. // 像素面积
  588. if (drawingPropertiesList.Contains(MeasureAttributes.PixelArea.ToString()))
  589. {
  590. sizeF = g.MeasureString(frontStr + "" + Math.Round(this.area, decimalPlaces) + "px²", textfont);
  591. rectangleF8.Width = sizeF.Width;
  592. rectangleF8.Height = sizeF.Height;
  593. g.DrawString(frontStr + "" + Math.Round(this.area, decimalPlaces) + "px²", textfont, textbrush, rectangleF8);
  594. }
  595. else
  596. {
  597. rectangleF8 = new RectangleF();
  598. }
  599. // 物理面积
  600. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalArea.ToString()))
  601. {
  602. string s = Math.Round(this.area * unitLength, decimalPlaces).ToString();
  603. if (s.IndexOf(".") == -1)
  604. {
  605. for (int i = 0; i < decimalPlaces; i++)
  606. {
  607. if (i == 0)
  608. s += ".";
  609. s += "0";
  610. }
  611. }
  612. else
  613. {
  614. int a = s.Length - s.IndexOf(".") - 1;
  615. if (a < decimalPlaces)
  616. {
  617. for (int i = 0; i < decimalPlaces - a; i++)
  618. {
  619. s += "0";
  620. }
  621. }
  622. }
  623. sizeF = g.MeasureString(frontStr + s + this.unit + "²", textfont);
  624. rectangleF9.Width = sizeF.Width;
  625. rectangleF9.Height = sizeF.Height;
  626. g.DrawString(frontStr + s + this.unit + "²", textfont, textbrush, rectangleF9);
  627. }
  628. else
  629. {
  630. rectangleF9 = new RectangleF();
  631. }
  632. // 像素周长
  633. if (drawingPropertiesList.Contains(MeasureAttributes.PixelCircumference.ToString()))
  634. {
  635. sizeF = g.MeasureString(frontStr + "" + Math.Round(this.length, decimalPlaces) + "px", textfont);
  636. rectangleF10.Width = sizeF.Width;
  637. rectangleF10.Height = sizeF.Height;
  638. g.DrawString(frontStr + "" + Math.Round(this.length, decimalPlaces) + "px", textfont, textbrush, rectangleF10);
  639. }
  640. else
  641. {
  642. rectangleF10 = new RectangleF();
  643. }
  644. // 物理周长
  645. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalCircumference.ToString()))
  646. {
  647. string s = Math.Round(this.length * unitLength, decimalPlaces).ToString();
  648. if (s.IndexOf(".") == -1)
  649. {
  650. for (int i = 0; i < decimalPlaces; i++)
  651. {
  652. if (i == 0)
  653. s += ".";
  654. s += "0";
  655. }
  656. }
  657. else
  658. {
  659. int a = s.Length - s.IndexOf(".") - 1;
  660. if (a < decimalPlaces)
  661. {
  662. for (int i = 0; i < decimalPlaces - a; i++)
  663. {
  664. s += "0";
  665. }
  666. }
  667. }
  668. sizeF = g.MeasureString(frontStr + s + this.unit, textfont);
  669. rectangleF11.Width = sizeF.Width;
  670. rectangleF11.Height = sizeF.Height;
  671. g.DrawString(frontStr + s + this.unit, textfont, textbrush, rectangleF11);
  672. }
  673. else
  674. {
  675. rectangleF11 = new RectangleF();
  676. }
  677. }
  678. pen.Dispose();
  679. }
  680. if (this.configurationFile)
  681. this.pointChange = false;
  682. this.mouseUpAttribute = false;
  683. pointChangeObject.Remove(this.drawToolType);
  684. }
  685. }
  686. /// <summary>
  687. /// 停止绘制时
  688. /// </summary>
  689. /// <param name="up"></param>
  690. public override void MouseUp(bool up)
  691. {
  692. mouseUpPointChange = up;
  693. }
  694. public void AddPoint(Point point)
  695. {
  696. pointArray.Add(point);
  697. }
  698. public override int HandleCount
  699. {
  700. get
  701. {
  702. return pointArray.Count + 1;
  703. }
  704. }
  705. /// <summary>
  706. /// Get handle pointscroll by 1-based number
  707. /// </summary>
  708. /// <param name="handleNumber"></param>
  709. /// <returns></returns>
  710. public override PointF GetHandle(int handleNumber)
  711. {
  712. if (handleNumber == 1)
  713. return pointArray[0];
  714. else if(handleNumber == 2)
  715. return pointArray[1];
  716. else
  717. return this.pointL;
  718. }
  719. public override Cursor GetHandleCursor(int handleNumber)
  720. {
  721. return handleCursor;
  722. }
  723. public override void MoveHandleTo(Point point, int handleNumber)
  724. {
  725. if (handleNumber == 1 || handleNumber == 2)
  726. {
  727. this.mouseUpPointChange = true;
  728. pointArray[handleNumber - 1] = point;
  729. }
  730. else
  731. {
  732. if (this.moveKb == 1)
  733. this.rectangleF1.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  734. else if (this.moveKb == 2)
  735. this.rectangleF2.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  736. else if (this.moveKb == 3)
  737. this.rectangleF3.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  738. else if (this.moveKb == 4)
  739. this.rectangleF4.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  740. else if (this.moveKb == 5)
  741. this.rectangleF5.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  742. else if (this.moveKb == 6)
  743. this.rectangleF6.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  744. else if (this.moveKb == 7)
  745. this.rectangleF7.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  746. else if (this.moveKb == 8)
  747. this.rectangleF8.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  748. else if (this.moveKb == 9)
  749. this.rectangleF9.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  750. else if (this.moveKb == 10)
  751. this.rectangleF10.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  752. else if (this.moveKb == 11)
  753. this.rectangleF11.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  754. this.pointL = point;
  755. }
  756. Invalidate();
  757. }
  758. public override void Move(int deltaX, int deltaY)
  759. {
  760. int n = pointArray.Count;
  761. PointF point;
  762. for (int i = 0; i < n; i++)
  763. {
  764. point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
  765. pointArray[i] = point;
  766. }
  767. this.startPoint = pointArray[0];
  768. int x = ISurfaceBox.UnscaleScalar(deltaX);
  769. int y = ISurfaceBox.UnscaleScalar(deltaY);
  770. pointL.X += x;
  771. pointL.Y += y;
  772. this.rectangleF1.Offset(x, y);
  773. this.rectangleF2.Offset(x, y);
  774. this.rectangleF3.Offset(x, y);
  775. this.rectangleF4.Offset(x, y);
  776. this.rectangleF5.Offset(x, y);
  777. this.rectangleF6.Offset(x, y);
  778. this.rectangleF7.Offset(x, y);
  779. this.rectangleF8.Offset(x, y);
  780. this.rectangleF9.Offset(x, y);
  781. this.rectangleF10.Offset(x, y);
  782. this.rectangleF11.Offset(x, y);
  783. Invalidate();
  784. }
  785. /// <summary>
  786. /// 用于创建一个路径或者是闭合的范围
  787. /// 用于响应点击选中
  788. /// 需要咨询用户是仅点击线还是矩形选择
  789. /// </summary>
  790. protected virtual void CreateObjects()
  791. {
  792. if (AreaPath != null)
  793. return;
  794. AreaPath = new GraphicsPath();
  795. AreaPath.AddRectangle(GetBoundingBox());
  796. AreaPath.CloseFigure();
  797. AreaRegion = new Region(AreaPath);
  798. }
  799. /// <summary>
  800. /// Invalidate object.
  801. /// When object is invalidated, path used for hit test
  802. /// is released and should be created again.
  803. /// </summary>
  804. protected void Invalidate()
  805. {
  806. if (AreaPath != null)
  807. {
  808. AreaPath.Dispose();
  809. AreaPath = null;
  810. }
  811. if (AreaPen != null)
  812. {
  813. AreaPen.Dispose();
  814. AreaPen = null;
  815. }
  816. if (AreaRegion != null)
  817. {
  818. AreaRegion.Dispose();
  819. AreaRegion = null;
  820. }
  821. }
  822. protected GraphicsPath AreaPath
  823. {
  824. get
  825. {
  826. return areaPath;
  827. }
  828. set
  829. {
  830. areaPath = value;
  831. }
  832. }
  833. protected Pen AreaPen
  834. {
  835. get
  836. {
  837. return areaPen;
  838. }
  839. set
  840. {
  841. areaPen = value;
  842. }
  843. }
  844. protected Region AreaRegion
  845. {
  846. get
  847. {
  848. return areaRegion;
  849. }
  850. set
  851. {
  852. areaRegion = value;
  853. }
  854. }
  855. /// <summary>
  856. /// Draw tracker for selected object
  857. /// </summary>
  858. /// <param name="g"></param>
  859. public override void DrawTracker(Graphics g)
  860. {
  861. if (!Selected)
  862. return;
  863. SolidBrush brush = new SolidBrush(Color.FromArgb(MarkpointAreaColor));
  864. Pen pen = new Pen(Color.FromArgb(MarkpointLineColor), MarkpointLineWidth);
  865. for (int i = 1; i <= HandleCount; i++)
  866. {
  867. if (i == HandleCount)
  868. {
  869. brush = new SolidBrush(Color.Transparent);
  870. pen = new Pen(Color.Transparent);
  871. }
  872. switch (MarkpointStyle)
  873. {
  874. case 0:
  875. g.FillRectangle(brush, GetHandleRectangle(i));
  876. g.DrawRectangle(pen, GetHandleRectangle(i));
  877. break;
  878. case 1:
  879. g.FillEllipse(brush, GetHandleRectangle(i));
  880. g.DrawEllipse(pen, GetHandleRectangle(i));
  881. break;
  882. case 2:
  883. g.FillPolygon(brush, GetHandlePoint(i));
  884. g.DrawPolygon(pen, GetHandlePoint(i));
  885. break;
  886. }
  887. }
  888. //g.DrawRectangle(new Pen(Color.White), GetBoundingBox());
  889. brush.Dispose();
  890. pen.Dispose();
  891. RectangleF r = GetBoundingBox();
  892. //g.DrawRectangle(new Pen(Color.White), r.X, r.Y, r.Width, r.Height);
  893. }
  894. /// <summary>
  895. /// Hit test.
  896. /// Return value: -1 - no hit
  897. /// 0 - hit anywhere
  898. /// > 1 - handle number
  899. /// </summary>
  900. /// <param name="pointscroll"></param>
  901. /// <returns></returns>
  902. public override int HitTest(Point point)
  903. {
  904. if (Selected)
  905. {
  906. for (int i = 1; i <= HandleCount; i++)
  907. {
  908. if (GetHandleRectangle(i).Contains(point))
  909. return i;
  910. }
  911. if (this.rectangleF1.Contains(point))
  912. {
  913. this.pointL = point;
  914. moveKb = 1;
  915. return this.pointArray.Count;
  916. }
  917. else if (this.rectangleF2.Contains(point))
  918. {
  919. this.pointL = point;
  920. moveKb = 2;
  921. return this.pointArray.Count;
  922. }
  923. else if (this.rectangleF3.Contains(point))
  924. {
  925. this.pointL = point;
  926. moveKb = 3;
  927. return this.pointArray.Count;
  928. }
  929. else if (this.rectangleF4.Contains(point))
  930. {
  931. this.pointL = point;
  932. moveKb = 4;
  933. return this.pointArray.Count;
  934. }
  935. else if (this.rectangleF5.Contains(point))
  936. {
  937. this.pointL = point;
  938. moveKb = 5;
  939. return this.pointArray.Count;
  940. }
  941. else if (this.rectangleF6.Contains(point))
  942. {
  943. this.pointL = point;
  944. moveKb = 6;
  945. return this.pointArray.Count;
  946. }
  947. else if (this.rectangleF7.Contains(point))
  948. {
  949. this.pointL = point;
  950. moveKb = 7;
  951. return this.pointArray.Count;
  952. }
  953. else if (this.rectangleF8.Contains(point))
  954. {
  955. this.pointL = point;
  956. moveKb = 8;
  957. return this.pointArray.Count;
  958. }
  959. else if (this.rectangleF9.Contains(point))
  960. {
  961. this.pointL = point;
  962. moveKb = 9;
  963. return this.pointArray.Count;
  964. }
  965. else if (this.rectangleF10.Contains(point))
  966. {
  967. this.pointL = point;
  968. moveKb = 10;
  969. return this.pointArray.Count;
  970. }
  971. else if (this.rectangleF11.Contains(point))
  972. {
  973. this.pointL = point;
  974. moveKb = 11;
  975. return this.pointArray.Count;
  976. }
  977. }
  978. if (PointInObject(point))
  979. return 0;
  980. return -1;
  981. }
  982. protected override bool PointInObject(Point point)
  983. {
  984. CreateObjects();
  985. return AreaRegion.IsVisible(point);
  986. }
  987. public override bool IntersectsWith(Rectangle rectangle)
  988. {
  989. CreateObjects();
  990. return AreaRegion.IsVisible(rectangle);
  991. }
  992. public override RectangleF GetBoundingBox()
  993. {
  994. RectangleF rectangle;
  995. float minx = 0, maxx = 0, miny = 0, maxy = 0;
  996. for (int i = 0; i < pointArray.Count; i++)
  997. {
  998. if (i == 0)
  999. {
  1000. minx = maxx = pointArray[i].X;
  1001. miny = maxy = pointArray[i].Y;
  1002. }
  1003. else
  1004. {
  1005. if (pointArray[i].X > maxx) maxx = pointArray[i].X;
  1006. if (pointArray[i].X < minx) minx = pointArray[i].X;
  1007. if (pointArray[i].Y > maxy) maxy = pointArray[i].Y;
  1008. if (pointArray[i].Y < miny) miny = pointArray[i].Y;
  1009. }
  1010. }
  1011. rectangle = new RectangleF(minx, miny, maxx - minx, maxy - miny);
  1012. return rectangle;
  1013. }
  1014. internal void setNextPoint(Point p)
  1015. {
  1016. AddPoint(p);
  1017. }
  1018. internal void setEndPoint(Point p)
  1019. {
  1020. endPoint = p;
  1021. }
  1022. public override List<PointF> GetPoints()
  1023. {
  1024. return pointArray;
  1025. }
  1026. public override ParentStyleModel GetStyle()
  1027. {
  1028. return measureStyleModel;
  1029. }
  1030. }
  1031. }