فهرست منبع

Fix sizing of images

Kenric Nugteren 6 ماه پیش
والد
کامیت
9876363f85
2فایلهای تغییر یافته به همراه66 افزوده شده و 5 حذف شده
  1. 36 5
      inabox.dxf/DxfUtils.cs
  2. 30 0
      inabox.dxf/Objects.cs

+ 36 - 5
inabox.dxf/DxfUtils.cs

@@ -20,8 +20,11 @@ public class DxfImportSettings
 
     public string? LayoutName { get; set; }
 
-    public DxfImportSettings(Size? imageSize = null, string[]? supportFolders = null, string? layoutName = null)
+    public bool AutoFit { get; set; }
+
+    public DxfImportSettings(Size? imageSize = null, string[]? supportFolders = null, string? layoutName = null, bool autoFit = true)
     {
+        AutoFit = autoFit;
         ImageSize = imageSize ?? new(2048, 2048);
         SupportFolders = supportFolders;
         LayoutName = layoutName ?? "Model";
@@ -69,8 +72,7 @@ public class DxfData
         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);
+            UpdateSizeAndOrigin();
         }
     }
 
@@ -80,8 +82,33 @@ public class DxfData
         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);
+        UpdateSizeAndOrigin();
+    }
+
+    private void UpdateSizeAndOrigin()
+    {
+        if (Settings.AutoFit)
+        {
+            var bounds = DxfUtils.CalculateDxfSize(this) ?? new();
+            var margin = 0.1f * Math.Min(bounds.Width, bounds.Height);
+            if (bounds.IsEmpty)
+            {
+                bounds.Size = new(100, 100);
+                bounds.Location = new(0, 0);
+            }
+            else
+            {
+                bounds.Size = new(bounds.Width + margin * 2, bounds.Height + margin * 2);
+                bounds.Location = new(bounds.X - margin, bounds.Y - margin);
+            }
+            Size = bounds.Size;
+            Origin = bounds.Location;
+        }
+        else
+        {
+            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);
+        }
     }
 }
 
@@ -128,6 +155,10 @@ public static class DxfUtils
         {
             return null;
         }
+        else if (el is Arc a)
+        {
+            return new DxfArc(a);
+        }
         else
         {
             return null;

+ 30 - 0
inabox.dxf/Objects.cs

@@ -112,6 +112,36 @@ internal class DxfInsert : IDxfObject
     }
 }
 
+internal class DxfArc : IDxfObject
+{
+    public Arc Arc { get; set; }
+
+    public DxfArc(Arc arc)
+    {
+        Arc = arc;
+    }
+
+    public void Draw(DrawData data)
+    {
+        if (!data.Data.ShouldDraw(Arc)) return;
+
+        foreach(var obj in Arc.ToPolyline2D(100).Explode())
+        {
+            DxfUtils.ConvertEl(obj)?.Draw(data);
+        }
+    }
+
+    public RectangleF? GetBounds(TransformData data)
+    {
+        if (!data.Data.ShouldDraw(Arc)) return null;
+
+        var halfSize = new netDxf.Vector3(Arc.Radius, Arc.Radius, 0);
+        return Utils.RectangleFromPoints(
+            data.TransformPoint(Arc.Center - halfSize),
+            data.TransformPoint(Arc.Center + halfSize));
+    }
+}
+
 internal class DxfEllipse : IDxfObject
 {
     public Ellipse Ellipse { get; set; }