MeasureMulLine.cs 67 KB

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