import pythoncom import win32com.client def selectObject(): polylineCoord = [] wincad = win32com.client.Dispatch("AutoCAD.Application") doc = wincad.ActiveDocument msp = doc.ModelSpace try: doc.SelectionSets.Item("SS1").Delete() except: print("Delete selection failed") slt = doc.SelectionSets.Add("SS1") doc.Utility.Prompt("请选择多段线,右键结束\n")#cad里显示的提示 slt.SelectOnScreen() if slt.Count == 0: doc.Utility.Prompt("未选择对象!\n") else: entity = slt[0] name = entity.EntityName for i in range(len(entity.Coordinates)): polylineCoord.append(round(entity.Coordinates[i])) # 坐标分组 polylineCoord = [polylineCoord[x:x+2] for x in range(0,len(polylineCoord),2)] print(polylineCoord) if name == 'main': selectObject()根据上述函数用python改成一个在cad点击后获得点坐标的函数,并打印出来
时间: 2024-02-03 13:13:51 浏览: 132
pythoncom36.dll
下面是用Python实现在CAD中选择多段线并获取坐标的函数:
```python
import pythoncom
import win32com.client
def select_polyline():
polyline_coord = []
acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument
msp = doc.ModelSpace
try:
doc.SelectionSets.Item("SS1").Delete()
except:
pass
slt = doc.SelectionSets.Add("SS1")
doc.Utility.Prompt("请选择多段线,右键结束\n")
slt.SelectOnScreen()
if slt.Count == 0:
doc.Utility.Prompt("未选择对象!\n")
else:
entity = slt[0]
name = entity.EntityName
for i in range(len(entity.Coordinates)):
polyline_coord.append(round(entity.Coordinates[i], 2))
polyline_coord = [polyline_coord[x:x+2] for x in range(0, len(polyline_coord), 2)]
print(polyline_coord)
if __name__ == '__main__':
select_polyline()
```
在CAD中运行这个函数后,该函数会提示用户选择一个多段线,并在选择结束后获取该多段线的坐标并打印出来。
阅读全文