Objects.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using InABox.Core;
  2. using netDxf;
  3. using netDxf.Blocks;
  4. using netDxf.Entities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace InABox.Dxf;
  13. internal interface IDxfObject
  14. {
  15. void Draw(DrawData data);
  16. RectangleF? GetBounds(TransformData data);
  17. }
  18. internal class DxfLine : IDxfObject
  19. {
  20. public Line Line { get; set; }
  21. public void Draw(DrawData data)
  22. {
  23. if (!data.Data.ShouldDraw(Line)) return;
  24. data.PushTransform();
  25. //data.ArbitraryAxis(Line.Normal);
  26. data.Graphics.DrawLine(
  27. data.ResolveColour(Line.Color, Line), (float)Line.Thickness,
  28. DrawData.ConvertPoint(Line.StartPoint), DrawData.ConvertPoint(Line.EndPoint));
  29. data.PopTransform();
  30. }
  31. public RectangleF? GetBounds(TransformData data)
  32. {
  33. if (!data.Data.ShouldDraw(Line)) return null;
  34. return Utils.RectangleFromPoints(
  35. data.TransformPoint(Line.StartPoint),
  36. data.TransformPoint(Line.EndPoint));
  37. }
  38. }
  39. internal class DxfInsert : IDxfObject
  40. {
  41. public Insert Insert { get; set; }
  42. public List<IDxfObject> Objects { get; set; }
  43. public DxfInsert(Insert insert)
  44. {
  45. Insert = insert;
  46. Objects = insert.Block.Entities.Select(DxfUtils.ConvertEl).NotNull().ToList();
  47. }
  48. public void Draw(DrawData data)
  49. {
  50. if (!data.Data.ShouldDraw(Insert)) return;
  51. // var transformation = Insert.GetTransformation();
  52. // var translation = Insert.Position - transformation * Insert.Block.Origin;
  53. data.PushTransform();
  54. data.Translate(new PointF((float)Insert.Position.X, (float)Insert.Position.Y));
  55. data.ArbitraryAxis(Insert.Normal);
  56. data.Rotate((float)Insert.Rotation);
  57. data.Scale((float)Insert.Scale.X, (float)Insert.Scale.Y);
  58. data.PushBlockColour(Insert.Color, Insert);
  59. foreach(var entity in Objects)
  60. {
  61. entity.Draw(data);
  62. }
  63. data.PopBlockColour();
  64. data.PopTransform();
  65. // foreach(var entity in Insert.Explode())
  66. // {
  67. // // var ent = entity.Clone() as EntityObject;
  68. // // ent.TransformBy(transformation, translation);
  69. // DxfUtils.ConvertEl(entity)?.Draw(data);
  70. // }
  71. // foreach(var obj in Insert.Explode())
  72. // {
  73. // DxfUtils.ConvertEl(obj)?.Draw(data);
  74. // }
  75. }
  76. public RectangleF? GetBounds(TransformData data)
  77. {
  78. if (!data.Data.ShouldDraw(Insert)) return null;
  79. data.PushTransform();
  80. data.Translate(new PointF((float)Insert.Position.X, (float)Insert.Position.Y));
  81. data.ArbitraryAxis(Insert.Normal);
  82. data.Rotate((float)Insert.Rotation);
  83. data.Scale((float)Insert.Scale.X, (float)Insert.Scale.Y);
  84. var bounds = Utils.CombineBounds(Objects.Select(x => x.GetBounds(data)));
  85. data.PopTransform();
  86. return bounds;
  87. }
  88. }
  89. internal class DxfEllipse : IDxfObject
  90. {
  91. public Ellipse Ellipse { get; set; }
  92. public DxfEllipse(Ellipse ellipse)
  93. {
  94. Ellipse = ellipse;
  95. }
  96. public void Draw(DrawData data)
  97. {
  98. if (!data.Data.ShouldDraw(Ellipse)) return;
  99. foreach(var obj in Ellipse.ToPolyline2D(100).Explode())
  100. {
  101. DxfUtils.ConvertEl(obj)?.Draw(data);
  102. }
  103. // var center = DrawData.ConvertPoint(Ellipse.Center);
  104. // var size = new SizeF((float)Ellipse.MajorAxis, (float)Ellipse.MinorAxis);
  105. // var startAngle = (float)(Ellipse.StartAngle);
  106. // var endAngle = (float)(Ellipse.EndAngle);
  107. // data.Graphics.DrawArc(new Pen(Color.Black, data.ConvertThickness((float)Ellipse.Thickness)), center.X - size.Width / 2, center.Y - size.Height / 2, size.Width, size.Height, startAngle, Utils.Mod(endAngle - startAngle, 360));
  108. }
  109. public RectangleF? GetBounds(TransformData data)
  110. {
  111. if (!data.Data.ShouldDraw(Ellipse)) return null;
  112. var halfSize = new netDxf.Vector3(Ellipse.MajorAxis / 2, Ellipse.MinorAxis / 2, 0);
  113. return Utils.RectangleFromPoints(
  114. data.TransformPoint(Ellipse.Center - halfSize),
  115. data.TransformPoint(Ellipse.Center + halfSize));
  116. }
  117. }
  118. internal class DxfSolid : IDxfObject
  119. {
  120. public Solid Solid { get; set; }
  121. public DxfSolid(Solid solid)
  122. {
  123. Solid = solid;
  124. }
  125. public void Draw(DrawData data)
  126. {
  127. if (!data.Data.ShouldDraw(Solid)) return;
  128. var vertices = new Vector2[]
  129. {
  130. Solid.FirstVertex,
  131. Solid.SecondVertex,
  132. Solid.FourthVertex, // Apparently the third and fourth are the wrong way round, so I've mirrored that here.
  133. Solid.ThirdVertex
  134. };
  135. data.Graphics.FillPolygon(data.ResolveColour(Solid.Color, Solid), vertices.ToArray(x => DrawData.ConvertPoint(x)));
  136. }
  137. public RectangleF? GetBounds(TransformData data)
  138. {
  139. if (!data.Data.ShouldDraw(Solid)) return null;
  140. var vertices = new Vector2[]
  141. {
  142. Solid.FirstVertex,
  143. Solid.SecondVertex,
  144. Solid.FourthVertex, // Apparently the third and fourth are the wrong way round, so I've mirrored that here.
  145. Solid.ThirdVertex
  146. };
  147. return Utils.RectangleFromPoints(
  148. vertices.ToArray(data.TransformPoint));
  149. }
  150. }
  151. internal class DxfPolyline2D : IDxfObject
  152. {
  153. public Polyline2D Polyline { get; set; }
  154. public DxfPolyline2D(Polyline2D polyline)
  155. {
  156. Polyline = polyline;
  157. }
  158. public void Draw(DrawData data)
  159. {
  160. if (!data.Data.ShouldDraw(Polyline)) return;
  161. var entities = Polyline.Explode();
  162. foreach(var entity in entities)
  163. {
  164. DxfUtils.ConvertEl(entity)?.Draw(data);
  165. }
  166. // if(Polyline.SmoothType == PolylineSmoothType.NoSmooth)
  167. // {
  168. // var vertices = Polyline.Vertexes.ToArray(x => new PointF((float)x.Position.X, (float)x.Position.Y));
  169. // if (Polyline.IsClosed)
  170. // {
  171. // data.Graphics.DrawPolygon(
  172. // new Pen(Color.Black, data.ConvertThickness((float)Polyline.Thickness)),
  173. // vertices);
  174. // }
  175. // else
  176. // {
  177. // data.Graphics.DrawLines(
  178. // new Pen(Color.Black, data.ConvertThickness((float)Polyline.Thickness)),
  179. // vertices);
  180. // }
  181. // }
  182. // else
  183. // {
  184. // }
  185. }
  186. public RectangleF? GetBounds(TransformData data)
  187. {
  188. if (!data.Data.ShouldDraw(Polyline)) return null;
  189. var entities = Polyline.Explode();
  190. return Utils.CombineBounds(entities.Select(x => DxfUtils.ConvertEl(x)?.GetBounds(data)));
  191. }
  192. }
  193. internal class DxfMText : IDxfObject
  194. {
  195. public MText MText { get; set; }
  196. public DxfMText(MText text)
  197. {
  198. MText = text;
  199. }
  200. private Font GetFont()
  201. {
  202. FontFamily fontFamily;
  203. if (MText.Style.FontFamilyName.IsNullOrWhiteSpace())
  204. {
  205. fontFamily = SystemFonts.DefaultFont.FontFamily;
  206. }
  207. else
  208. {
  209. fontFamily = new FontFamily(MText.Style.FontFamilyName);
  210. }
  211. return new Font(fontFamily, (float)MText.Height, MText.Style.FontStyle switch
  212. {
  213. netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
  214. netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
  215. netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
  216. });
  217. }
  218. private string GetText()
  219. {
  220. return MText.PlainText().Replace("^M", "");
  221. }
  222. private static Bitmap _placeholderBitmap = new Bitmap(1, 1);
  223. private void Transform(TransformData data, Font font, string text, Graphics g)
  224. {
  225. data.Translate(new PointF((float)MText.Position.X, (float)MText.Position.Y));
  226. data.Rotate((float)MText.Rotation);
  227. data.Scale(1, -1);
  228. var size = g.MeasureString(text, font, new PointF(), StringFormat.GenericTypographic);
  229. switch (MText.AttachmentPoint)
  230. {
  231. case MTextAttachmentPoint.MiddleLeft:
  232. case MTextAttachmentPoint.MiddleCenter:
  233. case MTextAttachmentPoint.MiddleRight:
  234. data.Translate(new PointF(0, -size.Height / 2));
  235. break;
  236. case MTextAttachmentPoint.BottomLeft:
  237. case MTextAttachmentPoint.BottomCenter:
  238. case MTextAttachmentPoint.BottomRight:
  239. data.Translate(new PointF(0, -size.Height));
  240. break;
  241. default:
  242. var ascent = font.FontFamily.GetCellAscent(font.Style);
  243. var lineSpace = font.FontFamily.GetLineSpacing(font.Style);
  244. var baseline = ascent * font.Height / font.FontFamily.GetEmHeight(font.Style);
  245. var ratio = font.GetHeight(g) / lineSpace;
  246. data.Translate(new PointF(0, -baseline + ascent * ratio));
  247. break;
  248. }
  249. switch (MText.AttachmentPoint)
  250. {
  251. case MTextAttachmentPoint.TopLeft:
  252. case MTextAttachmentPoint.MiddleLeft:
  253. case MTextAttachmentPoint.BottomLeft:
  254. break;
  255. case MTextAttachmentPoint.TopCenter:
  256. case MTextAttachmentPoint.MiddleCenter:
  257. case MTextAttachmentPoint.BottomCenter:
  258. data.Translate(new PointF(-(float)size.Width / 2, 0));
  259. break;
  260. case MTextAttachmentPoint.TopRight:
  261. case MTextAttachmentPoint.MiddleRight:
  262. case MTextAttachmentPoint.BottomRight:
  263. data.Translate(new PointF(-(float)size.Width, 0));
  264. break;
  265. }
  266. }
  267. public void Draw(DrawData data)
  268. {
  269. if (!data.Data.ShouldDraw(MText)) return;
  270. var text = GetText();
  271. if (MText.Style.FontFamilyName.IsNullOrWhiteSpace())
  272. {
  273. data.Graphics.SetFont((float)MText.Height, MText.Style.FontStyle switch
  274. {
  275. netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
  276. netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
  277. netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
  278. });
  279. }
  280. else
  281. {
  282. data.Graphics.SetFont(MText.Style.FontFamilyName, (float)MText.Height, MText.Style.FontStyle switch
  283. {
  284. netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
  285. netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
  286. netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
  287. });
  288. }
  289. var format = new TextFormat
  290. {
  291. LineAlignment = MText.AttachmentPoint switch
  292. {
  293. MTextAttachmentPoint.MiddleLeft or MTextAttachmentPoint.MiddleCenter or MTextAttachmentPoint.MiddleRight => TextLineAlignment.Center,
  294. MTextAttachmentPoint.BottomLeft or MTextAttachmentPoint.BottomCenter or MTextAttachmentPoint.BottomRight => TextLineAlignment.Bottom,
  295. _ => TextLineAlignment.Top,
  296. },
  297. Alignment = MText.AttachmentPoint switch
  298. {
  299. MTextAttachmentPoint.TopCenter or MTextAttachmentPoint.MiddleCenter or MTextAttachmentPoint.BottomCenter => TextAlignment.Center,
  300. MTextAttachmentPoint.TopRight or MTextAttachmentPoint.MiddleRight or MTextAttachmentPoint.BottomRight => TextAlignment.Right,
  301. _ => TextAlignment.Left,
  302. }
  303. };
  304. data.Graphics.DrawText(text, data.ResolveColour(MText.Color, MText), DrawData.ConvertPoint(MText.Position), (float)MText.Rotation, format);
  305. }
  306. public RectangleF? GetBounds(TransformData data)
  307. {
  308. if (!data.Data.ShouldDraw(MText)) return null;
  309. var font = GetFont();
  310. var text = GetText();
  311. data.PushTransform();
  312. using var g = Graphics.FromImage(_placeholderBitmap);
  313. Transform(data, font, text, g);
  314. var size = g.MeasureString(text, font, new PointF(), StringFormat.GenericTypographic);
  315. var bounds = Utils.RectangleFromPoints(
  316. data.TransformPoint(0, 0),
  317. data.TransformPoint(size.Width, size.Height));
  318. data.PopTransform();
  319. return bounds;
  320. }
  321. }
  322. internal class DxfDimension : IDxfObject
  323. {
  324. public Dimension Dimension { get; set; }
  325. public List<IDxfObject> Objects { get; set; }
  326. public DxfDimension(Dimension dimension)
  327. {
  328. Dimension = dimension;
  329. if(Dimension.Block is null)
  330. {
  331. Objects = new();
  332. }
  333. else
  334. {
  335. Objects = Dimension.Block.Entities.Select(DxfUtils.ConvertEl).NotNull().ToList();
  336. }
  337. }
  338. public void Draw(DrawData data)
  339. {
  340. if (!data.Data.ShouldDraw(Dimension)) return;
  341. data.PushBlockColour(Dimension.Color, Dimension);
  342. foreach(var entity in Objects)
  343. {
  344. entity.Draw(data);
  345. }
  346. data.PopBlockColour();
  347. }
  348. public RectangleF? GetBounds(TransformData data)
  349. {
  350. if (!data.Data.ShouldDraw(Dimension)) return null;
  351. return Utils.CombineBounds(Objects.Select(x => x.GetBounds(data)));
  352. }
  353. }