Ver código fonte

Lots of dxf stuff

Kenric Nugteren 6 meses atrás
pai
commit
41c941ebe5

+ 5 - 0
InABox.Core/Utils/Result.cs

@@ -175,6 +175,11 @@ namespace InABox.Core
             error = _error;
             return _status == Status.Ok;
         }
+        public bool GetOk([NotNullWhen(true)][MaybeNull] out T value)
+        {
+            value = _value;
+            return _status == Status.Ok;
+        }
 
         public Result<U, E> MapOk<U>(Func<T, U> mapFunc)
         {

+ 24 - 11
inabox.dxf/DrawData.cs

@@ -1,4 +1,5 @@
 using netDxf;
+using netDxf.Tables;
 using System;
 using System.Collections.Generic;
 using System.Drawing;
@@ -15,6 +16,21 @@ internal class DrawData
 
     private Stack<Matrix> MatrixStack = new();
 
+    public HashSet<string>? Layers { get; set; }
+
+    public bool HasLayer(Layer layer)
+    {
+        return Layers is null || Layers.Contains(layer.Name);
+    }
+    public void SetLayers(params string[] layers)
+    {
+        Layers = layers.ToHashSet();
+    }
+    public void SetLayers(IEnumerable<string> layers)
+    {
+        Layers = layers.ToHashSet();
+    }
+
     public void PushTransform()
     {
         MatrixStack.Push(Graphics.Transform);
@@ -27,33 +43,30 @@ internal class DrawData
 
     public void Translate(PointF point)
     {
-        Graphics.Transform.Translate(point.X, point.Y);
+        Graphics.TranslateTransform(point.X, point.Y);
     }
 
     public void Rotate(float angle)
     {
-        Graphics.Transform.Rotate(angle);
+        Graphics.RotateTransform(angle);
     }
 
     public void Scale(float scale)
     {
-        Graphics.Transform.Scale(scale, scale);
+        Graphics.ScaleTransform(scale, scale);
     }
     public void Scale(float scaleX, float scaleY)
     {
-        Graphics.Transform.Scale(scaleX, scaleY);
+        Graphics.ScaleTransform(scaleX, scaleY);
     }
 
-    public PointF ConvertPoint(Vector3 vec)
+    public static PointF ConvertPoint(Vector2 vec)
     {
-        var point = new PointF((float)vec.X, (float)vec.Y);
-        return Graphics.Transform.Transform(point);
+        return new PointF((float)vec.X, (float)vec.Y);
     }
 
-    public SizeF ConvertSize(SizeF size)
+    public static PointF ConvertPoint(Vector3 vec)
     {
-        var point = new PointF(size.Width, size.Height);
-        point = Graphics.Transform.TransformVector(point);
-        return new(point.X, point.Y);
+        return new PointF((float)vec.X, (float)vec.Y);
     }
 }

+ 350 - 319
inabox.dxf/DxfUtils.cs

@@ -1,397 +1,428 @@
 using System.Drawing;
 using System.Drawing.Drawing2D;
+using InABox.Core;
 using netDxf;
 using netDxf.Entities;
 using Point = System.Drawing.Point;
 
-namespace InABox.Dxf
+namespace InABox.Dxf;
+
+public class DxfImportSettings
 {
-    public static class DxfUtils
+    public Size ImageSize;
+
+    public string[]? SupportFolders;
+
+    public DxfImportSettings(Size? imageSize = null, string[]? supportFolders = null)
     {
-        public static event ProcessError OnProcessError;
+        ImageSize = imageSize ?? new(2048, 2048);
+        SupportFolders = supportFolders;
+    }
+}
 
-        // Draw a rotated string at a particular position.
-        private static void DrawRotatedTextAt(Graphics gr, float angle,
-            string txt, float x, float y, Font the_font, Brush the_brush)
-        {
-            // Save the graphics state.
-            var state = gr.Save();
-            gr.ResetTransform();
+public static class DxfUtils
+{
+    public static event ProcessError OnProcessError;
 
-            // Rotate.
-            gr.RotateTransform(angle);
+    // Draw a rotated string at a particular position.
+    private static void DrawRotatedTextAt(Graphics gr, float angle,
+        string txt, float x, float y, Font the_font, Brush the_brush)
+    {
+        // Save the graphics state.
+        var state = gr.Save();
+        gr.ResetTransform();
 
-            // Translate to desired position. Be sure to append
-            // the rotation so it occurs after the rotation.
-            gr.TranslateTransform(x, y, MatrixOrder.Append);
+        // Rotate.
+        gr.RotateTransform(angle);
 
-            // Draw the text at the origin.
-            gr.DrawString(txt, the_font, the_brush, 0, 0);
+        // Translate to desired position. Be sure to append
+        // the rotation so it occurs after the rotation.
+        gr.TranslateTransform(x, y, MatrixOrder.Append);
 
-            // Restore the graphics state.
-            gr.Restore(state);
-        }
+        // Draw the text at the origin.
+        gr.DrawString(txt, the_font, the_brush, 0, 0);
+
+        // Restore the graphics state.
+        gr.Restore(state);
+    }
 
-        private static void CheckPoints(double x, double y, ref double minX, ref double minY, ref double maxX, ref double maxY)
+    private static void CheckPoints(double x, double y, ref double minX, ref double minY, ref double maxX, ref double maxY)
+    {
+        minX = x < minX ? x : minX;
+        minY = y < minY ? y : minY;
+        maxX = x > maxX ? x : maxX;
+        maxY = y > maxY ? y : maxY;
+    }
+
+    private static float Scale(int value, float offset, double scale, int border)
+    {
+        return (value - offset) * (float)scale + border;
+    }
+
+    private static float Scale(double value, float offset, double scale, int border)
+    {
+        return ((float)value - offset) * (float)scale + border;
+    }
+
+    private static PointF VectorToPointF(Vector3 vector, PointF offset, float scale, Point border)
+    {
+        return new PointF(Scale(vector.X, offset.X, scale, border.X), Scale(vector.Y, offset.Y, scale, border.Y));
+    }
+
+    private static PointF VectorToPointF(Vector2 vector, PointF offset, int scale, Point border)
+    {
+        return new PointF(Scale(vector.X, offset.X, scale, border.X), Scale(vector.Y, offset.Y, scale, border.Y));
+    }
+
+    public delegate void ProcessError(string message);
+
+    internal static IDxfObject? ConvertEl(EntityObject el)
+    {
+        if(el is Line line)
         {
-            minX = x < minX ? x : minX;
-            minY = y < minY ? y : minY;
-            maxX = x > maxX ? x : maxX;
-            maxY = y > maxY ? y : maxY;
+            return new DxfLine { Line = line };
         }
-
-        private static float Scale(int value, float offset, double scale, int border)
+        else if(el is Insert insert)
         {
-            return (value - offset) * (float)scale + border;
+            return new DxfInsert(insert);
         }
-
-        private static float Scale(double value, float offset, double scale, int border)
+        else if(el is Ellipse ellipse)
         {
-            return ((float)value - offset) * (float)scale + border;
+            return new DxfEllipse(ellipse);
         }
-
-        private static PointF VectorToPointF(Vector3 vector, PointF offset, float scale, Point border)
+        else if(el is MText text)
         {
-            return new PointF(Scale(vector.X, offset.X, scale, border.X), Scale(vector.Y, offset.Y, scale, border.Y));
+            return new DxfMText(text);
         }
-
-        private static PointF VectorToPointF(Vector2 vector, PointF offset, int scale, Point border)
+        else if(el is Polyline2D ln2D)
+        {
+            return new DxfPolyline2D(ln2D);
+        }
+        else if(el is Dimension dim)
+        {
+            return new DxfDimension(dim);
+        }
+        else
         {
-            return new PointF(Scale(vector.X, offset.X, scale, border.X), Scale(vector.Y, offset.Y, scale, border.Y));
+            return null;
         }
+    }
+
+    public static Bitmap ProcessImage(Stream stream, string caption = null, bool dimensionmarks = true, DxfImportSettings? settings = null)
+    {
+        settings ??= new();
+
+        var minX = double.MaxValue;
+        var minY = double.MaxValue;
+        var maxX = double.MinValue;
+        var maxY = double.MinValue;
 
-        public delegate void ProcessError(string message);
+        var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
 
-        internal static IDxfObject? ConvertEl(EntityObject el)
+        // Lets Explode all the various bits into their component atoms 
+        // (i.e. everything is gonna be a line)
+        List<EntityObject> objects = new();
+        ProcessEntities(document.Entities.All, objects, new Vector3());
+
+        if (!objects.Any())
+            throw new Exception(string.Format("No Data Found in {0}!", caption));
+
+        foreach (var line in objects.OfType<Line>())
         {
-            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;
-            }
+            CheckPoints(line.StartPoint.X, line.StartPoint.Y, ref minX, ref minY, ref maxX, ref maxY);
+            CheckPoints(line.EndPoint.X, line.EndPoint.Y, ref minX, ref minY, ref maxX, ref maxY);
         }
 
-        public static Bitmap ProcessImage(Stream stream, string caption = null, bool dimensionmarks = true)
-        {         
-            var minX = double.MaxValue;
-            var minY = double.MaxValue;
-            var maxX = double.MinValue;
-            var maxY = double.MinValue;
+        var height = (int)Math.Abs(maxY - minY);
+        var width = (int)Math.Abs(maxX - minX);
+        
+        // Calculate the scaling factor to fit the image within the bounds
+        float ratioX = (float)settings.ImageSize.Width / height;
+        float ratioY = (float)settings.ImageSize.Height / width;
+
+        var scale = Math.Min(ratioX, ratioY);
+        var margin = 100;
+        var offset = new PointF((float)minX, (float)minY);
+        var border = new Point(margin, margin);
+
+        var _result = new Bitmap((int)(width * scale) + (margin * 2), (int)(height * scale) + (margin * 2));
+        var _pen = new Pen(new SolidBrush(Color.Black), 3.0F);
+        using (var _graphics = Graphics.FromImage(_result))
+        {
+            _graphics.SmoothingMode = SmoothingMode.AntiAlias;
+            var data = new DrawData() { Graphics = _graphics };
 
-            var document = DxfDocument.Load(stream);
+            data.SetLayers("Swipe Lines");
 
-            // Lets Explode all the various bits into their component atoms 
-            // (i.e. everything is gonna be a line)
-            List<EntityObject> objects = new();
-            ProcessEntities(document.Entities.All, objects, new Vector3());
+            Brush _brush = new SolidBrush(Color.White);
+            _graphics.FillRectangle(_brush, 0, 0, _result.Width, _result.Height);
 
-            if (!objects.Any())
-                throw new Exception(string.Format("No Data Found in {0}!", caption));
+            data.Graphics.ScaleTransform(0.5f, 0.5f);
+            data.Graphics.TranslateTransform(_result.Width / 2, _result.Height / 2);
 
-            foreach (var line in objects.OfType<Line>())
+            data.Graphics.ScaleTransform(scale, scale);
+            data.Graphics.TranslateTransform((float)-minX, (float)-minY);
+
+            // foreach(var block in document.Blocks)
+            // {
+            //     foreach(var el in block.Entities)
+            //     {
+            //         var item = ConvertEl(el);
+            //         item?.Draw(data);
+            //     }
+            // }
+            foreach(var el in document.Entities.All)
             {
-                CheckPoints(line.StartPoint.X, line.StartPoint.Y, ref minX, ref minY, ref maxX, ref maxY);
-                CheckPoints(line.EndPoint.X, line.EndPoint.Y, ref minX, ref minY, ref maxX, ref maxY);
+                var item = ConvertEl(el);
+                item?.Draw(data);
             }
 
-            var height = (int)Math.Abs(maxY - minY);
-            var width = (int)Math.Abs(maxX - minX);
-            
-            // Calculate the scaling factor to fit the image within the bounds
-            float ratioX = (float)2048 / height;
-            float ratioY = (float)2048 / width;
-
-            var scale = Math.Min(ratioX, ratioY);
-            var margin = 100;
-            var offset = new PointF((float)minX, (float)minY);
-            var border = new Point(margin, margin);
-
-            var _result = new Bitmap((int)(width * scale) + (margin * 2), (int)(height * scale) + (margin * 2));
-            var _pen = new Pen(new SolidBrush(Color.Black), 3.0F);
-            using (var _graphics = Graphics.FromImage(_result))
+            /*
+            foreach (var line in objects.OfType<Line>())
             {
-                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)
+                try
                 {
-                    var item = ConvertEl(el);
-                    item?.Draw(data);
+                    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);
                 }
-
-                /*
-                foreach (var line in objects.OfType<Line>())
+                catch (Exception e)
                 {
-                    try
-                    {
-                        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)
-                    {
-                        
-                    }
-
+                    
                 }
-                */
 
-                /*
-                foreach (var text in objects.OfType<MText>().Where(x=>!string.IsNullOrWhiteSpace(x.Value)))
+            }
+            */
+
+            /*
+            foreach (var text in objects.OfType<MText>().Where(x=>!string.IsNullOrWhiteSpace(x.Value)))
+            {
+                try
                 {
-                    try
-                    {
-                        
-                        var font = new Font(text.Style.FontFamilyName, 12.0f, (FontStyle)text.Style.FontStyle);
-                        var dimensions = _graphics.MeasureString(text.Value, font);
-
-                        var anchor = VectorToPointF(text.Position, offset, scale, border);
-                        
-                        var textwidth = Scale(text.RectangleWidth, 0, scale, 0);
-                        float textRatioX = text.RectangleWidth == 0 ? int.MaxValue : textwidth / dimensions.Width;
-
-                        var textheight = Scale(text.Height, 0, scale, 0);
-                        float textRatioY =  textheight / dimensions.Height;
-                        
-                        var textScale = Math.Min(textRatioX, textRatioY);
-                        
-                        font = new Font(text.Style.FontFamilyName, 12.0f * textScale, (FontStyle)text.Style.FontStyle);
-
-                        var topleft = (text.AttachmentPoint) switch
-                        {
-                            MTextAttachmentPoint.TopLeft => anchor,
-                            MTextAttachmentPoint.TopCenter => new PointF(anchor.X - (dimensions.Width / 2.0f), anchor.Y),
-                            MTextAttachmentPoint.TopRight => new PointF(anchor.X - dimensions.Width, anchor.Y),
-                            MTextAttachmentPoint.MiddleLeft => new PointF(anchor.X, anchor.Y - (textheight / 2.0f)),
-                            MTextAttachmentPoint.MiddleCenter => new PointF(anchor.X - (dimensions.Width / 2.0f), anchor.Y- (textheight / 2.0f)),
-                            MTextAttachmentPoint.MiddleRight => new PointF(anchor.X - dimensions.Width, anchor.Y- (textheight / 2.0f)),
-                            MTextAttachmentPoint.BottomLeft => anchor,
-                            MTextAttachmentPoint.BottomCenter => new PointF(anchor.X - (dimensions.Width / 2.0f), anchor.Y - textheight),
-                            MTextAttachmentPoint.BottomRight => new PointF(anchor.X - dimensions.Width, anchor.Y - textheight),
-
-                        };
-                        //var rectangle = new RectangleF(topleft, new SizeF(dimensions.Width, textheight));
-                        //_graphics.FillRectangle(new SolidBrush(Color.Yellow), rectangle);
-                        
-                        _graphics.DrawString(text.Value, font, new SolidBrush(Color.Navy),topleft);
-                    }
-                    catch (Exception e)
+                    
+                    var font = new Font(text.Style.FontFamilyName, 12.0f, (FontStyle)text.Style.FontStyle);
+                    var dimensions = _graphics.MeasureString(text.Value, font);
+
+                    var anchor = VectorToPointF(text.Position, offset, scale, border);
+                    
+                    var textwidth = Scale(text.RectangleWidth, 0, scale, 0);
+                    float textRatioX = text.RectangleWidth == 0 ? int.MaxValue : textwidth / dimensions.Width;
+
+                    var textheight = Scale(text.Height, 0, scale, 0);
+                    float textRatioY =  textheight / dimensions.Height;
+                    
+                    var textScale = Math.Min(textRatioX, textRatioY);
+                    
+                    font = new Font(text.Style.FontFamilyName, 12.0f * textScale, (FontStyle)text.Style.FontStyle);
+
+                    var topleft = (text.AttachmentPoint) switch
                     {
-                    }
+                        MTextAttachmentPoint.TopLeft => anchor,
+                        MTextAttachmentPoint.TopCenter => new PointF(anchor.X - (dimensions.Width / 2.0f), anchor.Y),
+                        MTextAttachmentPoint.TopRight => new PointF(anchor.X - dimensions.Width, anchor.Y),
+                        MTextAttachmentPoint.MiddleLeft => new PointF(anchor.X, anchor.Y - (textheight / 2.0f)),
+                        MTextAttachmentPoint.MiddleCenter => new PointF(anchor.X - (dimensions.Width / 2.0f), anchor.Y- (textheight / 2.0f)),
+                        MTextAttachmentPoint.MiddleRight => new PointF(anchor.X - dimensions.Width, anchor.Y- (textheight / 2.0f)),
+                        MTextAttachmentPoint.BottomLeft => anchor,
+                        MTextAttachmentPoint.BottomCenter => new PointF(anchor.X - (dimensions.Width / 2.0f), anchor.Y - textheight),
+                        MTextAttachmentPoint.BottomRight => new PointF(anchor.X - dimensions.Width, anchor.Y - textheight),
+
+                    };
+                    //var rectangle = new RectangleF(topleft, new SizeF(dimensions.Width, textheight));
+                    //_graphics.FillRectangle(new SolidBrush(Color.Yellow), rectangle);
                     
+                    _graphics.DrawString(text.Value, font, new SolidBrush(Color.Navy),topleft);
+                }
+                catch (Exception e)
+                {
                 }
                 
-                try
+            }
+            
+            try
+            {
+                var font = new Font("Arial", 20.0F);
+                var brush = new SolidBrush(Color.Blue);
+                _pen = new Pen(brush);
+
+                if (!string.IsNullOrWhiteSpace(caption))
                 {
-                    var font = new Font("Arial", 20.0F);
-                    var brush = new SolidBrush(Color.Blue);
-                    _pen = new Pen(brush);
+                    var crect = _graphics.MeasureString(caption, font);
+                    _graphics.DrawString(caption, font, brush,
+                        new PointF(_result.Width / 2.0F - crect.Width / 2.0F, 10));
+                }
 
-                    if (!string.IsNullOrWhiteSpace(caption))
-                    {
-                        var crect = _graphics.MeasureString(caption, font);
-                        _graphics.DrawString(caption, font, brush,
-                            new PointF(_result.Width / 2.0F - crect.Width / 2.0F, 10));
-                    }
+                if (dimensionmarks)
+                {
 
-                    if (dimensionmarks)
+                    var heightrect = _graphics.MeasureString(height.ToString(), font);
+                    _graphics.DrawLine(_pen, new PointF(_result.Width - (heightrect.Height + 10.0F), 100),
+                        new PointF(_result.Width - 30, 100));
+                    _graphics.DrawLine(_pen,
+                        new PointF(_result.Width - (heightrect.Height + 10.0F), _result.Height - 100),
+                        new PointF(_result.Width - 30, _result.Height - 100));
+                    _graphics.DrawLine(_pen, new PointF(_result.Width - (20.0F + heightrect.Height / 2.0F), 105),
+                        new PointF(_result.Width - (20.0F + heightrect.Height / 2.0F), _result.Height - 105));
+
+                    var aX = _result.Width - (20.0F + heightrect.Height / 2.0F);
+                    float aY = 105;
+                    _graphics.FillPolygon(brush, new[]
                     {
+                        new(aX, aY),
+                        new PointF(aX - 10.0F, aY + 10.0F),
+                        new PointF(aX + 10.0F, aY + 10.0F)
+                    });
 
-                        var heightrect = _graphics.MeasureString(height.ToString(), font);
-                        _graphics.DrawLine(_pen, new PointF(_result.Width - (heightrect.Height + 10.0F), 100),
-                            new PointF(_result.Width - 30, 100));
-                        _graphics.DrawLine(_pen,
-                            new PointF(_result.Width - (heightrect.Height + 10.0F), _result.Height - 100),
-                            new PointF(_result.Width - 30, _result.Height - 100));
-                        _graphics.DrawLine(_pen, new PointF(_result.Width - (20.0F + heightrect.Height / 2.0F), 105),
-                            new PointF(_result.Width - (20.0F + heightrect.Height / 2.0F), _result.Height - 105));
-
-                        var aX = _result.Width - (20.0F + heightrect.Height / 2.0F);
-                        float aY = 105;
-                        _graphics.FillPolygon(brush, new[]
-                        {
-                            new(aX, aY),
-                            new PointF(aX - 10.0F, aY + 10.0F),
-                            new PointF(aX + 10.0F, aY + 10.0F)
-                        });
-
-                        aY = _result.Height - 105;
-                        _graphics.FillPolygon(brush, new[]
-                        {
-                            new(aX, aY),
-                            new PointF(aX - 10.0F, aY - 10.0F),
-                            new PointF(aX + 10.0F, aY - 10.0F)
-                        });
-                        _graphics.FillRectangle(_brush, _result.Width - (heightrect.Height + 15.0F),
-                            _result.Height / 2.0F - heightrect.Width / 2.0F, heightrect.Height,
-                            heightrect.Width);
-                        DrawRotatedTextAt(_graphics, 270.0F, height.ToString(),
-                            _result.Width - (heightrect.Height + 15.0F),
-                            _result.Height / 2.0F + heightrect.Width / 2.0F, font, brush);
-
-                        var widthrect = _graphics.MeasureString(width.ToString(), font);
-                        _graphics.DrawLine(_pen, new PointF(100, _result.Height - (widthrect.Height + 10.0F)),
-                            new PointF(100, _result.Height - 30));
-                        _graphics.DrawLine(_pen,
-                            new PointF(_result.Width - 100, _result.Height - (widthrect.Height + 10.0F)),
-                            new PointF(_result.Width - 100, _result.Height - 30));
-                        _graphics.DrawLine(_pen, new PointF(105, _result.Height - (20.0F + widthrect.Height / 2.0F)),
-                            new PointF(_result.Width - 105, _result.Height - (20.0F + widthrect.Height / 2.0F)));
-
-                        aX = 105;
-                        aY = _result.Height - (20.0F + widthrect.Height / 2.0F);
-                        _graphics.FillPolygon(brush, new[]
-                        {
-                            new(aX, aY),
-                            new PointF(aX + 10.0F, aY - 10.0F),
-                            new PointF(aX + 10.0F, aY + 10.0F)
-                        });
-
-                        aX = _result.Width - 105;
-                        _graphics.FillPolygon(brush, new[]
-                        {
-                            new(aX, aY),
-                            new PointF(aX - 10.0F, aY - 10.0F),
-                            new PointF(aX - 10.0F, aY + 10.0F)
-                        });
-
-                        _graphics.FillRectangle(_brush, _result.Width / 2.0F - widthrect.Width / 2.0F,
-                            _result.Height - (widthrect.Height + 15.0F), widthrect.Width,
-                            widthrect.Height);
-                        _graphics.DrawString(width.ToString(), font, brush,
-                            new PointF(_result.Width / 2.0F - widthrect.Width / 2.0F,
-                                _result.Height - (widthrect.Height + 15.0F)));
-                    }
-                }
-                catch (Exception e)
-                {
+                    aY = _result.Height - 105;
+                    _graphics.FillPolygon(brush, new[]
+                    {
+                        new(aX, aY),
+                        new PointF(aX - 10.0F, aY - 10.0F),
+                        new PointF(aX + 10.0F, aY - 10.0F)
+                    });
+                    _graphics.FillRectangle(_brush, _result.Width - (heightrect.Height + 15.0F),
+                        _result.Height / 2.0F - heightrect.Width / 2.0F, heightrect.Height,
+                        heightrect.Width);
+                    DrawRotatedTextAt(_graphics, 270.0F, height.ToString(),
+                        _result.Width - (heightrect.Height + 15.0F),
+                        _result.Height / 2.0F + heightrect.Width / 2.0F, font, brush);
+
+                    var widthrect = _graphics.MeasureString(width.ToString(), font);
+                    _graphics.DrawLine(_pen, new PointF(100, _result.Height - (widthrect.Height + 10.0F)),
+                        new PointF(100, _result.Height - 30));
+                    _graphics.DrawLine(_pen,
+                        new PointF(_result.Width - 100, _result.Height - (widthrect.Height + 10.0F)),
+                        new PointF(_result.Width - 100, _result.Height - 30));
+                    _graphics.DrawLine(_pen, new PointF(105, _result.Height - (20.0F + widthrect.Height / 2.0F)),
+                        new PointF(_result.Width - 105, _result.Height - (20.0F + widthrect.Height / 2.0F)));
+
+                    aX = 105;
+                    aY = _result.Height - (20.0F + widthrect.Height / 2.0F);
+                    _graphics.FillPolygon(brush, new[]
+                    {
+                        new(aX, aY),
+                        new PointF(aX + 10.0F, aY - 10.0F),
+                        new PointF(aX + 10.0F, aY + 10.0F)
+                    });
 
+                    aX = _result.Width - 105;
+                    _graphics.FillPolygon(brush, new[]
+                    {
+                        new(aX, aY),
+                        new PointF(aX - 10.0F, aY - 10.0F),
+                        new PointF(aX - 10.0F, aY + 10.0F)
+                    });
+
+                    _graphics.FillRectangle(_brush, _result.Width / 2.0F - widthrect.Width / 2.0F,
+                        _result.Height - (widthrect.Height + 15.0F), widthrect.Width,
+                        widthrect.Height);
+                    _graphics.DrawString(width.ToString(), font, brush,
+                        new PointF(_result.Width / 2.0F - widthrect.Width / 2.0F,
+                            _result.Height - (widthrect.Height + 15.0F)));
                 }
-                
-                */
             }
+            catch (Exception e)
+            {
 
-            return _result;
+            }
+            
+            */
         }
 
-        private static void ProcessEntities(IEnumerable<EntityObject> entities, List<EntityObject> results, Vector3 offset)
+        _result.RotateFlip(RotateFlipType.RotateNoneFlipY);
+
+        return _result;
+    }
+
+    private static void ProcessEntities(IEnumerable<EntityObject> entities, List<EntityObject> results, Vector3 offset)
+    {
+        foreach (var entity in entities)
         {
-            foreach (var entity in entities)
+            try
             {
-                try
+                if (entity is Line l)
+                    AddLine(l, results, offset);
+                else if (entity is Insert i)
+                    InsertToLines(i, results, offset);
+                else if (entity is Polyline2D p2d)
+                    PolylineToLines(p2d, results, offset);
+                else if (entity is Arc a)
+                    ArcToLines(a, results, offset);
+                else if (entity is Ellipse e)
+                    EllipseToLines(e, results, offset);
+                else if (entity is MText t)
+                    AddText(t, results, offset);
+                else if (entity is Dimension d)
+                    DimensionToLines(d, results, offset);
+                else
                 {
-                    if (entity is Line l)
-                        AddLine(l, results, offset);
-                    else if (entity is Insert i)
-                        InsertToLines(i, results, offset);
-                    else if (entity is Polyline2D p2d)
-                        PolylineToLines(p2d, results, offset);
-                    else if (entity is Arc a)
-                        ArcToLines(a, results, offset);
-                    else if (entity is Ellipse e)
-                        EllipseToLines(e, results, offset);
-                    else if (entity is MText t)
-                        AddText(t, results, offset);
-                    else if (entity is Dimension d)
-                        DimensionToLines(d, results, offset);
-                    else
-                    {
-                    }
-                }
-                catch (Exception e)
-                {
-
                 }
+            }
+            catch (Exception e)
+            {
 
             }
-        }
 
-        private static void DimensionToLines(Dimension dimension, List<EntityObject> results, Vector3 offset)
-        {
-            ProcessEntities(dimension.Block.Entities, results, offset);
         }
+    }
 
-        private static void AddText(MText text, List<EntityObject> results, Vector3 offset)
-        {
-            text.Position += offset;
-            results.Add(text);
-        }
+    private static void DimensionToLines(Dimension dimension, List<EntityObject> results, Vector3 offset)
+    {
+        ProcessEntities(dimension.Block.Entities, results, offset);
+    }
 
-        private static void AddLine(Line line, List<EntityObject> results, Vector3 offset)
-        {
-            line.StartPoint += offset;
-            line.EndPoint += offset;
-            results.Add(line);
-        }
+    private static void AddText(MText text, List<EntityObject> results, Vector3 offset)
+    {
+        text.Position += offset;
+        results.Add(text);
+    }
 
-        private static void EllipseToLines(Ellipse ellipse, List<EntityObject> results, Vector3 offset)
-        {
-            var precision = Math.Max(2, (int)((ellipse.MajorAxis + ellipse.MinorAxis) * 2.0F * Math.PI));
-            var polyline = ellipse.ToPolyline2D(precision);
-            var segments = polyline.Explode();
-            ProcessEntities(segments, results, offset);
-        }
+    private static void AddLine(Line line, List<EntityObject> results, Vector3 offset)
+    {
+        line.StartPoint += offset;
+        line.EndPoint += offset;
+        results.Add(line);
+    }
 
-        private static void InsertToLines(Insert insert, List<EntityObject> results, Vector3 offset)
-        {
-            ProcessEntities(insert.Block.Entities, results, insert.Position + offset);
-        }
+    private static void EllipseToLines(Ellipse ellipse, List<EntityObject> results, Vector3 offset)
+    {
+        var precision = Math.Max(2, (int)((ellipse.MajorAxis + ellipse.MinorAxis) * 2.0F * Math.PI));
+        var polyline = ellipse.ToPolyline2D(precision);
+        var segments = polyline.Explode();
+        ProcessEntities(segments, results, offset);
+    }
 
-        private static void PolylineToLines(Polyline2D polyline, List<EntityObject> results, Vector3 offset)
-        {
-            var components = polyline.Explode();
-            ProcessEntities(components, results, offset);
-        }
+    private static void InsertToLines(Insert insert, List<EntityObject> results, Vector3 offset)
+    {
+        ProcessEntities(insert.Block.Entities, results, insert.Position + offset);
+    }
 
-        private static void ArcToLines(Arc arc, List<EntityObject> results, Vector3 offset)
+    private static void PolylineToLines(Polyline2D polyline, List<EntityObject> results, Vector3 offset)
+    {
+        var components = polyline.Explode();
+        ProcessEntities(components, results, offset);
+    }
+
+    private static void ArcToLines(Arc arc, List<EntityObject> results, Vector3 offset)
+    {
+        var precision = Math.Max(2, (int)(arc.Radius * 2.0F * Math.PI));
+        var polyline = arc.ToPolyline2D(precision);
+        var segments = polyline.Explode();
+        ProcessEntities(segments, results, offset);
+    }
+
+    public static Result<Bitmap, Exception> DXFToBitmap(string filename, DxfImportSettings? settings = null)
+    {
+        using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
+        try
         {
-            var precision = Math.Max(2, (int)(arc.Radius * 2.0F * Math.PI));
-            var polyline = arc.ToPolyline2D(precision);
-            var segments = polyline.Explode();
-            ProcessEntities(segments, results, offset);
+            return Result.Ok(ProcessImage(stream, Path.GetFileNameWithoutExtension(filename), settings: settings));
         }
-
-        public static Bitmap? DXFToBitmap(string filename)
+        catch (Exception e)
         {
-            using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
-            try
-            {
-                return ProcessImage(stream, Path.GetFileNameWithoutExtension(filename));
-            }
-            catch (Exception e)
-            {
-                OnProcessError?.Invoke(e.Message);
-                return null;
-            }
+            OnProcessError?.Invoke(e.Message);
+            return Result.Error(e);
         }
     }
 }

