MeasurePolygon.cs 48 KB

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