DxfUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. using System.Numerics;
  4. using InABox.Core;
  5. using netDxf;
  6. using netDxf.Entities;
  7. using netDxf.Objects;
  8. using netDxf.Tables;
  9. using Svg;
  10. using Syncfusion.Pdf;
  11. using Point = System.Drawing.Point;
  12. namespace InABox.Dxf;
  13. public class DxfImportSettings
  14. {
  15. public Size ImageSize;
  16. public string[]? SupportFolders;
  17. public string? LayoutName { get; set; }
  18. public DxfImportSettings(Size? imageSize = null, string[]? supportFolders = null, string? layoutName = null)
  19. {
  20. ImageSize = imageSize ?? new(2048, 2048);
  21. SupportFolders = supportFolders;
  22. LayoutName = layoutName ?? "Model";
  23. }
  24. }
  25. public class DxfData
  26. {
  27. public DxfDocument Document { get; set; }
  28. public DxfImportSettings Settings { get; set; }
  29. public SizeF Size { get; set; }
  30. public PointF Origin { get; set; }
  31. public Layout Layout { get; set; }
  32. public IEnumerable<string> LayoutNames => Document.Layouts.Select(x => x.Name);
  33. public HashSet<string>? Layers { get; set; }
  34. public bool HasLayer(Layer layer)
  35. {
  36. return Layers is null || Layers.Contains(layer.Name);
  37. }
  38. public void SetLayers(params string[] layers)
  39. {
  40. Layers = layers.ToHashSet();
  41. }
  42. public void SetLayers(IEnumerable<string> layers)
  43. {
  44. Layers = layers.ToHashSet();
  45. }
  46. public bool ShouldDraw(EntityObject obj)
  47. {
  48. return obj.IsVisible && HasLayer(obj.Layer);
  49. }
  50. public string LayoutName
  51. {
  52. get => Layout.Name;
  53. set
  54. {
  55. Layout = Document.Layouts.First(x => x.Name == value);
  56. Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
  57. Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
  58. }
  59. }
  60. public DxfData(DxfDocument document, DxfImportSettings settings)
  61. {
  62. Document = document;
  63. Settings = settings;
  64. Layout = settings.LayoutName is not null ? document.Layouts.First(x => x.Name == settings.LayoutName) : document.Layouts.First();
  65. Size = new((float)(Layout.MaxLimit.X - Layout.MinLimit.X), (float)(Layout.MaxLimit.Y - Layout.MinLimit.Y));
  66. Origin = new((float)Layout.MinLimit.X, (float)Layout.MinLimit.Y);
  67. }
  68. }
  69. public static class DxfUtils
  70. {
  71. public static event ProcessError OnProcessError;
  72. public delegate void ProcessError(string message);
  73. internal static IDxfObject? ConvertEl(EntityObject el)
  74. {
  75. if(el is Line line)
  76. {
  77. return new DxfLine { Line = line };
  78. }
  79. else if(el is Insert insert)
  80. {
  81. return new DxfInsert(insert);
  82. }
  83. else if(el is Ellipse ellipse)
  84. {
  85. return new DxfEllipse(ellipse);
  86. }
  87. else if(el is MText text)
  88. {
  89. return new DxfMText(text);
  90. }
  91. else if(el is Polyline2D ln2D)
  92. {
  93. return new DxfPolyline2D(ln2D);
  94. }
  95. else if(el is Dimension dim)
  96. {
  97. return new DxfDimension(dim);
  98. }
  99. else if(el is Solid solid)
  100. {
  101. return new DxfSolid(solid);
  102. }
  103. else if (el is netDxf.Entities.Point point)
  104. {
  105. return null;
  106. }
  107. else if (el is netDxf.Entities.Viewport viewport)
  108. {
  109. return null;
  110. }
  111. else
  112. {
  113. return null;
  114. }
  115. }
  116. public static void DrawDxf(DxfData data, IGraphics graphics, float width, float height)
  117. {
  118. // Calculate the scaling factor to fit the image within the bounds
  119. float ratioX = (float)data.Settings.ImageSize.Width / data.Size.Width;
  120. float ratioY = (float)data.Settings.ImageSize.Height / data.Size.Height;
  121. var scale = Math.Min(ratioX, ratioY);
  122. var drawData = new DrawData() { Graphics = graphics, Data = data };
  123. graphics.Clear(Color.White);
  124. // drawData.Translate(graphics.VisibleClipBounds.Width / 2, graphics.VisibleClipBounds.Height / 2);
  125. drawData.Scale(scale, scale);
  126. drawData.Translate(-data.Origin.X, -data.Origin.Y);
  127. // drawData.Translate(-data.Origin.X - data.Size.Width / 2, -data.Origin.Y - data.Size.Height / 2);
  128. foreach(var el in data.Layout.AssociatedBlock.Entities)
  129. {
  130. var item = ConvertEl(el);
  131. item?.Draw(drawData);
  132. }
  133. graphics.Finish();
  134. }
  135. public static DxfData LoadDxf(string filename, DxfImportSettings? settings = null)
  136. {
  137. using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
  138. settings ??= new();
  139. var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
  140. document.BuildDimensionBlocks = true;
  141. return new(document, settings);
  142. }
  143. public static DxfData LoadDxf(Stream stream, DxfImportSettings? settings = null)
  144. {
  145. settings ??= new();
  146. var document = DxfDocument.Load(stream, settings.SupportFolders ?? Array.Empty<string>());
  147. return new(document, settings);
  148. }
  149. /// <summary>
  150. /// Returns <see langword="null"/> if the bounds are completely empty.
  151. /// </summary>
  152. /// <param name="data"></param>
  153. /// <returns></returns>
  154. public static RectangleF? CalculateDxfSize(DxfData data)
  155. {
  156. var transformData = new TransformData { Data = data };
  157. RectangleF? bounds = null;
  158. foreach(var el in data.Document.Entities.All)
  159. {
  160. if(ConvertEl(el) is IDxfObject obj)
  161. {
  162. bounds = Utils.CombineBounds(bounds, obj.GetBounds(transformData));
  163. }
  164. }
  165. return bounds;
  166. }
  167. public static Bitmap ProcessImage(DxfData data)
  168. {
  169. var height = data.Size.Height;
  170. var width = data.Size.Width;
  171. // Calculate the scaling factor to fit the image within the bounds
  172. float ratioX = (float)data.Settings.ImageSize.Width / width;
  173. float ratioY = (float)data.Settings.ImageSize.Height / height;
  174. var scale = Math.Min(ratioX, ratioY);
  175. var _result = new Bitmap((int)(width * scale), (int)(height * scale));
  176. using (var _graphics = Graphics.FromImage(_result))
  177. {
  178. _graphics.SmoothingMode = SmoothingMode.AntiAlias;
  179. DrawDxf(data, new GdiGraphics(_graphics), _result.Width, _result.Height);
  180. }
  181. _result.RotateFlip(RotateFlipType.RotateNoneFlipY);
  182. return _result;
  183. }
  184. public static PdfDocument ProcessPdf(DxfData data)
  185. {
  186. var doc = new PdfDocument();
  187. doc.PageSettings.Size = data.Size;
  188. doc.PageSettings.SetMargins(0);
  189. var page = doc.Pages.Add();
  190. var graphics = page.Graphics;
  191. var drawData = new DrawData() { Graphics = new PdfGraphics(page.Graphics), Data = data };
  192. drawData.PushTransform();
  193. drawData.Translate(data.Size.Width / 2, data.Size.Height / 2);
  194. drawData.Scale(1, -1);
  195. drawData.Translate(-data.Size.Width / 2, -data.Size.Height / 2);
  196. drawData.Translate(-data.Origin.X, -data.Origin.Y);
  197. if (data.Layout.IsPaperSpace)
  198. {
  199. var modelSpace = data.Document.Layouts.First(x => x.Name == "Model");
  200. foreach(var el in modelSpace.AssociatedBlock.Entities)
  201. {
  202. var item = ConvertEl(el);
  203. item?.Draw(drawData);
  204. }
  205. }
  206. foreach(var el in data.Layout.AssociatedBlock.Entities)
  207. {
  208. var item = ConvertEl(el);
  209. item?.Draw(drawData);
  210. }
  211. drawData.PopTransform();
  212. drawData.Graphics.Finish();
  213. return doc;
  214. }
  215. // public static PdfDocument ProcessPdf(DxfData data)
  216. // {
  217. // var doc = new PdfDocument();
  218. // var size = data.Size;
  219. // var origin = data.Origin;
  220. // foreach(var layout in data.LayoutNames)
  221. // {
  222. // data.LayoutName = layout;
  223. // Matrix4x4 transform;
  224. // if (data.Layout.IsPaperSpace)
  225. // {
  226. // var viewport = data.Layout.Viewport;
  227. // var scale = (float)(viewport.ViewHeight == 0 ? 0 : viewport.Height / viewport.ViewHeight);
  228. // data.Origin = new((float)(viewport.ViewCenter.X - viewport.Width / 2), (float)(viewport.ViewCenter.Y - viewport.Height / 2));
  229. // data.Size = new((float)viewport.Width, (float)viewport.Height);
  230. // var normal = -System.Numerics.Vector3.Cross(viewport.UcsXAxis.ToVec3(), viewport.UcsYAxis.ToVec3());
  231. // transform = Matrix4x4.CreateLookAt(viewport.UcsOrigin.ToVec3(), viewport.UcsOrigin.ToVec3() + normal, viewport.UcsYAxis.ToVec3())
  232. // * Matrix4x4.CreateScale(scale)
  233. // * Matrix4x4.CreateRotationZ((float)viewport.TwistAngle);
  234. // }
  235. // else
  236. // {
  237. // data.Size = size;
  238. // data.Origin = origin;
  239. // transform = Matrix4x4.Identity;
  240. // }
  241. // var section = doc.Sections.Add();
  242. // section.PageSettings.Size = data.Size;
  243. // section.PageSettings.SetMargins(0);
  244. // var page = section.Pages.Add();
  245. // var graphics = page.Graphics;
  246. // var drawData = new DrawData() { Graphics = new PdfGraphics(page.Graphics), Data = data };
  247. // drawData.PushTransform();
  248. // drawData.Translate(data.Size.Width / 2, data.Size.Height / 2);
  249. // drawData.Scale(1, -1);
  250. // drawData.Translate(-data.Size.Width / 2, -data.Size.Height / 2);
  251. // drawData.Translate(-data.Origin.X, -data.Origin.Y);
  252. // drawData.TransformBy(transform);
  253. // if (data.Layout.IsPaperSpace)
  254. // {
  255. // var modelSpace = data.Document.Layouts.First(x => x.Name == "Model");
  256. // foreach(var el in modelSpace.AssociatedBlock.Entities)
  257. // {
  258. // var item = ConvertEl(el);
  259. // item?.Draw(drawData);
  260. // }
  261. // }
  262. // foreach(var el in data.Layout.AssociatedBlock.Entities)
  263. // {
  264. // var item = ConvertEl(el);
  265. // item?.Draw(drawData);
  266. // }
  267. // drawData.PopTransform();
  268. // drawData.Graphics.Finish();
  269. // }
  270. // return doc;
  271. // }
  272. public static SvgDocument ProcessSvg(DxfData data)
  273. {
  274. var doc = new SvgDocument
  275. {
  276. ViewBox = new(data.Origin.X, data.Origin.Y, data.Size.Width, data.Size.Height)
  277. };
  278. var drawData = new DrawData() { Graphics = new SvgGraphics(doc), Data = data };
  279. foreach(var el in data.Layout.AssociatedBlock.Entities)
  280. {
  281. var item = ConvertEl(el);
  282. item?.Draw(drawData);
  283. }
  284. drawData.Graphics.Finish();
  285. return doc;
  286. }
  287. public static Bitmap ProcessImage(Stream stream, DxfImportSettings? settings = null)
  288. {
  289. return ProcessImage(LoadDxf(stream, settings));
  290. }
  291. public static Result<Bitmap, Exception> DXFToBitmap(string filename, DxfImportSettings? settings = null)
  292. {
  293. using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
  294. try
  295. {
  296. return Result.Ok(ProcessImage(stream, settings: settings));
  297. }
  298. catch (Exception e)
  299. {
  300. OnProcessError?.Invoke(e.Message);
  301. return Result.Error(e);
  302. }
  303. }
  304. }