Explorar o código

提取选中实体关键点并新增Points依赖属性

在DxfView中新增Points依赖属性(List<PointF>),实现选中圆、直线、多段线、点、圆弧等实体时自动提取其关键点(如圆心、中心点等)并存储。支持属性变更回调,清除选择时同步清空Points,便于后续数据绑定和几何信息处理。
孝锋 徐 hai 8 meses
pai
achega
5940847cbe
Modificáronse 1 ficheiros con 66 adicións e 0 borrados
  1. 66 0
      TeamAAS-VM/DxfModule/DxfView.xaml.cs

+ 66 - 0
TeamAAS-VM/DxfModule/DxfView.xaml.cs

@@ -18,6 +18,7 @@ using WpfLine = System.Windows.Shapes.Line;
 using DxfLine = netDxf.Entities.Line;
 using DxfLwPolyline = netDxf.Entities.Polyline2D;
 using Microsoft.Win32;
+using System.Drawing;
 
 namespace TeamAAS_VP.DxfModule
 {
@@ -37,6 +38,28 @@ namespace TeamAAS_VP.DxfModule
             _dxfParser = new DxfParserService();
             InitializeInteractionController();
         }
+
+        #region 依赖属性-PointF集合
+        public static readonly DependencyProperty PointsProperty =
+            DependencyProperty.Register("Points", typeof(List<PointF>), typeof(DxfView),
+                new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnPointsChanged)));
+        public List<PointF> Points
+        {
+            get { return (List<PointF>)GetValue(PointsProperty); }
+            set { SetValue(PointsProperty, value); }
+        }
+
+        private static void OnPointsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+        {
+            var view = d as DxfView;
+            if (view != null)
+            {
+                // Handle points changed
+            }
+        }
+        #endregion
+
+
         private void InitializeInteractionController()
         {
             _interactionController = new InteractionController(DrawingCanvas);
@@ -179,6 +202,48 @@ namespace TeamAAS_VP.DxfModule
                 {
                     sb.AppendLine($"\n... 还有 {e.SelectedEntities.Count - 10} 个实体");
                 }
+
+                //如果选中的是圆,则输出圆心坐标,如果是直线,则输出中心点坐标,如果是矩形,则输出中心点坐标,如果是点,则输出点坐标,如果是圆弧,则输出中心点坐标
+                if (Points!=null)
+                {
+                    Points = new List<PointF>();
+                }
+                Points.Clear();
+                foreach (var item in e.SelectedEntities)
+                {
+                    if (item is Circle circle)
+                    {
+                        Points.Add(new PointF((float)circle.Center.X, (float)circle.Center.Y));
+                    }
+                    else if (item is DxfLine line)
+                    {
+                        float centerX = (float)((line.StartPoint.X + line.EndPoint.X) / 2);
+                        float centerY = (float)((line.StartPoint.Y + line.EndPoint.Y) / 2);
+                        Points.Add(new PointF(centerX, centerY));
+                    }
+                    else if (item is DxfLwPolyline polyline && polyline.IsClosed)
+                    {
+                        //计算多边形中心点
+                        float centerX = 0;
+                        float centerY = 0;
+                        foreach (var vertex in polyline.Vertexes)
+                        {
+                            centerX += (float)vertex.Position.X;
+                            centerY += (float)vertex.Position.Y;
+                        }
+                        centerX /= polyline.Vertexes.Count;
+                        centerY /= polyline.Vertexes.Count;
+                        Points.Add(new PointF(centerX, centerY));
+                    }
+                    else if (item is  netDxf.Entities.Point point)
+                    {
+                        Points.Add(new PointF((float)point.Position.X, (float)point.Position.Y));
+                    }
+                    else if (item is Arc arc)
+                    {
+                        Points.Add(new PointF((float)arc.Center.X, (float)arc.Center.Y));
+                    }
+                }
             }
             else
             {
@@ -213,6 +278,7 @@ namespace TeamAAS_VP.DxfModule
 
         private void BtnClearSelection_Click(object sender, RoutedEventArgs e)
         {
+            Points?.Clear();
             _interactionController?.ClearSelection();
         }