MeasureTracePolygon.cs 45 KB

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