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 RenderEntity(EntityObject entity, double scale = 1.0) { var elements = new List(); 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 RenderSpline(Spline spline, double scale) { var elements = new List(); // Approximate spline with lines using control points var controlPoints = new List(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)); } } } } } }