编辑代码

import ezdxf

doc = ezdxf.new(dxfversion='R2018')
msp = doc.modelspace()
doc.layers.add("WALL", color=1)
doc.layers.add("STAIR", color=4)
doc.layers.add("DIM", color=2)

# 全局参数
core_x = 8100 - 2400 - 240  # 核心筒X坐标(贴东墙)
core_y_start = 11500 - 4500  # 核心筒Y起始坐标(贴南墙)
floor_heights = [0, 3600, 6900, 10200, 13500, 16800]  # 各楼层绝对标高

def draw_stairs(msp, floor, floor_height):
    """差异化绘制楼梯"""
    stair_tread = 260
    if floor == 0:  # 首层3.6m
        stair_riser = 172
        steps = 21
        mid_platform = 10  # 第10步为中间平台
    else:  # 标准层3.3m
        stair_riser = 165
        steps = 20
        mid_platform = 9   # 第9步为中间平台
    
    # 第一跑
    start_x = core_x + 240
    start_y = core_y_start + 240
    for i in range(steps):
        y = start_y + i * stair_riser
        x = start_x + i * stair_tread
        msp.add_line((x, y), (x + stair_tread, y + stair_riser), 
                     dxfattribs={'layer': 'STAIR'})
        # 中间平台
        if (floor == 0 and i == mid_platform) or (floor > 0 and i == mid_platform):
            msp.add_lwpolyline(
                [(x + stair_tread, y + stair_riser), 
                 (x + stair_tread + 1200, y + stair_riser),
                 (x + stair_tread + 1200, y + stair_riser + 1200),
                 (x + stair_tread, y + stair_riser + 1200)],
                dxfattribs={'layer': 'STAIR'}
            )

# 生成各层
for floor in range(5):
    # 绘制核心筒墙体(与之前一致)
    # 绘制差异化楼梯
    draw_stairs(msp, floor, floor_heights[floor])
    
    # 标注层高
    msp.add_mtext(
        f"FL{floor+1} 层高: {3600 if floor==0 else 3300}mm\n标高: {floor_heights[floor]}mm",
        dxfattribs={'layer': 'DIM', 'char_height': 250}
    ).set_location((core_x - 2000, core_y_start - 1000))

doc.saveas("差异化层高楼梯.dxf")