Knowledge Advisor Journaling Guide Preface Getting Started Object Library Use Cases Glossary
© Dassault Systèmes 1994-2000. All rights reserved.
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.
Properties and Methods An object represents an element of an application, such as a worksheet, a cell, a chart, a form, or a report. In Visual Basic or VBScript code, you must identify an object before you can apply one of the object’s methods or change the value of one of its properties. A property is an attribute of an object that defines one of the object's characteristics, such as size, color, or screen location, or an aspect of its behavior, such as whether it is enabled or visible. To change the characteristics of an object, you change the values of its properties. To set the value of a property, follow the reference to an object with a period, the property name, an equal sign (=), and the new property value. For example, the following statement modifies the minimum tolerance value of a Length object by setting the MinimumTolerance property. length0.MinimumTolerance = 1 Not all properties can be set. There are some that are read-only. You can retrieve information about an object by returning the value of one of its properties. The
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.
CATIA Knowledgeware Automation Objects The CATIA knowledgeware automation objects are described in the figure below. Principles To Create: The knowledgeware features are all created from the collection which refers to their type. For example, to create a relation in a part type feature, you must first retrieve the collection object containing the part relations by using the Relations method on the Part object. To create a parameter in a part, you must retrieve the collection object containing the part parameters by using the Parameters method on the Part object. To Modify: To manipulate a knowledgeware object, you just have to use the methods and properties of the relevant object. Click any of the links to display the related object description
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
To create a Design Table Relations collection To manipulate a Design Table DesignTable 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.
Recording and Replaying a Macro Explains how to record and replay a macro which: creates a new parameter, renames and valuates this parameter and creates a formula constraining one of the document geometric parameter. 1. Create a hollow cylinder similar to the one below and save this initial document under the KwrMacro0.CATPart name.
The part parameters should be specified as follows: Pad First Limit: 100mm Pad Second Limit: 0mm Hole Type: 'Simple' Hole Extension: 'Up To Last' Hole Diameter: 50mm 2. Select the Tools->Macro->Start Recording... command from the standard tool bar. The Record Macro dialog box is displayed. 3. Select External File in the 'Macro in' list, then specify a path in the 'Macro Name' field (for example: e:\users \...\KwrMacro.CATScript).
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:
f. Select the Tools->Macro->Stop Recording command from the standard tool bar. This closes the KwrMacro.CATScript file which records all the interactions described above. End of interaction recording 5. Close your document but don't save the modifications under the initial document name. 6. Re-open your initial document (KwrMacro0.CATPart) 7. Select the Tools->Macro->Macros... command from the standard tool bar. 8. In the Macro dialog box, select the macro you have just recorded (KwrMacro.CATScript) and click Run to replay all the interactions recorded in the macro.
Editing and Modifying a Macro Gives you some basics about how to edit and modify a macro. 1. Re-open the initial KwrMacro0.CATPart document. 2. Edit the KwrMacro.CATScript file either by selecting the Tools->Macro->Edit... command from the standard tool bar or by using any available editor. A VBScript procedure similar to the one below is displayed (comments have been added and some variables created): Language="VBSCRIPT" Sub CATMain() ' Declare the Length0 variable as a Dimension type Dim Length0 As Dimension ' Retrieve the root of your active document ' and access the 'Parameters' feature Set Doc1 = CATIA.ActiveDocument.Part Set Par1 = Doc1.Parameters ' Create a Dimension type parameter, ' rename it ' and modify its value Set Length0 = Par1.CreateDimension("Length.1", "LENGTH", 0.000000) Length0.Rename "RefLength" Length0.Value = 70.000000 ' Retrieve the PartBody feature Dim Body1 As AnyObject Set Body1 = Doc1.Bodies.Item("PartBody") ' Retrieve the Pad.1 feature Dim Pad2 As AnyObject Set Pad2 = Body1.Shapes.Item("Pad.1") ' Constrain the Pad.1 First Limit with the Formula.1 relation ' 1 - Declare the Formula3 variable as a Formula type ' 2 - Get the Relations feature of the active document ' 3 - Get the Pad.1 First Limit ' 4 - Create the formula below: ' PartBody\Pad.1|FirstLimit\Length=RefLength/10 formula Dim Formula3 As Formula Set Rel1 = Doc1.Relations Set Di1 = Pad2.FirstLimit.Dimension Set Formula3 = Rel1.CreateFormula("Formula.1","", Di1, "RefLength") Formula3.Modify "RefLength / 10" ' Update the document CATIA.ActiveDocument.Part.Update End Sub 3. In the VBScript source above, modify the Formula.1 name and add a new formula defining the Pad
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.
2. Run the macro below on this document Language="VBSCRIPT" Dim Set Dim Set Set
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")
' Display the Formula.1 expression MsgBox(For0.Value) ' Deactivate Formula.2 and activate Formula.1 For1.Deactivate For0.Activate ' Modify Formula.1 For0.Modify("R1*10m*0.5m") ' Rename the formulas For0.Rename("F1") For1.Rename("F2") CATIA.ActiveDocument.Part.Update This is what you can see onscreen.
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.
Design Table Object Definition The Design object provides you with properties and methods to access design table data. This object does not allow you to create a design table.
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")_
then strRel1 = strRel1 & vbCrLf & myPar.Name & " " & myPar.Value &_ " " & "'" & myPar.Comment & "'" : HiddenNumber = HiddenNumber + 1 : myPar.Value = "true" Next ' Display the list of hidden parameters. ' The parameter names, values and comments ' are displayed in a message box. ' Ask you to modify interactively the status of the ' parameters and to re-run the macro. strRel2 = "NOW, SHOW THE HIDDEN PARAMETERS" &_ vbCrLf & vbCrLf & "IN THE f(x) DIALOG BOX," &_ vbCrLf & " 1 ) RIGHT-CLICK THE PARAMETER VALUE FIELD " &_ vbCrLf & " 2 ) SELECT THE SHOW COMMAND FROM THE CONTEXTUAL MENU" &_ vbCrLf & vbCrLf & "THEN RE-RUN THE MACRO " If (HiddenNumber > 0) Then MsgBox(strRel1) : MsgBox(strRel2) : Exit Sub MsgBox "There are no hidden parameters in this document" End Sub
' ************************************************* ' 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"
For Each myPar in myParamCol if myPar.Name = "ProjectId"_ or myPar.Name = "ProjectKey"_ or myPar.Name = "StringLength"_ or myPar.Name = "SphereRadius"_ or myPar.Name = "SphereVolume"_ then myParamCol.Remove myPar.Name Next ' Create the ProjectId parameter ' This parameter is a string type parameter ' Its initial value is CATLifKnowledgeAdvisor Dim myString1 As Parameter Set myString1 = myParamCol.CreateString("ProjectId","CATLifKnowledgeAdvisor") ' Create the ProjectKey parameter ' This parameter is a string type parameter ' Its initial value is a blank string Dim myString2 As Parameter Set myString2 = myParamCol.CreateString("ProjectKey","") ' Create an integer type parameter ' Its name is Stringlength - its initial value is 0 Dim myStringLength As Parameter Set myStringLength = myParamCol.CreateInteger("StringLength",0) ' Create a Length type parameter ' Its initial name is blank ' Its initial value is 0 ' It is renamed and assigned a value after creation ' Note that units are specified in internal units ' i.e the units used by the geometric modeler (mm for Length) Dim mySphereRadius As Parameter Set mySphereRadius = myParamCol.CreateDimension("","LENGTH", 0) mySphereRadius.Rename "SphereRadius" mySphereRadius.Value = 50 ' Display the minum tolerance for "SphereRadius" msgbox("Minimum Tolerance for the LENGTH parameter " & mySphereRadius.MinimumTolerance) ' Create a Volume type parameter ' Its name is SphereVolume ' Its initial value is 0 m3 Dim mySphereVol As Parameter Set mySphereVol = myParamCol.CreateDimension("SphereVolume","VOLUME",0) ' Create a formula which specifies that the ProjectKey parameter value
' ' ' ' ' '
equals the three first characters of ProjectId To do so we use the CreateFormula method of the "Relations" collection object - the constrained parameter is passed as the third argument of the method - The formula expression is passed as the fourth argument. Note that the Extract function is provided by the Knowledge Advisor string function dictionary Dim Formula1 As Formula Set Formula1 = myRelCol.CreateFormula("Formula1", "", myString2,_ "ProjectId.Extract(0,3)" ) ' Create a formula which also applies to the ProjectKey parameter ' This formula will be created as inactive Dim Formula2 As Formula Set Formula2 = myRelCol.CreateFormula("Formula2", "", myString2,_ "ProjectId.Extract(4,3)" ) ' Create a formula which applies to the StringLength parameter ' The formula expression passed as the fourth argument ' uses the Length function which is provided with the string functions of the ' Knowledge Advisor dictionary Dim Formula3 As Formula Set Formula3 = myRelCol.CreateFormula("Formula3", "", myStringLength,_ "ProjectId.Length()" ) ' Create a formula which applies to the SphereVolume parameter ' It just calculates the volume from the radius Dim Formula4 As Formula Set Formula4 = myRelCol.CreateFormula("Formula4", "", mySphereVol,_ "(4/3)*PI*SphereRadius**3") ' Display the list of relations ' The formula names and expressions are displayed in a message box Dim strRel As String strRel = "Here is the list of relations" & vbCrLf For i = 1 to myRelCol.Count strRel = strRel & vbCrLf & myRelCol.Item(i).Name & " Next MsgBox(strRel) End Sub
" & myRelCol.Item(i).Value
' ************************************************************************ ' 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
' Check whether the design table file exists ' otherwise exit the procedure Dim fso Set fso = CreateObject("Scripting.FileSystemObject") pathNameDT = "e:/users/cre/adele/KwrMacTable.xls" if (fso.FileExists(pathNameDT) = False)_ then MsgBox (pathNameDT & " doesn't exist") : Exit Sub ' Create the "DesignTable.1" design table Dim Des0 As DesignTable Set Des0 = myRelCol.CreateDesignTable("DesignTable.1","",_ False,_ pathnameDT) ' Retrieve the collection object which contains ' all the part bodies Dim myBodCol As Bodies Set myBodCol = myDoc.Part.Bodies ' Scan the Bodies collection to determine ' whether a PartBody feature exists otherwise ' exit the procedure For Each myBod in myBodCol if myBod.Name = "PartBody" then PartBodyExists = "True" Next if (PartBodyExists "True") then MsgBox("No PartBody") : Exit Sub ' Retrieve the "PartBody" iten from the Bodies collection Dim Body1 As AnyObject Set Body1 = CATIA.ActiveDocument.Part.Bodies.Item("PartBody") ' Check there is at least one shape in the Part Dim myShaCol As Shapes Set myShaCol = Body1.Shapes if (myShaCol.Count = 0) then msgbox("No shapes in your part") : Exit Sub ' Retrieve the Pad.1 object Dim Pad2 As AnyObject Set Pad2 = Body1.Shapes.Item("Pad.1") ' Creates two associations Des0.AddAssociation Pad2.FirstLimit.Dimension, "A" Des0.AddAssociation Pad2.SecondLimit.Dimension, "B" ' Apply the third configuration of the design table ' to your document Des0.Configuration = 3 ' Add a new row ' A new row is added to the design table each time the ' macro is replayed on the document ' A limit of 10 rows is specified If (Des0.ConfigurationsNb < 11) Then Des0.AddNewRow() ' Create the rule below: ' if PartBody\Hole.1\Diameter > 10mm ' Message (""The hole diameter is # "", PartBody\Hole.1\Diameter)" Dim Rule0 As Rule Set Rule0 = myRelCol.CreateProgram("Rule.1",_ "", "" +_ chr(10) + "if PartBody\Hole.1\Diameter > 10mm" + chr(10) +_ "Message (""The hole diameter is # "", PartBody\Hole.1\Diameter)" ) ' Create the check below:
' 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.