Introduction To Visual Basic For Applications (VBA) : TMKT57 Product Modeling
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
TMKT-57 VB Introduction
TMKT-57 VB Introduction
4) Click on Start. This will start the recording of all manual operations until Stop is clicked!
TMKT-57 VB Introduction
TMKT-57 VB Introduction
TMKT-57 VB Introduction
TMKT-57 VB Introduction
10
TMKT-57 VB Introduction
11
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
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
TMKT-57 VB Introduction
14
TMKT-57 VB Introduction
15
1) Click Insert object resolution and then click on Plane.1 in the Specification Tree
TMKT-57 VB Introduction
16
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
TMKT-57 VB Introduction
18
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
TMKT-57 VB Introduction
20
TMKT-57 VB Introduction
21
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
TMKT-57 VB Introduction
23
2) The watch now shows all the information that can be accessed via VB script
TMKT-57 VB Introduction
24
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