MeasureSquare.cs 50 KB

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