基于ezdxf,放大DXF文件中的线段矩形(Python)

环境准备

创建环境:
conda create --name cad python=3.11
激活环境:
conda activate cad
退出环境:
conda deactivate
安装包:
pip install ezdxf notebook

放大线段

1绘制线段矩形,保存为DXF

2读取线段信息

import ezdxf

# Load the DXF file
doc = ezdxf.readfile("Line.dxf")

# 遍历 DXF 文件中的所有线段实体
for line in doc.modelspace().query("LINE"):
    # 输出线段的句柄、起点和终点坐标
    print("句柄:", line.dxf.handle)
    print("起点坐标:", line.dxf.start)
    print("终点坐标:", line.dxf.end)
    print()

"""
句柄: 2EA
起点坐标: (1948.962898771888, 2963.407349417252, 0.0)
终点坐标: (2048.962898771888, 2963.407349417252, 0.0)

句柄: 2EB
起点坐标: (2048.962898771888, 2963.407349417252, 0.0)
终点坐标: (2048.962898771888, 2863.407349417252, 0.0)

句柄: 2EC
起点坐标: (2048.962898771888, 2863.407349417252, 0.0)
终点坐标: (1948.962898771888, 2863.407349417252, 0.0)

句柄: 2ED
起点坐标: (1948.962898771888, 2863.407349417252, 0.0)
终点坐标: (1948.962898771888, 2963.407349417252, 0.0)

"""

3放大线段,首位相连

#使用句柄将线段长度放大两倍

# 定义一个函数来计算两点之间的距离
def distance(point1, point2):
    return ((point2[0] - point1[0])**2 + (point2[1] - point1[1])**2)**0.5

def get_end_point(doc,handle):
    for line in doc.modelspace().query("LINE"):
        if line.dxf.handle == handle:
            return line.dxf.end

# 获取 DXF 文件中所有线段实体
lines = doc.modelspace().query("LINE")

# 定义要修改的线段的目标长度
scale_factor = 2

start_name='2EA'
# print("start_name:",start_name)

# 循环遍历所有线段实体
while True:
    if start_name[-1]=='A':
        for line in lines:
            if line.dxf.handle == start_name:
                # 获取当前线段的起点和终点坐标
                start_point = line.dxf.start
                end_point = line.dxf.end
                
                # 计算当前线段的向量表示
                line_vector = (end_point[0] - start_point[0], end_point[1] - start_point[1])
                
                # 计算当前线段的长度
                current_length = distance(start_point, end_point)
                
                # 将线段向量按比例缩放以达到目标长度
                scaled_vector = (line_vector[0] * scale_factor, line_vector[1] * scale_factor)
                
                # 计算新的终点坐标
                new_end_point = (start_point[0] + scaled_vector[0], start_point[1] + scaled_vector[1])
                
                # 更新线段的终点坐标
                line.dxf.end = new_end_point
                
                break
        start_name = start_name[:-1]+'B'
    elif start_name[-1]=='B':
        for line in lines:
            if line.dxf.handle == start_name:
                # 获取当前线段的起点和终点坐标
                start_point = line.dxf.start
                end_point = line.dxf.end
                
                line_vector = (end_point[0] - start_point[0], end_point[1] - start_point[1])
                current_length = distance(start_point, end_point)
                scaled_vector = (line_vector[0] * scale_factor, line_vector[1] * scale_factor)
                
                # 更新线段的起点坐标
                if start_name[-1]=='B':
                    line.dxf.start = get_end_point(doc,start_name[:-1]+'A')
                elif start_name[-1]=='C':
                    line.dxf.start = get_end_point(doc,start_name[:-1]+'B')
                elif start_name[-1]=='D':
                    line.dxf.start = get_end_point(doc,start_name[:-1]+'C')
                start_point = line.dxf.start
                    
                # 计算新的终点坐标
                new_end_point = (start_point[0] + scaled_vector[0], start_point[1] + scaled_vector[1])
        
                # 更新线段的终点坐标
                line.dxf.end = new_end_point
                break
        start_name = start_name[:-1]+'C'
    elif start_name[-1]=='C':
        for line in lines:
            if line.dxf.handle == start_name:
                # 获取当前线段的起点和终点坐标
                start_point = line.dxf.start
                end_point = line.dxf.end
                
                # 计算当前线段的向量表示
                line_vector = (end_point[0] - start_point[0], end_point[1] - start_point[1])
                current_length = distance(start_point, end_poin
                scaled_vector = (line_vector[0] * scale_factor, line_vector[1] * scale_factor)
                
                # 更新线段的起点坐标
                if start_name[-1]=='B':
                    line.dxf.start = get_end_point(doc,start_name[:-1]+'A')
                elif start_name[-1]=='C':
                    line.dxf.start = get_end_point(doc,start_name[:-1]+'B')
                elif start_name[-1]=='D':
                    line.dxf.start = get_end_point(doc,start_name[:-1]+'C')
                start_point = line.dxf.start
                
                # 计算新的终点坐标
                new_end_point = (start_point[0] + scaled_vector[0], start_point[1] + scaled_vector[1])
                
                # 更新线段的终点坐标
                line.dxf.end = new_end_point
                break
        start_name = start_name[:-1]+'D'
    elif start_name[-1]=='D':
        for line in lines:
            if line.dxf.handle == start_name:
                # 获取当前线段的起点和终点坐标
                start_point = line.dxf.start
                end_point = line.dxf.end
                
                # 计算当前线段的向量表示
                line_vector = (end_point[0] - start_point[0], end_point[1] - start_point[1])
                current_length = distance(start_point, end_point)
                scaled_vector = (line_vector[0] * scale_factor, line_vector[1] * scale_factor)
                
                # 更新线段的起点坐标
                if start_name[-1]=='B':
                    line.dxf.start = get_end_point(doc,start_name[:-1]+'A')
                elif start_name[-1]=='C':
                    line.dxf.start = get_end_point(doc,start_name[:-1]+'B')
                elif start_name[-1]=='D':
                    line.dxf.start = get_end_point(doc,start_name[:-1]+'C')
                start_point = line.dxf.start
                
                # 计算新的终点坐标
                new_end_point = (start_point[0] + scaled_vector[0], start_point[1] + scaled_vector[1])
                
                # 更新线段的终点坐标
                line.dxf.end = new_end_point
                break
        break
        
    print("start_name:",start_name)

# 保存修改后的 DXF 文件
doc.saveas("Modified_Line.dxf")

4放大结果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值