Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to know the type of document in the complete catia tree

Status
Not open for further replies.

eperichon

Aerospace
Jan 16, 2013
7
CH
Hello,

Here is an interesting code that give the type of all documents in a tree. You could know if it is a part, a product, or a component. It could be very useful in order to introduce some userrefproperties or other thing in different type of file. Some code are used from other codes find on the web.

Code:
[COLOR=#4E9A06]'********************************************************
'By e. perichon , some code borrowed from forums
'********************************************************[/color]


Sub CATMain()

    GetNextNode CATIA.ActiveDocument.Product
   
End Sub

 

Sub GetNextNode(oCurrentProduct As Product)

    Dim oCurrentTreeNode As Product
    Dim StrNomenclature, StrDesignation, StrWindows As String
    Dim i As Integer
          
    [COLOR=#4E9A06]' Loop through every tree node for the current product[/color]
    For i = 1 To oCurrentProduct.Products.Count
        
        Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)
                
        [COLOR=#4E9A06]' Determine if the current node is a part, product or component[/color]
        
        If Right(StrWindows, 4) = "Part" Then
            'MsgBox oCurrentTreeNode.PartNumber & " is a part"
                
                                      
        ElseIf IsProduct(oCurrentTreeNode) = True Then
            'MsgBox oCurrentTreeNode.PartNumber & " is a product"
                        
        Else
           'MsgBox oCurrentTreeNode.PartNumber & " is a component"        
                   
        End If
        
        [COLOR=#4E9A06]' if sub-nodes exist below the current tree node, call the sub recursively[/color]
        If oCurrentTreeNode.Products.Count > 0 Then
            GetNextNode oCurrentTreeNode
        End If
              
        
   Next

End Sub

Function IsProduct(objCurrentProduct As Product) As Boolean

    Dim oTestProduct As ProductDocument
    
    Set oTestProduct = Nothing
    
    On Error Resume Next
      
        Set oTestProduct = CATIA.Documents.Item(objCurrentProduct.PartNumber & ".CATProduct")
        [COLOR=#4E9A06]'Set oTestProduct = CATIA.Documents.Item(objCurrentProduct.ReferenceProduct.Parent.Name & ".CATProduct")[/color]
        
        If Not oTestProduct Is Nothing Then
            IsProduct = True
        Else
            IsProduct = False
        End If
         
End Function

 
Replies continue below

Recommended for you


Sorry i have forgotten the red line that are importants...

Code:
'********************************************************
'By e. perichon , some code borrowed from forums
'********************************************************


Sub CATMain()

    GetNextNode CATIA.ActiveDocument.Product
   
End Sub

 

Sub GetNextNode(oCurrentProduct As Product)

    Dim oCurrentTreeNode As Product
    Dim StrNomenclature, StrDesignation, StrWindows As String
    Dim i As Integer

    [COLOR=#EF2929]StrWindows = oCurrentTreeNode.ReferenceProduct.Parent.FullName
    On Error Resume Next[/color]
       
    ' Loop through every tree node for the current product
    For i = 1 To oCurrentProduct.Products.Count
        
        Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)
                
        ' Determine if the current node is a part, product or component
        
        If Right(StrWindows, 4) = "Part" Then
            'MsgBox oCurrentTreeNode.PartNumber & " is a part"
                
                                      
        ElseIf IsProduct(oCurrentTreeNode) = True Then
            'MsgBox oCurrentTreeNode.PartNumber & " is a product"
                        
        Else
           'MsgBox oCurrentTreeNode.PartNumber & " is a component"        
                   
        End If
        
        ' if sub-nodes exist below the current tree node, call the sub recursively
        If oCurrentTreeNode.Products.Count > 0 Then
            GetNextNode oCurrentTreeNode
        End If
              
        
   Next

End Sub

Function IsProduct(objCurrentProduct As Product) As Boolean

    Dim oTestProduct As ProductDocument
    
    Set oTestProduct = Nothing
    
    On Error Resume Next
      
        Set oTestProduct = CATIA.Documents.Item(objCurrentProduct.PartNumber & ".CATProduct")
        'Set oTestProduct = CATIA.Documents.Item(objCurrentProduct.ReferenceProduct.Parent.Name & ".CATProduct")
        
        If Not oTestProduct Is Nothing Then
            IsProduct = True
        Else
            IsProduct = False
        End If
         
End Function
 
Hi eperichon,

What if I want to compile the output (list) to an existing file in my folder and expand the output (list) as the code runs through the entire tree? Where can I insert a chunk of code for this purpose?

By the way, this has helped me a lot...didn't know I can call out a sub () within itself.

 
Thanks for the code. I am looking to do something similar where I need to get the number of unique parts for each sub-assembly. Any ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top