DxfUtils.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 bool ShouldDraw(EntityObject obj)
  44. {
  45. return obj.IsVisible && HasLayer(obj.Layer);
  46. }
  47. public string LayoutName
  48. {
  49. get => Layout.Name;
  50. set
  51. {
  52. Layout = Document.Layouts.First(x => x.Name == value);
  53. Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
  54. Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
  55. }
  56. }
  57. public DxfData(DxfDocument document, DxfImportSettings settings)
  58. {
  59. Document = document;
  60. Settings = settings;
  61. Layout = settings.LayoutName is not null ? document.Layouts.First(x => x.Name == settings.LayoutName) : document.Layouts.First();
  62. Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
  63. Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
  64. }
  65. }
  66. public static class DxfUtils
  67. {
  68. public static event ProcessError OnProcessError;
  69. public delegate void ProcessError(string message);
  70. internal static IDxfObject? ConvertEl(EntityObject el)
  71. {
  72. if(el is Line line)
  73. {
  74. return new DxfLine { Line = line };
  75. }
  76. else if(el is Insert insert)
  77. {
  78. return new DxfInsert(insert);
  79. }
  80. else if(el is Ellipse ellipse)
  81. {
  82. return new DxfEllipse(ellipse);
  83. }
  84. else if(el is MText text)
  85. {
  86. return new DxfMText(text);
  87. }
  88. else if(el is Polyline2D ln2D)
  89. {
  90. return new DxfPolyline2D(ln2D);
  91. }
  92. else if(el is Dimension dim)
  93. {
  94. return new DxfDimension(dim);
  95. }
  96. else if(el is Solid solid)
  97. {
  98. return new DxfSolid(solid);
  99. }
  100. else if (el is netDxf.Entities.Point point)
  101. {
  102. return null;
  103. }
  104. else
  105. {
  106. return null;
  107. }
  108. }
  109. public static void DrawDxf(DxfData data, Graphics graphics)
  110. {
  111. // Calculate the scaling factor to fit the image within the bounds
  112. float ratioX = (float)data.Settings.ImageSize.Width / data.Size.Width;
  113. float ratioY = (float)data.Settings.ImageSize.Height / data.Size.Height;
  114. var scale = Math.Min(ratioX, ratioY);
  115. var drawData = new DrawData() { Graphics = graphics, Data = data };
  116. Brush _brush = new SolidBrush(Color.White);
  117. graphics.FillRectangle(_brush, 0, 0, graphics.VisibleClipBounds.Width, graphics.VisibleClipBounds.Height);
  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, _graphics);
  173. }
  174. _result.RotateFlip(RotateFlipType.RotateNoneFlipY);
  175. return _result;
  176. }
  177. public static Bitmap ProcessImage(Stream stream, DxfImportSettings? settings = null)
  178. {
  179. return ProcessImage(LoadDxf(stream, settings));
  180. }
  181. public static Result<Bitmap, Exception> DXFToBitmap(string filename, DxfImportSettings? settings = null)
  182. {
  183. using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
  184. try
  185. {
  186. return Result.Ok(ProcessImage(stream, settings: settings));
  187. }
  188. catch (Exception e)
  189. {
  190. OnProcessError?.Invoke(e.Message);
  191. return Result.Error(e);
  192. }
  193. }
  194. }