DxfUtils.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. using InABox.Core;
  4. using netDxf;
  5. using netDxf.Entities;
  6. using netDxf.Objects;
  7. using netDxf.Tables;
  8. using Svg;
  9. using Point = System.Drawing.Point;
  10. namespace InABox.Dxf;
  11. public class DxfImportSettings
  12. {
  13. public Size ImageSize;
  14. public string[]? SupportFolders;
  15. public string? LayoutName { get; set; }
  16. public DxfImportSettings(Size? imageSize = null, string[]? supportFolders = null, string? layoutName = null)
  17. {
  18. ImageSize = imageSize ?? new(2048, 2048);
  19. SupportFolders = supportFolders;
  20. LayoutName = layoutName;
  21. }
  22. }
  23. public class DxfData
  24. {
  25. public DxfDocument Document { get; set; }
  26. public DxfImportSettings Settings { get; set; }
  27. public SizeF Size { get; set; }
  28. public PointF Origin { get; set; }
  29. public Layout Layout { get; set; }
  30. public IEnumerable<string> LayoutNames => Document.Layouts.Select(x => x.Name);
  31. public HashSet<string>? Layers { get; set; }
  32. public bool HasLayer(Layer layer)
  33. {
  34. return Layers is null || Layers.Contains(layer.Name);
  35. }
  36. public void SetLayers(params string[] layers)
  37. {
  38. Layers = layers.ToHashSet();
  39. }
  40. public void SetLayers(IEnumerable<string> layers)
  41. {
  42. Layers = layers.ToHashSet();
  43. }
  44. public bool ShouldDraw(EntityObject obj)
  45. {
  46. return obj.IsVisible && HasLayer(obj.Layer);
  47. }
  48. public string LayoutName
  49. {
  50. get => Layout.Name;
  51. set
  52. {
  53. Layout = Document.Layouts.First(x => x.Name == value);
  54. Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
  55. Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
  56. }
  57. }
  58. public DxfData(DxfDocument document, DxfImportSettings settings)
  59. {
  60. Document = document;
  61. Settings = settings;
  62. Layout = settings.LayoutName is not null ? document.Layouts.First(x => x.Name == settings.LayoutName) : document.Layouts.First();
  63. Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
  64. Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
  65. }
  66. }
  67. public static class DxfUtils
  68. {
  69. public static event ProcessError OnProcessError;
  70. public delegate void ProcessError(string message);
  71. internal static IDxfObject? ConvertEl(EntityObject el)
  72. {
  73. if(el is Line line)
  74. {
  75. return new DxfLine { Line = line };
  76. }
  77. else if(el is Insert insert)
  78. {
  79. return new DxfInsert(insert);
  80. }
  81. else if(el is Ellipse ellipse)
  82. {
  83. return new DxfEllipse(ellipse);
  84. }
  85. else if(el is MText text)
  86. {
  87. return new DxfMText(text);
  88. }
  89. else if(el is Polyline2D ln2D)
  90. {
  91. return new DxfPolyline2D(ln2D);
  92. }
  93. else if(el is Dimension dim)
  94. {
  95. return new DxfDimension(dim);
  96. }
  97. else if(el is Solid solid)
  98. {
  99. return new DxfSolid(solid);
  100. }
  101. else if (el is netDxf.Entities.Point point)
  102. {
  103. return null;
  104. }
  105. else
  106. {
  107. return null;
  108. }
  109. }
  110. public static void DrawDxf(DxfData data, IGraphics graphics, float width, float height)
  111. {
  112. // Calculate the scaling factor to fit the image within the bounds
  113. float ratioX = (float)data.Settings.ImageSize.Width / data.Size.Width;
  114. float ratioY = (float)data.Settings.ImageSize.Height / data.Size.Height;
  115. var scale = Math.Min(ratioX, ratioY);
  116. var drawData = new DrawData() { Graphics = graphics, Data = data };
  117. graphics.Clear(Color.White);
  118. // drawData.Translate(graphics.VisibleClipBounds.Width / 2, graphics.VisibleClipBounds.Height / 2);
  119. drawData.Scale(scale, scale);
  120. drawData.Translate(-data.Origin.X, -data.Origin.Y);
  121. // drawData.Translate(-data.Origin.X - data.Size.Width / 2, -data.Origin.Y - data.Size.Height / 2);
  122. foreach(var el in data.Document.Entities.All)
  123. {
  124. var item = ConvertEl(el);
  125. item?.Draw(drawData);
  126. }
  127. }
  128. public static DxfData LoadDxf(string filename, DxfImportSettings? settings = null)
  129. {
  130. using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
  131. settings ??= new();
  132. var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
  133. document.BuildDimensionBlocks = true;
  134. return new(document, settings);
  135. }
  136. public static DxfData LoadDxf(Stream stream, DxfImportSettings? settings = null)
  137. {
  138. settings ??= new();
  139. var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
  140. return new(document, settings);
  141. }
  142. /// <summary>
  143. /// Returns <see langword="null"/> if the bounds are completely empty.
  144. /// </summary>
  145. /// <param name="data"></param>
  146. /// <returns></returns>
  147. public static RectangleF? CalculateDxfSize(DxfData data)
  148. {
  149. var transformData = new TransformData { Data = data };
  150. RectangleF? bounds = null;
  151. foreach(var el in data.Document.Entities.All)
  152. {
  153. if(ConvertEl(el) is IDxfObject obj)
  154. {
  155. bounds = Utils.CombineBounds(bounds, obj.GetBounds(transformData));
  156. }
  157. }
  158. return bounds;
  159. }
  160. public static Bitmap ProcessImage(DxfData data)
  161. {
  162. var height = data.Size.Height;
  163. var width = data.Size.Width;
  164. // Calculate the scaling factor to fit the image within the bounds
  165. float ratioX = (float)data.Settings.ImageSize.Width / width;
  166. float ratioY = (float)data.Settings.ImageSize.Height / height;
  167. var scale = Math.Min(ratioX, ratioY);
  168. var _result = new Bitmap((int)(width * scale), (int)(height * scale));
  169. using (var _graphics = Graphics.FromImage(_result))
  170. {
  171. _graphics.SmoothingMode = SmoothingMode.AntiAlias;
  172. DrawDxf(data, new GdiGraphics(_graphics), _result.Width, _result.Height);
  173. }
  174. _result.RotateFlip(RotateFlipType.RotateNoneFlipY);
  175. return _result;
  176. }
  177. public static SvgDocument ProcessSvg(DxfData data)
  178. {
  179. var doc = new SvgDocument
  180. {
  181. ViewBox = new(data.Origin.X, data.Origin.Y, data.Size.Width, data.Size.Height)
  182. };
  183. var drawData = new DrawData() { Graphics = new SvgGraphics(doc), Data = data };
  184. foreach(var el in data.Document.Entities.All)
  185. {
  186. var item = ConvertEl(el);
  187. item?.Draw(drawData);
  188. }
  189. return doc;
  190. }
  191. public static Bitmap ProcessImage(Stream stream, DxfImportSettings? settings = null)
  192. {
  193. return ProcessImage(LoadDxf(stream, settings));
  194. }
  195. public static Result<Bitmap, Exception> DXFToBitmap(string filename, DxfImportSettings? settings = null)
  196. {
  197. using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
  198. try
  199. {
  200. return Result.Ok(ProcessImage(stream, settings: settings));
  201. }
  202. catch (Exception e)
  203. {
  204. OnProcessError?.Invoke(e.Message);
  205. return Result.Error(e);
  206. }
  207. }
  208. }