AutoRulerDraw.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 AutoRulerDraw : 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 lineLength = 1;//标尺长度
  31. public bool getLengthFromConfig = true;
  32. double micronRatio = 1;
  33. decimal gainNumber = 0;
  34. public AutoRulerDraw(ISurfaceBox surfaceBox) : base()
  35. {
  36. this.objectType = DrawClass.Other;
  37. this.drawToolType = DrawToolType.AutoRulerDraw;
  38. rulerModel = surfaceBox.GetRulerStyleModel();
  39. startPoint = new PointF(0, 0);
  40. Dictionary<MeasurementUnit, double> measurementUnitDictionary = surfaceBox.getMeasureInfo();
  41. this.micronRatio = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少纳米长度
  42. this.gainNumber = surfaceBox.GetGainMultiple();//获取放大倍数
  43. if (this.gainNumber == 0)
  44. this.gainNumber = 20;
  45. Initialize();
  46. }
  47. public AutoRulerDraw(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  48. {
  49. this.objectType = DrawClass.Other;
  50. this.drawToolType = DrawToolType.AutoRulerDraw;
  51. this.ISurfaceBox = surfaceBox;
  52. rulerModel = (RulerModel)parentStyleModel;
  53. startPoint = points[0];
  54. lineLength = Convert.ToInt32(content.ToString());
  55. getLengthFromConfig = false;
  56. }
  57. /// <summary>
  58. /// Clone this instance
  59. /// </summary>
  60. public override DrawObject Clone()
  61. {
  62. AutoRulerDraw autoRuler = new AutoRulerDraw(ISurfaceBox);
  63. autoRuler.objectType = DrawClass.Other;
  64. autoRuler.drawToolType = DrawToolType.AutoRulerDraw;
  65. autoRuler.ISurfaceBox = ISurfaceBox;
  66. autoRuler.startPoint = this.startPoint;
  67. autoRuler.lineLength = this.lineLength;
  68. autoRuler.getLengthFromConfig = this.getLengthFromConfig;
  69. FillDrawObjectFields(autoRuler);
  70. return autoRuler;
  71. }
  72. public override void Draw(Graphics g)
  73. {
  74. g.SmoothingMode = SmoothingMode.AntiAlias;
  75. if (getLengthFromConfig)//判断是否从配置文件获取长度
  76. {
  77. lineLength = Convert.ToInt32(this.rulerModel.autoRulerValue);//标尺长度 要除以放大倍数
  78. }
  79. int actualLineLength = lineLength / (int)gainNumber;//除以放大倍数后的实际物理长度
  80. int pixelLength = Convert.ToInt32(actualLineLength / micronRatio);//换算为像素长度
  81. SizeF windowSize = this.ISurfaceBox.GetDocumentSize();//窗体尺寸
  82. //线样式
  83. Pen linePen = new Pen(Color.FromArgb(this.rulerModel.lineColor));
  84. linePen.Width = (int)this.rulerModel.lineWidth;
  85. //边框样式
  86. Pen borderPen = new Pen(Color.FromArgb(this.rulerModel.borderColor));
  87. borderPen.Width = (int)this.rulerModel.borderWidth;
  88. int lineWidthOffset = (int)this.rulerModel.lineWidth / 2 - 1;
  89. int offset = borderPen.Width > 0 ? (int)(borderPen.Width / 2) - 2 : 0;
  90. //背景大小
  91. decimal backLength = pixelLength * this.rulerModel.backgroundSize / 100;
  92. //垂线
  93. decimal verticalLength = pixelLength * this.rulerModel.verticalLineLength / 100;
  94. //文字
  95. string text = actualLineLength + "μm";
  96. decimal textHeight = pixelLength * this.rulerModel.textHeight / 100;
  97. Font textFont = new Font(this.rulerModel.textFont, (float)this.rulerModel.textFontSize);
  98. int fontNewHeight = lineWidthOffset + (int)(textHeight + new Decimal(textFont.Height));
  99. int abc = fontNewHeight > ((int)verticalLength / 2 - 2) ? fontNewHeight : ((int)verticalLength / 2 - 2);
  100. //标尺最终尺寸
  101. int rulerWidth = pixelLength + (int)(backLength * 2) + (int)(linePen.Width * 2) + offset * 2;
  102. int rulerHeight = (int)verticalLength / 2 + abc + (int)(backLength * 2) + lineWidthOffset + 2 + offset * 2;
  103. int rulerPosition = this.rulerModel.rulerPosition;//标尺位置
  104. decimal rulerMargin = this.rulerModel.rulerMargin / 100;//标尺边距
  105. float marginLR = (float)rulerMargin * windowSize.Width;//左右边距
  106. float marginTB = (float)rulerMargin * windowSize.Height;//上下边距
  107. float offsetBorder = borderPen.Width > 0 ? borderPen.Width / 2 : 0;
  108. switch (rulerPosition)
  109. {
  110. case 1://左上
  111. startPoint.X = rulerWidth / 2f - lineWidthOffset + offsetBorder + marginLR;
  112. startPoint.Y = fontNewHeight + (float)backLength - lineWidthOffset + offsetBorder + marginTB;
  113. break;
  114. case 2://右上
  115. startPoint.X = windowSize.Width - (rulerWidth / 2f - lineWidthOffset + offsetBorder) - marginLR;
  116. startPoint.Y = fontNewHeight + (float)backLength - lineWidthOffset + offsetBorder + marginTB;
  117. break;
  118. case 3://左下
  119. startPoint.X = rulerWidth / 2f - lineWidthOffset + offsetBorder + marginLR;
  120. startPoint.Y = windowSize.Height - (rulerHeight - (fontNewHeight + (float)backLength)) - offsetBorder - marginTB;
  121. break;
  122. case 4://右下
  123. startPoint.X = windowSize.Width - (rulerWidth / 2f - lineWidthOffset + offsetBorder) - marginLR;
  124. startPoint.Y = windowSize.Height - (rulerHeight - (fontNewHeight + (float)backLength)) - offsetBorder - marginTB;
  125. break;
  126. }
  127. DrawRulerHelper.drawRuler(this.rulerModel, g, startPoint, pixelLength, text, out rectangle);
  128. }
  129. public override int HandleCount
  130. {
  131. get
  132. {
  133. return 1;
  134. }
  135. }
  136. /// <summary>
  137. /// Get handle pointscroll by 1-based number
  138. /// </summary>
  139. /// <param name="handleNumber"></param>
  140. /// <returns></returns>
  141. public override PointF GetHandle(int handleNumber)
  142. {
  143. return new PointF(rectangle.X, rectangle.Y);
  144. }
  145. /// <summary>
  146. /// Hit test.
  147. /// Return value: -1 - no hit
  148. /// 0 - hit anywhere
  149. /// > 1 - handle number
  150. /// </summary>
  151. /// <param name="pointscroll"></param>
  152. /// <returns></returns>
  153. public override int HitTest(Point point)
  154. {
  155. if (Selected)
  156. {
  157. for (int i = 1; i <= HandleCount; i++)
  158. {
  159. if (GetHandleRectangle(i).Contains(point))
  160. return i;
  161. }
  162. }
  163. if (PointInObject(point))
  164. return 0;
  165. return -1;
  166. }
  167. protected override bool PointInObject(Point point)
  168. {
  169. return rectangle.Contains(point);
  170. }
  171. public override bool IntersectsWith(Rectangle rectangle)
  172. {
  173. return Rectangle.IntersectsWith(rectangle);
  174. }
  175. public override Cursor GetHandleCursor(int handleNumber)
  176. {
  177. return Cursors.Default;
  178. }
  179. public override void MoveHandleTo(Point point, int handleNumber)
  180. {
  181. startPoint = point;
  182. }
  183. public override void Move(int deltaX, int deltaY)
  184. {
  185. int x = ISurfaceBox.UnscaleScalar(deltaX);
  186. int y = ISurfaceBox.UnscaleScalar(deltaY);
  187. startPoint.X += x;
  188. startPoint.Y += y;
  189. rectangle.X += x;
  190. rectangle.Y += y;
  191. }
  192. public override RectangleF GetBoundingBox()
  193. {
  194. return rectangle;
  195. }
  196. public override List<PointF> GetPoints()
  197. {
  198. List<PointF> points = new List<PointF>();
  199. points.Add(startPoint);
  200. return points;
  201. }
  202. public override ParentStyleModel GetStyle()
  203. {
  204. return rulerModel;
  205. }
  206. public override string GetContent()
  207. {
  208. return lineLength.ToString();
  209. }
  210. }
  211. }