+ 87 - 6
inabox.dxf/Objects.cs

@@ -21,8 +21,9 @@ internal class DxfLine : IDxfObject
 
     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));
+        if (!Line.IsVisible || !data.HasLayer(Line.Layer)) return;
+
+        data.Graphics.DrawLine(new Pen(Color.Black, (float)Line.Thickness), DrawData.ConvertPoint(Line.StartPoint), DrawData.ConvertPoint(Line.EndPoint));
     }
 }
 
@@ -40,8 +41,10 @@ internal class DxfInsert : IDxfObject
 
     public void Draw(DrawData data)
     {
+        if (!Insert.IsVisible) return;
+
         data.PushTransform();
-        data.Translate(data.ConvertPoint(Insert.Position));
+        data.Translate(new PointF((float)Insert.Position.X, (float)Insert.Position.Y));
         data.Rotate((float)Insert.Rotation);
         data.Scale((float)Insert.Scale.X, (float)Insert.Scale.Y);
         foreach(var obj in Objects)
@@ -63,9 +66,11 @@ internal class DxfEllipse : IDxfObject
 
     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);
+        if (!Ellipse.IsVisible || !data.HasLayer(Ellipse.Layer)) return;
+
+        var center = DrawData.ConvertPoint(Ellipse.Center);
+        var size = new SizeF((float)Ellipse.MajorAxis, (float)Ellipse.MinorAxis);
+        data.Graphics.DrawEllipse(new Pen(Color.Black, (float)Ellipse.Thickness), center.X - size.Width / 2, center.Y - size.Height / 2, size.Width, size.Height);
     }
 }
 
