DrawPrestoredRuler.cs 12 KB

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