MeasurePointHLine.cs 48 KB

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