DxfUtils.cs 9.2 KB

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