@@ -80,7 +85,30 @@ internal class DxfPolyline2D : IDxfObject
 
     public void Draw(DrawData data)
     {
+        if (!Polyline.IsVisible || !data.HasLayer(Polyline.Layer)) return;
+
+        if(Polyline.SmoothType == PolylineSmoothType.NoSmooth)
+        {
+            if (Polyline.Layer.Name != "Swipe Lines") return;
+
+            var vertices = Polyline.Vertexes.ToArray(x => new PointF((float)x.Position.X, (float)x.Position.Y));
+            if (Polyline.IsClosed)
+            {
+                data.Graphics.DrawPolygon(
+                    new Pen(Color.Black, (float)Polyline.Thickness),
+                    vertices);
+            }
+            else
+            {
+                data.Graphics.DrawLines(
+                    new Pen(Color.Black, (float)Polyline.Thickness),
+                    vertices);
+            }
+        }
+        else
+        {
 
+        }
     }
 }
 
@@ -95,7 +123,55 @@ internal class DxfMText : IDxfObject
 
     public void Draw(DrawData data)
     {
+        if (!MText.IsVisible || !data.HasLayer(MText.Layer)) return;
+
+        var fontFamily = new FontFamily(MText.Style.FontFamilyName);
+        var font = new Font(fontFamily, (float)MText.Height, MText.Style.FontStyle switch
+        {
+            netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
+            netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
+            netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
+        });
+        data.PushTransform();
+        data.Translate(new PointF((float)MText.Position.X, (float)MText.Position.Y));
+        data.Rotate((float)MText.Rotation);
+        data.Scale(1, -1);
+
+        var size = data.Graphics.MeasureString(MText.PlainText(), font);
+        switch (MText.AttachmentPoint)
+        {
+            case MTextAttachmentPoint.MiddleLeft:
+            case MTextAttachmentPoint.MiddleCenter:
+            case MTextAttachmentPoint.MiddleRight:
+                data.Translate(new PointF(0, -size.Height / 2));
+                break;
+            case MTextAttachmentPoint.BottomLeft:
+            case MTextAttachmentPoint.BottomCenter:
+            case MTextAttachmentPoint.BottomRight:
+                data.Translate(new PointF(0, -size.Height));
+                break;
+        }
+
+        switch (MText.AttachmentPoint)
+        {
+            case MTextAttachmentPoint.TopLeft:
+            case MTextAttachmentPoint.MiddleLeft:
+            case MTextAttachmentPoint.BottomLeft:
+                break;
+            case MTextAttachmentPoint.TopCenter:
+            case MTextAttachmentPoint.MiddleCenter:
+            case MTextAttachmentPoint.BottomCenter:
+                data.Translate(new PointF(-(float)size.Width / 2, 0));
+                break;
+            case MTextAttachmentPoint.TopRight:
+            case MTextAttachmentPoint.MiddleRight:
+            case MTextAttachmentPoint.BottomRight:
+                data.Translate(new PointF(-(float)size.Width, 0));
+                break;
+        }
 
+        data.Graphics.DrawString(MText.PlainText(), font, new SolidBrush(Color.Black), new PointF(0, 0));
+        data.PopTransform();
     }
 }
 
