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.

 

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:

 

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.