MeasureLength.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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 MeasureLength : MeasureDrawObject
  19. {
  20. public PointList pointArray;
  21. private const string entryLength = "Length";
  22. private const string entryPoint = "Point";
  23. /// <summary>
  24. /// Graphic objects for hit test
  25. /// </summary>
  26. private GraphicsPath areaPath = null;
  27. private Pen areaPen = null;
  28. private Region areaRegion = null;
  29. ///<summary>
  30. ///长度
  31. ///</summary>
  32. //private double length = 0.0;
  33. /// <summary>
  34. /// 测量信息矩形定义
  35. /// </summary>
  36. private RectangleF rectangleF1 = new RectangleF();
  37. private RectangleF rectangleF2 = new RectangleF();
  38. private RectangleF rectangleF3 = new RectangleF();
  39. private RectangleF rectangleF4 = new RectangleF();
  40. private RectangleF rectangleF5 = new RectangleF();
  41. private RectangleF rectangleF6 = new RectangleF();
  42. private RectangleF rectangleF7 = new RectangleF();
  43. private RectangleF rectangleF8 = new RectangleF();
  44. private RectangleF rectangleF9 = new RectangleF();
  45. /// <summary>
  46. /// 文本上用于拖动的点
  47. /// </summary>
  48. private Point pointL = new Point();
  49. /// <summary>
  50. /// 绘制限制(第一次绘制时)
  51. /// </summary>
  52. public bool pointChange = true;
  53. /// <summary>
  54. /// 区分移动文本
  55. /// </summary>
  56. private int moveKb;
  57. /// <summary>
  58. /// 绘制限制(从配置文件加载)
  59. /// </summary>
  60. public bool configurationFile = false;
  61. /// <summary>
  62. /// 绘制限制(更改属性时)
  63. /// </summary>
  64. private bool SavePointChange;
  65. /// <summary>
  66. /// 绘制属性
  67. /// </summary>
  68. private string[] drawingPropertiesList;
  69. /// <summary>
  70. /// 绘制属性(克隆)
  71. /// </summary>
  72. private string[] drawingPropertiesListClone;
  73. /// <summary>
  74. /// 旋转角度
  75. /// </summary>
  76. private double angleClass;
  77. /// <summary>
  78. /// 属性绘制点
  79. /// </summary>
  80. private PointF rotationPoint;
  81. /// <summary>
  82. /// 限制绘制(绘制中)
  83. /// </summary>
  84. public bool mouseUpPointChange = false;
  85. /// <summary>
  86. /// 测量样式信息model
  87. /// </summary>
  88. private MeasureStyleModel.MeasureLength measureStyleModel;
  89. public MeasureStyleModel.MeasureLength MeasureStyleModel
  90. {
  91. set
  92. {
  93. this.measureStyleModel = value;
  94. }
  95. }
  96. public MeasureLength(ISurfaceBox surfaceBox, int x1, int y1, bool clone) : base(surfaceBox)
  97. {
  98. this.objectType = DrawClass.Measure;
  99. this.drawToolType = DrawToolType.MeasureLength;
  100. measureStyleModel = surfaceBox.GetMeasureStyleModel().measureLength;
  101. pointArray = new PointList();
  102. startPoint.X = x1;
  103. startPoint.Y = y1;
  104. if(clone)
  105. pointArray.Add(new Point(x1, y1));
  106. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  107. this.unitString = surfaceBox.GetPxPerUnit()[1];
  108. this.unit = surfaceBox.GetPxPerUnit()[2];
  109. Initialize();
  110. }
  111. public MeasureLength(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  112. {
  113. this.objectType = DrawClass.Measure;
  114. this.drawToolType = DrawToolType.MeasureLength;
  115. this.ISurfaceBox = surfaceBox;
  116. measureStyleModel = (MeasureStyleModel.MeasureLength)parentStyleModel;
  117. pointArray = DrawRulerHelper.DeepCopyListByReflect(points);
  118. startPoint = points[0];
  119. this.measurementUnit = (MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), surfaceBox.GetPxPerUnit()[0]);
  120. this.unitString = surfaceBox.GetPxPerUnit()[1];
  121. this.unit = surfaceBox.GetPxPerUnit()[2];
  122. this.configurationFile = true;
  123. }
  124. /// <summary>
  125. /// Clone this instance
  126. /// </summary>
  127. public override DrawObject Clone()
  128. {
  129. MeasureLength drawPolygon = new MeasureLength(ISurfaceBox, (int)this.pointArray[0].X, (int)this.pointArray[0].Y, false);
  130. drawPolygon.ISurfaceBox = ISurfaceBox;
  131. foreach (PointF p in this.pointArray)
  132. {
  133. drawPolygon.pointArray.Add(p);
  134. }
  135. if (drawingPropertiesList != null)
  136. drawPolygon.drawingPropertiesListClone = drawingPropertiesList;
  137. FillDrawObjectFields(drawPolygon);
  138. return drawPolygon;
  139. }
  140. public override DrawObject Clone(ISurfaceBox surfaceBox)
  141. {
  142. MeasureLength drawPolygon = new MeasureLength(surfaceBox, this.GetPoints(), this.measureStyleModel, null);
  143. if (drawingPropertiesList != null)
  144. drawPolygon.drawingPropertiesListClone = drawingPropertiesList;
  145. FillDrawObjectFields(drawPolygon);
  146. return drawPolygon;
  147. }
  148. /// <summary>
  149. /// 测量属性
  150. /// </summary>
  151. /// <returns></returns>
  152. public override Dictionary<System.Enum, object> GetData()
  153. {
  154. if (data.ContainsKey(MeasureAttributes.MeasureMethod))
  155. data[MeasureAttributes.MeasureMethod] = PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text");
  156. else
  157. data.Add(MeasureAttributes.MeasureMethod, PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"));
  158. if (data.ContainsKey(MeasureAttributes.MeasureUnitCN))
  159. data[MeasureAttributes.MeasureUnitCN] = this.unitString;
  160. else
  161. data.Add(MeasureAttributes.MeasureUnitCN, this.unitString);
  162. if (data.ContainsKey(MeasureAttributes.MeasureUnitEN))
  163. data[MeasureAttributes.MeasureUnitEN] = this.unit;
  164. else
  165. data.Add(MeasureAttributes.MeasureUnitEN, this.unit);
  166. if (data.ContainsKey(MeasureAttributes.PixelStartX))
  167. data[MeasureAttributes.PixelStartX] = startPoint.X;
  168. else
  169. data.Add(MeasureAttributes.PixelStartX, startPoint.X);
  170. if (data.ContainsKey(MeasureAttributes.PixelStartY))
  171. data[MeasureAttributes.PixelStartY] = startPoint.Y;
  172. else
  173. data.Add(MeasureAttributes.PixelStartY, startPoint.Y);
  174. if (data.ContainsKey(MeasureAttributes.PhysicalStartX))
  175. data[MeasureAttributes.PhysicalStartX] = Math.Round(startPoint.X * unitLength, decimalPlaces);
  176. else
  177. data.Add(MeasureAttributes.PhysicalStartX, Math.Round(startPoint.X * unitLength, decimalPlaces));
  178. if (data.ContainsKey(MeasureAttributes.PhysicalStartY))
  179. data[MeasureAttributes.PhysicalStartY] = Math.Round(startPoint.Y * unitLength, decimalPlaces);
  180. else
  181. data.Add(MeasureAttributes.PhysicalStartY, Math.Round(startPoint.Y * unitLength, decimalPlaces));
  182. if (data.ContainsKey(MeasureAttributes.PixelLength))
  183. data[MeasureAttributes.PixelLength] = Math.Round(length, decimalPlaces);
  184. else
  185. data.Add(MeasureAttributes.PixelLength, Math.Round(length, decimalPlaces));
  186. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  187. if (s.IndexOf(".") == -1)
  188. {
  189. for (int i = 0; i < decimalPlaces; i++)
  190. {
  191. if (i == 0)
  192. s += ".";
  193. s += "0";
  194. }
  195. }
  196. else
  197. {
  198. int a = s.Length - s.IndexOf(".") - 1;
  199. if (a < decimalPlaces)
  200. {
  201. for (int i = 0; i < decimalPlaces - a; i++)
  202. {
  203. s += "0";
  204. }
  205. }
  206. }
  207. if (data.ContainsKey(MeasureAttributes.PhysicalLength))
  208. data[MeasureAttributes.PhysicalLength] = s;
  209. else
  210. data.Add(MeasureAttributes.PhysicalLength, s);
  211. return data;
  212. }
  213. public override void Draw(Graphics g)
  214. {
  215. drawingProperties.TryGetValue(this.drawToolType, out drawingPropertiesList);
  216. pointChangeObject.TryGetValue(this.drawToolType, out SavePointChange);
  217. Font textfont = new Font(measureStyleModel.font, measureStyleModel.fontSize);
  218. Brush textbrush = new SolidBrush(Color.FromArgb(measureStyleModel.textColor));
  219. g.SmoothingMode = SmoothingMode.AntiAlias;
  220. Color color = Color.FromArgb(this.measureStyleModel.lineColor);
  221. Pen pen = new Pen(color, this.measureStyleModel.lineWidth);
  222. pen.DashStyle = (DashStyle)this.measureStyleModel.lineStyle;
  223. measureStyleModel = this.ISurfaceBox.GetMeasureStyleModel().measureLength;
  224. if (this.Selected)
  225. {
  226. if (measureStyleModel.chooseStyle != null)
  227. {
  228. textfont = new Font(measureStyleModel.chooseStyle.font, measureStyleModel.chooseStyle.fontSize);
  229. textbrush = new SolidBrush(Color.FromArgb(measureStyleModel.chooseStyle.textColor));
  230. pen = new Pen(Color.FromArgb(measureStyleModel.chooseStyle.lineColor), measureStyleModel.chooseStyle.lineWidth);
  231. pen.DashStyle = (DashStyle)measureStyleModel.chooseStyle.lineStyle;
  232. }
  233. }
  234. if (!this.Selected)
  235. {
  236. this.pointChange = true;
  237. rectangleF1 = new RectangleF();
  238. rectangleF2 = new RectangleF();
  239. rectangleF3 = new RectangleF();
  240. rectangleF4 = new RectangleF();
  241. rectangleF5 = new RectangleF();
  242. rectangleF6 = new RectangleF();
  243. rectangleF7 = new RectangleF();
  244. rectangleF8 = new RectangleF();
  245. rectangleF9 = new RectangleF();
  246. }
  247. Matrix mtxSave = g.Transform;
  248. Matrix matrix = g.Transform;
  249. string frontStr = string.Empty;
  250. if (this.measureStyleModel.showSuffix)
  251. {
  252. frontStr = this.measureStyleModel.suffixName;
  253. }
  254. if (this.measureStyleModel.showSerial)
  255. {
  256. #region [获取序号]
  257. List<DrawObject> newGraphicsList = new List<DrawObject>();
  258. int n = ISurfaceBox.GraphicsList.Count;
  259. int num = 0;
  260. for (int i = n - 1; i >= 0; i--)
  261. {
  262. newGraphicsList.Add(ISurfaceBox.GraphicsList[i].Clone());
  263. }
  264. var newGraphics = newGraphicsList.Where(m => m.ID.Equals(this.ID)).FirstOrDefault();
  265. if (newGraphics != null)
  266. {
  267. num = newGraphicsList.IndexOf(newGraphics) + 1;
  268. }
  269. #endregion
  270. frontStr = $"[{ num }]{frontStr}";
  271. }
  272. if (this.measureStyleModel.showAlias)
  273. {
  274. frontStr = this.measureStyleModel.aliasName + frontStr;
  275. }
  276. if (pointArray.Count == 2)
  277. {
  278. g.DrawLine(pen, pointArray[0], pointArray[1]);
  279. length = BasicCalculationHelper.GetDistance(pointArray[0], pointArray[1], 10);
  280. }
  281. SizeF sizeF = new SizeF();
  282. if (pointArray.Count >= 3)
  283. {
  284. if (drawingPropertiesList == null)
  285. {
  286. drawingPropertiesList = drawingPropertiesListClone;
  287. this.configurationFile = true;
  288. }
  289. // 垂足坐标
  290. PointF footPoint = new PointF();
  291. // 垂足坐标
  292. footPoint = BasicCalculationHelper.GetDropFeet(pointArray[1], pointArray[0], pointArray[2]);
  293. // 点到直线距离
  294. double lengthF = BasicCalculationHelper.GetDistance(footPoint, pointArray[2], 2);
  295. // 第二个点与X轴的夹角
  296. double point2ToX = Math.Round(BasicCalculationHelper.AngleText(pointArray[0],
  297. new PointF(pointArray[0].X + 10, pointArray[0].Y), pointArray[1]), 2);
  298. // 第三个点与X轴的夹角
  299. double point3ToX = Math.Round(BasicCalculationHelper.AngleText(pointArray[0],
  300. new PointF(pointArray[0].X + 10, pointArray[0].Y), pointArray[2]), 2);
  301. // 两点直线与X轴夹角
  302. double angle = BasicCalculationHelper.AngleText(pointArray[0], new PointF(pointArray[0].X + 20, pointArray[0].Y), pointArray[1]);
  303. if (point2ToX > 90)
  304. {
  305. point2ToX = 180 - point2ToX;
  306. }
  307. if (point3ToX > 90)
  308. {
  309. point3ToX = 180 - point3ToX;
  310. }
  311. if (angle > 90)
  312. {
  313. angle = 180 - angle;
  314. }
  315. //判断第二个点相对于第一个点的象限
  316. int x2 = (int)(pointArray[1].X - pointArray[0].X);
  317. if (x2 == 0)
  318. x2 = 1;
  319. int y2 = (int)(pointArray[1].Y - pointArray[0].Y);
  320. if (y2 == 0)
  321. y2 = 1;
  322. int i2 = 0;
  323. if (x2 > 0 && y2 > 0) //第4象限
  324. {
  325. i2 = 4;
  326. }
  327. else if (x2 > 0 && y2 < 0) //第1象限
  328. {
  329. i2 = 1;
  330. }
  331. else if (x2 < 0 && y2 < 0) //第2象限
  332. {
  333. i2 = 2;
  334. }
  335. else if (x2 < 0 && y2 > 0) //第3象限
  336. {
  337. i2 = 3;
  338. }
  339. //判断第三个点相对于第一个点的象限
  340. int x3 = (int)(pointArray[2].X - pointArray[0].X);
  341. if (x3 == 0)
  342. x3 = 1;
  343. int y3 = (int)(pointArray[2].Y - pointArray[0].Y);
  344. if (y3 == 0)
  345. y3 = 1;
  346. int i3 = 0;
  347. if (x3 > 0 && y3 > 0) //第4象限
  348. {
  349. i3 = 4;
  350. }
  351. else if (x3 > 0 && y3 < 0) //第1象限
  352. {
  353. i3 = 1;
  354. }
  355. else if (x3 < 0 && y3 < 0) //第2象限
  356. {
  357. i3 = 2;
  358. }
  359. else if (x3 < 0 && y3 > 0) //第3象限
  360. {
  361. i3 = 3;
  362. }
  363. double angle1 = 0.0;
  364. double angle2 = 0.0;
  365. if ((pointArray[1].Y == pointArray[0].Y) && (i3 == 1 || i3 == 2))
  366. {
  367. angle1 = 270;
  368. angle2 = 90;
  369. }
  370. else if ((pointArray[1].Y == pointArray[0].Y) && (i3 == 3 || i3 == 4))
  371. {
  372. angle1 = 90;
  373. angle2 = 270;
  374. }
  375. else if ((pointArray[1].X == pointArray[0].X) && (i3 == 1 || i3 == 4))
  376. {
  377. angle1 = 0;
  378. angle2 = 180;
  379. }
  380. else if ((pointArray[1].X == pointArray[0].X) && (i3 == 2 || i3 == 3))
  381. {
  382. angle1 = 180;
  383. angle2 = 0;
  384. }
  385. else if ((i2 == 1 && i3 == 1) || (i2 == 3 && i3 == 1))
  386. {
  387. if (point3ToX > point2ToX)
  388. {
  389. angle1 = 270 - angle;
  390. angle2 = 90 - angle;
  391. }
  392. else
  393. {
  394. angle1 = 90 - angle;
  395. angle2 = 270 - angle;
  396. }
  397. }
  398. else if ((i2 == 1 && i3 == 2) || (i2 == 3 && i3 == 2))
  399. {
  400. angle1 = 270 - angle;
  401. angle2 = 90 - angle;
  402. }
  403. else if ((i2 == 1 && i3 == 3) || (i2 == 3 && i3 == 3))
  404. {
  405. if (point3ToX > point2ToX)
  406. {
  407. angle1 = 90 - angle;
  408. angle2 = 270 - angle;
  409. }
  410. else
  411. {
  412. angle1 = 270 - angle;
  413. angle2 = 90 - angle;
  414. }
  415. }
  416. else if ((i2 == 1 && i3 == 4) || (i2 == 3 && i3 == 4))
  417. {
  418. angle1 = 90 - angle;
  419. angle2 = 270 - angle;
  420. }
  421. else if ((i2 == 2 && i3 == 1) || (i2 == 4 && i3 == 1))
  422. {
  423. angle1 = 270 + angle;
  424. angle2 = 90 + angle;
  425. }
  426. else if ((i2 == 2 && i3 == 2) || (i2 == 4 && i3 == 2))
  427. {
  428. if (point3ToX > point2ToX)
  429. {
  430. angle1 = 270 + angle;
  431. angle2 = 90 + angle;
  432. }
  433. else
  434. {
  435. angle1 = 90 + angle;
  436. angle2 = 270 + angle;
  437. }
  438. }
  439. else if ((i2 == 2 && i3 == 3) || (i2 == 4 && i3 == 3))
  440. {
  441. angle1 = 90 + angle;
  442. angle2 = 270 + angle;
  443. }
  444. else if ((i2 == 2 && i3 == 4) || (i2 == 4 && i3 == 4))
  445. {
  446. if (point3ToX > point2ToX)
  447. {
  448. angle1 = 90 + angle;
  449. angle2 = 270 + angle;
  450. }
  451. else
  452. {
  453. angle1 = 270 + angle;
  454. angle2 = 90 + angle;
  455. }
  456. }
  457. PointF point = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[0].X + (int)lengthF,
  458. pointArray[0].Y), pointArray[0], angle1);
  459. PointF point1 = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[1].X - (int)lengthF,
  460. pointArray[1].Y), pointArray[1], angle2);
  461. // 求中点坐标
  462. float x = (point.X + point1.X) / 2;
  463. float y = (point.Y + point1.Y) / 2;
  464. // 绘制长度
  465. g.DrawLine(pen, point, point1);
  466. g.DrawLine(pen, point, pointArray[0]);
  467. g.DrawLine(pen, point1, pointArray[1]);
  468. // 长度
  469. length = BasicCalculationHelper.GetDistance(pointArray[0], pointArray[1], 10);
  470. pen.Dispose();
  471. //计算需要旋转的角度
  472. this.angleClass = BasicCalculationHelper.Angle(point, point1, new PointF(point.X, point1.Y));
  473. int offsetValuePoint = measureStyleModel.fontSize + measureStyleModel.fontSize / 2;
  474. int offsetValue1Point = measureStyleModel.lineWidth * 2 / 3;
  475. int offsetPoint = offsetValue1Point;
  476. if (drawingPropertiesList != null)
  477. {
  478. // 像素长度
  479. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  480. {
  481. offsetPoint += offsetValuePoint;
  482. }
  483. // 物理长度
  484. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  485. {
  486. offsetPoint += offsetValuePoint;
  487. }
  488. // 测量方式
  489. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  490. {
  491. offsetPoint += offsetValuePoint;
  492. }
  493. // 测量单位(中文)
  494. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  495. {
  496. offsetPoint += offsetValuePoint;
  497. }
  498. // 测量单位(英文)
  499. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  500. {
  501. offsetPoint += offsetValuePoint;
  502. }
  503. // 像素起始点X
  504. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  505. {
  506. offsetPoint += offsetValuePoint;
  507. }
  508. // 像素起始点Y
  509. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  510. {
  511. offsetPoint += offsetValuePoint;
  512. }
  513. // 物理起始点X
  514. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  515. {
  516. offsetPoint += offsetValuePoint;
  517. }
  518. // 物理起始点Y
  519. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  520. {
  521. offsetPoint += offsetValuePoint;
  522. }
  523. }
  524. PointF pointF1;
  525. PointF pointF2;
  526. if (angleClass < 90)
  527. pointF1 = BasicCalculationHelper.GetAnglePoint(new Point((int)point.X + offsetPoint + offsetValue1Point, (int)point.Y), point, angleClass - 90);
  528. else
  529. pointF1 = BasicCalculationHelper.GetAnglePoint(new Point((int)point.X + offsetPoint + offsetValue1Point, (int)point.Y), point, angleClass + 90);
  530. if (angleClass < 90)
  531. pointF2 = BasicCalculationHelper.GetAnglePoint(new Point((int)point1.X + offsetPoint + offsetValue1Point, (int)point1.Y), point1, angleClass - 90);
  532. else
  533. pointF2 = BasicCalculationHelper.GetAnglePoint(new Point((int)point1.X + offsetPoint + offsetValue1Point, (int)point1.Y), point1, angleClass + 90);
  534. PointF pointF3 = new PointF((pointF1.X + pointF2.X) / 2, (pointF1.Y + pointF2.Y) / 2);
  535. if (measureStyleModel.linePosition == 0)
  536. this.rotationPoint = pointArray[0];
  537. else if (measureStyleModel.linePosition == 1)
  538. this.rotationPoint = pointF3;
  539. else if (measureStyleModel.linePosition == 2)
  540. this.rotationPoint = pointArray[1];
  541. else if (measureStyleModel.linePosition == 3)
  542. this.rotationPoint = point;
  543. else if (measureStyleModel.linePosition == 4)
  544. this.rotationPoint = new PointF(x, y);
  545. else if (measureStyleModel.linePosition == 5)
  546. this.rotationPoint = point1;
  547. // 画布旋转
  548. if (this.angleClass < 90)
  549. {
  550. matrix.RotateAt((float)this.angleClass, this.rotationPoint);
  551. g.Transform = matrix;
  552. }
  553. else
  554. {
  555. this.angleClass = BasicCalculationHelper.Angle(point1, point, new PointF(point1.X, point.Y));
  556. matrix.RotateAt((float)this.angleClass, this.rotationPoint);
  557. g.Transform = matrix;
  558. }
  559. // 是否绘制
  560. if (this.pointChange || this.SavePointChange || this.mouseUpPointChange || this.mouseUpAttribute)
  561. {
  562. // 属性文本的间隔定义
  563. int offsetValue = measureStyleModel.fontSize + measureStyleModel.fontSize / 2;
  564. int offsetValue1 = measureStyleModel.lineWidth * 2 / 3;
  565. int offset = offsetValue1;
  566. this.pointL = new Point((int)this.rotationPoint.X, (int)this.rotationPoint.Y);
  567. if (drawingPropertiesList != null)
  568. {
  569. // 像素长度
  570. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  571. {
  572. sizeF = g.MeasureString(frontStr + Math.Round(length, decimalPlaces) + "px", textfont);
  573. rectangleF8.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  574. offset += offsetValue;
  575. }
  576. // 物理长度
  577. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  578. {
  579. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  580. if (s.IndexOf(".") == -1)
  581. {
  582. for (int i = 0; i < decimalPlaces; i++)
  583. {
  584. if (i == 0)
  585. s += ".";
  586. s += "0";
  587. }
  588. }
  589. else
  590. {
  591. int a = s.Length - s.IndexOf(".") - 1;
  592. if (a < decimalPlaces)
  593. {
  594. for (int i = 0; i < decimalPlaces - a; i++)
  595. {
  596. s += "0";
  597. }
  598. }
  599. }
  600. sizeF = g.MeasureString(frontStr + s + this.unit, textfont);
  601. rectangleF9.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  602. offset += offsetValue;
  603. }
  604. // 测量方式
  605. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  606. {
  607. sizeF = g.MeasureString(frontStr + PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"), textfont);
  608. rectangleF1.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  609. offset += offsetValue;
  610. }
  611. // 测量单位(中文)
  612. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  613. {
  614. sizeF = g.MeasureString(frontStr + this.unitString, textfont);
  615. rectangleF2.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  616. offset += offsetValue;
  617. }
  618. // 测量单位(英文)
  619. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  620. {
  621. sizeF = g.MeasureString(frontStr + this.unit, textfont);
  622. rectangleF3.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  623. offset += offsetValue;
  624. }
  625. // 像素起始点X
  626. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  627. {
  628. sizeF = g.MeasureString(frontStr + pointArray[0].X, textfont);
  629. rectangleF4.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  630. offset += offsetValue;
  631. }
  632. // 像素起始点Y
  633. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  634. {
  635. sizeF = g.MeasureString(frontStr + pointArray[0].Y, textfont);
  636. rectangleF5.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  637. offset += offsetValue;
  638. }
  639. // 物理起始点X
  640. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  641. {
  642. sizeF = g.MeasureString(frontStr + Math.Round(pointArray[0].X * unitLength, decimalPlaces), textfont);
  643. rectangleF6.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  644. offset += offsetValue;
  645. }
  646. // 物理起始点Y
  647. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  648. {
  649. sizeF = g.MeasureString(frontStr + Math.Round(pointArray[0].Y * unitLength, decimalPlaces), textfont);
  650. rectangleF7.Location = new Point((int)this.rotationPoint.X + offsetValue1 - (int)sizeF.Width / 2, (int)this.rotationPoint.Y + offset);
  651. offset += offsetValue;
  652. }
  653. }
  654. }
  655. if (drawingPropertiesList != null)
  656. {
  657. // 测量方式
  658. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureMethod.ToString()))
  659. {
  660. sizeF = g.MeasureString(frontStr + PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"), textfont);
  661. rectangleF1.Width = sizeF.Width;
  662. rectangleF1.Height = sizeF.Height;
  663. g.DrawString(frontStr + PdnResources.GetString("Menu.MeasureAction.LengthMeasurement.Text"), textfont, textbrush, rectangleF1);
  664. }
  665. else
  666. {
  667. rectangleF1 = new RectangleF();
  668. }
  669. // 测量单位(中文)
  670. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitCN.ToString()))
  671. {
  672. sizeF = g.MeasureString(frontStr + this.unitString, textfont);
  673. rectangleF2.Width = sizeF.Width;
  674. rectangleF2.Height = sizeF.Height;
  675. g.DrawString(frontStr + this.unitString, textfont, textbrush, rectangleF2);
  676. }
  677. else
  678. {
  679. rectangleF2 = new RectangleF();
  680. }
  681. // 测量单位(英文)
  682. if (drawingPropertiesList.Contains(MeasureAttributes.MeasureUnitEN.ToString()))
  683. {
  684. sizeF = g.MeasureString(frontStr + this.unit, textfont);
  685. rectangleF3.Width = sizeF.Width;
  686. rectangleF3.Height = sizeF.Height;
  687. g.DrawString(frontStr + this.unit, textfont, textbrush, rectangleF3);
  688. }
  689. else
  690. {
  691. rectangleF3 = new RectangleF();
  692. }
  693. // 像素起始点X
  694. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartX.ToString()))
  695. {
  696. sizeF = g.MeasureString(frontStr + pointArray[0].X, textfont);
  697. rectangleF4.Width = sizeF.Width;
  698. rectangleF4.Height = sizeF.Height;
  699. g.DrawString(frontStr + pointArray[0].X, textfont, textbrush, rectangleF4);
  700. }
  701. else
  702. {
  703. rectangleF4 = new RectangleF();
  704. }
  705. // 像素起始点Y
  706. if (drawingPropertiesList.Contains(MeasureAttributes.PixelStartY.ToString()))
  707. {
  708. sizeF = g.MeasureString(frontStr + pointArray[0].Y, textfont);
  709. rectangleF5.Width = sizeF.Width;
  710. rectangleF5.Height = sizeF.Height;
  711. g.DrawString(frontStr + pointArray[0].Y, textfont, textbrush, rectangleF5);
  712. }
  713. else
  714. {
  715. rectangleF5 = new RectangleF();
  716. }
  717. // 物理起始点X
  718. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartX.ToString()))
  719. {
  720. sizeF = g.MeasureString(frontStr + Math.Round(pointArray[0].X * unitLength, decimalPlaces), textfont);
  721. rectangleF6.Width = sizeF.Width;
  722. rectangleF6.Height = sizeF.Height;
  723. g.DrawString(frontStr + Math.Round(pointArray[0].X * unitLength, decimalPlaces), textfont, textbrush, rectangleF6);
  724. }
  725. else
  726. {
  727. rectangleF6 = new RectangleF();
  728. }
  729. // 物理起始点Y
  730. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalStartY.ToString()))
  731. {
  732. sizeF = g.MeasureString(frontStr + Math.Round(pointArray[0].Y * unitLength, decimalPlaces), textfont);
  733. rectangleF7.Width = sizeF.Width;
  734. rectangleF7.Height = sizeF.Height;
  735. g.DrawString(frontStr + Math.Round(pointArray[0].Y * unitLength, decimalPlaces), textfont, textbrush, rectangleF7);
  736. }
  737. else
  738. {
  739. rectangleF7 = new RectangleF();
  740. }
  741. // 像素长度
  742. if (drawingPropertiesList.Contains(MeasureAttributes.PixelLength.ToString()))
  743. {
  744. sizeF = g.MeasureString(frontStr + Math.Round(length, decimalPlaces) + "px", textfont);
  745. rectangleF8.Width = sizeF.Width;
  746. rectangleF8.Height = sizeF.Height;
  747. g.DrawString(frontStr + Math.Round(length, decimalPlaces) + "px", textfont, textbrush, rectangleF8);
  748. }
  749. else
  750. {
  751. rectangleF8 = new RectangleF();
  752. }
  753. // 物理长度
  754. if (drawingPropertiesList.Contains(MeasureAttributes.PhysicalLength.ToString()))
  755. {
  756. string s = Math.Round(length * unitLength, decimalPlaces).ToString();
  757. if (s.IndexOf(".") == -1)
  758. {
  759. for (int i = 0; i < decimalPlaces; i++)
  760. {
  761. if (i == 0)
  762. s += ".";
  763. s += "0";
  764. }
  765. }
  766. else
  767. {
  768. int a = s.Length - s.IndexOf(".") - 1;
  769. if (a < decimalPlaces)
  770. {
  771. for (int i = 0; i < decimalPlaces - a; i++)
  772. {
  773. s += "0";
  774. }
  775. }
  776. }
  777. sizeF = g.MeasureString(frontStr + s + this.unit, textfont);
  778. rectangleF9.Width = sizeF.Width;
  779. rectangleF9.Height = sizeF.Height;
  780. g.DrawString(frontStr + s + this.unit, textfont, textbrush, rectangleF9);
  781. }
  782. else
  783. {
  784. rectangleF9 = new RectangleF();
  785. }
  786. }
  787. //还原为原始旋转矩阵
  788. g.Transform = mtxSave;
  789. }
  790. matrix.Dispose();
  791. if (this.configurationFile)
  792. this.pointChange = false;
  793. this.mouseUpAttribute = false;
  794. pointChangeObject.Remove(this.drawToolType);
  795. }
  796. /// <summary>
  797. /// 停止绘制时
  798. /// </summary>
  799. /// <param name="up"></param>
  800. public override void MouseUp(bool up)
  801. {
  802. mouseUpPointChange = up;
  803. }
  804. public void AddPoint(Point point)
  805. {
  806. pointArray.Add(point);
  807. }
  808. public override int HandleCount
  809. {
  810. get
  811. {
  812. return 6;
  813. }
  814. }
  815. /// <summary>
  816. /// Get handle pointscroll by 1-based number
  817. /// </summary>
  818. /// <param name="handleNumber"></param>
  819. /// <returns></returns>
  820. public override PointF GetHandle(int handleNumber)
  821. {
  822. if (handleNumber < 1)
  823. handleNumber = 1;
  824. if (handleNumber > pointArray.Count && handleNumber <= 3)
  825. handleNumber = pointArray.Count;
  826. if (handleNumber == 1)
  827. return pointArray[0];
  828. else if (handleNumber == 2)
  829. return pointArray[1];
  830. else if (handleNumber == 6)
  831. return pointL;
  832. else if (handleNumber > 2 && pointArray.Count >= 3)
  833. {
  834. // 垂足坐标
  835. PointF footPoint = new PointF();
  836. // 垂足坐标
  837. footPoint = BasicCalculationHelper.GetDropFeet(pointArray[1], pointArray[0], pointArray[2]);
  838. // 点到直线距离
  839. double length = BasicCalculationHelper.GetDistance(footPoint, pointArray[2], 2);
  840. // 第二个点与X轴的夹角
  841. double point2ToX = Math.Round(BasicCalculationHelper.AngleText(pointArray[0],
  842. new PointF(pointArray[0].X + 10, pointArray[0].Y), pointArray[1]), 2);
  843. // 第三个点与X轴的夹角
  844. double point3ToX = Math.Round(BasicCalculationHelper.AngleText(pointArray[0],
  845. new PointF(pointArray[0].X + 10, pointArray[0].Y), pointArray[2]), 2);
  846. // 两点直线与X轴夹角
  847. double angle = BasicCalculationHelper.AngleText(pointArray[0], new PointF(pointArray[0].X + 20, pointArray[0].Y), pointArray[1]);
  848. if (point2ToX > 90)
  849. {
  850. point2ToX = 180 - point2ToX;
  851. }
  852. if (point3ToX > 90)
  853. {
  854. point3ToX = 180 - point3ToX;
  855. }
  856. if (angle > 90)
  857. {
  858. angle = 180 - angle;
  859. }
  860. //判断第二个点相对于第一个点的象限
  861. int x2 = (int)(pointArray[1].X - pointArray[0].X);
  862. if (x2 == 0)
  863. x2 = 1;
  864. int y2 = (int)(pointArray[1].Y - pointArray[0].Y);
  865. if (y2 == 0)
  866. y2 = 1;
  867. int i2 = 0;
  868. if (x2 > 0 && y2 > 0) //第4象限
  869. {
  870. i2 = 4;
  871. }
  872. else if (x2 > 0 && y2 < 0) //第1象限
  873. {
  874. i2 = 1;
  875. }
  876. else if (x2 < 0 && y2 < 0) //第2象限
  877. {
  878. i2 = 2;
  879. }
  880. else if (x2 < 0 && y2 > 0) //第3象限
  881. {
  882. i2 = 3;
  883. }
  884. //判断第三个点相对于第一个点的象限
  885. int x3 = (int)(pointArray[2].X - pointArray[0].X);
  886. if (x3 == 0)
  887. x3 = 1;
  888. int y3 = (int)(pointArray[2].Y - pointArray[0].Y);
  889. if (y3 == 0)
  890. y3 = 1;
  891. int i3 = 0;
  892. if (x3 > 0 && y3 > 0) //第4象限
  893. {
  894. i3 = 4;
  895. }
  896. else if (x3 > 0 && y3 < 0) //第1象限
  897. {
  898. i3 = 1;
  899. }
  900. else if (x3 < 0 && y3 < 0) //第2象限
  901. {
  902. i3 = 2;
  903. }
  904. else if (x3 < 0 && y3 > 0) //第3象限
  905. {
  906. i3 = 3;
  907. }
  908. double angle1 = 0.0;
  909. double angle2 = 0.0;
  910. if ((pointArray[1].Y == pointArray[0].Y) && (i3 == 1 || i3 == 2))
  911. {
  912. angle1 = 270;
  913. angle2 = 90;
  914. }
  915. else if ((pointArray[1].Y == pointArray[0].Y) && (i3 == 3 || i3 == 4))
  916. {
  917. angle1 = 90;
  918. angle2 = 270;
  919. }
  920. else if ((pointArray[1].X == pointArray[0].X) && (i3 == 1 || i3 == 4))
  921. {
  922. angle1 = 0;
  923. angle2 = 180;
  924. }
  925. else if ((pointArray[1].X == pointArray[0].X) && (i3 == 2 || i3 == 3))
  926. {
  927. angle1 = 180;
  928. angle2 = 0;
  929. }
  930. else if ((i2 == 1 && i3 == 1) || (i2 == 3 && i3 == 1))
  931. {
  932. if (point3ToX > point2ToX)
  933. {
  934. angle1 = 270 - angle;
  935. angle2 = 90 - angle;
  936. }
  937. else
  938. {
  939. angle1 = 90 - angle;
  940. angle2 = 270 - angle;
  941. }
  942. }
  943. else if ((i2 == 1 && i3 == 2) || (i2 == 3 && i3 == 2))
  944. {
  945. angle1 = 270 - angle;
  946. angle2 = 90 - angle;
  947. }
  948. else if ((i2 == 1 && i3 == 3) || (i2 == 3 && i3 == 3))
  949. {
  950. if (point3ToX > point2ToX)
  951. {
  952. angle1 = 90 - angle;
  953. angle2 = 270 - angle;
  954. }
  955. else
  956. {
  957. angle1 = 270 - angle;
  958. angle2 = 90 - angle;
  959. }
  960. }
  961. else if ((i2 == 1 && i3 == 4) || (i2 == 3 && i3 == 4))
  962. {
  963. angle1 = 90 - angle;
  964. angle2 = 270 - angle;
  965. }
  966. else if ((i2 == 2 && i3 == 1) || (i2 == 4 && i3 == 1))
  967. {
  968. angle1 = 270 + angle;
  969. angle2 = 90 + angle;
  970. }
  971. else if ((i2 == 2 && i3 == 2) || (i2 == 4 && i3 == 2))
  972. {
  973. if (point3ToX > point2ToX)
  974. {
  975. angle1 = 270 + angle;
  976. angle2 = 90 + angle;
  977. }
  978. else
  979. {
  980. angle1 = 90 + angle;
  981. angle2 = 270 + angle;
  982. }
  983. }
  984. else if ((i2 == 2 && i3 == 3) || (i2 == 4 && i3 == 3))
  985. {
  986. angle1 = 90 + angle;
  987. angle2 = 270 + angle;
  988. }
  989. else if ((i2 == 2 && i3 == 4) || (i2 == 4 && i3 == 4))
  990. {
  991. if (point3ToX > point2ToX)
  992. {
  993. angle1 = 90 + angle;
  994. angle2 = 270 + angle;
  995. }
  996. else
  997. {
  998. angle1 = 270 + angle;
  999. angle2 = 90 + angle;
  1000. }
  1001. }
  1002. PointF point = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[0].X + (int)length,
  1003. pointArray[0].Y), pointArray[0], angle1);
  1004. PointF point1 = BasicCalculationHelper.GetAnglePoint(new PointF(pointArray[1].X - (int)length,
  1005. pointArray[1].Y), pointArray[1], angle2);
  1006. // 求中点坐标
  1007. float x = (point.X + point1.X) / 2;
  1008. float y = (point.Y + point1.Y) / 2;
  1009. if (handleNumber == 3)
  1010. return point;
  1011. else if (handleNumber == 4)
  1012. return point1;
  1013. else
  1014. return new PointF(x, y);
  1015. }
  1016. else
  1017. {
  1018. return pointArray[0];
  1019. }
  1020. }
  1021. public override Cursor GetHandleCursor(int handleNumber)
  1022. {
  1023. return handleCursor;
  1024. }
  1025. public override void MoveHandleTo(Point point, int handleNumber)
  1026. {
  1027. if (handleNumber < 1)
  1028. handleNumber = 1;
  1029. if (handleNumber > pointArray.Count && handleNumber != 6)
  1030. handleNumber = pointArray.Count;
  1031. if (handleNumber == 6)
  1032. {
  1033. point = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1034. if (this.moveKb == 1)
  1035. this.rectangleF1.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1036. else if (this.moveKb == 2)
  1037. this.rectangleF2.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1038. else if (this.moveKb == 3)
  1039. this.rectangleF3.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1040. else if (this.moveKb == 4)
  1041. this.rectangleF4.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1042. else if (this.moveKb == 5)
  1043. this.rectangleF5.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1044. else if (this.moveKb == 6)
  1045. this.rectangleF6.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1046. else if (this.moveKb == 7)
  1047. this.rectangleF7.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1048. else if (this.moveKb == 8)
  1049. this.rectangleF8.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1050. else if (this.moveKb == 9)
  1051. this.rectangleF9.Offset(point.X - this.pointL.X, point.Y - this.pointL.Y);
  1052. this.pointL = point;
  1053. }
  1054. else
  1055. {
  1056. this.mouseUpPointChange = true;
  1057. if (handleNumber == 1)
  1058. {
  1059. this.rectangleF1.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1060. this.rectangleF2.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1061. this.rectangleF3.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1062. this.rectangleF4.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1063. this.rectangleF5.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1064. this.rectangleF6.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1065. this.rectangleF7.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1066. this.rectangleF8.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1067. this.rectangleF9.Offset(point.X - this.pointArray[0].X, point.Y - this.pointArray[0].Y);
  1068. }
  1069. pointArray[handleNumber - 1] = point;
  1070. }
  1071. this.startPoint = pointArray[0];
  1072. Invalidate();
  1073. }
  1074. public override void Move(int deltaX, int deltaY)
  1075. {
  1076. int n = pointArray.Count;
  1077. PointF point;
  1078. for (int i = 0; i < n; i++)
  1079. {
  1080. point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
  1081. pointArray[i] = point;
  1082. }
  1083. this.startPoint = pointArray[0];
  1084. int x = ISurfaceBox.UnscaleScalar(deltaX);
  1085. int y = ISurfaceBox.UnscaleScalar(deltaY);
  1086. pointL.X += x;
  1087. pointL.Y += y;
  1088. this.rectangleF1.Offset(x, y);
  1089. this.rectangleF2.Offset(x, y);
  1090. this.rectangleF3.Offset(x, y);
  1091. this.rectangleF4.Offset(x, y);
  1092. this.rectangleF5.Offset(x, y);
  1093. this.rectangleF6.Offset(x, y);
  1094. this.rectangleF7.Offset(x, y);
  1095. this.rectangleF8.Offset(x, y);
  1096. this.rectangleF9.Offset(x, y);
  1097. Invalidate();
  1098. }
  1099. /// <summary>
  1100. /// 用于创建一个路径或者是闭合的范围
  1101. /// 用于响应点击选中
  1102. /// 需要咨询用户是仅点击线还是矩形选择
  1103. /// </summary>
  1104. protected virtual void CreateObjects()
  1105. {
  1106. if (AreaPath != null)
  1107. return;
  1108. AreaPath = new GraphicsPath();
  1109. AreaPath.AddRectangle(GetBoundingBox());
  1110. AreaPath.CloseFigure();
  1111. AreaRegion = new Region(AreaPath);
  1112. }
  1113. /// <summary>
  1114. /// Invalidate object.
  1115. /// When object is invalidated, path used for hit test
  1116. /// is released and should be created again.
  1117. /// </summary>
  1118. protected void Invalidate()
  1119. {
  1120. if (AreaPath != null)
  1121. {
  1122. AreaPath.Dispose();
  1123. AreaPath = null;
  1124. }
  1125. if (AreaPen != null)
  1126. {
  1127. AreaPen.Dispose();
  1128. AreaPen = null;
  1129. }
  1130. if (AreaRegion != null)
  1131. {
  1132. AreaRegion.Dispose();
  1133. AreaRegion = null;
  1134. }
  1135. }
  1136. protected GraphicsPath AreaPath
  1137. {
  1138. get
  1139. {
  1140. return areaPath;
  1141. }
  1142. set
  1143. {
  1144. areaPath = value;
  1145. }
  1146. }
  1147. protected Pen AreaPen
  1148. {
  1149. get
  1150. {
  1151. return areaPen;
  1152. }
  1153. set
  1154. {
  1155. areaPen = value;
  1156. }
  1157. }
  1158. protected Region AreaRegion
  1159. {
  1160. get
  1161. {
  1162. return areaRegion;
  1163. }
  1164. set
  1165. {
  1166. areaRegion = value;
  1167. }
  1168. }
  1169. /// <summary>
  1170. /// Draw tracker for selected object
  1171. /// </summary>
  1172. /// <param name="g"></param>
  1173. public override void DrawTracker(Graphics g)
  1174. {
  1175. if (!Selected)
  1176. return;
  1177. SolidBrush brush = new SolidBrush(Color.FromArgb(MarkpointAreaColor));
  1178. Pen pen = new Pen(Color.FromArgb(MarkpointLineColor), MarkpointLineWidth);
  1179. for (int i = 1; i <= HandleCount; i++)
  1180. {
  1181. if (i == 6)
  1182. {
  1183. brush = new SolidBrush(Color.Transparent);
  1184. pen = new Pen(Color.Transparent);
  1185. }
  1186. switch (MarkpointStyle)
  1187. {
  1188. case 0:
  1189. g.FillRectangle(brush, GetHandleRectangle(i));
  1190. g.DrawRectangle(pen, GetHandleRectangle(i));
  1191. break;
  1192. case 1:
  1193. g.FillEllipse(brush, GetHandleRectangle(i));
  1194. g.DrawEllipse(pen, GetHandleRectangle(i));
  1195. break;
  1196. case 2:
  1197. g.FillPolygon(brush, GetHandlePoint(i));
  1198. g.DrawPolygon(pen, GetHandlePoint(i));
  1199. break;
  1200. }
  1201. }
  1202. pen.Dispose();
  1203. brush.Dispose();
  1204. }
  1205. /// <summary>
  1206. /// Hit test.
  1207. /// Return value: -1 - no hit
  1208. /// 0 - hit anywhere
  1209. /// > 1 - handle number
  1210. /// </summary>
  1211. /// <param name="pointscroll"></param>
  1212. /// <returns></returns>
  1213. public override int HitTest(Point point)
  1214. {
  1215. if (Selected)
  1216. {
  1217. for (int i = 1; i <= HandleCount; i++)
  1218. {
  1219. if (GetHandleRectangle(i).Contains(point))
  1220. return i;
  1221. }
  1222. if (GetRectanglePosition(this.rectangleF1, point, this.rotationPoint, this.angleClass))
  1223. {
  1224. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1225. moveKb = 1;
  1226. return 6;
  1227. }
  1228. else if (GetRectanglePosition(this.rectangleF2, point, this.rotationPoint, this.angleClass))
  1229. {
  1230. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1231. moveKb = 2;
  1232. return 6;
  1233. }
  1234. else if (GetRectanglePosition(this.rectangleF3, point, this.rotationPoint, this.angleClass))
  1235. {
  1236. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1237. moveKb = 3;
  1238. return 6;
  1239. }
  1240. else if (GetRectanglePosition(this.rectangleF4, point, this.rotationPoint, this.angleClass))
  1241. {
  1242. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1243. moveKb = 4;
  1244. return 6;
  1245. }
  1246. else if (GetRectanglePosition(this.rectangleF5, point, this.rotationPoint, this.angleClass))
  1247. {
  1248. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1249. moveKb = 5;
  1250. return 6;
  1251. }
  1252. else if (GetRectanglePosition(this.rectangleF6, point, this.rotationPoint, this.angleClass))
  1253. {
  1254. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1255. moveKb = 6;
  1256. return 6;
  1257. }
  1258. else if (GetRectanglePosition(this.rectangleF7, point, this.rotationPoint, this.angleClass))
  1259. {
  1260. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1261. moveKb = 7;
  1262. return 6;
  1263. }
  1264. else if (GetRectanglePosition(this.rectangleF8, point, this.rotationPoint, this.angleClass))
  1265. {
  1266. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1267. moveKb = 8;
  1268. return 6;
  1269. }
  1270. else if (GetRectanglePosition(this.rectangleF9, point, this.rotationPoint, this.angleClass))
  1271. {
  1272. this.pointL = SetOffsetAfterRotation(point, this.rotationPoint, this.angleClass);
  1273. moveKb = 9;
  1274. return 6;
  1275. }
  1276. }
  1277. if (PointInObject(point))
  1278. return 0;
  1279. return -1;
  1280. }
  1281. protected override bool PointInObject(Point point)
  1282. {
  1283. CreateObjects();
  1284. return AreaRegion.IsVisible(point);
  1285. }
  1286. public override bool IntersectsWith(Rectangle rectangle)
  1287. {
  1288. CreateObjects();
  1289. return AreaRegion.IsVisible(rectangle);
  1290. }
  1291. public override RectangleF GetBoundingBox()
  1292. {
  1293. Rectangle rectangle;
  1294. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  1295. for (int i = 0; i < pointArray.Count; i++)
  1296. {
  1297. if (i == 0)
  1298. {
  1299. minx = maxx = (int)pointArray[i].X;
  1300. miny = maxy = (int)pointArray[i].Y;
  1301. }
  1302. else
  1303. {
  1304. if (pointArray[i].X > maxx) maxx = (int)pointArray[i].X;
  1305. if (pointArray[i].X < minx) minx = (int)pointArray[i].X;
  1306. if (pointArray[i].Y > maxy) maxy = (int)pointArray[i].Y;
  1307. if (pointArray[i].Y < miny) miny = (int)pointArray[i].Y;
  1308. }
  1309. }
  1310. rectangle = new Rectangle(minx, miny, maxx - minx, maxy - miny);
  1311. return rectangle;
  1312. }
  1313. internal void setNextPoint(Point p)
  1314. {
  1315. AddPoint(p);
  1316. startPoint = endPoint;
  1317. endPoint = p;
  1318. }
  1319. internal void setEndPoint(Point p)
  1320. {
  1321. endPoint = p;
  1322. }
  1323. public override List<PointF> GetPoints()
  1324. {
  1325. return pointArray;
  1326. }
  1327. public override ParentStyleModel GetStyle()
  1328. {
  1329. return measureStyleModel;
  1330. }
  1331. }
  1332. }