DxfUtils.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Point = System.Drawing.Point;
  9. namespace InABox.Dxf;
  10. public class DxfImportSettings
  11. {
  12. public Size ImageSize;
  13. public string[]? SupportFolders;
  14. public string? LayoutName { get; set; }
  15. public DxfImportSettings(Size? imageSize = null, string[]? supportFolders = null, string? layoutName = null)
  16. {
  17. ImageSize = imageSize ?? new(2048, 2048);
  18. SupportFolders = supportFolders;
  19. LayoutName = layoutName;
  20. }
  21. }
  22. public class DxfData
  23. {
  24. public DxfDocument Document { get; set; }
  25. public DxfImportSettings Settings { get; set; }
  26. public SizeF Size { get; set; }
  27. public PointF Origin { get; set; }
  28. public Layout Layout { get; set; }
  29. public IEnumerable<string> LayoutNames => Document.Layouts.Select(x => x.Name);
  30. public HashSet<string>? Layers { get; set; }
  31. public bool HasLayer(Layer layer)
  32. {
  33. return Layers is null || Layers.Contains(layer.Name);
  34. }
  35. public void SetLayers(params string[] layers)
  36. {
  37. Layers = layers.ToHashSet();
  38. }
  39. public void SetLayers(IEnumerable<string> layers)
  40. {
  41. Layers = layers.ToHashSet();
  42. }
  43. public string LayoutName
  44. {
  45. get => Layout.Name;
  46. set
  47. {
  48. Layout = Document.Layouts.First(x => x.Name == value);
  49. Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
  50. Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
  51. }
  52. }
  53. public DxfData(DxfDocument document, DxfImportSettings settings)
  54. {
  55. Document = document;
  56. Settings = settings;
  57. Layout = settings.LayoutName is not null ? document.Layouts.First(x => x.Name == settings.LayoutName) : document.Layouts.First();
  58. Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
  59. Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
  60. }
  61. }
  62. public static class DxfUtils
  63. {
  64. public static event ProcessError OnProcessError;
  65. public delegate void ProcessError(string message);
  66. internal static IDxfObject? ConvertEl(EntityObject el)
  67. {
  68. if(el is Line line)
  69. {
  70. return new DxfLine { Line = line };
  71. }
  72. else if(el is Insert insert)
  73. {
  74. return new DxfInsert(insert);
  75. }
  76. else if(el is Ellipse ellipse)
  77. {
  78. return new DxfEllipse(ellipse);
  79. }
  80. else if(el is MText text)
  81. {
  82. return new DxfMText(text);
  83. }
  84. else if(el is Polyline2D ln2D)
  85. {
  86. return new DxfPolyline2D(ln2D);
  87. }
  88. else if(el is Dimension dim)
  89. {
  90. return new DxfDimension(dim);
  91. }
  92. else if(el is Solid solid)
  93. {
  94. return new DxfSolid(solid);
  95. }
  96. else if (el is netDxf.Entities.Point point)
  97. {
  98. return null;
  99. }
  100. else
  101. {
  102. return null;
  103. }
  104. }
  105. public static void DrawDxf(DxfData data, Graphics graphics)
  106. {
  107. // Calculate the scaling factor to fit the image within the bounds
  108. float ratioX = (float)data.Settings.ImageSize.Width / data.Size.Width;
  109. float ratioY = (float)data.Settings.ImageSize.Height / data.Size.Height;
  110. var scale = Math.Min(ratioX, ratioY);
  111. var drawData = new DrawData() { Graphics = graphics, Data = data };
  112. Brush _brush = new SolidBrush(Color.White);
  113. graphics.FillRectangle(_brush, 0, 0, graphics.VisibleClipBounds.Width, graphics.VisibleClipBounds.Height);
  114. drawData.Graphics.TranslateTransform(graphics.VisibleClipBounds.Width / 2, graphics.VisibleClipBounds.Height / 2);
  115. drawData.Graphics.ScaleTransform(scale, scale);
  116. drawData.Graphics.TranslateTransform(-data.Origin.X - data.Size.Width / 2, -data.Origin.Y - data.Size.Height / 2);
  117. foreach(var el in data.Document.Entities.All)
  118. {
  119. var item = ConvertEl(el);
  120. item?.Draw(drawData);
  121. }
  122. }
  123. public static DxfData LoadDxf(string filename, DxfImportSettings? settings = null)
  124. {
  125. using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
  126. settings ??= new();
  127. var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
  128. document.BuildDimensionBlocks = true;
  129. return new(document, settings);
  130. }
  131. public static DxfData LoadDxf(Stream stream, DxfImportSettings? settings = null)
  132. {
  133. settings ??= new();
  134. var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
  135. return new(document, settings);
  136. }
  137. public static Bitmap ProcessImage(DxfData data)
  138. {
  139. var height = data.Size.Height;
  140. var width = data.Size.Width;
  141. // Calculate the scaling factor to fit the image within the bounds
  142. float ratioX = (float)data.Settings.ImageSize.Width / width;
  143. float ratioY = (float)data.Settings.ImageSize.Height / height;
  144. var scale = Math.Min(ratioX, ratioY);
  145. var margin = 100;
  146. var _result = new Bitmap((int)(width * scale) + (margin * 2), (int)(height * scale) + (margin * 2));
  147. using (var _graphics = Graphics.FromImage(_result))
  148. {
  149. _graphics.SmoothingMode = SmoothingMode.AntiAlias;
  150. DrawDxf(data, _graphics);
  151. }
  152. _result.RotateFlip(RotateFlipType.RotateNoneFlipY);
  153. return _result;
  154. }
  155. public static Bitmap ProcessImage(Stream stream, DxfImportSettings? settings = null)
  156. {
  157. return ProcessImage(LoadDxf(stream, settings));
  158. }
  159. public static Result<Bitmap, Exception> DXFToBitmap(string filename, DxfImportSettings? settings = null)
  160. {
  161. using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
  162. try
  163. {
  164. return Result.Ok(ProcessImage(stream, settings: settings));
  165. }
  166. catch (Exception e)
  167. {
  168. OnProcessError?.Invoke(e.Message);
  169. return Result.Error(e);
  170. }
  171. }
  172. }