DxfParserService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using netDxf;
  2. using netDxf.Entities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using DxfLine = netDxf.Entities.Line;
  10. namespace TeamAAS_VP.DxfModule
  11. {
  12. public class DxfParserService
  13. {
  14. public DxfDocument Document { get; private set; }
  15. public bool LoadDxfFile(string filePath)
  16. {
  17. try
  18. {
  19. if (!File.Exists(filePath))
  20. return false;
  21. Document = DxfDocument.Load(filePath);
  22. return Document != null;
  23. }
  24. catch (Exception ex)
  25. {
  26. Console.WriteLine($"Error loading DXF file: {ex.Message}");
  27. return false;
  28. }
  29. }
  30. public List<EntityObject> GetAllEntities()
  31. {
  32. if (Document == null)
  33. return new List<EntityObject>();
  34. var entities = new List<EntityObject>();
  35. // netDxf uses Document.Layouts to access entity collections
  36. foreach (var layout in Document.Layouts)
  37. {
  38. entities.AddRange(layout.AssociatedBlock.Entities);
  39. }
  40. return entities;
  41. }
  42. public Dictionary<string, int> GetEntityStatistics()
  43. {
  44. if (Document == null)
  45. return new Dictionary<string, int>();
  46. var entities = GetAllEntities();
  47. var grouped = entities.GroupBy(e => e.Type.ToString());
  48. var stats = new Dictionary<string, int>();
  49. foreach (var group in grouped)
  50. {
  51. stats[group.Key] = group.Count();
  52. }
  53. return stats;
  54. }
  55. public (double minX, double minY, double maxX, double maxY) GetBounds()
  56. {
  57. var entities = GetAllEntities();
  58. if (entities.Count == 0)
  59. return (0, 0, 0, 0);
  60. double minX = double.MaxValue;
  61. double minY = double.MaxValue;
  62. double maxX = double.MinValue;
  63. double maxY = double.MinValue;
  64. foreach (var entity in entities)
  65. {
  66. var bounds = GetEntityBounds(entity);
  67. minX = Math.Min(minX, bounds.minX);
  68. minY = Math.Min(minY, bounds.minY);
  69. maxX = Math.Max(maxX, bounds.maxX);
  70. maxY = Math.Max(maxY, bounds.maxY);
  71. }
  72. return (minX, minY, maxX, maxY);
  73. }
  74. private (double minX, double minY, double maxX, double maxY) GetEntityBounds(EntityObject entity)
  75. {
  76. switch (entity)
  77. {
  78. case DxfLine line:
  79. return (
  80. Math.Min(line.StartPoint.X, line.EndPoint.X),
  81. Math.Min(line.StartPoint.Y, line.EndPoint.Y),
  82. Math.Max(line.StartPoint.X, line.EndPoint.X),
  83. Math.Max(line.StartPoint.Y, line.EndPoint.Y)
  84. );
  85. case Circle circle:
  86. return (
  87. circle.Center.X - circle.Radius,
  88. circle.Center.Y - circle.Radius,
  89. circle.Center.X + circle.Radius,
  90. circle.Center.Y + circle.Radius
  91. );
  92. case Arc arc:
  93. return (
  94. arc.Center.X - arc.Radius,
  95. arc.Center.Y - arc.Radius,
  96. arc.Center.X + arc.Radius,
  97. arc.Center.Y + arc.Radius
  98. );
  99. default:
  100. return (0, 0, 0, 0);
  101. }
  102. }
  103. }
  104. }