×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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

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

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

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

(OP)
I try this configuration and it's not gives proper results.

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

did not try hard enough...

CODE --> vba

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

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

(OP)
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


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

Quote (you)

macro just stops/crash
what line ? what error message ?

Eric N.
indocti discant et ament meminisse periti

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

(OP)
On this line in While loop:

CODE --> VBA

Set objPrd = objPrd.Parent 

Error msg:
Run-time error '13':
Type mismatch

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

(OP)
Problem solved. Correct VBA code:

CODE --> VBA

Sub CATMain()

Dim Sel1 As Object
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources