Objects.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using InABox.Core;
  2. using netDxf.Entities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace InABox.Dxf;
  11. internal interface IDxfObject
  12. {
  13. void Draw(DrawData data);
  14. }
  15. internal class DxfLine : IDxfObject
  16. {
  17. public Line Line { get; set; }
  18. public void Draw(DrawData data)
  19. {
  20. if (!Line.IsVisible || !data.HasLayer(Line.Layer)) return;
  21. data.Graphics.DrawLine(new Pen(Color.Black, (float)Line.Thickness), DrawData.ConvertPoint(Line.StartPoint), DrawData.ConvertPoint(Line.EndPoint));
  22. }
  23. }
  24. internal class DxfInsert : IDxfObject
  25. {
  26. public Insert Insert { get; set; }
  27. public List<IDxfObject> Objects { get; set; }
  28. public DxfInsert(Insert insert)
  29. {
  30. Insert = insert;
  31. Objects = insert.Block.Entities.Select(DxfUtils.ConvertEl).NotNull().ToList();
  32. }
  33. public void Draw(DrawData data)
  34. {
  35. if (!Insert.IsVisible) return;
  36. data.PushTransform();
  37. data.Translate(new PointF((float)Insert.Position.X, (float)Insert.Position.Y));
  38. data.Rotate((float)Insert.Rotation);
  39. data.Scale((float)Insert.Scale.X, (float)Insert.Scale.Y);
  40. foreach(var obj in Objects)
  41. {
  42. obj.Draw(data);
  43. }
  44. data.PopTransform();
  45. }
  46. }
  47. internal class DxfEllipse : IDxfObject
  48. {
  49. public Ellipse Ellipse { get; set; }
  50. public DxfEllipse(Ellipse ellipse)
  51. {
  52. Ellipse = ellipse;
  53. }
  54. public void Draw(DrawData data)
  55. {
  56. if (!Ellipse.IsVisible || !data.HasLayer(Ellipse.Layer)) return;
  57. var center = DrawData.ConvertPoint(Ellipse.Center);
  58. var size = new SizeF((float)Ellipse.MajorAxis, (float)Ellipse.MinorAxis);
  59. data.Graphics.DrawEllipse(new Pen(Color.Black, (float)Ellipse.Thickness), center.X - size.Width / 2, center.Y - size.Height / 2, size.Width, size.Height);
  60. }
  61. }
  62. internal class DxfPolyline2D : IDxfObject
  63. {
  64. public Polyline2D Polyline { get; set; }
  65. public DxfPolyline2D(Polyline2D polyline)
  66. {
  67. Polyline = polyline;
  68. }
  69. public void Draw(DrawData data)
  70. {
  71. if (!Polyline.IsVisible || !data.HasLayer(Polyline.Layer)) return;
  72. if(Polyline.SmoothType == PolylineSmoothType.NoSmooth)
  73. {
  74. if (Polyline.Layer.Name != "Swipe Lines") return;
  75. var vertices = Polyline.Vertexes.ToArray(x => new PointF((float)x.Position.X, (float)x.Position.Y));
  76. if (Polyline.IsClosed)
  77. {
  78. data.Graphics.DrawPolygon(
  79. new Pen(Color.Black, (float)Polyline.Thickness),
  80. vertices);
  81. }
  82. else
  83. {
  84. data.Graphics.DrawLines(
  85. new Pen(Color.Black, (float)Polyline.Thickness),
  86. vertices);
  87. }
  88. }
  89. else
  90. {
  91. }
  92. }
  93. }
  94. internal class DxfMText : IDxfObject
  95. {
  96. public MText MText { get; set; }
  97. public DxfMText(MText text)
  98. {
  99. MText = text;
  100. }
  101. public void Draw(DrawData data)
  102. {
  103. if (!MText.IsVisible || !data.HasLayer(MText.Layer)) return;
  104. var fontFamily = new FontFamily(MText.Style.FontFamilyName);
  105. var font = new Font(fontFamily, (float)MText.Height, MText.Style.FontStyle switch
  106. {
  107. netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
  108. netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
  109. netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
  110. });
  111. data.PushTransform();
  112. data.Translate(new PointF((float)MText.Position.X, (float)MText.Position.Y));
  113. data.Rotate((float)MText.Rotation);
  114. data.Scale(1, -1);
  115. var size = data.Graphics.MeasureString(MText.PlainText(), font);
  116. switch (MText.AttachmentPoint)
  117. {
  118. case MTextAttachmentPoint.MiddleLeft:
  119. case MTextAttachmentPoint.MiddleCenter:
  120. case MTextAttachmentPoint.MiddleRight:
  121. data.Translate(new PointF(0, -size.Height / 2));
  122. break;
  123. case MTextAttachmentPoint.BottomLeft:
  124. case MTextAttachmentPoint.BottomCenter:
  125. case MTextAttachmentPoint.BottomRight:
  126. data.Translate(new PointF(0, -size.Height));
  127. break;
  128. }
  129. switch (MText.AttachmentPoint)
  130. {
  131. case MTextAttachmentPoint.TopLeft:
  132. case MTextAttachmentPoint.MiddleLeft:
  133. case MTextAttachmentPoint.BottomLeft:
  134. break;
  135. case MTextAttachmentPoint.TopCenter:
  136. case MTextAttachmentPoint.MiddleCenter:
  137. case MTextAttachmentPoint.BottomCenter:
  138. data.Translate(new PointF(-(float)size.Width / 2, 0));
  139. break;
  140. case MTextAttachmentPoint.TopRight:
  141. case MTextAttachmentPoint.MiddleRight:
  142. case MTextAttachmentPoint.BottomRight:
  143. data.Translate(new PointF(-(float)size.Width, 0));
  144. break;
  145. }
  146. data.Graphics.DrawString(MText.PlainText(), font, new SolidBrush(Color.Black), new PointF(0, 0));
  147. data.PopTransform();
  148. }
  149. }
  150. internal class DxfDimension : IDxfObject
  151. {
  152. public Dimension Dimension { get; set; }
  153. public DxfDimension(Dimension dimension)
  154. {
  155. Dimension = dimension;
  156. }
  157. public void Draw(DrawData data)
  158. {
  159. if (!Dimension.IsVisible || !data.HasLayer(Dimension.Layer)) return;
  160. if(Dimension is AlignedDimension aligned)
  161. {
  162. data.Graphics.DrawLine(new Pen(Color.Black, (float)10), DrawData.ConvertPoint(aligned.FirstReferencePoint), DrawData.ConvertPoint(aligned.SecondReferencePoint));
  163. }
  164. }
  165. }