Browse Source

Initial Dxf stuff

Kenric Nugteren 6 months ago
parent
commit
8bc5c96820
5 changed files with 254 additions and 6 deletions
  1. 59 0
      inabox.dxf/DrawData.cs
  2. 52 6
      inabox.dxf/DxfUtils.cs
  3. 4 0
      inabox.dxf/InABox.Dxf.csproj
  4. 115 0
      inabox.dxf/Objects.cs
  5. 24 0
      inabox.dxf/Utils.cs

+ 59 - 0
inabox.dxf/DrawData.cs

@@ -0,0 +1,59 @@
+using netDxf;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Drawing2D;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace InABox.Dxf;
+
+internal class DrawData
+{
+    public Graphics Graphics { get; set; }
+
+    private Stack<Matrix> MatrixStack = new();
+
+    public void PushTransform()
+    {
+        MatrixStack.Push(Graphics.Transform);
+    }
+
+    public void PopTransform()
+    {
+        Graphics.Transform = MatrixStack.Pop();
+    }
+
+    public void Translate(PointF point)
+    {
+        Graphics.Transform.Translate(point.X, point.Y);
+    }
+
+    public void Rotate(float angle)
+    {
+        Graphics.Transform.Rotate(angle);
+    }
+
+    public void Scale(float scale)
+    {
+        Graphics.Transform.Scale(scale, scale);
+    }
+    public void Scale(float scaleX, float scaleY)
+    {
+        Graphics.Transform.Scale(scaleX, scaleY);
+    }
+
+    public PointF ConvertPoint(Vector3 vec)
+    {
+        var point = new PointF((float)vec.X, (float)vec.Y);
+        return Graphics.Transform.Transform(point);
+    }
+
+    public SizeF ConvertSize(SizeF size)
+    {
+        var point = new PointF(size.Width, size.Height);
+        point = Graphics.Transform.TransformVector(point);
+        return new(point.X, point.Y);
+    }
+}

+ 52 - 6
inabox.dxf/DxfUtils.cs

@@ -62,6 +62,38 @@ namespace InABox.Dxf
 
         public delegate void ProcessError(string message);
 
