在做图的时候,点基础坐标一般先画一根多段线,然后导出多段线坐标再做表格。为提高工作效率,我想到用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
acad = Autocad(create_if_not_exists=True) acad.prompt("AutoCAD对象已初始化\n")
selection = acad.get_selection() if not selection: print("未选中任何对象") exit()
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
for i in range(point_count): x = points[i * 2] y = points[i * 2 + 1] ws.append([i + 1, x, y])
excel_filename = "polyline_coordinates.xlsx" wb.save(excel_filename) print(f"坐标已保存到 {excel_filename}")
|

脚本运行效果