DrawAutoRuler.cs 13 KB

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