| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using netDxf;
- using netDxf.Entities;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using DxfLine = netDxf.Entities.Line;
- namespace TeamAAS_VP.DxfModule
- {
- public class DxfParserService
- {
- public DxfDocument Document { get; private set; }
- public bool LoadDxfFile(string filePath)
- {
- try
- {
- if (!File.Exists(filePath))
- return false;
- Document = DxfDocument.Load(filePath);
- return Document != null;
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Error loading DXF file: {ex.Message}");
- return false;
- }
- }
- public List<EntityObject> GetAllEntities()
- {
- if (Document == null)
- return new List<EntityObject>();
- var entities = new List<EntityObject>();
- // netDxf uses Document.Layouts to access entity collections
- foreach (var layout in Document.Layouts)
- {
- entities.AddRange(layout.AssociatedBlock.Entities);
- }
- return entities;
- }
- public Dictionary<string, int> GetEntityStatistics()
- {
- if (Document == null)
- return new Dictionary<string, int>();
- var entities = GetAllEntities();
- var grouped = entities.GroupBy(e => e.Type.ToString());
- var stats = new Dictionary<string, int>();
- foreach (var group in grouped)
- {
- stats[group.Key] = group.Count();
- }
- return stats;
- }
- public (double minX, double minY, double maxX, double maxY) GetBounds()
- {
- var entities = GetAllEntities();
- if (entities.Count == 0)
- return (0, 0, 0, 0);
- double minX = double.MaxValue;
- double minY = double.MaxValue;
- double maxX = double.MinValue;
- double maxY = double.MinValue;
- foreach (var entity in entities)
- {
- var bounds = GetEntityBounds(entity);
- minX = Math.Min(minX, bounds.minX);
- minY = Math.Min(minY, bounds.minY);
- maxX = Math.Max(maxX, bounds.maxX);
- maxY = Math.Max(maxY, bounds.maxY);
- }
- return (minX, minY, maxX, maxY);
- }
- private (double minX, double minY, double maxX, double maxY) GetEntityBounds(EntityObject entity)
- {
- switch (entity)
- {
- case DxfLine line:
- return (
- Math.Min(line.StartPoint.X, line.EndPoint.X),
- Math.Min(line.StartPoint.Y, line.EndPoint.Y),
- Math.Max(line.StartPoint.X, line.EndPoint.X),
- Math.Max(line.StartPoint.Y, line.EndPoint.Y)
- );
- case Circle circle:
- return (
- circle.Center.X - circle.Radius,
- circle.Center.Y - circle.Radius,
- circle.Center.X + circle.Radius,
- circle.Center.Y + circle.Radius
- );
- case Arc arc:
- return (
- arc.Center.X - arc.Radius,
- arc.Center.Y - arc.Radius,
- arc.Center.X + arc.Radius,
- arc.Center.Y + arc.Radius
- );
- default:
- return (0, 0, 0, 0);
- }
- }
- }
- }
|