我有一个.stp文件,使用不同的CAD软件导出,我可以用CATIA打开它。 然后,CATIA将列出产品/部件树,就好像它是本机CATIA .CATProduct。
我的目标是使用CATIA自动打开这样的.stp文件,并保存所有包含的部件/产品,其名称是从每个部件/产品中的一个UserRefProperties中提取的。 因此,我想用VBA创建一个宏,完成后将以批处理模式运行。
当我尝试将部件/产品保存在此.stp文件中时出现了我的第一个问题,Catia的保存功能根据需要处理我的文件,并将每个部件/产品保存为单独的文件。 但是,使用VBA我似乎无法保存任何这些部件/产品,因为.ExportData和.SaveAs方法似乎只能处理.PartDocument或.ProductDocument对象而不是我一直在尝试的对象保存: .Product objects。
我正在尝试做的例子:
Sub catmain()
Dim oProdDoc As ProductDocument
Set oProdDoc = CATIA.ActiveDocument
Dim oRootProd As Product
Set oRootProd = oProdDoc.Product
Dim oInstances As Products
Set oInstances = oRootProd.Products
For k = 1 To oInstances.Count
Dim oInst As Product
Set oInst = oInstances.Item(k)
oInst.ExportData "X:\path", ".CATPart"
next
end sub
如果CATIA可以根据需要保存我的.stp文件的内容,我当然可以对VBA执行相同的操作,对吗?
非常感谢任何帮助。
答案 0 :(得分:1)
树根处的产品可以保存为CATProduct文档。 树中的任何子产品也可以保存为CATProduct。 作为树的叶子的部分可以保存为CATPart。
您可以像这样保存根产品:
Dim rootProdDoc As ProductDocument
set rootProdDoc = CATIA.ActiveDocument
rootProdDoc.SaveAs "C:\Temp\" & rootProd.PartNumber & ".CATProduct"
然而,当你这样做时,CATIA会抱怨"这会激活其他保存操作,你想继续吗?"这样做是因为零件尚未保存。回答是的CATIA将保存您的组件和所有部件。但是,由于您无法控制部件保存,因此您将无法为所需的文档设置名称。
因为你必须回答一个对话框,它会阻止你制作一个批处理程序。
执行此操作的正确方法是首先保存叶子文档,然后工作" up"树的根级别。然后,当你需要它时,一切都会被保存。
----------Class clsSaveInfo definition--------------
Public level As Integer
Public prod As Product
-----------------(module definition)---------------
Option Explicit
Sub CATMain()
CATIA.DisplayFileAlerts = False
'get the root product
Dim rootProd As Product
Set rootProd = CATIA.ActiveDocument.Product
'make a dictionary to track product structure
Dim docsToSave As Scripting.Dictionary
Set docsToSave = New Scripting.Dictionary
'some parameters
Dim level As Integer
Dim maxLevel As Integer
'read the assembly
level = 0
Call slurp(level, rootProd, docsToSave, maxLevel)
Dim i
Dim kx As String
Dim info As clsSaveInfo
Do Until docsToSave.count = 0
Dim toRemove As Collection
Set toRemove = New Collection
For i = 0 To docsToSave.count - 1
kx = docsToSave.keys(i)
Set info = docsToSave.item(kx)
If info.level = maxLevel Then
Dim suffix As String
If TypeName(info.prod) = "Part" Then
suffix = ".CATPart"
Else
suffix = ".CATProduct"
End If
Dim partProd As Product
Set partProd = info.prod
Dim partDoc As Document
Set partDoc = partProd.ReferenceProduct.Parent
partDoc.SaveAs ("C:\Temp\" & partProd.partNumber & suffix)
toRemove.add (kx)
End If
Next
'remove the saved products from the dictionary
For i = 1 To toRemove.count
docsToSave.Remove (toRemove.item(i))
Next
'decrement the level we are looking for
maxLevel = maxLevel - 1
Loop
End Sub
Sub slurp(ByVal level As Integer, ByRef aProd As Product, ByRef allDocs As Scripting.Dictionary, ByRef maxLevel As Integer)
'increment the level
level = level + 1
'track the max level
If level > maxLevel Then maxLevel = level
'see if the part is already in the save list, if not add it
If allDocs.Exists(aProd.partNumber) = False Then
Dim info As clsSaveInfo
Set info = New clsSaveInfo
info.level = level
Set info.prod = aProd
Call allDocs.add(aProd.partNumber, info)
End If
'slurp up children
Dim i
For i = 1 To aProd.products.count
Dim subProd As Product
Set subProd = aProd.products.item(i)
Call slurp(level, subProd, allDocs, maxLevel)
Next
End Sub