LWPOLYLINE "bulge" to ARC conversion Area and Perimeter of entity #826
Replies: 3 comments 5 replies
-
The |
Beta Was this translation helpful? Give feedback.
-
Here is the code to solve a fraction of the above problem. from ezdxf.math import bulge_to_arc, bulge_center
from ezdxf.math.bulge import signed_bulge_radius
from ezdxf.acc.vector import Vec2
from ezdxf import math
dwg = ezdxf.readfile('your_dxf_file.dxf')
modelspace = dwg.modelspace()
# Get the LWPOLYLINE entities from the DXF file
entities = dwg.entities
lwpolylines = [e for e in entities if e.dxftype() == 'LWPOLYLINE']
for pline in lwpolylines:
for index, (x, y, start_width, end_width, bulge) in enumerate(pline):
start_pline = pline[index+0]
start_point = (start_pline[0], start_pline[1])
if index+1 >= len(pline):
break
next_pline = pline[index+1]
end_point = (next_pline[0], next_pline[1])
# Check if the start & end points are the same
if start_point == end_point:
bulge = 0.0 # Set the bulge value to 0
# Print the updated values
print("Start Point:", start_point)
print("End Point:", end_point)
print("Bulge value: ", bulge)
elif bulge != 0:
center_point, start_angle, end_angle, radius = math.bulge_to_arc(start_point, end_point, bulge)
# Print the updated values
print("Start Point:", start_point)
print("End Point:", end_point)
print("Center Point: {}".format(center_point))
print("Bulge value: ", bulge)
print("Start Angle: {}".format(start_angle))
print("End Angle: {}".format(end_angle))
print("Radius: {}".format(radius)) Output: Start Point: (0.0, 10.0) Start Point: (20.0, 50.0) |
Beta Was this translation helpful? Give feedback.
-
To calculate the Area and Perimeter of a shape by using Path and Flattening: import ezdxf
import shapely.geometry
doc = ezdxf.readfile('your_file_path.dxf')
msp = doc.modelspace()
count = 0
for entity in msp.query("LWPOLYLINE POLYLINE"):
count += 1
path = ezdxf.path.make_path(entity)
vertices = list(path.flattening(0.01))
polygon = shapely.geometry.Polygon(vertices)
area = polygon.area
perimeter = polygon.length
print(f"Polygon #{count}: Area = {area:.3f}, Perimeter = {perimeter:.3f}")
print(f"Total number of polygons encountered: {count}") The DXF file should contain closed contours entity LWPOLYLINE and POLYLINE |
Beta Was this translation helpful? Give feedback.
-
@mozman
Here is the problem description:
Option 1
parse all the entities for start/end coordinates and match the points, from there figure out the Area and Perimeter
Option 1 - while should work seems that is the long and problematic rout to take hence the Option 2
Option 2
parse all the LWPOLYLINE and extract (start, end, bulge)
from bulge convert to arc (start_point, end_point, start_angle, end_angle, radius, center (x,y))
No matter what I do I cant extract the bulge information's, should be easy using this available functions:
This iterates trough all LWPOLYLINE
This should extract all the "bulge" informations:
This is the error I get:
How would one determine all the Areas and Perimeters available for all CLOSED entities in the DXF file?
fundamentally this is the question
CLOSED is defined as two variants:
Is this relevant to my problem?
#409
You can access virtual LINE and ARC entities of LWPOLYLINE entities: https://ezdxf.mozman.at/docs/dxfentities/lwpolyline.html#ezdxf.entities.LWPolyline.virtual_entities
Virtual entity means, the ARC and LINE entities are not assigned to any layout.
For approximation (flattening) you can convert the LWPOLYLINE into a Path object: https://ezdxf.mozman.at/docs/path.html
I appreciate any help I can get! Thanks
Beta Was this translation helpful? Give feedback.
All reactions