Preface Getting Started Object Library Use Cases Glossary: Knowledge Advisor Journaling Guide
Preface Getting Started Object Library Use Cases Glossary: Knowledge Advisor Journaling Guide
Preface Getting Started Object Library Use Cases Glossary: Knowledge Advisor Journaling Guide
Journaling Guide
Preface
Getting Started
Object Library
Use Cases
Glossary
Preface
Welcome to CATIA Knowledgeware automation, a fast and easy way to write macros and
create simple applications to be plugged to CATIA. Whether you are an experienced
professional or a brand new to programming languages, CATIA knowledgeware provides you
with a set of tools to simplify, automate and improve your daily life as a CATIA end-user.
This preface gives you some information to brush-up on the concepts used in Visual Basic or
VBScript and introduce you to the knowledgeware automation objects. If you are an absolute
beginner in programming languages, it is much better to go to the http://www.microsoft.com
site, then refer to the CATIA Infrastructure Journaling Guide.
RoadMap
This guide provides the basis for understanding how to create macros using knowledgeware
features. These knowledgeware features are supplied in the Infrastructure product (parameters,
formulas and design tables) as well as in the Knowledge Advisor product (rules and checks).
To keep up with the material presented in this guide, it is better if you have a background in
script languages. However, the Use Cases provided as samples are fully commented and
should help you understand how to proceed to write simple macros as well as fully-fledged
macros.
If you are a beginner
Don't skip the Getting Started if you are a beginner. It is a knowledgeware-oriented
example described in the form of two interactive tasks. You just record a macro, take a
look at the code generated, modify it then replay it. It is very basic.
If you already get by in Script languages
If you already dabble in script languages, you can get straight to the Object Library or to
the Use Cases.
Most knowledge applications rely on the Infrastructure and Part Design products. For
information refer to the CATIA Infrastructure and CATIA Part Design Journaling Guides.
Automation in a Nutshell
CATIA is an OLE Automation server for Windows NT which allows you to record and replay
macros for both Windows NT and UNIX.
The macros recorded from the Tools->Macro menu are generated in a syntax very close to the
Visual Basic language. Statements such Dim... As ... which are not supported in VBScript are
supported by the CATIA macro language so that a macro recorded on Windows NT can be
replayed on UNIX or the reverse.
Automation is a feature of the Microsoft Component Object Model (COM), an
industry-standard technology that applications use to expose their objects to development
tools, macro languages, and other applications that support Automation. For example, CATIA
knowledware exposes a parameter, a formula, a rule or a check, each as a different type of
object. A word processor exposes objects such as an application, a document, a paragraph, a
sentence, a bookmark, or a selection.
When an application supports Automation, the objects the application exposes can be
accessed by Visual Basic. To manipulate these objects, you have to invoke methods on the
object or get / set the object's properties.
To create an Automation application, you need know about:
the macro language itself (VBScript or Visual Basic for example)
the properties and methods supported by the application objects. The objects with their
properties, and methods supported by an application are usually defined in the
application's object library.
following statement uses a message box to display the maximum tolerance of a Length
parameter:
MsgBox(length0.MaximumTolerance)
A method is an action that an object can perform. The following statement uses the Rename
method to rename a parameter object:
parameter.Rename("String1")
Collections
A collection is an object that contains several other objects, usually, but not always, of the
same type. In Microsoft Excel, for example, the Workbooks object contains all the open
Workbook objects. In Visual Basic, the Forms collection contains all the Form objects in an
application. In CATIA knowledgeware the Relations object contains all the knowledgeware
features of relation type (formulas, rules, checks and design tables).
A collection is always denoted as a plural name to help easily recognize a collection among
other objects. The collection index begins at 1, and not 0. Usually, an object in the collection is
reached using its index, but it can also be reached using the name you assign to it. The
following statement uses the item method of the Collection object to retrieve the
KwrMacro0\Volume.1 parameter from its name, myPar1 being a Parameters object.
myPar1.item("KwrMacro0\Volume.1")
CATIA collections are described by the Collection object. Refer to the 'CATIA Infrastructure
Journaling Guide' for more information.
To create a parameter
Parameters collection
To manipulate a parameter
Parameter object
To Create a Formula
Relations collection
To manipulate a formula
Formula Object
To create a Rule
Relations collection
To manipulate a Rule
Program Object
To Create a Check
Relations collection
To manipulate a Check
Check Object
Getting Started
Beginners in CATIA Automation can create useful applications by learning just a few of the
keywords. If you don't know anything about script languages, just carry out the scenario below.
It doesn't really teach you how to program but it is intended to give you a feeling for what a
macro looks like.
To really get started programming:
1. take a look at any book dealing with the subject. 'Step by Step' books available from
Microsoft Press can be recommended but your corner bookshop is probably full of books
about Visual Basic for beginners.
2. don't hesitate to search for information on specialized Internet sites
3. then refer to the CATIA Infrastructure Journaling Guide.
4. Press Start. From now on, all the interactions will be recorded in the macro
you have just specified.
Start of interaction recording
. Click the
icon.
b. In the "Formulas" dialog box, select the Length item with 'Single
Value' in the 'New Parameter of type' list, then click 'New Parameter
of type'. A new parameter with Length.1 name appears in 'Edit name,
value or formula'.
c. In 'Edit name, value or formula', replace the Length.1 name with
Reference_Length and assign the 70mm value to this parameter.
Then click Apply.
d. In the parameter list select the PartBody\Pad.1|FirstLimit\Length
parameter. Click Add Formula and enter the formula below in the
formula editor:
Reference_Length / 10.
e. Click OK. This is what you should see onscreen:
second limit:
Language="VBSCRIPT"
Sub
Dim
Set
Set
Set
CATMain()
Length0 As Dimension
Doc1 = CATIA.ActiveDocument.Part
Par1 = Doc1.Parameters
Length0 = Par1.CreateDimension("Length.1", "LENGTH", 0.000000)
Length0.Rename "RefLength"
Length0.Value = 70.000000
Dim Body1 As AnyObject
Set Body1 = Doc1.Bodies.Item("PartBody")
Dim Pad2 As AnyObject
Set Pad2 = Body1.Shapes.Item("Pad.1")
Dim Formula3 As Formula
Set Rel1 = Doc1.Relations
Set Di1 = Pad2.FirstLimit.Dimension
' Modify the formula creation - the same result is obtained
Set Formula3 = Rel1.CreateFormula("Formula.1","", Di1, "RefLength / 10")
' Rename Formula.1
Formula3.Rename "F1"
' Add a new formula
Dim Formula4 As Formula
Set Rel1 = Doc1.Relations
Set Di2 = Pad2.SecondLimit.Dimension
Set Formula4 = Rel1.CreateFormula("Formula.2","", Di2, "RefLength * 10")
' Rename the new formula
Formula4.Rename "F2"
CATIA.ActiveDocument.Part.Update
End Sub
4. Save this macro under the KwrMacro1.CATScript name.
5. Run the KwrMacro1.CATScript new macro on your initial document. This is what you can see
onscreen:
The Formula.1 has been renamed, the new formula F2 has been added to the document and the
whole document is updated.
Object Library
Here is the hierarchy of knowledgeware automation objects.
IUnknown
|
+---CATBaseUnknown
|
+---CATBaseDispatch
|
+---AnyObject
| |
| +-----Parameter
| |
|
| |
+---------IntParam
| |
|
| |
+---------BoolParam
| |
|
| |
+---------RealParam
| |
|
| |
+---------StrParam
| |
|
| |
+---------Dimension
| |
|
| |
+--------Length
| |
|
| |
+--------Angle
| |
| +-----Relation
|
|
|
+---Formula
|
|
|
+---Rule
|
|
|
+---Check
|
|
|
+---DesignTable
|
+-------Collection
|
+---Relations
|
+---Parameter
Objects such as Dimension, Formula and Rule are not described because they provide
neither properties nor methods. They are manipulated by the properties and methods of their
parent object.
Parameter Object
Derived Objects
Definition
As an end-user, you can add comments to a parameter and decide to hide a parameter from the specification view.
Reminder
To hide a parameter or add a comment to a parameter,
you just have to right-click the value field in any
parameter edition box, then select the Edit Comment... or
Hide command from the contextual menu.
Parameters are described by the Parameter object which provides you with properties to determine whether a parameter is
hidden or not and to retrieve the comment associated with the parameter.
Parameters with multiple-values cannot be managed in CATIA macros.
Properties
parameter.Hidden
Returns True if the parameter is hidden, otherwise returns False.
parameter.Comment
Returns the comment associated with the parameter.
Methods
parameter.Rename(newparameterName)
Renames the parameter.
Example
See Managing Hidden Parameters.
Parameter Object
Derived Objects
IntParam Object
BoolParam Object
StrParam Object
RealParam Object
IntParam Object
Definition
An IntParam object describes a parameter whose value can only be an integer.
Properties
parameter.Value
Returns or sets the parameter value.
BoolParam Object
Definition
A boolean is a parameter whose value can only be True or False.
Properties
parameter.Value
Returns or sets the value of a boolean parameter.
Example
The extract below creates the "Ischecked" parameter of boolean type.
' ...
Dim BoolParam0 As BoolParam
Dim myDoc As Document
Set myDoc = CATIA.ActiveDocument
Dim myParamCol As Parameters
Set myParamCol = myDoc.Part.Parameters
Set BoolParam0 = myParamCol.CreateBoolean("Boolean.1", True)
BoolParam0.Rename "IsChecked"
BoolParam0.Value = False
' ...
StrParam Object
Definition
An StrParam is an object which describes a string type parameter.
Properties
parameter.Value
Returns or sets the value of a string parameter.
RealParam Object
Derived Objects
Definition
A RealParam object describes a parameter whose value can only be a real.
Properties
parameter.Value
Returns or sets the value of the real parameter.
parameter.RangeMin
Returns or sets the lower bound of a real parameter.
parameter.RangeMax
Returns or sets the upper bound of a real parameter.
Example
The extract below specifies a lower(minimum) bound of 10.0 and an upper(maximum) bound of 30.0.
...
Dim RealParam0 As RealParam
Dim
Set
Dim
Set
Set
myDoc As Document
myDoc = CATIA.ActiveDocument
myParamCol As Parameters
myParamCol = myDoc.Part.Parameters
RealParam0 = myParamCol.CreateReal("Real.1", 0.000000)
RealParam0.Value = 12.500000
RealParam0.RangeMin = 10.000000
RealParam0.RangeMax = 30.000000
After the macro is finished running, edit the Real.1 parameter lower and upper bounds.
This is what you can see onscreen when you select the Range-> Edit... command:
Length Object
Definition
A Length object describes a parameter whose value can only be a length. A length object derives from
the Dimension object which itself derives from the RealParam object. A dimension object is created by
the CreateDimension method. To manipulate a Length object, you have to use the methods and
properties provided by the RealParam object.
Properties
parameter.MinimumTolerance
Returns the minimum tolerance of a Length object.
parameter.MaximumTolerance
Returns the maximum tolerance of a Length object.
Example
When applied to a CATPart document, the extract below creates the W=10mm and A=45deg dimension
parameters.
' ...
Dim RealParam0 As RealParam
Dim myDoc As Document
Set myDoc = CATIA.ActiveDocument
Dim myParamCol As Parameters
Set myParamCol = myDoc.Part.Parameters
Set Length0 = myParamCol.CreateDimension("W","Length", 10.00)
Set Angle0 = myParamCol.CreateDimension("A","Angle", 45)
msgbox("Minimum Tolerance for Length " & Length0.MinimumTolerance)
msgbox("Maximum Tolerance for Angle " & Angle0.MaximumTolerance)
' ...
Check that the values displayed in the message boxes are the same as those specified in the
Tools->Options->Tolerance tab.
Angle Object
Definition
An Angle object describes a parameter whose value can only be an angle. An angle object derives from
the Dimension object which itself derives from the RealParam object. A dimension object is created by
the CreateDimension method. To manipulate an Angle object, you have to use the methods and
properties provided by the RealParam object.
Properties
parameter.MinimumTolerance
Returns the minimum tolerance of an Angle object.
parameter.MaximumTolerance
Returns the maximum tolerance of an Angle object.
Example
When applied to a CATPart document, the extract below creates the W=10mm and A=45deg dimension
parameters.
' ...
Dim RealParam0 As RealParam
Dim myDoc As Document
Set myDoc = CATIA.ActiveDocument
Dim myParamCol As Parameters
Set myParamCol = myDoc.Part.Parameters
Set Length0 = myParamCol.CreateDimension("W","Length", 10.00)
Set Angle0 = myParamCol.CreateDimension("A","Angle", 45)
msgbox("Minimum Tolerance for Length " & Length0.MinimumTolerance)
msgbox("Maximum Tolerance for Angle " & Angle0.MaximumTolerance)
' ...
Check that the values displayed in the message boxes are the same as those specified in the
Tools->Options->Tolerance tab.
Relation Object
Definition
The Relation object provides you with properties and methods to access the knowledgeware relation
data. This object does not allow you to create a relation.
Properties
relation.Value
Returns the relation definition (the expression).
relation.Comment
Returns the comment associated with the relation.
relation.Activated
Returns True if the relation is activated, otherwise returns False.
relation.Hidden
Returns True if the relation is hidden, otherwise returns False.
Methods
relation.Activate()
Makes a relation active.
relation.Deactivate()
Deactivates a relation.
relation.Rename(newName)
Replaces the relation name with the one specified in the argument.
relation.Modify(newRelation)
Replaces the relation with the one specified in the argument.
Example
1. Open the initial document of Recording and Replaying Macros and add two formulas constraining
Volume.1. Formula.1 is initially deactivated.
myDoc As Document
myDoc = CATIA.ActiveDocument
myPar1 As Parameters
myPar1 = myDoc.Part.Parameters
myRel1 = myDoc.Part.Relations
Dim
Set
Dim
Set
For0
For0
For1
For1
As AnyObject
= myRel1.Item("Formula.1")
As AnyObject
= myRel1.Item("Formula.2")
Check Object
Definition
A check object inherits the properties and methods of the Relation object. In addition, the severity property allows you to determine whether
a check has been declared of silent, warning or information type.
To manipulate checks, you must have the Knowledge Advisor product installed.
Properties
check.Severity
Returns or sets the severity of the check. The check validity is an integer whose value is 1 for Silent, 2 for Information, 3 for
Warning.
Example
Language="VBSCRIPT"
Dim Check0 As Check
Dim myDoc As Document
Set myDoc = CATIA.ActiveDocument
Set myRel1 = myDoc.Part.Relations
Set Check0 = CmyRel1.CreateCheck( "Check.1",_
"Check.1 is not valid",_
"PartBody\Pad.1\FirstLimit\Length > 50mm" )
Check0.Rename "Check.1"
Check0.Severity = 2
You can run this macro on any part provided it has a FirstLimit\Length parameter.
Properties
designtable.CopyMode
Returns the value of the "Duplicate data in CATIA model" check box of the design table dialog
box. Returns True if the design table data have been duplicated in the CATIA document.
Otherwise, returns False.
designtable.Configuration
Returns or sets the active configuration number.
designtable.ConfigurationsNb
Returns the number of configurations in the design table.
Methods
designtable.AddAssociation(param, sheetColumnName)
Associates the parameter specified in the first argument with the column whose name is specified
in the second argument.
designtable.RemoveAssociation(sheetColumnName)
Removes the association created with the column whose name is specified in the argument.
designtable.AddNewRow()
Adds a new row in the design table. The new row contains the active parameter values. If you
deactivate the design table, modify the document parameters, then add a new row, the values
contained in the new row will be those you have just modified in the document and not those of
the last selected configuration.
Example
See Managing Design Tables, Rules and Checks.
Relations Collection
Definition
The Relations collection object provides you with methods to create and manage all type of relations.
The knowledgeware relations are:
the formulas
the rules
the checks
and the design tables.
Properties
The object has no properties but inherits the Collection object properties.
Methods
relations.CreateFormula(formulaName, comment, parameter, expression)
Creates a formula. The parameter to be constrained by the formula is specified as the third
argument.
relations.CreateProgram(ruleName, comment, ruleBody)
Creates a rule.
relations.CreateCheck(checkName, comment, checkBody)
Creates a check.
relations.CreateDesignTable(designtableName, comment, copyMode, ExcelSheetFilePath)
Creates a design table.
relations.Item(relationRank)
Returns the relation specified in the argument.
relations.Remove(relationName)
Removes the relation specified in the argument.
Example
See Creating and Manipulating Parameters and Formulas and Managing Design Tables, Rules and
Checks.
Parameters Collection
Definition
The Parameters collection object provides you with methods to create parameters.
Properties
The object has no properties but inherits the Collection object properties.
Methods
parameters.CreateString(parameterName,parameterValue)
Returns a string parameter with the name and value as specified in the arguments.
parameters.CreateReal(parameterName,parameterValue)
Returns a real parameter.
parameters.CreateInteger(parameterName,parameterValue)
Returns an integer.
parameters.CreateBoolean(parameterName,parameterValue)
Returns a boolean.
parameters.CreateDimension(parameterName,parameterType,parameterValue)
Returns a dimension type parameter with the name, type and value as specified in the arguments.
parameters.Item(parameterRank)
Returns the parameter specified in the argument.
parameters.Remove(parameterName)
Removes the parameter specified in the argument.
Example
See Creating and Manipulating Parameters and Formulas.
Use Cases
This guide provides three fully-fledged samples to help you begin learning CATIA automation
principles. Each sample is broken into steps, and complete code is supplied for each step. The
comments in the first part explain how to run the macro.
' **********************************************************
' MANAGING HIDDEN PARAMETERS
' Macro delivered: KwrMacParameters.CATScript
' **********************************************************
'
' Abstract:
' -------' This macro:
' 1 - scans the list of parameters of a CATPart document
' 2 - determines whether a parameter is hidden
' 3 - displays the list of hidden parameters
'
along with their values and comments
' 4 - asks you to modify the status (Hidden->Show) of the parameters
'
and to re-run to macro.
' Running the macro
' ----------------' It is better to run this macro on the KwrMacroHiddenParam.CATPart document which is
' delivered as a CATIA Infrastructure sample. This document contains hidden
parameters.
' However, you can run this macro on any CATPart document
Sub CATMain()
' Retrieve your active document - CATIA is your application
' You get the active document by using the ActiveDocument property
' on your application object
Dim myDoc As Document
Set myDoc = CATIA.ActiveDocument
' Check whether the document is a CATPart
' Analyse the pathname of the document
' If the extension .CATPart is not found, a message is displayed
' but you exit the procedure
' InStrRev is a standard VB function
Dim strPartName, strCATPart
strPartName = myDoc.Name
if (InStrRev(strPartName,".CATPart",-1) = 0)_
then MsgBox("Your document should be a .CATPart") : Exit Sub
' Retrieve the collection object which contains
' all the document parameters
Dim myParamCol As Parameters
Set myParamCol = myDoc.Part.Parameters
' Declare the variables to be used in the message box
' which displays the hidden parameters
Dim strRel0 As String
Dim strRel1 As String
Dim strRel2 As String
Dim HiddenNumber As Integer
strRel0 = "Name
Value
Comments"
strRel1 = "Here is the list of hidden parameters" & vbCrLf & strRel0
' Scan the paraneter list,
' test whether the value returned by the Hidden property is "True"
' Count the number of hidden parameters
For Each myPar in myParamCol
if (myPar.Hidden = "True")_
' *************************************************
' CREATING AND MANIPULATING PARAMETERS AND FORMULAS
' Macro delivered: KwrMacParmAndForm.CATScript
' *************************************************
'
' Abstract:
' -------' This macro explains how to:
'
1 - retrieve the Relations and Parameters objects
'
2 - create String, Integer and Length type parameters
'
3 - create formulas and display their list in a message box.
'
' Running the macro
' ----------------' You can run this macro on the KwrMacro0.CATPart document which is
' delivered as a CATIA Infrastructure sample, but this macro operates
' on any CATPart document
Sub CATMain()
' Retrieve your active document - CATIA is your application
' You get the active document by using the ActiveDocument property
' on your application object
Dim myDoc As Document
Set myDoc = CATIA.ActiveDocument
' Check whether the document is a CATPart
' Analyse the pathname of the document
' If the extension .CATPart is not found, a message is displayed
' but you exit the procedure
' InStrRev is a standard VB function
Dim strPartName, strCATPart, myPos
strPartName = myDoc.Name
if (InStrRev(strPartName,".CATPart",-1) = 0)_
then MsgBox("Your document should be a .CATPart") : Exit Sub
' Retrieve the collection object which contains
' all the document relations
' myDoc.Part is satisfied as the document extension
' has been checked
' Note: Statements below could not be applied to a CATProduct
Dim myRelCol As Relations
Set myRelCol = myDoc.Part.Relations
For Each myRel in myRelCol
myRelCol.Remove myRel.Name
Next
' Retrieve the collection object which contains
' all the document parameters
Dim myParamCol As Parameters
Set myParamCol = myDoc.Part.Parameters
' Just to be clean before creating parameters
' Scan the collection of parameters and remove parameters
' with name
"ProjectId"
'
'
'
'
or
or
or
or
"ProjectKey"
"StringLength"
"SphereRadius"
"SphereVolume"
'
'
'
'
'
'
' ************************************************************************
' MANAGING DESIGN TABLES, RULES AND CHECKS
' ************************************************************************
' Abstract
' --------' This macro which illustrates how to create and manage a design table
'
1 - Checks whether a file which is used as an argument in
'
the CreateDesignTable method exists
'
2 - Creates a design table from an existing Excel file
'
3 - Creates two associations
'
4 - Applies a new configuration to the document
'
5 - Creates a rule
'
6 - Creates a check
' Running the macro
' -----------------' Run this macro on the KwrMacro0.CATPart document which is delivered
' as an Infrastructure sample.
' This macro uses as an input data the KwrMacTable.xls file which is delivered
' as an Infrastructure sample
' You must replace the value of the pathNameDT variable with the path
' of the file where this Excel table has been unloaded.
' To run this macro under UNIX, replace the Excel file with a .txt file.
Sub CATMain()
' Retrieve your active document - CATIA is your application
' You get the active document by using the ActiveDocument property
' on your application object
Dim myDoc As Document
Set myDoc = CATIA.ActiveDocument
' Check whether the document is a CATPart
' Analyse the pathname of the document
' If the extension .CATPart is not found, a message is displayed
' but you exit the procedure
' InStrRev is a standard VB function
Dim strPartName, strCATPart
strPartName = myDoc.Name
if (InStrRev(strPartName,".CATPart",-1) = 0)_
then MsgBox("Your document should be a .CATPart") : Exit Sub
' Retrieve the collection object which contains
' all the document relations
' myDoc.Part is satisfied as the document extension
' has been checked
Dim myRelCol As Relations
Set myRelCol = myDoc.Part.Relations
' Just to be clean before creating relationms
' Scan the collection of relations and remove the relations
' with name
"DesignTable.1"
'
or "Rule.1"
'
or "Check.1"
For Each myRel in myRelCol
if myRel.Name = "DesignTable.1"_
or myRel.Name = "Rule.1"_
or myRel.Name = "Check.1"_
then myRelCol.Remove myRel.Name
Next
'
PartBody\Pad.1\FirstLimit\Length > 100mm
Dim Check1 As Check
Set Check1 = myRelCol.CreateCheck ( "Check.1", "Increase the pad thickness",_
"/* Check created by CRE 03/16/00 */" + chr(10) + "PartBody\Pad.1\FirstLimit\Length
> 100mm" )
' Update the document
CATIA.ActiveDocument.Part.Update
End Sub
Glossary
A
automation
A technology that applications use to expose their objects to development tools, macro
languages, and other applications that support Automation.
C
collection
An object that contains zero or more objects of the same type. In CATIA knowledgeware
applications, the Parameters and Relations collections provide you with all the required
creation methods.
I
in-process macro
A macro which is interpreted in the same process as CATIA using the scripting engine(s)
hosted by CATIA. An in-process macro is run from the Tools->Macros command and
processed by CATIA just like any other command.
M
macro
A set of instructions recorded and saved under a .CATScript file. When the macro is run,
CATIA carries out the instructions of the macro.
method
An operation that acts on an object.
O
out-process macro
A macro which is run from another application running in another process. In this case,
the macro should first connect to CATIA to then access its data. This connection starts
CATIA if no CATIA process is being running. The script is interpreted by the scripting
engine hosted by the application from which you start the macro.
P
property
A named attribute of an object. Properties define object characteristics such as size,
color, and screen location, or the state of an object, such as enabled or disabled.