最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vba - Is there a way to create formula in Catia via Macro to link parameters? - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Dec 3, 2024 at 23:36 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Nov 19, 2024 at 10:31 issamoissamo 157 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

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

I'm not an expert in Catia, but I skimmed read http://catiadoc.free.fr/online/interfaces/interface_Relations.htm#CreateFormula try the following:

Sub CATMain()
    Dim partDocument1 As PartDocument
    Dim part1 As Part
    Dim relations1 As Relations
    Dim formula1 As Formula
    Dim parameters1 As Parameters
    Dim paramHPointX As Parameter
    Dim paramRPointX As Parameter

    Set partDocument1 = CATIA.ActiveDocument
    Set part1 = partDocument1.Part
    Set parameters1 = part1.Parameters
    Set relations1 = part1.Relations
    Set paramHPointX = parameters1.Item("Construction_Position\H_Point\H_Point_X")
    Set paramRPointX = parameters1.Item("Construction_Position\R_Point\R_Point_X")

    'formula linkin
    ' Formula syntax: "iName", "iComment", "Formula"
    Set formula1 = relations1.CreateFormula("Formula.4", "", paramRPointX.Name & " = " & paramHPointX.Name)
    part1.Update
End Sub
发布评论

评论列表(0)

  1. 暂无评论