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 - extract CAT.Products names from tree. 2

Status
Not open for further replies.

CozminC

Industrial
Jan 30, 2014
14
RO
Hello all,

Here is the situation.

In CATIA i have a CAT.Product that contains other Products(those are containing also other products.)
I need to extract the names of all products wich are starting with "R" lettter from my tree.The problem is that the "R" products can be found at different expanesion levels of the tree.

I have uploaded a picture that depicts what i want to mean.


Thank you for your time.
 
 http://files.engineering.com/getfile.aspx?folder=8584d694-98c0-482a-9627-3dadebc6469c&file=Arbore.jpg
Replies continue below

Recommended for you

check for recursive script to go in tree structure... many example if you search for that.

Eric N.
indocti discant et ament meminisse periti
 
Thank you both for your answers.

After i read the reply of "itsmyjob" i have searched for recursive script for catia tree structure and i have found some userfull info.
Some info were wrong from the first time : there are product with names starting with : "C","_","M","G" and 12313R (The "R" is not at the begining but is the 10'th character). By the way all the products that i am interested in have names starting with numbers.

I managed to write some code and when running the script i can prompt all the names (and some properties) of all the products that i am interested in and also adding them
in to the selection.

Code:
1.Sub CATMain()
2. 
3.GetNextNode CATIA.ActiveDocument.Product
4. 
5.End Sub
6. 
7. 
8.    
9.Sub GetNextNode(oCurrentProduct As Product)
10. 
11. 
12.    Dim oCurrentTreeNode As Product
13.    Dim i As Integer
14. 
15.    Dim m1 as String
16.    Dim m2 as String
17.    m1 = "_"
18.    m2 = "M"
19.    Dim g as String
20.    g = "G"
21.    Dim c as String
22.    c = "C"
23.    Dim nume as String
24.    nume = ""
25.    Dim revision as String
26.    revizie = ""
27.    Dim description as String
28.    descriere = ""
29.    Dim pozitie_separator as Integer
30.    pozitie_separator = 0
31.    Dim full_name as String
32.    fullname = ""
33.  
34. 
35.   Dim selection1 as Selection
36.   Set selection1 = CATIA.ActiveDocument.Selection
37. 
38. 
39.       
40.For i = 1 To oCurrentProduct.Products.Count
41.        Set oCurrentTreeNode = oCurrentProduct.Products.Item(i)
42. 
43. 
44.           
45.    If  IsProduct(oCurrentTreeNode) = False Then
46.  
47.             nume = oCurrentTreeNode.Name
48.             
49.              If m1<>left(nume,1) AND g<>left(nume,1) AND m2<>left(nume,1) AND c<>left(nume,1)  Then
50. 
51.         
52. 
53.                   'MsgBox nume& " REFERINTA"
54.                  'MsgBox oCurrentTreeNode.Revision& " REVIZIE"
55.                   revizie=oCurrentTreeNode.Revision
56.                   'MsgBox oCurrentTreeNode.DescriptionInst& "DESCRIERE"
57.                   descriere=oCurrentTreeNode.DescriptionInst
58.                   
59.                    nume = left(nume,10)
60.                    revizie = left(revizie,3)
61.                    pozitie_separator=INSTR(1, descriere, ":")
62.                    descriere=left(descriere,pozitie_separator - 1)
63. 
64.                    'MsgBox nume&"/"&revizie&"/"&descriere
65.                    
66.                    fullname = nume&revizie&descriere
67.                     
68.                    selection1.Add oCurrentProduct.Products.Item(i)
69. 
70. 
71.    End If
72.    End If
73.       
74.       
75.        ' if sub-nodes exist below the current tree node, call the sub recursively
76.       If oCurrentTreeNode.Products.Count > 0 Then
77.            GetNextNode oCurrentTreeNode
78.        End If
79.     
80.              
81.   Next
82. 
83.          
84. 
85.End Sub
86. 
87. 
88.Function IsProduct(objCurrentProduct As Product) As Boolean
89. 
90.    Dim oTestProduct As ProductDocument
91.    Set oTestProduct = Nothing
92. 
93.    On Error Resume Next
94.     
95.    Set oTestProduct = CATIA.Documents.Item(objCurrentProduct.PartNumber & ".CATProduct")  
96. 
97.        If Not oTestProduct Is Nothing Then
98.            IsProduct = True
99.        Else
100.            IsProduct = False
101.        End If
102.        
103.End Function
Problems : sometimes i must get the list of all products (good ones) in an excel file
sometimes i must compare them from catia with an excel file that has allready data, and hide the ones that are not found in excel.

I don't know where to write the code to open the excel and complete or verify becouse of the recursive function.I have tried earlier and i got computer and catia crashed after it opened me 360 excell workbooks. (opened one each time he found a good product)
 
You need to make a list of all the things you need to do and solve each issue one at a time. By the way, the Instr() function returns the location of a specific character in a string. If you wanted to check if the First character of a part number is an R you can use:
If Instr(oPart.Name, "R") = 1 then
DoSomething
End If
 
Thank you all. Your answers were very helpful .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top