123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- using PaintDotNet.Base.SettingModel;
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Globalization;
- using System.Runtime.Serialization;
- using System.Windows.Forms;
- using PaintDotNet.Base.CommTool;
- using System.IO;
- using PaintDotNet.Annotation.Enum;
- using System.Collections.Generic;
- using System.Drawing.Imaging;
- using System.ComponentModel;
- namespace PaintDotNet.Annotation.Label
- {
- /// <summary>
- /// 标注->自动标尺
- /// </summary>
- public class DrawAutoRuler : DrawObject
- {
- private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
- /// <summary>
- /// 标注样式信息model
- /// </summary>
- public RulerModel rulerModel;
- public int lineLength = 1;//标尺长度
- public bool getLengthFromConfig = true;//是否从配置文件获取长度
- private double micronRatio = 1;
- private decimal gainNumber = 0;//放大倍数
- private bool isFirstDraw = true;//是否第一次绘制,决定初始位置
- public DrawAutoRuler(ISurfaceBox surfaceBox) : base()
- {
- this.objectType = DrawClass.Label;
- this.drawToolType = DrawToolType.DrawAutoRuler;
- rulerModel = surfaceBox.GetRulerStyleModel();
- startPoint = new PointF(0, 0);
- //Dictionary<MeasurementUnit, double> measurementUnitDictionary = surfaceBox.getMeasureInfo();
- //this.micronRatio = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少纳米长度
- //this.gainNumber = surfaceBox.GetMic_Rulers();//获取放大倍数
- //if (this.gainNumber == 0)
- // this.gainNumber = 20;
- Initialize();
- }
- public DrawAutoRuler(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
- {
- this.objectType = DrawClass.Label;
- this.drawToolType = DrawToolType.DrawAutoRuler;
- this.ISurfaceBox = surfaceBox;
- this.isFirstDraw = false;
- this.gainNumber = surfaceBox.GetGainMultiple();//获取放大倍数
- if (this.gainNumber == 0)
- this.gainNumber = 1;
- rulerModel = (RulerModel)parentStyleModel;
- startPoint = points[0];
- lineLength = Convert.ToInt32(content.ToString());
- getLengthFromConfig = false;
- }
- /// <summary>
- /// Clone this instance
- /// </summary>
- public override DrawObject Clone()
- {
- DrawAutoRuler autoRuler = new DrawAutoRuler(ISurfaceBox);
- autoRuler.objectType = DrawClass.Label;
- autoRuler.drawToolType = DrawToolType.DrawAutoRuler;
- autoRuler.ISurfaceBox = ISurfaceBox;
- autoRuler.isFirstDraw = false;
- autoRuler.startPoint = this.startPoint;
- autoRuler.lineLength = this.lineLength;
- autoRuler.getLengthFromConfig = this.getLengthFromConfig;
- FillDrawObjectFields(autoRuler);
- return autoRuler;
- }
- public override DrawObject Clone(ISurfaceBox surfaceBox)
- {
- DrawAutoRuler autoRuler = new DrawAutoRuler(surfaceBox);
- autoRuler.objectType = DrawClass.Label;
- autoRuler.drawToolType = DrawToolType.DrawAutoRuler;
- autoRuler.ISurfaceBox = surfaceBox;
- autoRuler.isFirstDraw = false;
- autoRuler.startPoint = this.startPoint;
- autoRuler.lineLength = this.lineLength;
- autoRuler.getLengthFromConfig = this.getLengthFromConfig;
- FillDrawObjectFields(autoRuler);
- return autoRuler;
- }
- public override void Draw(Graphics g)
- {
- this.gainNumber = ISurfaceBox.GetMic_Rulers();//获取放大倍数
- if (this.gainNumber == 0)
- this.gainNumber = 1;
- if (getLengthFromConfig)//判断是否从配置文件获取长度
- {
- lineLength = Convert.ToInt32(this.rulerModel.autoRulerValue);//标尺长度 要除以放大倍数
- }
- if (gainNumber < 1)
- {
- return;
- }
- Dictionary<MeasurementUnit, double> measurementUnitDictionary = ISurfaceBox.getMeasureInfo();
- #region[以微米为基础单位]
- double micron = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少微米长度
- double actualLineLength = (int)(lineLength / gainNumber);//除以放大倍数后的实际物理长度
- int pixelLength = Convert.ToInt32(actualLineLength / micron);//换算为像素长度
- #endregion
- g.SmoothingMode = SmoothingMode.AntiAlias;
- //实际单位
- MeasurementUnit unit = ISurfaceBox.GetMeasurementUnit();
- micronRatio = measurementUnitDictionary[unit];//每像素实际单位长度
- #region [单位缩写]
- string unitAbbreviation = string.Empty;
- switch (unit)
- {
- case MeasurementUnit.Inch:
- actualLineLength = actualLineLength * micronRatio / micron;
- unitAbbreviation = "in";
- break;
- case MeasurementUnit.Centimeter:
- actualLineLength = actualLineLength * micronRatio / micron;
- unitAbbreviation = "cm";
- break;
- case MeasurementUnit.Millimeter:
- actualLineLength = actualLineLength * micronRatio / micron;
- unitAbbreviation = "mm";
- break;
- case MeasurementUnit.Micron:
- unitAbbreviation = "µm";
- break;
- case MeasurementUnit.Nano:
- actualLineLength = actualLineLength * micronRatio / micron;
- unitAbbreviation = "nm";
- break;
- case MeasurementUnit.Pixel:
- unitAbbreviation = "PX";
- break;
- case MeasurementUnit.Mil:
- actualLineLength = actualLineLength * micronRatio / micron;
- unitAbbreviation = "mil";
- break;
- }
- #endregion
- //this.gainNumber = ISurfaceBox.GetMic_Rulers();//获取放大倍数
- //if (this.gainNumber == 0)
- // this.gainNumber = 1;
- //if (getLengthFromConfig)//判断是否从配置文件获取长度
- //{
- // lineLength = Convert.ToInt32(this.rulerModel.autoRulerValue);//标尺长度 要除以放大倍数
- //}
- //if (gainNumber < 1)
- //{
- // return;
- //}
- //int actualLineLength = (int)(lineLength / gainNumber);//除以放大倍数后的实际物理长度
- //int pixelLength = Convert.ToInt32(actualLineLength / micronRatio);//换算为像素长度
- SizeF windowSize = this.ISurfaceBox.GetDocumentSize();//窗体尺寸
- //线样式
- Pen linePen = new Pen(Color.FromArgb(this.rulerModel.lineColor));
- linePen.Width = (int)this.rulerModel.lineWidth;
- //边框样式
- Pen borderPen = new Pen(Color.FromArgb(this.rulerModel.borderColor));
- borderPen.Width = (int)this.rulerModel.borderWidth;
- int lineWidthOffset = (int)this.rulerModel.lineWidth / 2 - 1;
- int offset = borderPen.Width > 0 ? (int)(borderPen.Width / 2) - 2 : 0;
- //背景大小
- decimal backLength = pixelLength * this.rulerModel.backgroundSize / 100;
- //垂线
- decimal verticalLength = pixelLength * this.rulerModel.verticalLineLength / 100;
- //文字
- string text = actualLineLength + unitAbbreviation;
- //string text = Math.Round(actualLineLength, MeasureDrawObject.decimalPlaces).ToString() + unitAbbreviation;//小数保留
- decimal textHeight = pixelLength * this.rulerModel.textHeight / 100;
- Font textFont = new Font(this.rulerModel.textFont, (float)this.rulerModel.textFontSize);
- int fontNewHeight = lineWidthOffset + (int)(textHeight + new Decimal(textFont.Height));
- int abc = fontNewHeight > ((int)verticalLength / 2 - 2) ? fontNewHeight : ((int)verticalLength / 2 - 2);
- //标尺最终尺寸
- int rulerWidth = pixelLength + (int)(backLength * 2) + (int)(linePen.Width * 2) + offset * 2;
- int rulerHeight = (int)verticalLength / 2 + abc + (int)(backLength * 2) + lineWidthOffset + 2 + offset * 2;
- int rulerPosition = this.rulerModel.rulerPosition;//标尺位置
- decimal rulerMargin = this.rulerModel.rulerMargin / 100;//标尺边距
- float marginLR = (float)rulerMargin * windowSize.Width;//左右边距
- float marginTB = (float)rulerMargin * windowSize.Height;//上下边距
- float offsetBorder = borderPen.Width > 0 ? borderPen.Width / 2 : 0;
- if (isFirstDraw)
- {
- switch (rulerPosition)
- {
- case 1://左上
- startPoint.X = rulerWidth / 2f - lineWidthOffset + offsetBorder + marginLR;
- startPoint.Y = fontNewHeight + (float)backLength - lineWidthOffset + offsetBorder + marginTB;
- break;
- case 2://右上
- startPoint.X = windowSize.Width - (rulerWidth / 2f - lineWidthOffset + offsetBorder) - marginLR;
- startPoint.Y = fontNewHeight + (float)backLength - lineWidthOffset + offsetBorder + marginTB;
- break;
- case 3://左下
- startPoint.X = rulerWidth / 2f - lineWidthOffset + offsetBorder + marginLR;
- startPoint.Y = windowSize.Height - (rulerHeight - (fontNewHeight + (float)backLength)) - offsetBorder - marginTB;
- break;
- case 4://右下
- startPoint.X = windowSize.Width - (rulerWidth / 2f - lineWidthOffset + offsetBorder) - marginLR;
- startPoint.Y = windowSize.Height - (rulerHeight - (fontNewHeight + (float)backLength)) - offsetBorder - marginTB;
- break;
- }
- isFirstDraw = false;
- }
- DrawRulerHelper.drawRuler(this.rulerModel, g, startPoint, pixelLength, text, out rectangle);
- }
- public override int HandleCount
- {
- get
- {
- return 1;
- }
- }
- /// <summary>
- /// Get handle pointscroll by 1-based number
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- public override PointF GetHandle(int handleNumber)
- {
- return new PointF(rectangle.X, rectangle.Y);
- }
- /// <summary>
- /// Hit test.
- /// Return value: -1 - no hit
- /// 0 - hit anywhere
- /// > 1 - handle number
- /// </summary>
- /// <param name="pointscroll"></param>
- /// <returns></returns>
- public override int HitTest(Point point)
- {
- if (Selected)
- {
- for (int i = 1; i <= HandleCount; i++)
- {
- if (GetHandleRectangle(i).Contains(point))
- return i;
- }
- }
- if (PointInObject(point))
- return 0;
- return -1;
- }
- protected override bool PointInObject(Point point)
- {
- return rectangle.Contains(point);
- }
- public override bool IntersectsWith(Rectangle rectangle)
- {
- return Rectangle.IntersectsWith(rectangle);
- }
- public override Cursor GetHandleCursor(int handleNumber)
- {
- return Cursors.Default;
- }
- public override void MoveHandleTo(Point point, int handleNumber)
- {
- startPoint = point;
- }
- public override void Move(int deltaX, int deltaY)
- {
- int x = ISurfaceBox.UnscaleScalar(deltaX);
- int y = ISurfaceBox.UnscaleScalar(deltaY);
- startPoint.X += x;
- startPoint.Y += y;
- rectangle.X += x;
- rectangle.Y += y;
- }
- public override RectangleF GetBoundingBox()
- {
- return rectangle;
- }
- public override List<PointF> GetPoints()
- {
- List<PointF> points = new List<PointF>();
- points.Add(startPoint);
- return points;
- }
- public override ParentStyleModel GetStyle()
- {
- return rulerModel;
- }
- public override string GetContent()
- {
- return lineLength.ToString();
- }
- }
- }
|