MeasureHLine.cs 41 KB

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