DxfUtils.cs 12 KB

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