| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- using netDxf.Entities;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media;
- using WpfShapes = System.Windows.Shapes;
- using DxfLine = netDxf.Entities.Line;
- using WpfLine = System.Windows.Shapes.Line;
- using DxfEllipse = netDxf.Entities.Ellipse;
- using WpfEllipse = System.Windows.Shapes.Ellipse;
- using WpfPoint = System.Windows.Point;
- namespace TeamAAS_VP.DxfModule
- {
- public class GraphicsRenderer
- {
- private const double DefaultStrokeThickness = 1.0;
- public static List<UIElement> RenderEntity(EntityObject entity, double scale = 1.0)
- {
- var elements = new List<UIElement>();
- switch (entity)
- {
- case DxfLine line:
- elements.Add(RenderLine(line, scale));
- break;
- case Circle circle:
- elements.Add(RenderCircle(circle, scale));
- break;
- case Arc arc:
- elements.Add(RenderArc(arc, scale));
- break;
- case netDxf.Entities.Point point:
- elements.Add(RenderPoint(point, scale));
- break;
- case Text text:
- elements.Add(RenderText(text, scale));
- break;
- case MText mtext:
- elements.Add(RenderMText(mtext, scale));
- break;
- case Spline spline:
- elements.AddRange(RenderSpline(spline, scale));
- break;
- }
- return elements;
- }
- private static UIElement RenderLine(DxfLine line, double scale)
- {
- var wpfLine = new WpfLine
- {
- X1 = line.StartPoint.X * scale,
- Y1 = -line.StartPoint.Y * scale, // Flip Y axis
- X2 = line.EndPoint.X * scale,
- Y2 = -line.EndPoint.Y * scale,
- Stroke = new SolidColorBrush(GetColor(line.Color)),
- StrokeThickness = DefaultStrokeThickness,
- Tag = line // Store original entity
- };
- return wpfLine;
- }
- private static UIElement RenderCircle(Circle circle, double scale)
- {
- var wpfEllipse = new WpfEllipse
- {
- Width = circle.Radius * 2 * scale,
- Height = circle.Radius * 2 * scale,
- Stroke = new SolidColorBrush(GetColor(circle.Color)),
- StrokeThickness = DefaultStrokeThickness,
- Fill = Brushes.Transparent,
- Tag = circle
- };
- System.Windows.Controls.Canvas.SetLeft(wpfEllipse, (circle.Center.X - circle.Radius) * scale);
- System.Windows.Controls.Canvas.SetTop(wpfEllipse, (-circle.Center.Y - circle.Radius) * scale);
- return wpfEllipse;
- }
- private static UIElement RenderArc(Arc arc, double scale)
- {
- var path = new WpfShapes.Path
- {
- Stroke = new SolidColorBrush(GetColor(arc.Color)),
- StrokeThickness = DefaultStrokeThickness,
- Fill = Brushes.Transparent,
- Tag = arc
- };
- var geometry = new PathGeometry();
- var figure = new PathFigure();
- // Calculate start and end points
- double startAngleRad = arc.StartAngle * Math.PI / 180.0;
- double endAngleRad = arc.EndAngle * Math.PI / 180.0;
- double startX = (arc.Center.X + arc.Radius * Math.Cos(startAngleRad)) * scale;
- double startY = (-arc.Center.Y - arc.Radius * Math.Sin(startAngleRad)) * scale;
- double endX = (arc.Center.X + arc.Radius * Math.Cos(endAngleRad)) * scale;
- double endY = (-arc.Center.Y - arc.Radius * Math.Sin(endAngleRad)) * scale;
- figure.StartPoint = new WpfPoint(startX, startY);
- var arcSegment = new ArcSegment
- {
- Point = new WpfPoint(endX, endY),
- Size = new System.Windows.Size(arc.Radius * scale, arc.Radius * scale),
- SweepDirection = SweepDirection.Counterclockwise,
- IsLargeArc = Math.Abs(arc.EndAngle - arc.StartAngle) > 180
- };
- figure.Segments.Add(arcSegment);
- geometry.Figures.Add(figure);
- path.Data = geometry;
- return path;
- }
- private static List<UIElement> RenderSpline(Spline spline, double scale)
- {
- var elements = new List<UIElement>();
- // Approximate spline with lines using control points
- var controlPoints = new List<netDxf.Vector3>(spline.ControlPoints);
- if (controlPoints.Count < 2)
- return elements;
- for (int i = 0; i < controlPoints.Count - 1; i++)
- {
- var p1 = controlPoints[i];
- var p2 = controlPoints[i + 1];
- var line = new WpfLine
- {
- X1 = p1.X * scale,
- Y1 = -p1.Y * scale,
- X2 = p2.X * scale,
- Y2 = -p2.Y * scale,
- Stroke = new SolidColorBrush(GetColor(spline.Color)),
- StrokeThickness = DefaultStrokeThickness,
- Tag = spline
- };
- elements.Add(line);
- }
- return elements;
- }
- private static UIElement RenderPoint(netDxf.Entities.Point point, double scale)
- {
- var ellipse = new WpfEllipse
- {
- Width = 3,
- Height = 3,
- Fill = new SolidColorBrush(GetColor(point.Color)),
- Tag = point
- };
- System.Windows.Controls.Canvas.SetLeft(ellipse, point.Position.X * scale - 1.5);
- System.Windows.Controls.Canvas.SetTop(ellipse, -point.Position.Y * scale - 1.5);
- return ellipse;
- }
- private static UIElement RenderText(Text text, double scale)
- {
- var textBlock = new System.Windows.Controls.TextBlock
- {
- Text = text.Value,
- Foreground = new SolidColorBrush(GetColor(text.Color)),
- FontSize = text.Height * scale,
- Tag = text
- };
- System.Windows.Controls.Canvas.SetLeft(textBlock, text.Position.X * scale);
- System.Windows.Controls.Canvas.SetTop(textBlock, -text.Position.Y * scale);
- return textBlock;
- }
- private static UIElement RenderMText(MText mtext, double scale)
- {
- var textBlock = new System.Windows.Controls.TextBlock
- {
- Text = mtext.Value,
- Foreground = new SolidColorBrush(GetColor(mtext.Color)),
- FontSize = mtext.Height * scale,
- Tag = mtext
- };
- System.Windows.Controls.Canvas.SetLeft(textBlock, mtext.Position.X * scale);
- System.Windows.Controls.Canvas.SetTop(textBlock, -mtext.Position.Y * scale);
- return textBlock;
- }
- private static Color GetColor(netDxf.AciColor aciColor)
- {
- // Convert DXF AciColor to WPF Color
- if (aciColor.IsByLayer || aciColor.IsByBlock)
- return Colors.White;
- var rgb = aciColor.ToColor();
- return Color.FromRgb(rgb.R, rgb.G, rgb.B);
- }
- public static void HighlightElement(UIElement element, bool isSelected)
- {
- if (element is WpfShapes.Shape shape)
- {
- if (isSelected)
- {
- shape.Stroke = Brushes.Yellow;
- shape.StrokeThickness = 2.0;
- }
- else
- {
- // Restore original color from entity
- if (shape.Tag is EntityObject entity)
- {
- shape.Stroke = new SolidColorBrush(GetColor(entity.Color));
- shape.StrokeThickness = DefaultStrokeThickness;
- }
- }
- }
- else if (element is System.Windows.Controls.TextBlock textBlock)
- {
- if (isSelected)
- {
- textBlock.Foreground = Brushes.Yellow;
- }
- else
- {
- if (textBlock.Tag is Text text)
- {
- textBlock.Foreground = new SolidColorBrush(GetColor(text.Color));
- }
- else if (textBlock.Tag is MText mtext)
- {
- textBlock.Foreground = new SolidColorBrush(GetColor(mtext.Color));
- }
- }
- }
- }
- }
- }
|