- 积分
- 1379
- 明经币
- 个
- 注册时间
- 2023-2-2
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 箭头_Row 于 2023-2-28 18:48 编辑
先上代码:
代码逻辑如下:
STEP01:定义函数,在图中选择多段线(仅限模型空间操作,因为目前本人还不会判断选择正在操作的空间)
STEP02:获取多段线属性列表,顶点坐标列表,使用round函数取整。
STEP03:获取凸度列表,删除多段线。
STEP04:绘制多段线,添加凸度,赋值属性。
BUG:如果UCS坐标是旋转过的UCS,无法智能以自定义的UCS去取用户坐标点取整数,会依然以WCS世界坐标系中的坐标值取整。如何解决这个BUG,有么有大佬提供点思路,提供一些关于UCS用户坐标系相关的资料研究研究。
- # env Python = 3.11.1 pywin32 = 305
- # -*- coding: utf-8 -*-
- """
- =============================
- Author: Meditation/箭头
- Email: 1191101855@qq.com
- Last Update: 2023.02.14 20:22
- =============================
- """
- import win32com.client as win32
- import pythoncom
- # AutoCAD 2022的ProgramID
- ProgramID = "AutoCAD.Application.24.1"
- # 获取CAD程序
- Acadapp = win32.Dispatch(ProgramID)
- # 指定当前活动文档
- doc = Acadapp.ActiveDocument
- AcadA = doc.Application
- msp = doc.ModelSpace
- print(doc.Path, '\\', doc.Name, sep='')
- # 数据转换
- def vtpnt(x, y, z=0):
- """坐标点转化为浮点数"""
- return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))
- def vtobj(obj):
- """转化为对象数组"""
- return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj)
- def vtlstf(lst):
- """列表转化为浮点数"""
- return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, lst)
-
- def vtlsti(lst):
- """列表转化为整数"""
- return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, lst)
- def vtlstvariant(lst):
- """列表转化为变体"""
- return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, lst)
- def My_Select():
- # 新建选择集
- try:
- doc.SelectionSets.Item("SS1").Delete()
- except:
- print("Delete selection failed")
-
- slt = doc.SelectionSets.Add("SS1")
- slt.SelectOnScreen()
- return slt
- def Polyline_XY_Zero():
- """注意如果坐标系与当前坐标系不一致,可能导致数据error"""
- slt = My_Select()
- count = 0
- for Entity in slt:
- Coor = []
- if Entity.objectname == 'AcDbPolyline':
- Entity_Properties = [Entity.Layer, Entity.Color, Entity.Linetype ,Entity.LinetypeScale ,Entity.Lineweight]
- Entity_closed = Entity.closed
- Entity_Coor = Entity.Coordinates
- # print(Entity_Coor)
- # 获取多线段的顶点数
- Entity_count = int(len(Entity_Coor)/2)
- # 获取凸度列表
- Entity_Bulge = []
- for i in range(Entity_count):
- RetVal = Entity.GetBulge(i)
- Entity_Bulge.append(RetVal)
- # print(Entity_Bulge)
- # 坐标值精度归0
- for xy in Entity_Coor:
- Entity_Coor_round = round(xy,0)
- Coor.append(Entity_Coor_round)
- Entity.Erase()
- new_Coor = vtlstf(Coor)
- Entity = doc.ModelSpace.AddLightWeightPolyline(new_Coor)
- # 添加凸度
- for i in range(len(Entity_Bulge)):
- Entity.SetBulge(i, Entity_Bulge[i])
- [Entity.Layer, Entity.Color, Entity.Linetype ,Entity.LinetypeScale ,Entity.Lineweight] = Entity_Properties
- Entity.closed = Entity_closed
- Coor = []
- count += 1
- print(f'多段线Z轴归零: {count} 条!')
- doc.Utility.Prompt(f'多段线Z轴归零: {count} 条!')
- if __name__ == "__main__":
- a = Polyline_XY_Zero.__name__
- b = Polyline_XY_Zero.__doc__
- c = Polyline_XY_Zero.__init__
- print(a, b, c,sep='\n')
- Polyline_XY_Zero()
复制代码
|
|