In this CATIA VBA tutorial, the goal is going to be to display the number of parts and the number of unique parts in each sub-assembly in a pop-up form/window. Because the total number of subassemblies for each CATIA product we run the code on will be different, our user form will have to be dynamic, meaning its size will change with the number of subassemblies. If you’ve signed up for my email newsletter then you’ve received the CATScript code to scroll down through a product tree. We are going to use this CATIA macro as the backbone for our new dynamic form VBA macro. When I say unique parts I mean we do not want to count instances of a part.

Confused? I made a short video to demonstrate how this works:

The first thing we need to do is to declare all of our global variables, or any variables with complete program scope. We are going to be counting a lot of parts so most of our global variables are counters.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Option Explicit
 
'----------------------- Global variables-------------
 
Public pastrPartDoc() As String
 
Public topProduct As String
 
Public totalCounter As Integer
 
Public productCounter As Integer
 
Public partCounter As Integer
 
Public uPartCounter As Integer
 
Public prodArray() As String
 
Public compArray() As String
 
Public ProdName, compName As String
 
Public labelCounter As Long
 
 
 
‘---begin primary code to traverse Product Structure and count number of total and unique parts for each sub-assembly
 
Sub CATMain()
 
'---set counters initial value----
 
totalCounter = 0
 
productCounter = 0
 
partCounter = 0
 
uPartCounter = 0
 
'--- Start recursively walk down the Product Tree
 
  Dim oMyProduct As Product
 
  Set oMyProduct = CATIA.ActiveDocument.Product
 
  topProduct = oMyProduct.PartNumber
 
  Dim iLevel As Integer
 
  iLevel = -1
 
  Call TraverseProduct(oMyProduct, iLevel)
 
‘--complete TraverseProduct code is included in my email newsletter, I will not repeat it here-----
 
'---output data to userform
 
UserForm1.Labela.Caption = "The total number of components of " & topProduct & " is: " & totalCounter
 
UserForm1.Labelb.Caption = "The total number of assemblies is: " & productCounter
 
UserForm1.Labelc.Caption = "The total number of parts is: " & partCounter
 
UserForm1.Labeld.Caption = "The total number of unique parts is: " & uPartCounter
 
labelCounter = 1
 
Dim x, m As Integer
 
m = UBound(prodArray)
 
'loop through all products saved in the array
 
For x = 1 To m
 
    ProdName = prodArray(x)
 
    compName = compArray(x)
 
    Call addLabel
 
    labelCounter = labelCounter + 1
 
Next
 
'display the form
 
UserForm1.Show vbModeless
 
End Sub

So what’s going on here? Essentially, the program is walking down the complete specification tree, going to each component and determining if it is a part or a product and then adding it to the count. Each part or product is saved in an array and each time something new is added it checks to see if that part already exists in the array. This is how we get the unique count.

 

What about the dynamic form part? The Call addLabel code calls a subroutine that adds a new label for every product. This is where you see the name of the product and the number of child parts. Here’s the code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Sub addLabel()
 
Dim theLabel As Object
 
Set theLabel = UserForm1.Controls.Add("Forms.Label.1", "Test" & labelCounter, True)
 
    With theLabel
 
       .Caption = "Component Name: " & ProdName & ". Number of child components is: " & compName
 
       .Left = 250
 
       .Height = 10
 
       .Width = 300
 
       .Top = 20 * labelCounter
 
    End With
 
End Sub

count unique parts in catia

So, that’s how you create a dynamic form in VBA. CATIA automation sure is fun, isn’t it?

 

To recap, the goals of this CATIA VBA example were to:

1. To count the number of unique parts in each sub-assembly

2. Develop a dynamic form in VBA

Learn more about CATIA automation.