接上一篇文章,當我們可以繪制圖形標記后,就可以在此操作類上面進行擴展,
比如測量類工具,目前整理出的常用繪圖和測量功能如下:
測量工具類:(圖形標記類請參考本系列文章:繪圖處理之圖形標記)
功能 | 說明 |
標尺 | 線段長度測量 |
折尺 | 曲線長度測量 |
心胸比 | 兩根線段按比例測量 |
交叉尺 | 兩根線段互相垂直測量 |
Cobb角 | 兩根線段的垂線交點角度測量,一般用於脊柱曲率 |
開角 | 兩根線段的延長交點的角度測量 |
T型尺 | 兩根線段相交的任意角度搖擺測量和長度測量 |
角度測量 | 角度測量 |
圓形測量 | 圓形或橢圓面積測量,包括CT值,平均值,方差,最大最小值 |
矩形測量 | 矩形面積測量,包括CT值,平均值,方差,最大最小值 |
多邊形測量 | 多邊形面積 |
以線段測量為例,看效果:
在繪制圖形的基礎上,我們創建一個Text Block來顯示測量結果:
//文本對象 TextBlock txtMeasure; /// <summary> /// 創建測量結果文本 /// </summary> /// <param name="point"></param> public void CreateMeasure(Point point) { if (!isMeasure) { return; } txtMeasure = new TextBlock(); txtMeasure.Text = "0.0mm"; txtMeasure.FontSize = ShapeManager.shapeMeasureFontSize; txtMeasure.Foreground = ShapeManager.shapeMeasureColor; txtMeasure.Height = 30; txtMeasure.Width = 100; txtMeasure.SetValue(Canvas.LeftProperty, point.X); txtMeasure.SetValue(Canvas.TopProperty, point.Y); canvas.Children.Add(txtMeasure); measureList.Add(txtMeasure); }
計算兩點之間的距離:其中dpi是縮放比例
/// <summary> /// 求平面中兩點之間距離 /// </summary> /// <param name="p1">點1</param> /// <param name="p2">點2</param> /// <returns></returns> public static double GetDistance(Point p1, Point p2) { double result = 0; result = Math.Sqrt((p1.X * dpiX - p2.X * dpiX) * (p1.X * dpiX - p2.X * dpiX) + (p1.Y * dpiY - p2.Y * dpiY) * (p1.Y * dpiY - p2.Y * dpiY)); return result; }
一些角度類型的測量計算相對復雜一點,需要計算角度大小和繪制弧線
/// <summary> /// 弧線畫筆對象 /// </summary> public Stroke circleStroke; /// <summary> /// 繪制角度弧線 /// </summary> private void DrawAxiesCircle(Point point0, Point point1, Point point2) { if (inkCanvas.Strokes.Count > 0 && circleStroke != null) { if (inkCanvas.Strokes.Contains(circleStroke)) { inkCanvas.Strokes.Remove(circleStroke); } } //計算角度 double a = Math.Sqrt((point1.X - point2.X) * (point1.X - point2.X) + (point1.Y - point2.Y) * (point1.Y - point2.Y)); double b = Math.Sqrt((point1.X - point0.X) * (point1.X - point0.X) + (point1.Y - point0.Y) * (point1.Y - point0.Y)); double c = Math.Sqrt((point2.X - point0.X) * (point2.X - point0.X) + (point2.Y - point0.Y) * (point2.Y - point0.Y)); double cTheta = (a * a + b * b - c * c) / (2 * a * b); double theta = Math.Acos(cTheta) * 180 / Math.PI; //繪制弧線 double r = 30; r = a > b ? b : a; double rMax = a; if (rMax > b) { rMax = b; } if (r > 0.5 * rMax) { r = 0.5 * rMax; } double theta0 = Math.Atan((point1.Y - point2.Y) / (point2.X - point1.X + 1e-10)) * 180 / Math.PI; if (point1.X > point2.X) { theta0 = 180 + theta0; } List<Point> pointList = new List<Point>(); double sin_ab = ((point2.X - point1.X) * (point0.Y - point1.Y) - (point2.Y - point1.Y) * (point0.X - point1.X)) / (a * b); ; if (sin_ab <= 0) { if (theta < 1) { for (double delta = 0.0001; delta <= theta;) { double th = delta + theta0; pointList.Add(new Point(point1.X + r * Math.Cos(th * Math.PI / 180), point1.Y - r * Math.Sin(th * Math.PI / 180))); delta = delta + 0.0001; } } else if (theta > 1 && theta < 20) { for (double delta = 0.01; delta <= theta;) { double th = delta + theta0; pointList.Add(new Point(point1.X + r * Math.Cos(th * Math.PI / 180), point1.Y - r * Math.Sin(th * Math.PI / 180))); delta = delta + 0.01; } } else { for (double delta = 0; delta <= theta; delta++) { double th = delta + theta0; pointList.Add(new Point(point1.X + r * Math.Cos(th * Math.PI / 180), point1.Y - r * Math.Sin(th * Math.PI / 180))); } } } else { if (theta < 1) { for (double delta = -theta; delta <= 0;) { double th = delta + theta0; pointList.Add(new Point(point1.X + r * Math.Cos(th * Math.PI / 180), point1.Y - r * Math.Sin(th * Math.PI / 180))); delta = delta + 0.0001; } } else if (theta > 1 && theta < 20) { for (double delta = -theta; delta <= 0;) { double th = delta + theta0; pointList.Add(new Point(point1.X + r * Math.Cos(th * Math.PI / 180), point1.Y - r * Math.Sin(th * Math.PI / 180))); delta = delta + 0.01; } } else { for (double delta = -theta; delta <= 0; delta++) { double th = delta + theta0; pointList.Add(new Point(point1.X + r * Math.Cos(th * Math.PI / 180), point1.Y - r * Math.Sin(th * Math.PI / 180))); } } } if (pointList.Count > 0) { StylusPointCollection point = new StylusPointCollection(pointList); circleStroke = new Stroke(point) { DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone(), }; inkCanvas.Strokes.Add(circleStroke); txt.Text = theta.ToString(ShapeManager.measureDigit) + "°"; } ReSetAnglePoint(); }
在圓形和矩形的面積測量中,我們可以使用GetArea()方法來獲取圖形面積:
/// <summary> /// 計算面積測量結果 /// </summary> public void CalculateMeasure() { if (!isMeasure) { return; } Ellipse ellipse = (Ellipse)shape; ellipse.UpdateLayout(); txtMeasure.Text = Math.Sqrt(ellipse.RenderedGeometry.GetArea()).ToString(ShapeManager.measureDigit) + "mm²"; ReSetMeasurePoint(); }
其他一些需要注意的細節:
1.當單元格放大縮小時,圖形也要重新計算各個控制點的位置來同步放大或縮小
監聽畫布大小變化事件:
private void ToolInkCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
重新繪制元素:
/// <summary> /// 重新繪制畫布元素大小 /// </summary> public void ReSetShapeSize() { for (int i = 0; i < shapeManager.shapeList.Count; i++) { sizeScaleX = ToolInkCanvas.ActualWidth / shapeManager.shapeList[i].cvsWidth; sizeScaleY = ToolInkCanvas.ActualHeight / shapeManager.shapeList[i].cvsHeight; shapeManager.shapeList[i].cvsWidth = ToolInkCanvas.ActualWidth; shapeManager.shapeList[i].cvsHeight = ToolInkCanvas.ActualHeight; if (shapeManager.shapeList[i] is TextInfo) { shapeManager.shapeList[i].ReSetScalePoints(sizeScaleX, sizeScaleY, "text", shapeManager.shapeList[i]); } else if (shapeManager.shapeList[i] is CrossRulerInfo) { shapeManager.shapeList[i].ReSetScalePoints(sizeScaleX, sizeScaleY, "crossRuler", shapeManager.shapeList[i]); } else if (shapeManager.shapeList[i] is AngleRulerInfo) { shapeManager.shapeList[i].ReSetScalePoints(sizeScaleX, sizeScaleY, "angle", shapeManager.shapeList[i]); } else if (shapeManager.shapeList[i] is TRulerInfo) { shapeManager.shapeList[i].ReSetScalePoints(sizeScaleX, sizeScaleY, "tRulerInfo", shapeManager.shapeList[i]); } else if (shapeManager.shapeList[i] is CobbAngleInfo) { shapeManager.shapeList[i].ReSetScalePoints(sizeScaleX, sizeScaleY, "cobbAngleInfo", shapeManager.shapeList[i]); } else if (shapeManager.shapeList[i] is OpenAngleInfo) { shapeManager.shapeList[i].ReSetScalePoints(sizeScaleX, sizeScaleY, "openAngleInfo", shapeManager.shapeList[i]); } else { shapeManager.shapeList[i].ReSetScalePoints(sizeScaleX, sizeScaleY); } } }
2.文字可能會擋住圖像,要能分離並拖動到其他位置,監聽鼠標事件和重新設置位置即可。
3.標記的復制與粘貼,思路是將List集合中用戶所選定的標記插入到另一個單元格的List集合中,並支持Ctrl C和Ctrl V 快捷鍵。
測量標記匯總(Demo):