@@ -110,6 +186,11 @@ internal class DxfDimension : IDxfObject
 
     public void Draw(DrawData data)
     {
+        if (!Dimension.IsVisible || !data.HasLayer(Dimension.Layer)) return;
 
+        if(Dimension is AlignedDimension aligned)
+        {
+            data.Graphics.DrawLine(new Pen(Color.Black, (float)10), DrawData.ConvertPoint(aligned.FirstReferencePoint), DrawData.ConvertPoint(aligned.SecondReferencePoint));
+        }
     }
 }

+ 1 - 1
inabox.wpf/DynamicGrid/Columns/DynamicImageManagerColumn.cs

@@ -209,7 +209,7 @@ public class DynamicImageManagerColumn<T> : DynamicImagePreviewColumn<T>
                     {
                         MessageBox.Show("Error - " + message);
                     });
-                    bmp = DxfUtils.DXFToBitmap(filename);
+                    DxfUtils.DXFToBitmap(filename).GetOk(out bmp);
                 }
                 else
                     bmp = System.Drawing.Image.FromFile(filename) as Bitmap;

+ 1 - 1
inabox.wpf/Themes/Generic.xaml

@@ -62,7 +62,7 @@
                                       HorizontalScrollBarVisibility="Auto"
                                       VerticalScrollBarVisibility="Auto">
                             <Border Background="Transparent" x:Name="PART_ContentBorder">
-                                <ContentControl x:Name="PART_ZoomContent" VerticalAlignment="Center" Content="{TemplateBinding Content}">
+                                <ContentControl x:Name="PART_ZoomContent" VerticalAlignment="Center" Content="{TemplateBinding Content}" HorizontalAlignment="Center">
                                     <ContentControl.LayoutTransform>
                                         <ScaleTransform ScaleX="{Binding Scale,RelativeSource={RelativeSource TemplatedParent}}"
                                                         ScaleY="{Binding Scale,RelativeSource={RelativeSource TemplatedParent}}"/>