123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using InABox.Core;
- using netDxf;
- using netDxf.Entities;
- using netDxf.Objects;
- using netDxf.Tables;
- using Svg;
- using Point = System.Drawing.Point;
- namespace InABox.Dxf;
- public class DxfImportSettings
- {
- public Size ImageSize;
- public string[]? SupportFolders;
- 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 class DxfData
- {
- public DxfDocument Document { get; set; }
- public DxfImportSettings Settings { get; set; }
- public SizeF Size { get; set; }
- public PointF Origin { get; set; }
- public Layout Layout { get; set; }
- public IEnumerable<string> LayoutNames => Document.Layouts.Select(x => x.Name);
- 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 bool ShouldDraw(EntityObject obj)
- {
- return obj.IsVisible && HasLayer(obj.Layer);
- }
- public string LayoutName
- {
- 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);
- }
- }
- public DxfData(DxfDocument document, DxfImportSettings settings)
- {
- 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)
- {
- 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 if(el is Solid solid)
- {
- return new DxfSolid(solid);
- }
- else if (el is netDxf.Entities.Point point)
- {
- return null;
- }
- else
- {
- return null;
- }
- }
- 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;
- float ratioY = (float)data.Settings.ImageSize.Height / data.Size.Height;
- var scale = Math.Min(ratioX, ratioY);
- var drawData = new DrawData() { Graphics = graphics, Data = data };
- graphics.Clear(Color.White);
- // drawData.Translate(graphics.VisibleClipBounds.Width / 2, graphics.VisibleClipBounds.Height / 2);
- drawData.Scale(scale, scale);
- drawData.Translate(-data.Origin.X, -data.Origin.Y);
- // drawData.Translate(-data.Origin.X - data.Size.Width / 2, -data.Origin.Y - data.Size.Height / 2);
- foreach(var el in data.Document.Entities.All)
- {
- 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>());
- document.BuildDimensionBlocks = true;
- return new(document, settings);
- }
- public static DxfData LoadDxf(Stream stream, DxfImportSettings? settings = null)
- {
- settings ??= new();
- var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
- return new(document, settings);
- }
- /// <summary>
- /// Returns <see langword="null"/> if the bounds are completely empty.
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static RectangleF? CalculateDxfSize(DxfData data)
- {
- var transformData = new TransformData { Data = data };
- RectangleF? bounds = null;
- foreach(var el in data.Document.Entities.All)
- {
- if(ConvertEl(el) is IDxfObject obj)
- {
- bounds = Utils.CombineBounds(bounds, obj.GetBounds(transformData));
- }
- }
- return bounds;
- }
- 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)data.Settings.ImageSize.Width / width;
- float ratioY = (float)data.Settings.ImageSize.Height / height;
- var scale = Math.Min(ratioX, ratioY);
- var _result = new Bitmap((int)(width * scale), (int)(height * scale));
- using (var _graphics = Graphics.FromImage(_result))
- {
- _graphics.SmoothingMode = SmoothingMode.AntiAlias;
- 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));
- }
- public static Result<Bitmap, Exception> DXFToBitmap(string filename, DxfImportSettings? settings = null)
- {
- using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
- try
- {
- return Result.Ok(ProcessImage(stream, settings: settings));
- }
- catch (Exception e)
- {
- OnProcessError?.Invoke(e.Message);
- return Result.Error(e);
- }
- }
- }
|