Wednesday, January 25, 2012

VB Scripting for CATIA V5

Do you want to learn how to write VB script macros? In addition to the free power point tutorials I've made available to download, I've just released my latest book with expanded information entitled VB Scripting for CATIA V5.

There are many CAD engineers, designers, and technicians who want to write macros but simply don’t have time to sit down and learn everything they need to know. Through a series of example codes and tutorials I'll explain how to use and create CATScript macros for CATIA V5. No programming experience is required! This information is not featured in the user help documentation. The purpose of this text is to show beginners how they can approach different problems and for users to rewrite code shown in the examples to suite their specific needs. I'll cover core items to help teach beginners important concepts needed to create custom VB script macros for CATIA V5. Listed below are a few excerpts from the book:

What is VB script?

VBScript is a subset of the Visual Basic Programming language (VBA). All elements of VBScript are present in VBA, but some VBA elements are not implemented in VBScript. The result of the slimming down process is a very small language that is easy to use. In general, script languages are easier and faster to code in than the more structured, compiled languages such as C+ and C++. Code specific to CATIA is saved as a .CATScript file.

CATIA V5 running on Windows can be automated with any application which can connect to Windows Component Object Model (COM), including VBA (Excel, Word, CATIA, etc.), VBScript, JavaScript, Visual Basic 6.0, Microsoft Developers Studio.NET, and others. For CATIA V5 running on UNIX, emulators allow for VBScripts to be run with no interface building tools. Some CATScripts from this text may work under UNIX OS but not all due to differences between the two systems. However, this will not be covered in this text.

Fundamentals for Creating Custom Macros

A CATIA VB program or “macro” consists of a “Subroutine” named CATMain(). Variables are “dimmed” (declared) and then “set” (given a value). Variables are “dimmed” (declared) as a type. A type is either a “primitive” type (single, double, integer, string, etc.) or an object type (more complex). Strings are especially useful because they hold text. Message boxes are frequently used to display strings to users while the program is running. Looping is often used to perform iterative actions

·         Object – An entity (in CATIA or VB). Points, Pads, Parameters, etc. are all examples of CATIA objects.

·         Property – A characteristic of an object. For example, the name of a PartDocument is a property of that object.

·         Method – An action that an object can perform. For example PartDocument.SaveAs() is an action that the object can perform.

·         Collection – A group or list of similar objects which are put together for a specific reason.

Object oriented programming came about due to the need to represent more complex ideas within a program. For example, you could say that a person is described by his height, weight, and hair color, and that every person has certain actions that they can perform, such as walking, eating, and sleeping. These “properties” and “methods” make up the “class” called “Person.” Objects of this class can then be used in a program to represent individual people.

The Selection Object

To interactively access properties of an object in CATIA V5 you would simply right click on the object and select properties. The object has to be selected first. In the CATIA object model, there is a special object called “selection” which belongs to different documents. This Selection object is a container that holds anything that is currently selected in a session of CATIA V5. It can be accessed like this:

            Dim oSelection As Selection

     Set oSelection = CATIA.ActiveDocument.Selection

If nothing is selected then the selection is empty. If one or more objects are selected then the selection contains those one or more objects. To add an element to the selection:

            Dim oSel As Selection

     Set oSel = CATIA.ActiveDocument.Selection



A good practice is to always clear the selection before and after you use it. To check what has been selected you could use the following code, which will loop through all selected objects and display the name in a message box for each one:

            For I = 1 to oSel.Count

          Msgbox oSel.Item(i).Value.Name

     Next ‘i

You can search through selections by several different methods, including searching by name, type, color, etc. Once the search command has been issued, you then need to loop through the selection object to get the items that have been found.

·         To look for all object named “pad”: objSel.Search “Name=Pad.*,all”

·         To look for all points: objSel.Search “Type=Point,all”

·         To look for items colored red: objSel.Search “Color=Red,sel”

·         To look for items on layer 10: objSel.Search “Layer=10,all”

Available in paperback and Kindle eBook editions. 
Expanded eBook edition with 100 new pages available here.


  1. For I = 1 to oSel.Count
    Msgbox oSel.Item(i).Value.Name
    Next ‘i


    do you mean: Next i ??


    1. If it is a VBA program then yes, it is Next i. If it is a CATScript then no, it is simply Next. I add a comment of 'i to keep track of which Next statement goes with what variable if there are many (such as i, j, k, etc.). Make sense?

  2. sir
    Iam new to VB scripting. I have a doubt. i need to instantiate my powercopy to already existing planes. because i can create planes using planes in between command. now using macro i need to identify these planes and assign powercopy. these planes are my input for instantiation. And my no of planes is also not fixed. how should i proceed. please help.

    i don't know how to call planes and assign them my powercopy

    thank you..i know you can help me

    1. Hi Michael,

      Sounds like you need to get the planes in the selection, either by having user physical select them after starting the macro or by searching. Will the plane names all be similar? Are they all contained within one geometrical set?

  3. This comment has been removed by the author.

  4. This comment has been removed by the author.

  5. how to make macro for auto dimension into the drawings????????


I'd love to hear from you!