在CATIA中,装配体的结构主要通过以下三个基本元素来定义:特征、零件和产品。
在CATIA的装配结构中,每个产品都可以在不改变整体装配结构的情况下,替换其中的某个零件或子产品。例如,你可以把门锁上的旧锁芯替换成新的锁芯,而整个门锁的结构不会发生改变。这就是CATIA装配体结构的基本概念。
import win32com.client
import pywintypes # 导入pywintypes模块
import getpass
import time
global catia
global spacing, h, w
spacing = 30 # 视图摆放间距
# 启动CATIA应用
catia = win32com.client.Dispatch('CATIA.Application')
catia.visible=1
def get_view_size(view):
code='''
function catmain(view)
dim mysize(3)
view.size mysize
catmain=mysize
end function
'''
srv = catia.SystemService
mysize = srv.Evaluate(code, 0, 'catmain',[view])
return mysize
def project_view(sheet,prd):
# fornt view
fview=sheet.views.add('AutomaticNaming')
fgb=fview.generativebehavior
flk=fview.generativelinks
fgb.document=prd
fgb.definefrontview(0,1,0,0,0,1)
fgb.update()
fsize=get_view_size(fview)
# left view
lview=sheet.views.add('AutomaticNaming')
lgb = lview.generativebehavior
llk = lview.generativelinks
flk.copylinksto(llk)
lgb.defineprojectionview(fgb,1)
lgb.update()
lsize = get_view_size(lview)
# 设置左视图相对主视图的位置
lview.x=-lsize[0]+fsize[1]+spacing
lview.referenceview = fview
# top view
tview = sheet.views.add('AutomaticNaming')
tgb = tview.generativebehavior
tlk = tview.generativelinks
flk.copylinksto(tlk)
tgb.defineprojectionview(fgb, 2)
tgb.update()
tsize = get_view_size(tview)
# 设置俯视图相对主视图的位置
tview.y = fsize[2] - tsize[3] - spacing
tview.referenceview = fview
# 定位主视图
fview.x=-fsize[0]+spacing
fview.y=h-fsize[3]-spacing
def create_titleblock(bview,prd):
tables=bview.tables
tb=tables.add(0,0,4,3,20,50)
tb.computemode=0
tb.anchorpoint=8 #BottomRight的枚举值
tb.SetColumnSize(3,100) # 第三列列宽50
tb.mergecells(1, 3, 2, 1) #合并第三列1,2行
tb.mergecells(3, 3, 2, 1) #合并第三列3,4行
# 设置单元格对其方式,及字号字体
for row in range(1,tb.numberofrows+1):
for col in range(1,tb.numberofcolumns+1):
tb.setcellalignment(row,col,4)
text=tb.getcellobject(row,col)
text.setfontname(0,0,'SimSun (TrueType)')
text.setfontsize(0,0,5)
tb.x=w-10
tb.y=10
tb.setcellstring(1, 1, '设计')
tb.setcellstring(1, 2, getpass.getuser()) #获取计算机用户名
tb.setcellstring(2, 1, '日期')
date=time.strftime("%Y-%m-%d", time.localtime())
tb.setcellstring(2, 2, date)
tb.setcellstring(3, 1, '校核')
tb.setcellstring(3, 2, 'xx')
tb.setcellstring(4, 1, '日期')
tb.setcellstring(4, 2, date)
tb.setcellstring(1, 3, prd.partnumber)
tb.setcellstring(3, 3, 'xxx')
tb.computemode=1
try:
prddoc = catia.activedocument
prd = prddoc.product
drwdoc = catia.documents.add('Drawing')
sheet = drwdoc.sheets[0]
bview = sheet.views[1] # background view
sheet.papersize = 4 # A2
h = sheet.getpaperheight()
w = sheet.getpaperwidth()
sheet.name = prd.partnumber
project_view(sheet, prd)
create_titleblock(bview, prd)
catia.activewindow.activeviewer.reframe()
except pywintypes.com_error as e:
# 如果出现错误,可能是因为没有活动文档
print("无法获取活动文档,请确保CATIA应用程序中已有打开的文档。")
print(e)