Catia VBA 将 .stp 文件中的特定零件或产品保存为 .CATPart and/or .CATProduct

Catia VBA Saving specific parts or products from a .stp file as .CATPart and/or .CATProduct

我有一个 .stp 文件,用不同的 CAD 软件导出,我可以用 CATIA 打开它。 然后,CATIA 将列出 product/part 树,就好像它是本地 CATIA .CATProduct。

我的目标是使用 CATIA 自动打开这样的 .stp 文件,并使用从每个 parts/products 中的一个 UserRefProperties 中提取的名称保存所有包含的 parts/products。 因此我想用 VBA 创建一个宏,完成后将 运行 以批处理模式。

当我尝试将 parts/products 保存在此 .stp 文件中时,我的第一个问题出现了,Catia 的保存功能会根据需要处理我的文件,并将每个 part/product 保存为一个单独的文件。 但是,使用 VBA 我似乎无法保存这些 parts/products 中的任何一个,因为 .ExportData 和 .SaveAs 方法似乎只适用于 .PartDocument 或 .ProductDocument 对象而不是对象我一直在努力挽救: .产品对象。

我正在尝试做的事情的例子:

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 做同样的事情,对吗?

如有任何帮助,我们将不胜感激。

树根处的产品可以保存为 CATProduct 文档。 树中的任何子产品也可以保存为 CATProduct。 零件是树的叶子,可以保存为 CATPart。

您可以像这样保存根产品:

Dim rootProdDoc As ProductDocument
set rootProdDoc = CATIA.ActiveDocument
rootProdDoc.SaveAs "C:\Temp\" & rootProd.PartNumber & ".CATProduct"

但是,当您这样做时,CATIA 会抱怨 "This activates other save operations, do you want to continue?" 它这样做是因为零件尚未保存。回答是,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