Kenric Nugteren преди 5 месеца
родител
ревизия
151551280f
променени са 3 файла, в които са добавени 108 реда и са изтрити 4 реда
  1. 88 0
      inabox.dxf/DrawData.cs
  2. 19 4
      inabox.dxf/DxfUtils.cs
  3. 1 0
      inabox.dxf/InABox.Dxf.csproj

+ 88 - 0
inabox.dxf/DrawData.cs

@@ -1,5 +1,7 @@
 using netDxf;
 using netDxf.Tables;
+using Svg;
+using Svg.Transforms;
 using System;
 using System.Collections.Generic;
 using System.Drawing;
@@ -186,6 +188,8 @@ public interface IGraphics
     void SetFont(string fontName, float fontSize, FontStyle fontStyle = FontStyle.Regular);
 
     void DrawText(string text, Color color, PointF position);
+
+    void Clear(Color color);
 }
 
 public class GdiGraphics : IGraphics
@@ -239,4 +243,88 @@ public class GdiGraphics : IGraphics
     {
         Graphics.Transform = transform;
     }
+
+    public void Clear(Color color)
+    {
+        Graphics.Clear(color);
+    }
+}
+
+public class SvgGraphics : IGraphics
+{
+    public SvgDocument Document { get; set; }
+
+    private SvgMatrix _transform;
+
+    private SvgGroup _group;
+
+    public SvgGraphics(SvgDocument document)
+    {
+        Document = document;
+        _group = new SvgGroup();
+    }
+
+    public void SetTransform(Matrix transform)
+    {
+        _transform = new(transform.Elements.ToList());
+    }
+
+    public void DrawLine(Color color, float thickness, params PointF[] points)
+    {
+        if(points.Length == 2)
+        {
+            Document.Children.Add(new SvgLine
+            {
+                StartX = points[0].X,
+                StartY = points[0].Y,
+                EndX = points[1].X,
+                EndY = points[1].Y,
+                Transforms = new()
+                {
+                    _transform
+                },
+                Stroke = new SvgColourServer(color),
+                StrokeWidth = thickness
+            });
+        }
+        else
+        {
+            var line = new SvgPolyline
+            {
+                Transforms = new()
+                {
+                    _transform
+                },
+                Stroke = new SvgColourServer(color),
+                StrokeWidth = thickness
+            };
+            foreach(var point in points)
+            {
+                line.Points.Add(point.X);
+                line.Points.Add(point.Y);
+            }
+
+            Document.Children.Add(line);
+        }
+    }
+
+    public void FillPolygon(Color color, params PointF[] points)
+    {
+    }
+
+    public void SetFont(float fontSize, FontStyle fontStyle = FontStyle.Regular)
+    {
+    }
+
+    public void SetFont(string fontName, float fontSize, FontStyle fontStyle = FontStyle.Regular)
+    {
+    }
+
+    public void DrawText(string text, Color color, PointF position)
+    {
+    }
+
+    public void Clear(Color color)
+    {
+    }
 }

+ 19 - 4
inabox.dxf/DxfUtils.cs

@@ -5,6 +5,7 @@ using netDxf;
 using netDxf.Entities;
 using netDxf.Objects;
 using netDxf.Tables;
+using Svg;
 using Point = System.Drawing.Point;
 
 namespace InABox.Dxf;
@@ -127,7 +128,7 @@ public static class DxfUtils
         }
     }
 
-    public static void DrawDxf(DxfData data, Graphics graphics)
+    public static void DrawDxf(DxfData data, IGraphics graphics, float width, float height)
     {
         // Calculate the scaling factor to fit the image within the bounds
         float ratioX = (float)data.Settings.ImageSize.Width / data.Size.Width;
@@ -137,8 +138,7 @@ public static class DxfUtils
 
         var drawData = new DrawData() { Graphics = graphics, Data = data };
 
-        Brush _brush = new SolidBrush(Color.White);
-        graphics.FillRectangle(_brush, 0, 0, graphics.VisibleClipBounds.Width, graphics.VisibleClipBounds.Height);
+        graphics.Clear(Color.White);
 
         // drawData.Translate(graphics.VisibleClipBounds.Width / 2, graphics.VisibleClipBounds.Height / 2);
 
@@ -205,13 +205,28 @@ public static class DxfUtils
         using (var _graphics = Graphics.FromImage(_result))
         {
             _graphics.SmoothingMode = SmoothingMode.AntiAlias;
-            DrawDxf(data, _graphics);
+            DrawDxf(data, new GdiGraphics(_graphics), _result.Width, _result.Height);
         }
         _result.RotateFlip(RotateFlipType.RotateNoneFlipY);
 
         return _result;
     }
 
+    public static SvgDocument ProcessSvg(DxfData data)
+    {
+        var doc = new SvgDocument
+        {
+            ViewBox = new(data.Origin.X, data.Origin.Y, data.Size.Width, data.Size.Height)
+        };
+        var drawData = new DrawData() { Graphics = new SvgGraphics(doc), Data = data };
+        foreach(var el in data.Document.Entities.All)
+        {
+            var item = ConvertEl(el);
+            item?.Draw(drawData);
+        }
+        return doc;
+    }
+
     public static Bitmap ProcessImage(Stream stream, DxfImportSettings? settings = null)
     {
         return ProcessImage(LoadDxf(stream, settings));

+ 1 - 0
inabox.dxf/InABox.Dxf.csproj

@@ -8,6 +8,7 @@
 
     <ItemGroup>
         <PackageReference Include="netDxf.netstandard" Version="3.0.1" />
+        <PackageReference Include="Svg" Version="3.4.7" />
         <PackageReference Include="System.Drawing.Common" Version="8.0.6" />
     </ItemGroup>