+        internal static IDxfObject? ConvertEl(EntityObject el)
+        {
+            if(el is Line line)
+            {
+                return new DxfLine { Line = line };
+            }
+            else if(el is Insert insert)
+            {
+                return new DxfInsert(insert);
+            }
+            else if(el is Ellipse ellipse)
+            {
+                return new DxfEllipse(ellipse);
+            }
+            else if(el is MText text)
+            {
+                return new DxfMText(text);
+            }
+            else if(el is Polyline2D ln2D)
+            {
+                return new DxfPolyline2D(ln2D);
+            }
+            else if(el is Dimension dim)
+            {
+                return new DxfDimension(dim);
+            }
+            else
+            {
+                return null;
+            }
+        }
+
         public static Bitmap ProcessImage(Stream stream, string caption = null, bool dimensionmarks = true)
         {         
             var minX = double.MaxValue;
@@ -101,17 +133,29 @@ namespace InABox.Dxf
             var _pen = new Pen(new SolidBrush(Color.Black), 3.0F);
             using (var _graphics = Graphics.FromImage(_result))
             {
-                
+                var data = new DrawData() { Graphics = _graphics };
+
                 Brush _brush = new SolidBrush(Color.White);
                 _graphics.FillRectangle(_brush, 0, 0, _result.Width, _result.Height);
 
+                data.Graphics.ScaleTransform(scale, scale);
+                data.Graphics.TranslateTransform((float)-minX, (float)-minY);
+
+                foreach(var el in document.Entities.All)
+                {
+                    var item = ConvertEl(el);
+                    item?.Draw(data);
+                }
+
+                /*
                 foreach (var line in objects.OfType<Line>())
                 {
                     try
                     {
-                        var _start = VectorToPointF(line.StartPoint, offset, scale, border);
-                        var _end = VectorToPointF(line.EndPoint, offset, scale, border);
-                        _graphics.DrawLine(_pen, _start, _end);
+                        new DxfLine() { Line = line }.Draw(data);
+                        // var _start = VectorToPointF(line.StartPoint, offset, scale, border);
+                        // var _end = VectorToPointF(line.EndPoint, offset, scale, border);
+                        // _graphics.DrawLine(_pen, _start, _end);
                     }
                     catch (Exception e)
                     {
@@ -119,7 +163,9 @@ namespace InABox.Dxf
                     }
 
                 }
+                */
 
+                /*
                 foreach (var text in objects.OfType<MText>().Where(x=>!string.IsNullOrWhiteSpace(x.Value)))
                 {
                     try
@@ -251,6 +297,7 @@ namespace InABox.Dxf
 
                 }
                 
+                */
             }
 
             return _result;
@@ -290,7 +337,6 @@ namespace InABox.Dxf
 
         private static void DimensionToLines(Dimension dimension, List<EntityObject> results, Vector3 offset)
         {
-            //dimension.
             ProcessEntities(dimension.Block.Entities, results, offset);
         }
 
@@ -317,7 +363,7 @@ namespace InABox.Dxf
 
         private static void InsertToLines(Insert insert, List<EntityObject> results, Vector3 offset)
         {
-            ProcessEntities(insert.Block.Entities, results, insert.Position += offset);
+            ProcessEntities(insert.Block.Entities, results, insert.Position + offset);
         }
 
         private static void PolylineToLines(Polyline2D polyline, List<EntityObject> results, Vector3 offset)

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

@@ -24,4 +24,8 @@
       <None Remove=".gitignore" />
     </ItemGroup>
 
+    <ItemGroup>
+      <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
+    </ItemGroup>
+
 </Project>

+ 115 - 0
inabox.dxf/Objects.cs

@@ -0,0 +1,115 @@
+using InABox.Core;
+using netDxf.Entities;
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace InABox.Dxf;
+
+internal interface IDxfObject
+{
+    void Draw(DrawData data);
+}
+
+internal class DxfLine : IDxfObject
+{
+    public Line Line { get; set; }
+
+    public void Draw(DrawData data)
+    {
+        // data.Graphics.DrawLine(new Pen(Line.Color.ToColor(), 1), data.ConvertPoint(Line.StartPoint), data.ConvertPoint(Line.EndPoint));
+        data.Graphics.DrawLine(new Pen(Color.Black, 1), data.ConvertPoint(Line.StartPoint), data.ConvertPoint(Line.EndPoint));
+    }
+}
+
+internal class DxfInsert : IDxfObject
+{
+    public Insert Insert { get; set; }
+
+    public List<IDxfObject> Objects { get; set; }
+
+    public DxfInsert(Insert insert)
+    {
+        Insert = insert;
+        Objects = insert.Block.Entities.Select(DxfUtils.ConvertEl).NotNull().ToList();
+    }
+
+    public void Draw(DrawData data)
+    {
+        data.PushTransform();
+        data.Translate(data.ConvertPoint(Insert.Position));
+        data.Rotate((float)Insert.Rotation);
+        data.Scale((float)Insert.Scale.X, (float)Insert.Scale.Y);
+        foreach(var obj in Objects)
+        {
+            obj.Draw(data);
+        }
+        data.PopTransform();
+    }
+}
+
+internal class DxfEllipse : IDxfObject
+{
+    public Ellipse Ellipse { get; set; }
+
+    public DxfEllipse(Ellipse ellipse)
+    {
+        Ellipse = ellipse;
+    }
+
+    public void Draw(DrawData data)
+    {
+        var center = data.ConvertPoint(Ellipse.Center);
+        var size = data.ConvertSize(new((float)Ellipse.MajorAxis, (float)Ellipse.MinorAxis));
+        data.Graphics.DrawEllipse(new Pen(Color.Black, 1), center.X, center.Y, size.Width, size.Height);
+    }
+}
+
+internal class DxfPolyline2D : IDxfObject
+{
+    public Polyline2D Polyline { get; set; }
+
+    public DxfPolyline2D(Polyline2D polyline)
+    {
+        Polyline = polyline;
+    }
+
+    public void Draw(DrawData data)
+    {
+
+    }
+}
+
+internal class DxfMText : IDxfObject
+{
+    public MText MText { get; set; }
+
+    public DxfMText(MText text)
+    {
+        MText = text;
+    }
+
+    public void Draw(DrawData data)
+    {
+
+    }
+}
+
+internal class DxfDimension : IDxfObject
+{
+    public Dimension Dimension { get; set; }
+
+    public DxfDimension(Dimension dimension)
+    {
+        Dimension = dimension;
+    }
+
+    public void Draw(DrawData data)
+    {
+
+    }
+}

+ 24 - 0
inabox.dxf/Utils.cs

@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Drawing2D;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace InABox.Dxf;
+internal static class Utils
+{
+    public static PointF Transform(this Matrix matrix, PointF point)
+    {
+        var arr = new[] { point };
+        matrix.TransformPoints(arr);
+        return arr[0];
+    }
+    public static PointF TransformVector(this Matrix matrix, PointF point)
+    {
+        var arr = new[] { point };
+        matrix.TransformVectors(arr);
+        return arr[0];
+    }
+}