python ezdxf读取文件matplotlib画出来

import ezdxf
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.patches import Arc

# 指定要读取的DXF文件路径
dxf_file_path = '/home/chen/freecad/b.dxf'

# 加载DXF文件
doc = ezdxf.readfile(dxf_file_path)
msp = doc.modelspace()  # 获取模型空间

# 创建一个图形对象和坐标轴对象
fig, ax = plt.subplots()

# 准备一个列表用于存储所有的线段
lines = []

# 遍历模型空间中的所有实体
for entity in msp:
    if entity.dxftype() == 'LINE':  # 如果是直线
        # 添加起点和终点到lines列表
        lines.append([(entity.dxf.start.x, entity.dxf.start.y), 
                      (entity.dxf.end.x, entity.dxf.end.y)])
    elif entity.dxftype() == 'SPLINE':  # 如果是样条曲线
        fit_points = entity.control_points
        lines.append([(fit_points[0][0], fit_points[0][1]), 
                      (fit_points[-1][0], fit_points[-1][1])])
    elif entity.dxftype() == 'ARC':  # 如果是圆弧
        arc = entity
        center = (arc.dxf.center.x, arc.dxf.center.y)
        radius = arc.dxf.radius
        start_angle = arc.dxf.start_angle
        end_angle = arc.dxf.end_angle
        if start_angle > end_angle:
            end_angle += 360  # 确保角度范围正确
        
        # 使用matplotlib的Arc类绘制圆弧
        arc_patch = Arc(center, 2*radius, 2*radius, angle=0, 
                         theta1=start_angle, theta2=end_angle, 
                         color='blue', linewidth=0.5)
        ax.add_patch(arc_patch)
    else:
        print('未处理的实体类型:', entity.dxftype())

# 使用LineCollection绘制所有的线段
line_segments = LineCollection(lines, linewidths=0.5, colors='blue')
ax.add_collection(line_segments)

# 设置坐标范围
ax.set_xlim(min([min(l[0][0], l[1][0]) for l in lines])-5, max([max(l[0][0], l[1][0]) for l in lines])+5)
ax.set_ylim(min([min(l[0][1], l[1][1]) for l in lines])-5, max([max(l[0][1], l[1][1]) for l in lines])+5)

plt.gca().set_aspect('equal', adjustable='box')  # 固定比例
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值