Getting Started With CATIA V5 Macros: Emmett Ross January 2015
Getting Started With CATIA V5 Macros: Emmett Ross January 2015
Getting Started With CATIA V5 Macros: Emmett Ross January 2015
CATIA V5 Macros
Emmett Ross
January 2015
http://www.scripting4v5.com
Why use macros?
If you perform a task repeatedly, you can take
advantage of a macro to automate it. A macro is a
series of functions, written in a scripting language,
that you group in a single command to perform the
requested task automatically, saving time and
reducing the possibility of human error.
• Standardization
• Improve efficiency
• Expand CATIA capabilities
• Streamline tasks
Macro Examples
• The application of automation in the design process is virtually
unlimited.
• Examples of macros that CAD engineers might use:
– Generate Part List in Excel from CATIA
– Automatically place components at specific points
– Compare differences between two parts
– Print annotation data
– Take a screen capture
– Delete deactivated features
– Create a drawing
– Find specific geometry
– Change background color to white
3
CATIA V5 Macros
• Macros run from CATIA V5
• Written in VBA
How to create a macro
Macros are created by two primary methods:
1. Macro recorder
2. Write custom code with the macro editor
4
1/12/2015
MACRO RECORDER
• Many times extra lines of code are added which are not
necessary. This is based on the order of steps that you do as
you record the macro. These unnecessary lines can be
removed.
• Recorded macros do not contain any comments or
explanations of what is happening in the code and input
parameters are never recorded.
• Recorded macros are very simple and don’t contain If
statements, Loops, or other commonly used code.
• Record macros can be used for simple tasks but it is better
to write custom code; use the recorder if you get stuck or
to find out the syntax if you don’t know it.
To open and run macros
• Case Sensitivity: By default, VBA is not case sensitive and does not
differentiate between upper-case and lower-case spelling of words.
• Comments: Add comments to your statements using an apostrophe
('), either at the beginning of a separate line, or at the end of a
statement. It is recommended that you add comments wherever
possible to make your scripts easier to understand and maintain,
especially if another user has to make changes to it later on down
the road.
• Indentation: Indent or out dent script to reflect the logical structure
and nesting of the statements to make it easier to read.
• Parentheses: It is important to use parentheses correctly in any
statement to achieve the desired result and to avoid errors.
SYNTAX
Example:
• Good Variable Names: dimensionOne, dimension1
• Bad Variable Names: dimension One, 1dimension
VARIABLES NOTATION
• There is a popular naming convention where a letter is placed in
front of the variable name to notate what type of data the variable
is:
• Some programmers use “int” or “n” for integer and “str” for string.
Either or works as long as your notation is consistent and easy for
others to understand.
• By default, Excel doesn’t force you to declare
your variables. However, you should change that
setting immediately. Press Alt+F11 to launch the
Visual Basic Editor (VBE). Go to Tools>Options.
• In the Editor tab of the Options dialog, make sure
that there’s a check mark in front of “Require
Variable Declaration”
DIMMING AND SETTING
Option Explicit
Sub Main()
MsgBox “Hello world.”
End Sub
To create an icon for each macro:
• Go to Tools>Customize>Commands tab> scroll
down to Macros>click "Show Properties"
• Under Commands, select the macro then click the …
box. The icon browser pops up. Over 6000 to
choose from. Select one then hit close.
• Finally, drag and drop the .CATScript file from the
command window to whatever toolbar you would
like the icon to appear on
• Now you can click the Icon to run your macro!
• You can also setup a custom keyboard shortcut as
well.
CATIA Version Macro
This macro displays the current CATIA version and service pack number
Sub CATMain()
'---display the current CATIA version, R number and service pack
MsgBox "The current CATIA configuration is: V" & CATIA.SystemConfiguration.Version & _
"R" & CATIA.SystemConfiguration.Release & _
"(SP" & CATIA.SystemConfiguration.ServicePack & ")"
If CATIA.SystemConfiguration.IsProductAuthorized("FPE.prd") THEN