Преглед изворни кода

Fixed Polylines, arcs and dimensions.

Kenric Nugteren пре 6 месеци
родитељ
комит
d6103cd8aa
4 измењених фајлова са 166 додато и 363 уклоњено
  1. 17 15
      inabox.dxf/DrawData.cs
  2. 82 311
      inabox.dxf/DxfUtils.cs
  3. 62 37
      inabox.dxf/Objects.cs
  4. 5 0
      inabox.dxf/Utils.cs

+ 17 - 15
inabox.dxf/DrawData.cs

@@ -12,25 +12,12 @@ namespace InABox.Dxf;
 
 internal class DrawData
 {
+    public DxfData Data { get; set; }
+
     public Graphics Graphics { get; set; }
 
     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);
@@ -60,6 +47,21 @@ internal class DrawData
         Graphics.ScaleTransform(scaleX, scaleY);
     }
 
+    public float ConvertThickness(float thickness)
+    {
+        return thickness == 0 ? 1f / ScaleFactor() : thickness;
+    }
+
+    public PointF TransformVec(PointF vec)
+    {
+        return Graphics.Transform.TransformVector(vec);
+    }
+
+    public float ScaleFactor()
+    {
+        return ((System.Numerics.Vector2)TransformVec(new(1, 0))).Length();
+    }
+
     public static PointF ConvertPoint(Vector2 vec)
     {
         return new PointF((float)vec.X, (float)vec.Y);

+ 82 - 311
inabox.dxf/DxfUtils.cs

@@ -3,6 +3,8 @@ using System.Drawing.Drawing2D;
 using InABox.Core;
 using netDxf;
 using netDxf.Entities;
+using netDxf.Objects;
+using netDxf.Tables;
 using Point = System.Drawing.Point;
 
 namespace InABox.Dxf;
@@ -13,67 +15,71 @@ public class DxfImportSettings
 
     public string[]? SupportFolders;
 
-    public DxfImportSettings(Size? imageSize = null, string[]? supportFolders = null)
+    public string? LayoutName { get; set; }
+
+    public DxfImportSettings(Size? imageSize = null, string[]? supportFolders = null, string? layoutName = null)
     {
         ImageSize = imageSize ?? new(2048, 2048);
         SupportFolders = supportFolders;
+        LayoutName = layoutName;
     }
 }
 
-public static class DxfUtils
+public class DxfData
 {
-    public static event ProcessError OnProcessError;
+    public DxfDocument Document { get; set; }
 
-    // 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 DxfImportSettings Settings { get; set; }
 
-        // Rotate.
-        gr.RotateTransform(angle);
+    public SizeF Size { get; set; }
 
-        // Translate to desired position. Be sure to append
-        // the rotation so it occurs after the rotation.
-        gr.TranslateTransform(x, y, MatrixOrder.Append);
+    public PointF Origin { get; set; }
 
-        // Draw the text at the origin.
-        gr.DrawString(txt, the_font, the_brush, 0, 0);
+    public Layout Layout { get; set; }
 
-        // Restore the graphics state.
-        gr.Restore(state);
-    }
+    public IEnumerable<string> LayoutNames => Document.Layouts.Select(x => x.Name);
+
+    public HashSet<string>? Layers { get; set; }
 
-    private static void CheckPoints(double x, double y, ref double minX, ref double minY, ref double maxX, ref double maxY)
+    public bool HasLayer(Layer layer)
     {
-        minX = x < minX ? x : minX;
-        minY = y < minY ? y : minY;
-        maxX = x > maxX ? x : maxX;
-        maxY = y > maxY ? y : maxY;
+        return Layers is null || Layers.Contains(layer.Name);
     }
 
-    private static float Scale(int value, float offset, double scale, int border)
+    public void SetLayers(params string[] layers)
     {
-        return (value - offset) * (float)scale + border;
+        Layers = layers.ToHashSet();
     }
