一、application 版本
AutoCAD产品名 | 版本号 | ProgID |
---|
AutoCAD 2004 | R16 | AutoCAD.Application.16 |
AutoCAD 2005 | R16.1 | AutoCAD.Application.16.1 |
AutoCAD 2006 | R16.2 | AutoCAD.Application.16.2 |
AutoCAD 2007 | R17 | AutoCAD.Application.17 |
AutoCAD 2008 | R17.1 | AutoCAD.Application.17.1 |
AutoCAD 2009 | R17.2 | AutoCAD.Application.17.2 |
AutoCAD 2010 | R18 | AutoCAD.Application.18 |
AutoCAD 2011 | R18.1 | AutoCAD.Application.18.1 |
AutoCAD 2014 | R19.1 | AutoCAD.Application.19.1 |
AutoCAD 2015 | R20 | AutoCAD.Application.20 |
AutoCAD 2016 | R20.1 | AutoCAD.Application.20.1 |
AutoCAD 2017 | R21 | AutoCAD.Application.21 |
AutoCAD 2018 | R22 | AutoCAD.Application.22 |
AutoCAD 2019 | R23 | AutoCAD.Application.23 |
用户最后一次使用的CAD | 无 | AutoCAD.Application |
['16','16.1','16.2','17','17.1','17.2','18','18.1','19.1','20','20.1','21','22','23']
CAD类
import pythoncom
import win32com.client as win32
class Cad():
def __init__(self):
self.version = self.version()
self.cad = win32.Dispatch('AutoCAD.Application.%s' % self.version)
self.color = self.cad.GetInterfaceObject('AutoCAD.AcCmColor.%s' % self.version)
self.doc = self.cad.ActiveDocument
self.docs = self.cad.Documents
self.msp = self.doc.ModelSpace
self.utl = self.doc.Utility
def vtpnt(self, x, y, z=0):
"""坐标点转化为浮点数"""
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))
def vtobj(self, obj):
"""转化为对象数组"""
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj)
def vtfloat(self, lst):
"""列表转化为浮点数"""
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, lst)
def vtInt(self, list):
"""列表转化为整数"""
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)
def vtVariant(self, list):
"""列表转化为变体"""
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)
def select_all(self, name, filterType, filterData):
try:
ssget1 = self.doc.SelectionSets.Add("%s" % name)
except BaseException:
ssget1 = self.doc.SelectionSets("%s" % name)
ssget1.Clear()
filterType = self.vtInt(filterType)
filterData = self.vtVariant(filterData)
ssget1.Select(5, 0, 0, filterType, filterData)
return ssget1
def select_little(self, name, filterType, filterData, coordinates):
"""选择模式1,选择矩形框相交的对象"""
try:
ssget1 = self.doc.SelectionSets.Add("%s" % name)
except BaseException:
ssget1 = self.doc.SelectionSets("%s" % name)
ssget1.Clear()
filterType = self.vtInt(filterType)
filterData = self.vtVariant(filterData)
ssget1.Select(1, self.vtpnt(coordinates[0] - 30, coordinates[1] + 30),
self.vtpnt(coordinates[0] + 30, coordinates[1] - 30), filterType, filterData)
return ssget1
def select_poly(self, name, mode, lst, filterType, filterData):
try:
ssget1 = self.doc.SelectionSets.Add("%s" % name)
except BaseException:
ssget1 = self.doc.SelectionSets("%s" % name)
ssget1.Clear()
filterType = self.vtInt(filterType)
filterData = self.vtVariant(filterData)
ssget1.SelectByPolygon(mode, self.vtfloat(lst), filterType, filterData)
return ssget1
def get_union_points(self, objlist):
try:
self.msp.AddRegion(self.vtobj(objlist))
region_union = self.select_all('ss2', [8, 0], ['0', 'region'])
while len(region_union)>1:
region_union[0].Boolean(0, region_union[1])
region_union = self.select_all('ss2', [8, 0], ['0', 'region'])
line = region_union[0].Explode()
region_union[0].Delete()
region_union = self.select_all('ss2', [8, 0], ['0', 'region'])
points_lst = []
if len(region_union) > 0:
for i in region_union:
lines = i.Explode()
i.Delete()
points_lst.append(self.union2points(lines))
else:
points_lst.append(self.union2points(line))
return points_lst
except Exception as e:
print('get_union_points出错:' + str(e))
def union2points(self, lines):
"""
:param lines: 多个直线段对象
:return: 多边形坐标列表[x,y,x1,y1...xn,yn]
"""
point_list = []
for i in lines:
point_list.append([i.StartPoint, i.EndPoint])
i.Delete()
lst = [point_list[0]]
point_list.pop(0)
for _ in range(len(lines) - 1):
for i in point_list:
if lst[-1][-1] == i[0]:
lst.append(i)
point_list.remove(i)
break
elif lst[-1][-1] == i[1]:
lst.append([i[1], i[0]])
point_list.remove(i)
break
result_lst = []
for i in lst:
result_lst.extend([i[0][0], i[0][1]])
result_lst = self.delete_point(result_lst)
result_lst = self.coordinate_sorting(result_lst, -1)
return result_lst
def coordinate_sorting(self, lst, flag):
point_lst = []
for i in range(0, len(lst), 2):
point_lst.append((lst[i], lst[i + 1]))
point_lst.append(point_lst[0])
point_lst.insert(0, point_lst[-2])
max_point = (0, 0)
for i in range(1, len(point_lst) - 1):
if point_lst[i][0] > max_point[0]:
max_point = point_lst[i]
elif point_lst[i][0] == max_point[0]:
if point_lst[i][1] >= max_point[1]:
max_point = point_lst[i]
index_num = point_lst[1:-1].index(max_point) + 1
p1 = point_lst[index_num - 1]
p2 = point_lst[index_num]
p3 = point_lst[index_num + 1]
x1 = p2[0] - p1[0]
y1 = p2[1] - p1[1]
x2 = p3[0] - p2[0]
y2 = p3[1] - p2[1]
z = x1 * y2 - x2 * y1
if z < 0 and flag < 0:
point_lst.reverse()
elif z > 0 and flag > 0:
point_lst.reverse()
new_lst = []
for i in range(1, len(point_lst) - 1):
new_lst.extend(point_lst[i][:])
return new_lst
def degree(self, x, y, x1, y1):
try:
dx = x1 - x
dy = y1 - y
dy = dy + m.pow(10, -10)
degree = m.pi * (1 - np.sign(dy) / 2) - m.atan(dx / dy)
return degree
except Exception as e:
print('degree出错:' + str(e))
def delete_point(self, lst):
try:
lst.extend(lst[:2])
new_lst = []
for i in range(0, len(lst), 2):
new_lst.append((lst[i], lst[i + 1]))
new_lst.insert(0, new_lst[-2])
delete = []
for i in range(1, len(new_lst) - 1):
d1 = round(self.degree(new_lst[i - 1][0], new_lst[i - 1][1], new_lst[i][0], new_lst[i][1]), 4)
d2 = round(self.degree(new_lst[i][0], new_lst[i][1], new_lst[i + 1][0], new_lst[i + 1][1]), 4)
if d1 == d2:
delete.append((new_lst[i]))
del new_lst[0]
del new_lst[-1]
for i in delete:
while i in new_lst:
new_lst.remove(i)
lst = []
for i in new_lst:
lst.extend([i[0], i[1]])
return lst
except Exception as e:
print('delete_point出错:' + str(e))
def version(self):
applist = ['16','16.1','16.2','17','17.1','17.2','18','18.1','19.1','20','20.1','21','22','23']
for i in applist:
try:
win32.Dispatch('AutoCAD.Application.%s' % i)
return i
except Exception:
pass
return None
if __name__ == '__main__':
cad = Cad()
cad.cad.ZoomExtents()
ssget1 = cad.select_all('ss1', [2, 8], ["FWSX", "FWSX"])
insertionPoint = (434817.9258476031, 3476325.6359382486, 0.0)
ssget2 = cad.select_little('ss2', [0, 0], ['lwpolyline', 'lwpolyline'], insertionPoint)
new_lst = [434813.7128951931, 3476307.5265556867, 0,
434826.12550053425, 3476313.3009239333, 0,
434827.8843840688, 3476309.5200207722, 0,
434815.4717787276, 3476303.7456525257, 0]
ssget3 = cad.select_poly('ss3', 6, new_lst, [8, 0], ['面积插入', 'TEXT'])
二、基本操作
import pythoncom
import win32com.client as win32
cad = win32.Dispatch("AutoCAD.Application")
doc = cad.ActiveDocument
msp = doc.ModelSpace
color = cad.GetInterfaceObject("AutoCAD.AcCmColor.17")
三、数据转换
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 vtfloat(lst):
"""列表转化为浮点数"""
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, lst)
def vtInt(list):
"""列表转化为整数"""
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)
def vtVariant(list):
"""列表转化为变体"""
return win32.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)
四、创建选择集
try:
ssget1 = doc.SelectionSets.Add("SS1")
except BaseException:
ssget1 = doc.SelectionSets("SS1")
ssget1.Clear()
filterType = [2, 8]
filterData = ["FWSX", "FWSX"]
filterType = vtInt(filterType)
filterData = vtVariant(filterData)
ssget1.Select(5, 0, 0, filterType, filterData)
语法:
object.Select(Mode, Point1, Point2, FilterType, FilterData)
模式 | 说明 |
---|
0 | 选择完全在矩形区域内的所有对象,矩形对角由 Point1 和 Point2 定义 |
1 | 选择在矩形区域内和与矩形区域相交的对象,矩形对角由 Point1 和 Point2 定义 |
3 | 选择最近的选择集。如果用户在图纸空间和模型空间之间进行切换并试图使用选择集,该模式将被忽略 |
4 | 选择最近生成的可见对象 |
5 | 选择所有对象 |
- Point1:3维坐标点
- Point2:3维坐标点
- FilterType:过滤类型(DXF组码)
DXF代码 | 过滤器类型 |
---|
0 | 对象类型(字符串),如“Line”、“Circle”、“Arc”等等 |
2 | 对象类型(字符串),命名对象的表(给定)名称 |
8 | 图层名称(字符串),如“Layer0” |
60 | 对象的可见性(整数),0可见,1不可见 |
62 | 颜色号(整数),0到256的数字索引值,0代表随块,256代表随层,负值代表图层关闭 |
67 | 模型/图纸空间指示(整数),使用0或忽略为模型空间,1为图纸空间 |
其他对象选择方法:
方法 | 说明 |
---|
Addltem | 添加一个或多个对象到指定的选择集。 |
Select | 选择对象并将其放入活动的选择集中。用户可选择所有对象、矩形窗选区域或矩形框选区域的对象、多边形窗选区域或多边形框选区域的对象、栅选的对象、最近创建的对象、最近选择集的对象。 |
SelectAtPoint | 选择通过给定点的对象并将其放入活动的选择集中。 |
SelectByPolygon | 通过栅选的对象并将其放入活动的选择集中。 |
SelectOnScreen | 提示用户从屏幕中拾取对象并将其添加到活动的选择中。 |
五、对象常用方法
1、对象相交
rel = obj1.IntersectWith(obj2, 0)
print(rel)
(1218.1517971803642, 625.2198412500425, 0.0, 1144.7959481512107, 361.0629323395639, 0.0)
2、对象上色
color = cad.GetInterfaceObject('AutoCAD.AcCmColor.17')
color.ColorIndex = 1
color.SetRGB(0, 255, 255)
obj.TrueColor = color
3、图层对象
layer1 = doc.Layers.Add("户主名称")
doc.ActiveLayer = layer1
layer1.LayerOn = False
for i in doc.Layers:
if i.Name == '0':
layer2 = i
if i.Name == '面积插入':
layer3 = i
doc.ActiveLayer = layer2
layer3.Delete()
六、创建新图形
cad = Cad()
new_doc = cad.docs.Add(r'C:\Users\liu\Desktop\1.dwg')
startPoint = cad.vtpnt(0, 0)
endPoint = cad.vtpnt(10, 10)
lineobj = new_doc.ModelSpace.AddLine(startPoint, endPoint)
new_doc.SaveAs(r'C:\Users\liu\Desktop\1.dwg')
new_doc.Close('TRUE')