Developing proper names to use in part relations
There are times when you may need to create a relation (formula, rule, law, etc.) using automation. CATIA refers to parameters, geometry and other objects inside those relations based on their name so to accomplish this you will need to understand how to develop those names properly. There are many different ways to express the name of an object inside a relation depending on where the relation is located and where the object it is referring to is located. In this article, I will cover how to develop names of objects inside a part when the relation is also created in the same part.
In a follow-up article next week, I will detail other common scenarios where the relation is created in a product. In both articles, I will also provide code samples that are easy to reuse in your own programming projects.
Introduction
Most often, the relations you will create with automation will just be simple formulas that compute some parameter value based on one or more other parameter values. However, relations can get much more complex than that and can actually refer to almost any object in CATIA, not just parameters. For example, you may want to create a formula that calculates the volume of a solid or a complex knowledgeware rule that might compute various geometrical results using geometry constructors based on some if-then conditional logic. In all of these cases, we need to understand how to properly develop the name of the objects we want to refer to in the relation.
This is probably a good time to quickly mention licenses as well. To the best of my knowledge with all configurations of CATIA you can create formulas and laws using automation without the need for any special license. However, if you want to create rules, you will need the Knowledge Advisor (KWA) license or else an error will be generated in your program.
The scenarios
The table below summarizes a couple of common scenarios you are likely to encounter when creating or modifying relations inside a part.
| Case | Where is the relation? | Object referred to by the relation | Example syntax |
| #1 | Part | Any object inside the same part | `Geometrical Set.1\Parallel.1\Offset1` |
| #2 | Part | Parameter inside another part or product | `External Parameters\SpacingValue` |
In case #1, you want to refer to some object inside the part. As mentioned earlier, that object may be a parameter, some geometry, a body and so on. In case #2, you want to refer to a parameter in another part or product. You have probably noticed that when you do this interactively, that parameter automatically gets copied with link into the part where the relation is being created. The same thing needs to happen when programming it as well, but you need to handle that – it is not automatic! But don’t worry, it’s not difficult to do.
Case #1 – Any object inside the same part
In this case, we want to create a relation inside a part that refers to an object in the same part. That object might be a parameter, a body, a specific surface or almost anything else contained in the part. This is an easy situations to handle because CATIA provides an API that gives us exactly what we are looking for every time. You simply pass the object you want to get the name of to the GetNameToUseInRelation method provided by a Parameters object and it will return the proper name to use in your relation.
As an example, let’s assume we have three parameters in the root parameters collection of the part – Area, Length and Width and we want to create a formula that will valuate the Area parameter based on the Length and Width parameter values.
Area = Length * Width
Sub Example_Case1()
'Assume the part is open in its own window
Dim objPart As Part
Dim objParams As Parameters
Dim objParamArea As Parameter
Dim objParamLength As Parameter
Dim objParamWidth As Parameter
Dim strNameLength As String
Dim strNameWidth As String
Dim strFormulaBody As String
'Get at the involved parameters
Set objPart = CATIA.ActiveDocument.Part
Set objParams = objPart.Parameters.RootParameterSet.DirectParameters
Set objParamArea = objParams.Item("Area")
Set objParamLength = objParams.Item("Length")
Set objParamWidth = objParams.Item("Width")
'Develop the proper names to use in the relation
strNameLength = objParams.GetNameToUseInRelation(objParamLength)
strNameWidth = objParams.GetNameToUseInRelation(objParamWidth)
strFormulaBody = strNameLength &"*" &strNameWidth
'Create the formula
Set objFormula = objPart.Relations.CreateFormula("","",objParamArea, strFormulaBody)
End Sub
Case #2 – Parameter inside another part or product
In this case, you want to create a relation inside a part that refers to some parameter in a different part or product. This must be done in 2 steps. First, we need to copy the parameter from the other “as result with link” so that we have a linked copy of that other parameter inside the same part where the relation will be. This will create a set of parameters called “External Parameters”. Then, we simply use the same technique shown in Case #1 to get the proper name of that parameter.
Step 1: Copy the parameter from the other part with link
If you have followed this blog for a while, you know I am a strong advocate of building a library of reuseable functions, so I have made such a function below so it can be easily used again and again in other projects.
Function CreateExternalParameterInPart(ByRef iSourceParam As Parameter, _
ByRef iTargetPart As Part) As Parameter
'Arguments:
'iSourceParam The parameter to copy
'iTargetPart The part where the param will be pasted with link
'Notes:
' This procedure will work within a single assy
' or between two separate documents that
' are open in separate windows
Dim objSel1 As Selection
Dim objSel2 As Selection
'Get the selection object associated with the
'document containing the param to copy
Set objSel1 = iSourceParam.Context.Parent.Selection
'Get the selection object associated
'with the destination part
Set objSel2 = iTargetPart.Parent.Selection
If TypeName(iSourceParam.Context) = “Part” Then
With objSel1
.Clear
.Add iSourceParam
.Copy
.Clear
End With
With objSel2
.Clear
.Add iTargetPart
.PasteSpecial “CATPrtResult”
'Return the newly copied parameter as the result
'of this function. This param will be the only thing
'selected after the paste so just return item 1
Set CreateExternalParameterInPart = .Item2(1).Value
.Clear
End With
End If
End Function
Step 2: Develop the proper parameter names and create the formula
To keep this example simple, lets assume we have 2 separate parts named PART1.CATPart and PART2.CATPart open in CATIA and they are not in a common assembly (they are open in two separate windows). PART1 has a parameter called “Height” that we need to use in PART2. In PART2, we have two other parameters named “TotalHeight” and “Thickness”. We want to create a formula in PART2 that will simply make TotalHeight = Height + Thickness.
Sub Example_Case2()
'Assume the 2 parts are open in separate windows
Dim objPart1 As Part
Dim objPart2 As Part
Dim objParams As Parameters
Dim objParamHeight As Parameter
Dim objParamHeight2 As Parameter
Dim objParamThk As Parameter
Dim objParamTotH As Parameter
Dim strNameHeight As String
Dim strNameThk As String
Dim strFormulaBody As String
'Get at the Height parameter in PART1
Set objPart1 = CATIA.Documents.Item("PART1.CATPart")
Set objParams = objPart1.Parameters.RootParameterSet.DirectParameters
Set objParamHeight = objParams.Item("Height")
'Copy the Height parameter from PART1 into PART2
Set objPart2 = CATIA.Documents.Item("PART2.CATPart")
Set objParamHeight2 = CreateExternalParameterInPart(objParamHeight, objPart2)
'Get other params in PART2 to be used in the relation
Set objParams = objPart2.Parameters.RootParameterSet.DirectParameters
Set objParamThk = objParams.Item("Thickness")
Set objParamTotH = objParams.Item("TotalHeight")
'Develop the proper names to use in the relation
strNameHeight = objParams.GetNameToUseInRelation(objParamHeight2)
strNameThk = objParams.GetNameToUseInRelation(objParamThk)
strFormulaBody = strNameHeight &"+" &strNameThk
'Create the formula
Set objFormula = objPart2.Relations.CreateFormula("","",objParamHeight2, strFormulaBody)
End Sub
Additional Tips
- You may have noticed that CATIA will surround the name of an object used in a relation with special quote characters in some situations such as when the name has spaces in it. The GetNameToUseInRelation method as shown in this article will always judge whether those quotes are needed and will automatically return the name with them if required.
- In some cases your program might need to add some lengthy relations such as a KWA rule to different models that are structured differently or have different naming. In addition, rules are typically longer and might refer to some of the same objects again and again. In these cases, I like to develop the rule body in 2 steps. First, create a generic rule body using some placeholder name for each object the rule refers to. Then, once you have developed the proper name of each item, use the Replace function to replace the placeholder names with the actual names. Just make sure to choose unique placeholder names that do not show up anywhere else in the rule or I like to surround the name in some special characters to guard against this.
Below is a short example just to show the idea. Just imagine this technique with a much longer and more complex rule.
Dim strRuleBody As String
'Develop the generic rule with placeholders for each parameter name
strRuleBody = "if ||TempBoolean|| == true" &vbcrlf
strRuleBody = strRuleBody &"{ ||TempInteger|| = 1 }" &vbcrlf
strRuleBody = strRuleBody &"else" &vbcrlf
strRuleBody = strRuleBody &"{ ||TempInteger|| = 2 }"
'Replace the Temp placeholder names with the proper names
'Assume these names were developed earlier in the program
'strBoolParamName is the name of some boolean parameter
'strIntParamName is the name of some integer parameter
strRuleBody = Replace(strRuleBody, "||TempBoolean||", strBoolParamName)
strRuleBody = Replace(strRuleBody, "||TempInteger||", strIntParamName)
Conclusion
In this article, I introduced some common scenarios for developing parameter names to use in relations and then detailed two of those scenarios where the relation is created in a part. In the next article, I will cover the other scenarios where the relation is created in a product and may refer to parameters in other products, refer to any object within some part in the assembly or even refer to publications.
Please take a moment to rate this article…Just click the stars up near the title.
Add to:
del.icio.us,
StumbleUpon,
Digg,
Google
I am looking for a solution for another case:
Case Where is the relation? Object referred to by the relation
#? Part in a product parameter inside another product
I tried the function CreateExternalParameterInPart(). But it doesn’t handle the case that TypeName(iSourceParam.Context) = “Product”. Is there a solution for this particular case?
Harry
This article only covers relations within a part. Did you check out the next article that covers product relations? If you still have a problem, post a comment on that other article with specific details about your scenario.
-Mike
Hi,
Is there a way to find the default catia parameter name ?
I am trying to get the offset value of unequal Rectangular pattern using vba but i am unsuccessful. please help.
-Ashok