You are on page 1of 25

Introduction to Visual Basic for Applications (VBA)

TMKT57 Product Modeling

Introduction (1/2)
VBA is an event-driven programming language by Microsoft that allows the user to create customized features within MS Office applications. Short tutorials and examples can be found on the Internet (i.e. www.vbtutor.net/VBA/vba_tutorial.html)

TMKT-57 VB Introduction

Introduction (2/2)
This short tutorial will show:
How to record/edit macros in CATIA V5 and in Excel How to debug macros in Excel How to use Excel to debug scripts written in CATIA V5 How to use watches in Excel How to find the address to features in the Specification Tree in CATIA V5

TMKT-57 VB Introduction

How to Record a Macro (1/4)


CATIA V5 has a built-in function that allows to record the operations the user performs and save them in a VBA-script called macro. The macro can then be used or edited at will. This is a very useful function that speeds up the learning process: when you do not know how to write a script, try doing with mouse clicks what you would like CATIA to do automatically while recording everything in a macro. Then look in the saved macro and understand the code! Sadly this function does not always work. It is possible to record only a limited set of operations

TMKT-57 VB Introduction

How to Record a Macro (2/4)


In this example we will record a macro that will create a new part within an existing product. In CATIA, start from an empty product:
1) Select Tools > Macro > Start Recording

TMKT-57 VB Introduction

How to Record a Macro (3/4)


In the window that opens:
1) Here its written in which CATIA document the macro will be stored 2) Indicates the language to be used in the macro (CATScript or MS VBScript)

3) The name to assign the macro

4) Click on Start. This will start the recording of all manual operations until Stop is clicked!

TMKT-57 VB Introduction

How to Record a Macro (4/4)


Right-click on Product1 and choose Components > New Part. A new CATPart Part1 is created. Then click onStop to interrupt the macro recording. A macro has now been created with the code necessary to create a new CATPart within a CATProduct.
STOP

TMKT-57 VB Introduction

How to Edit a Macro (1/2)


To inspect the macro choose Macro > Macros from the Tools menu:
1) The recorded macro can be edited by selecting it and pressing Edit from the menu on the right

TMKT-57 VB Introduction

How to Edit a Macro (2/2)


The script will be presented in a new window:
1) Definition of the Document where we want to execute the oprations.

2-3) Lists all products in the CATProduct


4) Within product1 creates a new component of type CATPart

TMKT-57 VB Introduction

10

The VBScript Code (1/3)


All VBA scripts are contained between the Sub and End Sub statements The basic concept of object is needed:
An object is a code template, grouping properties and methods which can be retrieved or called Ex.: dayWeather.Temperature from an object called dayWeather we retrieve the property Temperature Ex.: dayWeather.Update the object called dayWeather is updated calling the method Update Ex.: events can be programmed combining properties and methods to obtain wanted behaviors

TMKT-57 VB Introduction

11

The VBScript Code (2/3)


Lets focus on the code in the VBScript, i.e.:
Set product2 = products1.AddNewComponent("Part", "")

1) This is an object that we decide to call product2, but could just as well have be chair77 or jklfdklfjl

2) This is the object within which we want to perfom and operation (create new Part). The name was freely chosen in the line above

3) This is the method that is applied to the object products1 that tells CATIA to create a new component. The type Part is given as an argument

The code: Set JohnJr = Sugar.AddNewComponent("Part", "") does exactly the same thing, but we have now called the object containing the product where to create the CATPart Sugar and the new part will be stored in the object JohnJr.

TMKT-57 VB Introduction

12

The VBScript Code (3/3)


Other VBScript code examples:

Set SunGear_GS = SunGear_HB.Item("Geometrical Set.1") To the object that we called SunGear_HB we apply the method Item which will search for something called Geometrical Set.1 whithin SunGear_HB. The result of the search will be stored in an object that we call SunGear_GS
In an Excel woksheet called Sheet1 we search for the range called Rib_1_Offset and store the value written in that cell in an object that we decided to call myNr

myNr = Worksheets("Sheet1").Range("Rib_1_Offset").Value

Please note that in the first example the code starts with the keyword Set. Set is used for setting object references, as opposed to assigning a value. In other terms, if using Set the created VB-object will continuously be linked to its reference, while if Set is not used, the value is written when the line is executed and not changed until a new assignment is executed.

TMKT-57 VB Introduction

13

Reaction Writing Shortcut (1/3)


When writing a Reaction you can automatically get the code that gives you access to a particular geometrical feature you are interested in manipulating through the script. An example will help clarify: assume you have a CATProduct that contains a CATPart in which there is a Plane that you want to interact with through a Reaction.

