MeasureThreePointAngle.cs 46 KB

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