HandModeRulerDraw.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using PaintDotNet.Base.SettingModel;
  2. using PaintDotNet.SettingModel;
  3. using System;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Globalization;
  7. using System.Runtime.Serialization;
  8. using System.Windows.Forms;
  9. //using PaintDotNet.Startup;
  10. using PaintDotNet.CommTool;
  11. using System.IO;
  12. using static PaintDotNet.SettingModel.LabelStyleModel;
  13. using PaintDotNet.Annotation.Enum;
  14. using System.Collections.Generic;
  15. using System.Drawing.Imaging;
  16. using PaintDotNet.Base.CommTool;
  17. using System.ComponentModel;
  18. namespace PaintDotNet.Annotation.Other
  19. {
  20. /// <summary>
  21. /// 标注->手动标尺
  22. /// </summary>
  23. public class HandModeRulerDraw : DrawObject
  24. {
  25. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
  26. /// <summary>
  27. /// 标注样式信息model
  28. /// </summary>
  29. public RulerModel rulerModel;
  30. public int pixelLength = 1;//标尺像素长度
  31. private int[] lengthEnum = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000};
  32. private PointF centerPoint;
  33. private string text;
  34. public HandModeRulerDraw(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
  35. {
  36. this.objectType = DrawClass.Other;
  37. this.drawToolType = DrawToolType.HandModeRulerDraw;
  38. rulerModel = surfaceBox.GetRulerStyleModel();
  39. startPoint.X = x1;
  40. startPoint.Y = y1;
  41. endPoint.X = x2;
  42. endPoint.Y = y2;
  43. }
  44. public HandModeRulerDraw(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  45. {
  46. this.objectType = DrawClass.Other;
  47. this.drawToolType = DrawToolType.HandModeRulerDraw;
  48. this.ISurfaceBox = surfaceBox;
  49. rulerModel = (RulerModel)parentStyleModel;
  50. startPoint = points[0];
  51. endPoint = points[1];
  52. centerPoint = points[2];
  53. pixelLength = Convert.ToInt32(content.ToString().Split(',')[0]);
  54. text = content.ToString().Split(',')[1];
  55. }
  56. /// <summary>
  57. /// Clone this instance
  58. /// </summary>
  59. public override DrawObject Clone()
  60. {
  61. HandModeRulerDraw handModeRulerDraw = new HandModeRulerDraw(ISurfaceBox, 0, 0, 1, 0);
  62. handModeRulerDraw.objectType = DrawClass.Other;
  63. handModeRulerDraw.drawToolType = DrawToolType.HandModeRulerDraw;
  64. handModeRulerDraw.ISurfaceBox = this.ISurfaceBox;
  65. handModeRulerDraw.startPoint = this.startPoint;
  66. handModeRulerDraw.endPoint = this.endPoint;
  67. handModeRulerDraw.centerPoint = this.centerPoint;
  68. handModeRulerDraw.pixelLength = this.pixelLength;
  69. handModeRulerDraw.text = this.text;
  70. FillDrawObjectFields(handModeRulerDraw);
  71. return handModeRulerDraw;
  72. }
  73. public override void Draw(Graphics g)
  74. {
  75. g.SmoothingMode = SmoothingMode.AntiAlias;
  76. if (text != null && text != "")
  77. {
  78. DrawRulerHelper.drawRuler(this.rulerModel, g, centerPoint, pixelLength, text, out rectangle);
  79. }
  80. }
  81. public void getAllData()
  82. {
  83. Dictionary<MeasurementUnit, double> measurementUnitDictionary = this.ISurfaceBox.getMeasureInfo();
  84. double micronRatio = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少微米长度
  85. pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度
  86. //判断像素长度所属区间并重新赋值
  87. if (pixelLength <= lengthEnum[0])
  88. pixelLength = lengthEnum[0];
  89. else if (pixelLength > lengthEnum[0] && pixelLength <= lengthEnum[1])
  90. pixelLength = lengthEnum[1];
  91. else if (pixelLength > lengthEnum[1] && pixelLength <= lengthEnum[2])
  92. pixelLength = lengthEnum[2];
  93. else if (pixelLength > lengthEnum[2] && pixelLength <= lengthEnum[3])
  94. pixelLength = lengthEnum[3];
  95. else if (pixelLength > lengthEnum[3] && pixelLength <= lengthEnum[4])
  96. pixelLength = lengthEnum[4];
  97. else if (pixelLength > lengthEnum[4] && pixelLength <= lengthEnum[5])
  98. pixelLength = lengthEnum[5];
  99. else if (pixelLength > lengthEnum[5] && pixelLength <= lengthEnum[6])
  100. pixelLength = lengthEnum[6];
  101. else if (pixelLength > lengthEnum[6] && pixelLength <= lengthEnum[7])
  102. pixelLength = lengthEnum[7];
  103. else if (pixelLength > lengthEnum[7] && pixelLength <= lengthEnum[8])
  104. pixelLength = lengthEnum[8];
  105. else if (pixelLength > lengthEnum[8] && pixelLength <= lengthEnum[9])
  106. pixelLength = lengthEnum[9];
  107. else if (pixelLength > lengthEnum[9] && pixelLength <= lengthEnum[10])
  108. pixelLength = lengthEnum[10];
  109. else
  110. pixelLength = lengthEnum[11];
  111. double lineLength = pixelLength * micronRatio;//物理长度
  112. SizeF windowSize = this.ISurfaceBox.GetDocumentSize();//窗体尺寸
  113. //线样式
  114. Pen linePen = new Pen(Color.FromArgb(this.rulerModel.lineColor));
  115. linePen.Width = (int)this.rulerModel.lineWidth;
  116. //边框样式
  117. Pen borderPen = new Pen(Color.FromArgb(this.rulerModel.borderColor));
  118. borderPen.Width = (int)this.rulerModel.borderWidth;
  119. int lineWidthOffset = (int)this.rulerModel.lineWidth / 2 - 1;
  120. int offset = borderPen.Width > 0 ? (int)(borderPen.Width / 2) - 2 : 0;
  121. //背景大小
  122. decimal backLength = pixelLength * this.rulerModel.backgroundSize / 100;
  123. //垂线
  124. decimal verticalLength = pixelLength * this.rulerModel.verticalLineLength / 100;
  125. //文字
  126. text = lineLength + "μm";
  127. decimal textHeight = pixelLength * this.rulerModel.textHeight / 100;
  128. Font textFont = new Font(this.rulerModel.textFont, (float)this.rulerModel.textFontSize);
  129. int fontNewHeight = lineWidthOffset + (int)(textHeight + new Decimal(textFont.Height));
  130. int abc = fontNewHeight > ((int)verticalLength / 2 - 2) ? fontNewHeight : ((int)verticalLength / 2 - 2);
  131. //标尺最终尺寸
  132. int rulerWidth = pixelLength + (int)(backLength * 2) + (int)(linePen.Width * 2) + offset * 2;
  133. int rulerHeight = (int)verticalLength / 2 + abc + (int)(backLength * 2) + lineWidthOffset + 2 + offset * 2;
  134. int rulerPosition = this.rulerModel.rulerPosition;//标尺位置
  135. decimal rulerMargin = this.rulerModel.rulerMargin / 100;//标尺边距
  136. float marginLR = (float)rulerMargin * windowSize.Width;//左右边距
  137. float marginTB = (float)rulerMargin * windowSize.Height;//上下边距
  138. float offsetBorder = borderPen.Width > 0 ? borderPen.Width / 2 : 0;
  139. switch (rulerPosition)
  140. {
  141. case 1://左上
  142. centerPoint.X = rulerWidth / 2f - lineWidthOffset + offsetBorder + marginLR;
  143. centerPoint.Y = fontNewHeight + (float)backLength - lineWidthOffset + offsetBorder + marginTB;
  144. break;
  145. case 2://右上
  146. centerPoint.X = windowSize.Width - (rulerWidth / 2f - lineWidthOffset + offsetBorder) - marginLR;
  147. centerPoint.Y = fontNewHeight + (float)backLength - lineWidthOffset + offsetBorder + marginTB;
  148. break;
  149. case 3://左下
  150. centerPoint.X = rulerWidth / 2f - lineWidthOffset + offsetBorder + marginLR;
  151. centerPoint.Y = windowSize.Height - (rulerHeight - (fontNewHeight + (float)backLength)) - offsetBorder - marginTB;
  152. break;
  153. case 4://右下
  154. centerPoint.X = windowSize.Width - (rulerWidth / 2f - lineWidthOffset + offsetBorder) - marginLR;
  155. centerPoint.Y = windowSize.Height - (rulerHeight - (fontNewHeight + (float)backLength)) - offsetBorder - marginTB;
  156. break;
  157. }
  158. }
  159. public override int HandleCount
  160. {
  161. get
  162. {
  163. return 1;
  164. }
  165. }
  166. /// <summary>
  167. /// Get handle pointscroll by 1-based number
  168. /// </summary>
  169. /// <param name="handleNumber"></param>
  170. /// <returns></returns>
  171. public override PointF GetHandle(int handleNumber)
  172. {
  173. return new PointF(rectangle.X, rectangle.Y);
  174. }
  175. /// <summary>
  176. /// Hit test.
  177. /// Return value: -1 - no hit
  178. /// 0 - hit anywhere
  179. /// > 1 - handle number
  180. /// </summary>
  181. /// <param name="pointscroll"></param>
  182. /// <returns></returns>
  183. public override int HitTest(Point point)
  184. {
  185. if (Selected)
  186. {
  187. for (int i = 1; i <= HandleCount; i++)
  188. {
  189. if (GetHandleRectangle(i).Contains(point))
  190. return i;
  191. }
  192. }
  193. if (PointInObject(point))
  194. return 0;
  195. return -1;
  196. }
  197. protected override bool PointInObject(Point point)
  198. {
  199. return rectangle.Contains(point);
  200. }
  201. public override bool IntersectsWith(Rectangle rectangle)
  202. {
  203. return Rectangle.IntersectsWith(rectangle);
  204. }
  205. public override Cursor GetHandleCursor(int handleNumber)
  206. {
  207. return Cursors.Default;
  208. }
  209. public override void MoveHandleTo(Point point, int handleNumber)
  210. {
  211. if (handleNumber == 2)
  212. {
  213. endPoint.X = point.X;
  214. getAllData();
  215. }
  216. }
  217. public override void Move(int deltaX, int deltaY)
  218. {
  219. int x = ISurfaceBox.UnscaleScalar(deltaX);
  220. int y = ISurfaceBox.UnscaleScalar(deltaY);
  221. startPoint.X += x;
  222. startPoint.Y += y;
  223. endPoint.X += x;
  224. endPoint.Y += y;
  225. centerPoint.X += x;
  226. centerPoint.Y += y;
  227. rectangle.X += x;
  228. rectangle.Y += y;
  229. }
  230. public override RectangleF GetBoundingBox()
  231. {
  232. return rectangle;
  233. }
  234. public override List<PointF> GetPoints()
  235. {
  236. List<PointF> points = new List<PointF>();
  237. points.Add(startPoint);
  238. points.Add(endPoint);
  239. points.Add(centerPoint);
  240. return points;
  241. }
  242. public override ParentStyleModel GetStyle()
  243. {
  244. return rulerModel;
  245. }
  246. public override string GetContent()
  247. {
  248. return pixelLength.ToString() + "," + text;
  249. }
  250. }
  251. }