-
-    private static float Scale(double value, float offset, double scale, int border)
+    public void SetLayers(IEnumerable<string> layers)
     {
-        return ((float)value - offset) * (float)scale + border;
+        Layers = layers.ToHashSet();
     }
 
-    private static PointF VectorToPointF(Vector3 vector, PointF offset, float scale, Point border)
+    public string LayoutName
     {
-        return new PointF(Scale(vector.X, offset.X, scale, border.X), Scale(vector.Y, offset.Y, scale, border.Y));
+        get => Layout.Name;
+        set
+        {
+            Layout = Document.Layouts.First(x => x.Name == value);
+            Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
+            Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
+        }
     }
 
-    private static PointF VectorToPointF(Vector2 vector, PointF offset, int scale, Point border)
+    public DxfData(DxfDocument document, DxfImportSettings settings)
     {
-        return new PointF(Scale(vector.X, offset.X, scale, border.X), Scale(vector.Y, offset.Y, scale, border.Y));
+        Document = document;
+        Settings = settings;
+
+        Layout = settings.LayoutName is not null ? document.Layouts.First(x => x.Name == settings.LayoutName) : document.Layouts.First();
+        Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
+        Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
     }
+}
 
+public static class DxfUtils
+{
+    public static event ProcessError OnProcessError;
     public delegate void ProcessError(string message);
 
     internal static IDxfObject? ConvertEl(EntityObject el)
@@ -108,308 +114,73 @@ public static class DxfUtils
         }
     }
 
-    public static Bitmap ProcessImage(Stream stream, string caption = null, bool dimensionmarks = true, DxfImportSettings? settings = null)
+    public static void DrawDxf(DxfData data, Graphics graphics)
     {
-        settings ??= new();
+        // Calculate the scaling factor to fit the image within the bounds
+        float ratioX = (float)data.Settings.ImageSize.Width / data.Size.Width;
+        float ratioY = (float)data.Settings.ImageSize.Height / data.Size.Height;
+
+        var scale = Math.Min(ratioX, ratioY);
 
-        var minX = double.MaxValue;
-        var minY = double.MaxValue;
-        var maxX = double.MinValue;
-        var maxY = double.MinValue;
+        var drawData = new DrawData() { Graphics = graphics, Data = data };
 
-        var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
+        Brush _brush = new SolidBrush(Color.White);
+        graphics.FillRectangle(_brush, 0, 0, graphics.VisibleClipBounds.Width, graphics.VisibleClipBounds.Height);
 
-        // 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());
+        drawData.Graphics.TranslateTransform(graphics.VisibleClipBounds.Width / 2, graphics.VisibleClipBounds.Height / 2);
 
-        if (!objects.Any())
-            throw new Exception(string.Format("No Data Found in {0}!", caption));
+        drawData.Graphics.ScaleTransform(scale, scale);
+        drawData.Graphics.TranslateTransform(-data.Origin.X - data.Size.Width / 2, -data.Origin.Y - data.Size.Height / 2);
 
-        foreach (var line in objects.OfType<Line>())
+        foreach(var el in data.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(drawData);
         }
+    }
+
+    public static DxfData LoadDxf(string filename, DxfImportSettings? settings = null)
+    {
+        using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
+        settings ??= new();
+
+        var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
+        return new(document, settings);
+    }
+    public static DxfData LoadDxf(Stream stream, DxfImportSettings? settings = null)
+    {
+        settings ??= new();
 
-        var height = (int)Math.Abs(maxY - minY);
-        var width = (int)Math.Abs(maxX - minX);
+        var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
+        return new(document, settings);
+    }
+
+    public static Bitmap ProcessImage(DxfData data)
+    {
+        var height = data.Size.Height;
+        var width = data.Size.Width;
         
         // 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;
+        float ratioX = (float)data.Settings.ImageSize.Width / width;
+        float ratioY = (float)data.Settings.ImageSize.Height / height;
 
         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 };
