Search code examples
vbacatia

Is there a way to create formula in Catia via Macro to link parameters?


enter image description here

The main goal is to link between two parameters via formula in Catia using macro. I've tried to record the process manually but the code didn't give the complete code:

Sub CATMain()
Dim partDocument1 As partDocument
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As part
Set part1 = partDocument1.part
Dim parameters1 As Parameters
Set parameters1 = part1.Parameters
Dim parameterSet1 As ParameterSet
Set parameterSet1 = parameters1.RootParameterSet
Dim parameterSets1 As ParameterSets
Set parameterSets1 = parameterSet1.ParameterSets
Dim parameterSet2 As ParameterSet
Set parameterSet2 = parameterSets1.GetItem("Construction_Position")
Dim parameterSets2 As ParameterSets
Set parameterSets2 = parameterSet2.ParameterSets
Dim parameterSet3 As ParameterSet
Set parameterSet3 = parameterSets2.GetItem("R_Point") ' Cannot print the method call put_Value for the object RealParam
Dim relations1 As Relations
Set relations1 = part1.Relations
Set parameterSet3 = parameterSets2.GetItem("R_Point")
Dim formula1 As Formula
Set formula1 = relations1.CreateFormula("Formula.2", "", ' Cannot print the method call CreateFormula for the object Relations
part1.Update
End Sub

The final formula that I get from this process 'manually' is

formula.2: Construction_Position\R_Point\R_Point_X=Construction_Position\H_Point\H_Point_X

What can I try next I tried with ChatGTP, but it's not working at all.


Solution

  • CreateFormula needs as third input the parameter which is managed by the formula, and as 4th parameter the formula as a string. the best way to get the "name" of a parameter to use in a formula is to use the GetNameToUseInRelation function. Also your code missing accessing one parameter-set level. Here an example how such a code could look like

    Sub CATMain()
    
    Dim oPart As Part
    Dim oRelations As Relations
    Dim oParameters As Parameters
    Dim parameterSet_H As ParameterSet
    Dim parameterSet_R As ParameterSet
    Dim parameterSet_Construction As ParameterSet
    Dim oParameter_H as Parameter
    Dim oParameter_R as Parameter
    Dim oFormula As Formula
    
    Set oPart = CATIA.ActiveDocument.Part
    Set oRelations = oPart.Relations
    Set oParameters = oPart.Parameters
    
    'get Parametersets
    Set parameterSet_Construction = oParameters.RootParameterSet.ParameterSets.Item("Construction_Position")
    Set parameterSet_H = parameterSet_Construction.ParameterSets.Item("H_Point")
    Set parameterSet_R = parameterSet_Construction.ParameterSets.Item("R_Point")
    
    'get Parameters
    Set oParameter_H = parameterSet_H.DirectParameters.Item("H_Point_X")
    Set oParameter_R = parameterSet_R.DirectParameters.Item("R_Point_X")
    
    'create formula
    Set oFormula = oRelations.CreateFormula("","", oParameter_R, oParameters.GetNameToUseInRelation(oParameter_H))
    
    oPart.Update
    
    End Sub