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:
Available in paperback and Kindle eBook editions.
Expanded eBook edition with 100 new pages available here.
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
oSel.Clear
oSel.Add(ObjectToAdd)
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.

0 comments:
Post a Comment