MeasurePolygon.cs 45 KB

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