-
-            data.SetLayers("Swipe Lines");
-
-            Brush _brush = new SolidBrush(Color.White);
-            _graphics.FillRectangle(_brush, 0, 0, _result.Width, _result.Height);
-
-            data.Graphics.ScaleTransform(0.5f, 0.5f);
-            data.Graphics.TranslateTransform(_result.Width / 2, _result.Height / 2);
-
-            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)
-            {
-                var item = ConvertEl(el);
-                item?.Draw(data);
-            }
-
-            /*
-            foreach (var line in objects.OfType<Line>())
-            {
-                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)))
-            {
-                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)
-                {
-                }
-                
-            }
-            
-            try
-            {
-                var font = new Font("Arial", 20.0F);
-                var brush = new SolidBrush(Color.Blue);
-                _pen = new Pen(brush);
-
-                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)
-                {
-
-                    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)
-            {
-
-            }
-            
-            */
+            DrawDxf(data, _graphics);
         }
-
         _result.RotateFlip(RotateFlipType.RotateNoneFlipY);
 
         return _result;
     }
 
-    private static void ProcessEntities(IEnumerable<EntityObject> entities, List<EntityObject> results, Vector3 offset)
-    {
-        foreach (var entity in entities)
-        {
-            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
-                {
-                }
-            }
-            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 AddLine(Line line, List<EntityObject> results, Vector3 offset)
-    {
-        line.StartPoint += offset;
-        line.EndPoint += offset;
-        results.Add(line);
-    }
-
-    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 InsertToLines(Insert insert, List<EntityObject> results, Vector3 offset)
-    {
-        ProcessEntities(insert.Block.Entities, results, insert.Position + 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)
+    public static Bitmap ProcessImage(Stream stream, DxfImportSettings? settings = null)
     {
-        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 ProcessImage(LoadDxf(stream, settings));
     }
 
     public static Result<Bitmap, Exception> DXFToBitmap(string filename, DxfImportSettings? settings = null)
@@ -417,7 +188,7 @@ public static class DxfUtils
         using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
         try
         {
-            return Result.Ok(ProcessImage(stream, Path.GetFileNameWithoutExtension(filename), settings: settings));
+            return Result.Ok(ProcessImage(stream, settings: settings));
         }
         catch (Exception e)
         {

+ 62 - 37
inabox.dxf/Objects.cs

@@ -1,4 +1,5 @@
 using InABox.Core;
+using netDxf;
 using netDxf.Entities;
 using System;
 using System.Collections.Generic;
@@ -21,9 +22,8 @@ internal class DxfLine : IDxfObject
 
     public void Draw(DrawData data)
     {
-        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));
+        if (!Line.IsVisible || !data.Data.HasLayer(Line.Layer)) return;
+        data.Graphics.DrawLine(new Pen(Color.Black, data.ConvertThickness((float)Line.Thickness)), DrawData.ConvertPoint(Line.StartPoint), DrawData.ConvertPoint(Line.EndPoint));
     }
 }
 
@@ -66,11 +66,15 @@ internal class DxfEllipse : IDxfObject
 
     public void Draw(DrawData data)
     {
-        if (!Ellipse.IsVisible || !data.HasLayer(Ellipse.Layer)) return;
+        if (!Ellipse.IsVisible || !data.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);
+
+        var startAngle = (float)(Ellipse.StartAngle);
+        var endAngle = (float)(Ellipse.EndAngle);
+
+        data.Graphics.DrawArc(new Pen(Color.Black, data.ConvertThickness((float)Ellipse.Thickness)), center.X - size.Width / 2, center.Y - size.Height / 2, size.Width, size.Height, startAngle, Utils.Mod(endAngle - startAngle, 360));
     }
 }
 
@@ -85,30 +89,34 @@ internal class DxfPolyline2D : IDxfObject
 
     public void Draw(DrawData data)
     {
-        if (!Polyline.IsVisible || !data.HasLayer(Polyline.Layer)) return;
+        if (!Polyline.IsVisible || !data.Data.HasLayer(Polyline.Layer)) return;
 
-        if(Polyline.SmoothType == PolylineSmoothType.NoSmooth)
+        var entities = Polyline.Explode();
+        foreach(var entity in entities)
         {
-            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);
-            }
+            DxfUtils.ConvertEl(entity)?.Draw(data);
         }
