一个读取CAD多段线坐标的脚本

在做图的时候,点基础坐标一般先画一根多段线,然后导出多段线坐标再做表格。为提高工作效率,我想到用python写一个脚本,能直接读取多段线坐标直接生成excel表格。

Python程序代码

cad用2013以上版本,输出的坐标是你画多段线的点的顺序,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from pyautocad import Autocad, APoint
from openpyxl import Workbook

# 初始化AutoCAD
acad = Autocad(create_if_not_exists=True)
acad.prompt("AutoCAD对象已初始化\n")

# 获取当前选中的对象
selection = acad.get_selection()
if not selection:
print("未选中任何对象")
exit()

# 创建一个新的Excel工作簿
wb = Workbook()
ws = wb.active
ws.title = "多段线坐标"

# 设置表头
ws.append(["点编号", "X坐标", "Y坐标"])

# 遍历选中的对象
for obj in selection:
if obj.ObjectName == "AcDbPolyline":
# 获取多段线的顶点
points = obj.Coordinates
point_count = len(points) // 2 # 每个点有X和Y两个坐标

# 将坐标写入Excel
for i in range(point_count):
x = points[i * 2]
y = points[i * 2 + 1]
ws.append([i + 1, x, y])

# 保存Excel文件
excel_filename = "polyline_coordinates.xlsx"
wb.save(excel_filename)
print(f"坐标已保存到 {excel_filename}")

脚本运行效果
-------------本文结束感谢您的阅读-------------
请我一杯咖啡吧!
郭志良 微信 微信
郭志良 支付宝 支付宝
欢迎关注我的其它发布渠道