DxfUtils.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. using netDxf;
  4. using netDxf.Entities;
  5. using Point = System.Drawing.Point;
  6. namespace InABox.Dxf
  7. {
  8. public static class DxfUtils
  9. {
  10. public static event ProcessError OnProcessError;
  11. // Draw a rotated string at a particular position.
  12. private static void DrawRotatedTextAt(Graphics gr, float angle,
  13. string txt, float x, float y, Font the_font, Brush the_brush)
  14. {
  15. // Save the graphics state.
  16. var state = gr.Save();
  17. gr.ResetTransform();
  18. // Rotate.
  19. gr.RotateTransform(angle);
  20. // Translate to desired position. Be sure to append
  21. // the rotation so it occurs after the rotation.
  22. gr.TranslateTransform(x, y, MatrixOrder.Append);
  23. // Draw the text at the origin.
  24. gr.DrawString(txt, the_font, the_brush, 0, 0);
  25. // Restore the graphics state.
  26. gr.Restore(state);
  27. }
  28. private static void CheckPoints(double x, double y, ref double minX, ref double minY, ref double maxX, ref double maxY)
  29. {
  30. minX = x < minX ? x : minX;
  31. minY = y < minY ? y : minY;
  32. maxX = x > maxX ? x : maxX;
  33. maxY = y > maxY ? y : maxY;
  34. }
  35. private static float Scale(int value, float offset, double scale, int border)
  36. {
  37. return (value - offset) * (float)scale + border;
  38. }
  39. private static float Scale(double value, float offset, int scale, int border)
  40. {
  41. return ((float)value - offset) * scale + border;
  42. }
  43. private static PointF VectorToPointF(Vector3 vector, PointF offset, int scale, Point border)
  44. {
  45. return new PointF(Scale(vector.X, offset.X, scale, border.X), Scale(vector.Y, offset.Y, scale, border.Y));
  46. }
  47. private static PointF VectorToPointF(Vector2 vector, PointF offset, int scale, Point border)
  48. {
  49. return new PointF(Scale(vector.X, offset.X, scale, border.X), Scale(vector.Y, offset.Y, scale, border.Y));
  50. }
  51. public delegate void ProcessError(string message);
  52. public static Bitmap ProcessImage(Stream stream, string caption)
  53. {
  54. var minX = double.MaxValue;
  55. var minY = double.MaxValue;
  56. var maxX = double.MinValue;
  57. var maxY = double.MinValue;
  58. var document = DxfDocument.Load(stream);
  59. // Lets Explode all the various bits into their component atoms
  60. // (i.e. everything is gonna be a line)
  61. var lines = new List<Line>();
  62. lines.AddRange(document.Entities.Lines);
  63. foreach (var polyline in document.Entities.Polylines2D)
  64. {
  65. var components = polyline.Explode();
  66. lines.AddRange(components.Where(x => x is Line).Select(x => x as Line));
  67. foreach (Arc arc in components.Where(x => x is Arc))
  68. ArcToLines(lines, arc);
  69. }
  70. foreach (var polyline in document.Entities.Polylines2D)
  71. {
  72. var components = polyline.Explode();
  73. lines.AddRange(components.Where(x => x is Line).Select(x => x as Line));
  74. foreach (Arc arc in components.Where(x => x is Arc))
  75. ArcToLines(lines, arc);
  76. }
  77. foreach (var arc in document.Entities.Arcs)
  78. ArcToLines(lines, arc);
  79. foreach (var ellipse in document.Entities.Ellipses)
  80. {
  81. var precision = (int)((ellipse.MajorAxis + ellipse.MinorAxis) * 2.0F * Math.PI);
  82. var polyline = ellipse.ToPolyline2D(precision);
  83. var segments = polyline.Explode();
  84. lines.AddRange(segments.Select(x => x as Line));
  85. }
  86. if (!lines.Any())
  87. throw new Exception(string.Format("No Data Found in {0}!", caption));
  88. foreach (var line in lines)
  89. {
  90. CheckPoints(line.StartPoint.X, line.StartPoint.Y, ref minX, ref minY, ref maxX, ref maxY);
  91. CheckPoints(line.EndPoint.X, line.EndPoint.Y, ref minX, ref minY, ref maxX, ref maxY);
  92. }
  93. var height = (int)Math.Abs(maxY - minY);
  94. var width = (int)Math.Abs(maxX - minX);
  95. var scale = 15;
  96. var margin = 100;
  97. var offset = new PointF((float)minX, (float)minY);
  98. var border = new Point(margin, margin);
  99. var result = new Bitmap(width * scale + margin * 2, height * scale + margin * 2);
  100. var pen = new Pen(new SolidBrush(Color.Black), 3.0F);
  101. using (var g = Graphics.FromImage(result))
  102. {
  103. Brush bg = new SolidBrush(Color.White);
  104. g.FillRectangle(bg, 0, 0, result.Width, result.Height);
  105. foreach (var line in lines)
  106. {
  107. var start = VectorToPointF(line.StartPoint, offset, scale, border);
  108. var end = VectorToPointF(line.EndPoint, offset, scale, border);
  109. g.DrawLine(pen, start, end);
  110. }
  111. var font = new Font("Arial", 20.0F);
  112. var brush = new SolidBrush(Color.Blue);
  113. pen = new Pen(brush);
  114. var crect = g.MeasureString(caption, font);
  115. g.DrawString(caption, font, brush, new PointF(result.Width / 2.0F - crect.Width / 2.0F, 10));
  116. var heightrect = g.MeasureString(height.ToString(), font);
  117. g.DrawLine(pen, new PointF(result.Width - (heightrect.Height + 10.0F), 100), new PointF(result.Width - 30, 100));
  118. g.DrawLine(pen, new PointF(result.Width - (heightrect.Height + 10.0F), result.Height - 100),
  119. new PointF(result.Width - 30, result.Height - 100));
  120. g.DrawLine(pen, new PointF(result.Width - (20.0F + heightrect.Height / 2.0F), 105),
  121. new PointF(result.Width - (20.0F + heightrect.Height / 2.0F), result.Height - 105));
  122. var aX = result.Width - (20.0F + heightrect.Height / 2.0F);
  123. float aY = 105;
  124. g.FillPolygon(brush, new[]
  125. {
  126. new(aX, aY),
  127. new PointF(aX - 10.0F, aY + 10.0F),
  128. new PointF(aX + 10.0F, aY + 10.0F)
  129. });
  130. aY = result.Height - 105;
  131. g.FillPolygon(brush, new[]
  132. {
  133. new(aX, aY),
  134. new PointF(aX - 10.0F, aY - 10.0F),
  135. new PointF(aX + 10.0F, aY - 10.0F)
  136. });
  137. g.FillRectangle(bg, result.Width - (heightrect.Height + 15.0F), result.Height / 2.0F - heightrect.Width / 2.0F, heightrect.Height,
  138. heightrect.Width);
  139. DrawRotatedTextAt(g, 270.0F, height.ToString(), result.Width - (heightrect.Height + 15.0F),
  140. result.Height / 2.0F + heightrect.Width / 2.0F, font, brush);
  141. var widthrect = g.MeasureString(width.ToString(), font);
  142. g.DrawLine(pen, new PointF(100, result.Height - (widthrect.Height + 10.0F)), new PointF(100, result.Height - 30));
  143. g.DrawLine(pen, new PointF(result.Width - 100, result.Height - (widthrect.Height + 10.0F)),
  144. new PointF(result.Width - 100, result.Height - 30));
  145. g.DrawLine(pen, new PointF(105, result.Height - (20.0F + widthrect.Height / 2.0F)),
  146. new PointF(result.Width - 105, result.Height - (20.0F + widthrect.Height / 2.0F)));
  147. aX = 105;
  148. aY = result.Height - (20.0F + widthrect.Height / 2.0F);
  149. g.FillPolygon(brush, new[]
  150. {
  151. new(aX, aY),
  152. new PointF(aX + 10.0F, aY - 10.0F),
  153. new PointF(aX + 10.0F, aY + 10.0F)
  154. });
  155. aX = result.Width - 105;
  156. g.FillPolygon(brush, new[]
  157. {
  158. new(aX, aY),
  159. new PointF(aX - 10.0F, aY - 10.0F),
  160. new PointF(aX - 10.0F, aY + 10.0F)
  161. });
  162. g.FillRectangle(bg, result.Width / 2.0F - widthrect.Width / 2.0F, result.Height - (widthrect.Height + 15.0F), widthrect.Width,
  163. widthrect.Height);
  164. g.DrawString(width.ToString(), font, brush,
  165. new PointF(result.Width / 2.0F - widthrect.Width / 2.0F, result.Height - (widthrect.Height + 15.0F)));
  166. }
  167. return result;
  168. }
  169. private static void ArcToLines(List<Line> lines, Arc arc)
  170. {
  171. var precision = Math.Max(2, (int)(arc.Radius * 2.0F * Math.PI));
  172. var polyline = arc.ToPolyline2D(precision);
  173. var segments = polyline.Explode();
  174. lines.AddRange(segments.Select(x => x as Line));
  175. }
  176. public static Bitmap? DXFToBitmap(string filename)
  177. {
  178. using var stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
  179. try
  180. {
  181. return ProcessImage(stream, Path.GetFileNameWithoutExtension(filename));
  182. }
  183. catch (Exception e)
  184. {
  185. OnProcessError?.Invoke(e.Message);
  186. return null;
  187. }
  188. }
  189. }
  190. }