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!

CATIA VBA : Get instance name path from selected item to root product

Status
Not open for further replies.

MichalPon

Industrial
Aug 17, 2016
9
GB
Hello,

I have issue with getting instance name path of selected item to root product.
Result that I want to get from CATIA in this case is string with the following path: "Product2.1\Part2.1"
Additional thing is that CATIA tree will be not known in the future. Getting path formula should work with different multilevel trees.

Simple CATIA tree:

+--------+
!Product1!
+--------+
!​
+- Product2 (Product2.1)​
!​
+- Part1 (Part1.1)​
+- Part1 (Part2.1) <- Selected Item by user​

VBA Code:
Dim Sel1 As Object
Dim Filter1(0)
Dim Status1 As String
Dim objSelPrd1 As Product

' [ Part Selection ] ***

Set Sel1 = CATIA.ActiveDocument.Selection
Filter1(0) = "Product"
Status1 = Sel1.SelectElement2(Filter1, "SELECT PART", False)

' [ Switch Part to Design Mode ] ***

Set objSelPrd1 = Sel1.Item2(1).LeafProduct
objSelPrd1.ApplyWorkMode DESIGN_MODE

MsgBox Sel1.Item2(1).LeafProduct.Name 'gives result "Part2.1"
MsgBox Sel1.Item2(1).LeafProduct.Parent.Name 'gives result "Products"
MsgBox Sel1.Item2(1).LeafProduct.Parent.Parent.Name 'gives result "Product2.1" but I don't know how many levels CATIA tree will have in different products

So I'm searching for a solution to get whole path as string from Part chosen by user no matter of CATIA tree complexity - in this case "Product2.1\Part2.1"

Thanks in advance,
Michal
 
Replies continue below

Recommended for you

once you get level up with object.LeafProduct, did you try to go up more level with object.LeafProduct.LeafProduct?



Eric N.
indocti discant et ament meminisse periti
 
I try this configuration and it's not gives proper results.
 
did not try hard enough...

Code:
Sub catmain()

    Dim Sel1 As Object
    Dim Filter1(0)
    Dim Status1 As String
    Dim objPrd As Product
    
    ' [ Part Selection ] ***
    
    Set Sel1 = CATIA.ActiveDocument.Selection
    Filter1(0) = "Product"
    Status1 = Sel1.SelectElement2(Filter1, "SELECT PART", False)
    
    ' [ Switch Part to Design Mode ] ***
    
    Set objPrd = Sel1.Item2(1).LeafProduct
    fullpath = objPrd.Name
    
    
    While Not reachroot
    
        Set objPrd = objPrd.Parent
        
        fullpath = objPrd.Name & "\" & fullpath
        
        checkParent = ""
        
        On Error Resume Next
        checkParent = objPrd.Parent.FullName
        On Error GoTo 0
        
        If checkParent = CATIA.ActiveDocument.Name Then reachroot = True
    Wend
     
    MsgBox fullpath

End Sub



Eric N.
indocti discant et ament meminisse periti
 
Thanks for developing code but there is still something wrong.

When I try to use it as CATScript I get infinite loop. In other case when I try to use as VBA I was asked to add a few dims but even after that step I not received MsgBox, macro just stops/crash without it.

Dims that I added:
Dim fullpath As String
Dim reachroot As Variant
Dim checkParent As String


 
On this line in While loop:

Code:
Set objPrd = objPrd.Parent

Error msg:
Run-time error '13':
Type mismatch
 
Problem solved. Correct VBA code:

Code:
Sub CATMain()

[indent]Dim Sel1 As Object[/indent]
    Dim Filter1(0)
    Dim Status1 As String
    Dim objPrd As Object
    Dim fullpath As String
    Dim reachroot As Variant
    Dim checkParent As String
    
    ' [ Part Selection ] ***
    
    Set Sel1 = CATIA.ActiveDocument.Selection
    Filter1(0) = "Product"
    Status1 = Sel1.SelectElement2(Filter1, "SELECT PART", False)
    
    Set objPrd = Sel1.Item2(1).LeafProduct
    fullpath = objPrd.Name
       
    While Not reachroot
  
        Set objPrd = objPrd.Parent       
        fullpath = objPrd.Name & "\" & fullpath

        checkParent = ""
        checkParent = objPrd.Parent.Name
  
        If checkParent = CATIA.ActiveDocument.Name Then reachroot = True
    Wend
     
    MsgBox fullpath

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top