TMKT-57 VB Introduction

14

Reaction Writing Shortcut (2/3)

2) Click on the Reactions tool

1) Choose the Knowledge Advisor workbench in the Knowledgeware folder

3) Choose VB action then click Edit action

TMKT-57 VB Introduction

15

Reaction Writing Shortcut (3/3)


3) The code necessary to access the Plane will be automatically generated

1) Click Insert object resolution and then click on Plane.1 in the Specification Tree

TMKT-57 VB Introduction

16

Debugging Code (1/9)


It happens rarely that a script works as planned from the very start. Most of times it is necessary to test and modify it several times, or debugging it. Sadly CATIA V5 student version does not have the VB Editor. The only thing CATIA will do is executing the script or show an error message telling the user in which line the error may be. This rises two issues:
The only type of errors CATIA will recognize are syntax errors Even if no syntax errors are present the script may produce faulty results due to logical errors in the operations described

Thus it is by far much easier to carry out the debugging and testing of a VBScript using the VB editor in Excel, simply copy/pasting the code in an Excel macro.

TMKT-57 VB Introduction

17

Debugging Code (2/9)


Assume that the following is a reaction that you want to debug:
1) To be able to run the script you need a Sub statement (which is implicit in the CATIA VBScript editor)
Set documents1 = CATIA.Documents Set partDocument1 = documents1.Item("Manual_Instances.CATPart") Set part1 = partDocument1.Part 2) Also needed is an Set hybridBodies1 = part1.HybridBodies instruction defining Set parameters1 = part1.Parameters what the object Set Relations1 = part1.Relations CATIA is Set selection1 = partDocument1.Selection selection1.Clear Set old_nr_ins = parameters1.Item("Old_Nr_Of_Instances") Set new_nr_ins = parameters1.Item("New_Nr_Of_Instances") if old_nr_ins.value < new_nr_ins.value then For I_nr = old_nr_ins.value +1 To new_nr_ins.value Set hybridBody1 = hybridBodies1.Add() hybridBody1.name ="Instance." & I_nr Next old_nr_ins.Value = new_nr_ins.Value part1.Update ' Updates part end if End sub

TMKT-57 VB Introduction

18

Debugging Code (3/9)


What you need to do is:
copy/paste the previous code from the CATIA Reactions editor window into an Excel VB Editor add a Sub-statement at the top of the script, for example:
Sub Script_to_be_tested()

add a definition for the object CATIA:


Set CATIA = GetObject("", "CATIA.Application")

The GetObject method can be applied to most applications: not only CATIA, but MS Word, MatLab, SolidWorks, etc. can be linked with the similar code.

TMKT-57 VB Introduction

19

Debugging Code (4/9)


It should look like this:

TMKT-57 VB Introduction

20

Debugging Code (5/9)


The VB Editor offers at least two very useful features:
Pressing F8 (Step Into) the script will be run one line at the time, which allows you to have full control of what the script is doing Righ-clicking on an object and selecting Add Watch allows you to monitor how the content of an object during the execution of the script. This feature is best used in conjunction with the step into function. An example will clarify the use of the Add Watch feature.

TMKT-57 VB Introduction

21

Debugging Code (6/9): Add Watch

1) Right-click on Relations1 in the code

2) Select Add Watch..

3) The values in this windows can then be left as they are. Just press OK to create a watch

TMKT-57 VB Introduction

22

Debugging Code (7/9): Add Watch


The watch is added at the bottom of the editor window and it can be seen that the object Relations1 is empty at first.

TMKT-57 VB Introduction

23

Debugging Code (8/9): Add Watch


If you now step into the code, as soon as the code line that sets the value of Relations1 is executed, the watch will show what is contained inside:
1) The line setting the value for the object Relations1 has just been executed

2) The watch now shows all the information that can be accessed via VB script

TMKT-57 VB Introduction

24

Debugging Code (9/9): Add Watch


Browsing through the watch you can find almost all the elements that you see in the Specification Tree. The watch can give you an idea of what method to use

TMKT-57 VB Introduction

25

Conclusions
To get started with a VBScript you can try:
Recording a macro in CATIA Using Insert Object Resolution within the Reaction editor in CATIA

Both methods can give you a good starting point. When you encounter problems and/or need to find the source of errors it can be very useful to take advantage of Excels VB editor environment. Some of the debugging capabilities have been presented in this short introduction:
Step Into (F8) Add Watch

TMKT-57 VB Introduction

You might also like