MeasureClosedCurve.cs 46 KB

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