Spaces:
Sleeping
Sleeping
# import ezdxf | |
# # Load the DXF file | |
# doc = ezdxf.readfile("out.dxf") | |
# # Iterate through all entities in the modelspace | |
# for entity in doc.modelspace(): | |
# entity_type = entity.dxftype() # Get the entity type | |
# print(f"Entity Type: {entity_type}") | |
# # Handle different entity types | |
# if entity_type == "LINE": | |
# print(f"Start: {entity.dxf.start}, End: {entity.dxf.end}") | |
# elif entity_type == "CIRCLE": | |
# print(f"Center: {entity.dxf.center}, Radius: {entity.dxf.radius}") | |
# elif entity_type == "ARC": | |
# print(f"Center: {entity.dxf.center}, Radius: {entity.dxf.radius}, Start Angle: {entity.dxf.start_angle}, End Angle: {entity.dxf.end_angle}") | |
# elif entity_type == "SPLINE": | |
# if entity.control_points: | |
# print(f"Control Points: {entity.control_points}") | |
# elif entity.fit_points: | |
# print(f"Fit Points: {entity.fit_points}") | |
# elif entity.knots: | |
# print(f"Knots: {entity.knots}") | |
# else: | |
# print("No control, fit, or knot points found for this SPLINE.") | |
# else: | |
# print(f"No specific handler for entity type: {entity_type}") | |
import numpy as np | |
import ezdxf | |
# Load the DXF file | |
doc = ezdxf.readfile("out.dxf") | |
def calculate_distance(p1, p2): | |
"""Calculate the distance between two points.""" | |
return np.linalg.norm(np.array(p1) - np.array(p2)) | |
def process_fit_points(fit_points): | |
"""Process fit points to calculate distances and bounding box.""" | |
distances = [] | |
for i in range(len(fit_points) - 1): | |
distances.append(calculate_distance(fit_points[i], fit_points[i + 1])) | |
# Calculate perimeter | |
perimeter = sum(distances) | |
# Calculate bounding box | |
fit_points_np = np.array(fit_points) | |
min_x, min_y = np.min(fit_points_np[:, :2], axis=0) | |
max_x, max_y = np.max(fit_points_np[:, :2], axis=0) | |
return { | |
"distances": distances, | |
"perimeter": perimeter, | |
"bounding_box": (min_x, min_y, max_x, max_y) | |
} | |
# Iterate through all entities in the modelspace | |
for entity in doc.modelspace(): | |
if entity.dxftype() == "SPLINE" and entity.fit_points: | |
print(f"Entity Type: SPLINE") | |
fit_points = entity.fit_points | |
results = process_fit_points(fit_points) | |
print(f"Perimeter: {results['perimeter']}") | |
print(f"Bounding Box: {results['bounding_box']}") | |
print(f"Distances: {results['distances'][:]}... (showing first 5)") | |