MeasureTracePolygon.cs 49 KB

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