MeasurePLine.cs 47 KB

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