123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- using PaintDotNet.Base.SettingModel;
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- using PaintDotNet.Annotation.Enum;
- using System.Collections.Generic;
- using PaintDotNet.Annotation.Measure;
- namespace PaintDotNet.Annotation.Label
- {
- /// <summary>
- /// 标注->手动标尺
- /// </summary>
- public class DrawHandModeRuler : DrawObject
- {
- private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
- /// <summary>
- /// 标注样式信息model
- /// </summary>
- public RulerModel rulerModel;
- double micronRatio = 1;
- double oldmicronRatio;
- /// <summary>
- /// 标尺长度集合
- /// </summary>
- private int[] lengthEnum = new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 50000, 1000000};
- private bool isMoveForward = true;
- public DrawHandModeRuler(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
- {
- this.objectType = DrawClass.Label;
- this.drawToolType = DrawToolType.DrawHandModeRuler;
- //Dictionary<MeasurementUnit, double> measurementUnitDictionary = surfaceBox.getMeasureInfo();
- //micronRatio = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少微米长度
- Dictionary<MeasurementUnit, double> measurementUnitDictionary = surfaceBox.getMeasureInfo();
- MeasurementUnit unit = surfaceBox.GetMeasurementUnit();
- micronRatio = measurementUnitDictionary[unit];//每像素多少微米长度
- oldmicronRatio = micronRatio;
- rulerModel = surfaceBox.GetRulerStyleModel();
- startPoint.X = x1;
- startPoint.Y = y1;
- endPoint.X = x2;
- endPoint.Y = y2;
- //change = false;
- int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度
- length = pixelLength * micronRatio;
- }
- public DrawHandModeRuler(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
- {
- this.objectType = DrawClass.Label;
- this.drawToolType = DrawToolType.DrawHandModeRuler;
- this.ISurfaceBox = surfaceBox;
- rulerModel = (RulerModel)parentStyleModel;
- startPoint = points[0];
- endPoint = points[1];
-
- }
- /// <summary>
- /// Clone this instance
- /// </summary>
- public override DrawObject Clone()
- {
- DrawHandModeRuler handModeRulerDraw = new DrawHandModeRuler(ISurfaceBox, 0, 0, 1, 0);
- handModeRulerDraw.objectType = DrawClass.Label;
- handModeRulerDraw.drawToolType = DrawToolType.DrawHandModeRuler;
- handModeRulerDraw.ISurfaceBox = this.ISurfaceBox;
- handModeRulerDraw.startPoint = this.startPoint;
- handModeRulerDraw.endPoint = this.endPoint;
- handModeRulerDraw.length = this.length;
- //handModeRulerDraw.pixelLength = pixelLength;
- FillDrawObjectFields(handModeRulerDraw);
- return handModeRulerDraw;
- }
- public override DrawObject Clone(ISurfaceBox surfaceBox)
- {
- DrawHandModeRuler handModeRulerDraw = new DrawHandModeRuler(surfaceBox, 0, 0, 1, 0);
- handModeRulerDraw.objectType = DrawClass.Label;
- handModeRulerDraw.drawToolType = DrawToolType.DrawHandModeRuler;
- handModeRulerDraw.ISurfaceBox = surfaceBox;
- handModeRulerDraw.startPoint = this.startPoint;
- handModeRulerDraw.endPoint = this.endPoint;
- handModeRulerDraw.length = this.length;
- //handModeRulerDraw.pixelLength = pixelLength;
- FillDrawObjectFields(handModeRulerDraw);
- return handModeRulerDraw;
- }
- public override void Draw(Graphics g)
- {
- g.SmoothingMode = SmoothingMode.AntiAlias;
- Dictionary<MeasurementUnit, double> measurementUnitDictionary = ISurfaceBox.getMeasureInfo();
- MeasurementUnit unit = ISurfaceBox.GetMeasurementUnit();
- double micron = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少微米长度
- micronRatio = measurementUnitDictionary[MeasurementUnit.Micron];//每像素多少微米长度
- int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度
- length = pixelLength * micronRatio;
-
- //length = pixelLength * micronRatio;
- //判断像素长度所属区间并重新赋值
- if (length <= lengthEnum[0])
- {
- length = lengthEnum[0];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[0] && length <= lengthEnum[1])
- {
- length = lengthEnum[1];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[1] && length <= lengthEnum[2])
- {
- length = lengthEnum[2];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[2] && length <= lengthEnum[3])
- {
- length = lengthEnum[3];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[3] && length <= lengthEnum[4])
- {
- length = lengthEnum[4];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[4] && length <= lengthEnum[5])
- {
- length = lengthEnum[5];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[5] && length <= lengthEnum[6])
- {
- length = lengthEnum[6];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[6] && length <= lengthEnum[7])
- {
- length = lengthEnum[7];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[7] && length <= lengthEnum[8])
- {
- length = lengthEnum[8];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[8] && length <= lengthEnum[9])
- {
- length = lengthEnum[9];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[9] && length <= lengthEnum[10])
- {
- length = lengthEnum[10];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[10] && length <= lengthEnum[11])
- {
- length = lengthEnum[11];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[11] && length <= lengthEnum[12])
- {
- length = lengthEnum[12];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else if (length > lengthEnum[12] && length <= lengthEnum[13])
- {
- length = lengthEnum[13];
- if (startPoint.X <= endPoint.X)
- endPoint.X = startPoint.X + (int)(length / micronRatio);
- else
- endPoint.X = startPoint.X - (int)(length / micronRatio);
- }
- else
- {
- length = lengthEnum[14];
- }
- #region [单位缩写]
- string unitAbbreviation = string.Empty;
- micronRatio = measurementUnitDictionary[unit];//每像素多少微米长度
- switch (unit)
- {
- case MeasurementUnit.Inch:
- length = length * micronRatio / micron;
- unitAbbreviation = "in";
- break;
- case MeasurementUnit.Centimeter:
- length = length * micronRatio / micron;
- unitAbbreviation = "cm";
- break;
- case MeasurementUnit.Millimeter:
- length = length * micronRatio / micron;
- unitAbbreviation = "mm";
- break;
- case MeasurementUnit.Micron:
- unitAbbreviation = "µm";
- break;
- case MeasurementUnit.Nano:
- length = length * micronRatio / micron;
- unitAbbreviation = "nm";
- break;
- case MeasurementUnit.Pixel:
- unitAbbreviation = "PX";
- break;
- case MeasurementUnit.Mil:
- length = length * micronRatio / micron;
- unitAbbreviation = "mil";
- break;
- }
- #endregion
- //else if (pixelLength > lengthEnum[7] / micronRatio && pixelLength <= lengthEnum[8] / micronRatio)
- // pixelLength = (int)(lengthEnum[8] / micronRatio);
- //else if (pixelLength > lengthEnum[8] / micronRatio && pixelLength <= lengthEnum[9] / micronRatio)
- // pixelLength = (int)(lengthEnum[9] / micronRatio);
- //else if (pixelLength > lengthEnum[9] / micronRatio && pixelLength <= lengthEnum[10] / micronRatio)
- // pixelLength = (int)(lengthEnum[10] / micronRatio);
- //else
- // pixelLength = lengthEnum[11];
- ////判断像素长度所属区间并重新赋值
- //if (pixelLength <= lengthEnum[0]/ micronRatio)
- // pixelLength = (int)(lengthEnum[0]/ micronRatio);
- //else if (pixelLength > lengthEnum[0]/ micronRatio && pixelLength <= lengthEnum[1]/ micronRatio)
- // pixelLength = (int)(lengthEnum[1] / micronRatio);
- //else if (pixelLength > lengthEnum[1]/ micronRatio && pixelLength <= lengthEnum[2]/ micronRatio)
- // pixelLength = (int)(lengthEnum[2] / micronRatio);
- //else if (pixelLength > lengthEnum[2] / micronRatio && pixelLength <= lengthEnum[3] / micronRatio)
- // pixelLength = (int)(lengthEnum[3] / micronRatio);
- //else if (pixelLength > lengthEnum[3] / micronRatio && pixelLength <= lengthEnum[4] / micronRatio)
- // pixelLength = (int)(lengthEnum[4] / micronRatio);
- //else if (pixelLength > lengthEnum[4] / micronRatio && pixelLength <= lengthEnum[5] / micronRatio)
- // pixelLength = (int)(lengthEnum[5] / micronRatio);
- //else if (pixelLength > lengthEnum[5] / micronRatio && pixelLength <= lengthEnum[6] / micronRatio)
- // pixelLength = (int)(lengthEnum[6] / micronRatio);
- //else if (pixelLength > lengthEnum[6] / micronRatio && pixelLength <= lengthEnum[7] / micronRatio)
- // pixelLength = (int)(lengthEnum[7] / micronRatio);
- //else if (pixelLength > lengthEnum[7] / micronRatio && pixelLength <= lengthEnum[8] / micronRatio)
- // pixelLength = (int)(lengthEnum[8] / micronRatio);
- //else if (pixelLength > lengthEnum[8] / micronRatio && pixelLength <= lengthEnum[9] / micronRatio)
- // pixelLength = (int)(lengthEnum[9] / micronRatio);
- //else if (pixelLength > lengthEnum[9] / micronRatio && pixelLength <= lengthEnum[10] / micronRatio)
- // pixelLength = (int)(lengthEnum[10] / micronRatio);
- //else
- // pixelLength = lengthEnum[11];
- //if (isMoveForward)
- //{
- // if (startPoint.X <= endPoint.X)
- // endPoint.X = startPoint.X + pixelLength;
- // else
- // endPoint.X = startPoint.X - pixelLength;
- //}
- //else
- //{
- // if (startPoint.X <= endPoint.X)
- // startPoint.X = endPoint.X - pixelLength;
- // else
- // startPoint.X = endPoint.X + pixelLength;
- //}
- //int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));
- //length = pixelLength * micronRatio;
- //double lineLength = length/* Math.Round(length)*/;//物理长度
- string text = length + unitAbbreviation;//文字
- //string text = Math.Round(length, MeasureDrawObject.decimalPlaces).ToString() + unitAbbreviation;//小数保留
- if (text != null && text != "")
- {
- 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;
- PointF lineL = new PointF(0, 0);
- PointF lineR = new PointF(0, 0);
- if (isMoveForward)
- {
- if (startPoint.X <= endPoint.X)
- {
- lineL.X = startPoint.X;
- lineL.Y = startPoint.Y;
- lineR.X = startPoint.X + (int)(length / micronRatio);
- lineR.Y = startPoint.Y;
- }
- else
- {
- lineL.X = startPoint.X - (int)(length / micronRatio);
- lineL.Y = startPoint.Y;
- lineR.X = startPoint.X;
- lineR.Y = startPoint.Y;
- }
- }
- else
- {
- if (startPoint.X <= endPoint.X)
- {
- lineL.X = endPoint.X - (int)(length / micronRatio);
- lineL.Y = endPoint.Y;
- lineR.X = endPoint.X;
- lineR.Y = endPoint.Y;
- }
- else
- {
- lineL.X = endPoint.X;
- lineL.Y = endPoint.Y;
- lineR.X = endPoint.X + (int)(length / micronRatio);
- lineR.Y = endPoint.Y;
- }
- }
- int lineWidthOffset = (int)rulerModel.lineWidth / 2 - 1;
- //垂线
- decimal verticalLength = (int)(length / micronRatio) * this.rulerModel.verticalLineLength / 100;
- PointF verticalLT = new PointF(lineL.X, lineL.Y - (int)(verticalLength / 2));
- PointF verticalLB = new PointF(lineL.X, lineL.Y + (int)(verticalLength / 2));
- PointF verticalRT = new PointF(lineR.X, lineR.Y - (int)(verticalLength / 2));
- PointF verticalRB = new PointF(lineR.X, lineR.Y + (int)(verticalLength / 2));
- // 文字
- decimal textHeight = (int)(length / micronRatio) * this.rulerModel.textHeight / 100;
- Font textFont;
- if (this.rulerModel.textBold == 0)
- textFont = new Font(this.rulerModel.textFont, (float)this.rulerModel.textFontSize,FontStyle.Regular);
- else
- textFont = new Font(this.rulerModel.textFont, (float)this.rulerModel.textFontSize, FontStyle.Bold);
- float textSizes = textFont.SizeInPoints * text.Length;
- float textX = lineL.X;
- if (rulerModel.textPosition == 1)
- {
- textX = (lineR.X - lineL.X) / 2 - (int)(textSizes / 2) + lineL.X;
- }
- else if (rulerModel.textPosition == 2)
- {
- textX = lineR.X - (int)(textSizes);
- }
- PointF textLT = new PointF(textX, lineL.Y - lineWidthOffset - (int)(textHeight + new Decimal(textFont.Height)));
- // 背景参照Y坐标,垂线高,使用垂线Y值,文字高使用文字Y值。
- float backRectangleReferY = textLT.Y < verticalLT.Y - 2 ? textLT.Y : verticalLT.Y - 2;
- // 背景大小
- decimal backLength = (int)(length / micronRatio) * this.rulerModel.backgroundSize / 100;
- float backRectangleX = verticalLT.X - (int)backLength - (int)linePen.Width;
- float backRectangleY = backRectangleReferY - (int)(backLength);
- float backRectangleW = (verticalRT.X - verticalLT.X) + (int)(backLength * 2) + (int)(linePen.Width * 2);
- float backRectangleH = (verticalLB.Y - backRectangleReferY) + (int)(backLength * 2) + lineWidthOffset + 2;
- RectangleF backRectangle = new RectangleF(backRectangleX, backRectangleY, backRectangleW, backRectangleH);
- // 边框
- int offset = borderPen.Width > 0 ? (int)(borderPen.Width / 2) - 2 : 0;
- RectangleF borderPenRectangle = new RectangleF(backRectangle.X - offset, backRectangle.Y - offset, backRectangle.Width + offset * 2, backRectangle.Height + offset * 2);
- // 绘制
- g.FillRectangle(new SolidBrush(Color.FromArgb(this.rulerModel.backColor)), backRectangle);
- g.DrawRectangle(borderPen, borderPenRectangle.X, borderPenRectangle.Y, borderPenRectangle.Width, borderPenRectangle.Height);
- g.DrawLine(linePen, lineL, lineR);
- g.DrawLine(linePen, verticalLT, verticalLB);
- g.DrawLine(linePen, verticalRT, verticalRB);
- g.DrawString(text, textFont, new SolidBrush(Color.FromArgb(this.rulerModel.textColor)), textLT.X, textLT.Y);
- rectangle = borderPenRectangle;
- linePen.Dispose();
- borderPen.Dispose();
- }
- }
- public override int HandleCount
- {
- get
- {
- return 2;
- }
- }
- /// <summary>
- /// Get handle pointscroll by 1-based number
- /// </summary>
- /// <param name="handleNumber"></param>
- /// <returns></returns>
- public override PointF GetHandle(int handleNumber)
- {
- float x = 0, y = 0;
- switch (handleNumber)
- {
- case 1:
- x = startPoint.X;
- y = startPoint.Y;
- break;
- case 2:
- x = endPoint.X;
- y = endPoint.Y;
- break;
- }
- return new PointF(x, 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)
- {
- switch (handleNumber)
- {
- case 1:
- return Cursors.SizeWE;
- case 2:
- return Cursors.SizeWE;
- default:
- return Cursors.Default;
- }
- }
- public override void MoveHandleTo(Point point, int handleNumber)
- {
- switch (handleNumber)
- {
- case 1:
- isMoveForward = false;
- startPoint.X = point.X;
- break;
- case 2:
- isMoveForward = true;
- endPoint.X = point.X;
- break;
- }
- int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度
- length = pixelLength * micronRatio;
- }
- public override void Move(int deltaX, int deltaY)
- {
- int x = ISurfaceBox.UnscaleScalar(deltaX);
- int y = ISurfaceBox.UnscaleScalar(deltaY);
- startPoint.X += x;
- startPoint.Y += y;
- endPoint.X += x;
- endPoint.Y += y;
- rectangle.X += x;
- rectangle.Y += y;
- int pixelLength = Math.Abs(Convert.ToInt32(endPoint.X - startPoint.X));//根据两点获取的标尺像素长度
- length = pixelLength * micronRatio;
- }
- public override RectangleF GetBoundingBox()
- {
- return rectangle;
- }
- public override List<PointF> GetPoints()
- {
- List<PointF> points = new List<PointF>();
- points.Add(startPoint);
- points.Add(endPoint);
- return points;
- }
- public override ParentStyleModel GetStyle()
- {
- return rulerModel;
- }
- }
- }
|