Python的ezdxf包操作dxf文件 获取属性 计算 长度,角度,面积,中心点
pip install ezdxf==1.0.2 -i https://pypi.mirrors.ustc.edu.cn/simple/
代码如下:
import math
#from shapely.geometry import Polygon
import ezdxf
def get_line_length_angle(e):
#return math.atan2(start[1]-end[1], start[0]-end[0])*180/math.pi
start=e.dxf.start
end=e.dxf.end
l=ezdxf.math.distance(start,end)
a= math.degrees(math.atan2(end[1]-start[1], end[0]-start[0]))
x=(start[0]+end[0])/2
y=(start[1]+end[1])/2
line_mid=x,y
return l,a if a>=0 else a+360,line_mid
# helper function
def print_entity_line(e):
print(e.dxf.dxftype)
print(e.dxf.all_existing_dxf_attribs())
print(“LINE on layer: %s\n” % e.dxf.layer)
print(“LINE on color: %s\n” % e.dxf.color)
print(“start point: %s\n” % e.dxf.start)
print(“end point: %s\n” % e.dxf.end)
print(“LINE on linetype: %s\n” % e.dxf.linetype)
print(“LINE on color: %s\n” % e.dxf.lineweight)
print(“LINE on lineweight: %s\n” % e.dxf.handle)
#print(“长度: %s\n” % math.dist(e.dxf.start, e.dxf.end))
l,a,line_mid=get_line_length_angle(e)
print(“长度: %s\n” % l)
print(“角度: %s\n” % a)
print(line_mid)
def print_entity_lwpolyline(e):
print(e.dxf.dxftype)
print(e.dxf.all_existing_dxf_attribs())
print(“LWPOLYLINE on layer: %s\n” % e.dxf.layer)
print(“LWPOLYLINE on color: %s\n” % e.dxf.color)
print(“LWPOLYLINE on linetype: %s\n” % e.dxf.linetype)
print(“LWPOLYLINE on lineweight: %s\n” % e.dxf.lineweight)
mlength=[]
#分解,炸开
for i in e.explode():
mlength.append(ezdxf.math.distance(i.dxf.start, i.dxf.end))
with e.points(‘xy’) as points:
print(points)
print(“长度: %s\n” % sum(mlength))
#print(“面积: %s\n” % Polygon(points).area)
print(“面积: %s\n” % ezdxf.math.area(points))
def get_arc_length_area(e):
end_a = e.dxf.end_angle
start_a=e.dxf.start_angle
r=e.dxf.radius
#弧长l = A * np.pi * R / 180
a=end_a+(360 if start_a>end_a else 0)-start_a
l=a*r*math.pi/180
#圆弧面积
area=l*r/2
#CAD圆弧面积=圆弧面积 加或减 三角面积
arc_delta=ezdxf.math.area([e.start_point,e.end_point,e.dxf.center])
cad_area=area – (arc_delta if end_a-start_a<180 else -1*arc_delta)
#圆弧中点坐标
x= e.dxf.center[0]+r*math.cos(math.radians(a/2+start_a));
y= e.dxf.center[1]+r*math.sin(math.radians(a/2+start_a));
arc_mid=x,y
return l,area,cad_area,arc_mid
def print_entity_arc(e):
print(e.dxf.dxftype)
print(e.dxf.all_existing_dxf_attribs())
print(“ARC on layer: %s\n” % e.dxf.layer)
print(“ARC on color: %s\n” % e.dxf.color)
print(“ARC on color: %s\n” % e.dxf.linetype)
print(“ARC on color: %s\n” % e.dxf.lineweight)
print(“center point: %s\n” % e.dxf.center)
print(“ARC on radius: %s\n” % e.dxf.radius)
print(“ARC on start_angle: %s\n” % e.dxf.start_angle)
print(“ARC on end_angle: %s\n” % e.dxf.end_angle)
l,area,cad_area,arc_mid=get_arc_length_area(e)
print(“弧长: %s\n” % l)
print(“圆弧面积: %s\n” % area)
print(“CAD计算圆弧面积: %s\n” % cad_area)
print(arc_mid)
def print_entity_circle(e):
print(e.dxf.dxftype)
print(e.dxf.all_existing_dxf_attribs())
print(“CIRCLE on layer: %s\n” % e.dxf.layer)
print(“CIRCLE on color: %s\n” % e.dxf.color)
print(“CIRCLE on linetype: %s\n” % e.dxf.linetype)
print(“CIRCLE on lineweight: %s\n” % e.dxf.lineweight)
print(“center point: %s\n” % e.dxf.center)
print(“CIRCLE on radius: %s\n” % e.dxf.radius)
print(“圆的周长: %s\n” % str(math.pi*e.dxf.radius*2))
print(“圆的面积: %s\n” % str(math.pi*e.dxf.radius**2))
#print(e.dxf.get(‘layer’))
def print_entity_polyline(e):
print(e.dxf.dxftype)
print(e.dxf.all_existing_dxf_attribs())
print(“POLYLINE on layer: %s\n” % e.dxf.layer)
print(“POLYLINE on color: %s\n” % e.dxf.color)
print(“POLYLINE on linetype: %s\n” % e.dxf.linetype)
print(“POLYLINE on lineweight: %s\n” % e.dxf.lineweight)
points=[]
for i in e.vertices:
print(i.dxf.all_existing_dxf_attribs())
points.append(i.dxf.location)
mlength=[]
arc_areas=[]
#分解,炸开
for i in e.explode():
#print_entity_line(i)
#points.append(i.dxf.start)
#points.append(i.dxf.end)
#print(i.dxftype)
if i.dxftype()==”ARC”:
l,area,cad_area,m_mid=get_arc_length_area(i)
mlength.append(l)
arc_areas.append(cad_area)
if i.dxftype()==”LINE”:
mlength.append(ezdxf.math.distance(i.dxf.start, i.dxf.end))
print(points)
print(mlength)
print(“长度: %s\n” % sum(mlength))
#print(“面积: %s\n” % Polygon(points).area)
print(“面积: %s\n” % str(ezdxf.math.area(points)+sum(arc_areas)))
”’
if e.is_poly_face_mesh:
mesh = MeshBuilder.from_polyface(e)
print(mesh.vertices)
print(mesh.faces)
”’
#读取 dxf 格式 r12
doc = ezdxf.readfile(“sample.dxf”)
# iterate over all entities in modelspace
msp = doc.modelspace()
”’
for e in msp:
if e.dxftype() == “LINE”:
print_entity(e)
”’
# entity query for all LINE entities in modelspace
for e in msp.query(“LINE”):
print_entity_line(e)
for e in msp.query(“LWPOLYLINE”):
print_entity_lwpolyline(e)
for e in msp.query(“ARC”):
print_entity_arc(e)
for e in msp.query(“CIRCLE”):
print_entity_circle(e)
for e in msp.query(“POLYLINE”):
print_entity_polyline(e)