-        else
-        {
 
-        }
+        // if(Polyline.SmoothType == PolylineSmoothType.NoSmooth)
+        // {
+        //     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, data.ConvertThickness((float)Polyline.Thickness)),
+        //             vertices);
+        //     }
+        //     else
+        //     {
+        //         data.Graphics.DrawLines(
+        //             new Pen(Color.Black, data.ConvertThickness((float)Polyline.Thickness)),
+        //             vertices);
+        //     }
+        // }
+        // else
+        // {
+
+        // }
     }
 }
 
@@ -123,21 +131,30 @@ internal class DxfMText : IDxfObject
 
     public void Draw(DrawData data)
     {
-        if (!MText.IsVisible || !data.HasLayer(MText.Layer)) return;
+        if (!MText.IsVisible || !data.Data.HasLayer(MText.Layer)) return;
 
-        var fontFamily = new FontFamily(MText.Style.FontFamilyName);
-        var font = new Font(fontFamily, (float)MText.Height, MText.Style.FontStyle switch
+        Font font;
+        if (MText.Style.FontFamilyName.IsNullOrWhiteSpace())
+        {
+            font = SystemFonts.DefaultFont;
+        }
+        else
         {
-            netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
-            netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
-            netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
-        });
+            var fontFamily = new FontFamily(MText.Style.FontFamilyName);
+            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);
+        var size = data.Graphics.MeasureString(MText.PlainText(), font, new PointF(), StringFormat.GenericTypographic);
         switch (MText.AttachmentPoint)
         {
             case MTextAttachmentPoint.MiddleLeft:
@@ -150,6 +167,13 @@ internal class DxfMText : IDxfObject
             case MTextAttachmentPoint.BottomRight:
                 data.Translate(new PointF(0, -size.Height));
                 break;
+            default:
+                var ascent = font.FontFamily.GetCellAscent(font.Style);
+                var lineSpace = font.FontFamily.GetLineSpacing(font.Style);
+                var baseline = ascent * font.Height / font.FontFamily.GetEmHeight(font.Style);
+                var ratio = font.GetHeight(data.Graphics) / lineSpace;
+                data.Translate(new PointF(0, -baseline + ascent * ratio));
+                break;
         }
 
         switch (MText.AttachmentPoint)
@@ -170,7 +194,7 @@ internal class DxfMText : IDxfObject
                 break;
         }
 
-        data.Graphics.DrawString(MText.PlainText(), font, new SolidBrush(Color.Black), new PointF(0, 0));
+        data.Graphics.DrawString(MText.PlainText(), font, new SolidBrush(Color.Black), new PointF(0, 0), StringFormat.GenericTypographic);
         data.PopTransform();
     }
 }
@@ -186,11 +210,12 @@ internal class DxfDimension : IDxfObject
 
     public void Draw(DrawData data)
     {
-        if (!Dimension.IsVisible || !data.HasLayer(Dimension.Layer)) return;
+        if (!Dimension.IsVisible || !data.Data.HasLayer(Dimension.Layer)) return;
 
-        if(Dimension is AlignedDimension aligned)
+        var entities = Dimension.Block.Entities;
+        foreach(var entity in entities)
         {
-            data.Graphics.DrawLine(new Pen(Color.Black, (float)10), DrawData.ConvertPoint(aligned.FirstReferencePoint), DrawData.ConvertPoint(aligned.SecondReferencePoint));
+            DxfUtils.ConvertEl(entity)?.Draw(data);
         }
     }
 }

+ 5 - 0
inabox.dxf/Utils.cs

@@ -21,4 +21,9 @@ internal static class Utils
         matrix.TransformVectors(arr);
         return arr[0];
     }
+
+    public static float Mod(float x, float y)
+    {
+        return (float)(x - y * Math.Floor(x / y));
+    }
 }