Spaces:
Running
Running
File size: 2,564 Bytes
a88b356 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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)")
|