Ian Cuz
Aerospace
- Dec 6, 2017
- 18
Hi All,
I am very inexperienced in scripting and wish to create a formula to populate a Mass parameter using a parameter for Density and the Volume of the MainBody
I have searched the forum (and Google in general) but have not found an example exactly as I require
I have found a reference to "GetNameToUseInRelation" to get the MainBody Object - but I cannot make this work successfully
As an alternative I have had to temporarily Rename the MainBody to feed to the smartVolume() method
My CATPart structure is shown in the attached jpg
I wish to learn how to create better more efficient code
Here is my solution for your comments and advise
Sub CATMain()
'*==========================================================
'* The purpose of this script is to Create a Formula for the Mass Value based upon the
'* Mass = MainBody Volume x Density equation
'*
'* The parameters for Mass and Density already exist in the CATPart before running this script
'*==========================================================
Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As Part
Set part1 = partDocument1.Part
Dim relations1 As Relations
Set relations1 = part1.Relations
Dim parameters1 As Parameters
Set parameters1 = part1.Parameters
'* the following lines of Code enable the script to Get the Active CATPart Name
'* and populate the parameters1.Item Name argument
strTemp = CATIA.ActiveDocument.Part.Name
'msgbox strTemp ' FOR TEST PURPOSES
strTemp2 = strTemp & "\Mass" ' using the Pre Existing Mass parameter name
'msgbox strTemp2 ' FOR TEST PURPOSES
Dim dimension1 As Parameter
Set dimension1 = parameters1.Item(strTemp2)
Dim theBody As AnyObject
set theBody = CATIA.ActiveDocument.Part.mainBody
'msgbox (typename(theBody)) ' FOR TEST PURPOSES
'msgbox theBody.name ' FOR TEST PURPOSES
'*==========================================================
'* I cannot get the GetNameToUseInRelation method to function so have resorted to using
'* a Renaming Operation option to enable the "CreateFormula" method to succeed
'*==========================================================
strBodyNameOld = theBody.name ' Capture existing MainBody Name
strBodyTemp = "PARTBODY"
theBody.name = strBodyTemp ' Assign Temporary name to the MainBody to use in the Creation of the Formula
Dim formula1 As Formula
Set formula1 = relations1.CreateFormula("Formula.1", "", dimension1, "Density * smartVolume(PARTBODY) ")
formula1.Rename "Formula_For_Mass_Calc"
theBody.name = strBodyNameOld ' Rename the MainBody back to it's Original Name
part1.Update
End Sub
Many thanks
Ian
I am very inexperienced in scripting and wish to create a formula to populate a Mass parameter using a parameter for Density and the Volume of the MainBody
I have searched the forum (and Google in general) but have not found an example exactly as I require
I have found a reference to "GetNameToUseInRelation" to get the MainBody Object - but I cannot make this work successfully
As an alternative I have had to temporarily Rename the MainBody to feed to the smartVolume() method
My CATPart structure is shown in the attached jpg
I wish to learn how to create better more efficient code
Here is my solution for your comments and advise
Sub CATMain()
'*==========================================================
'* The purpose of this script is to Create a Formula for the Mass Value based upon the
'* Mass = MainBody Volume x Density equation
'*
'* The parameters for Mass and Density already exist in the CATPart before running this script
'*==========================================================
Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As Part
Set part1 = partDocument1.Part
Dim relations1 As Relations
Set relations1 = part1.Relations
Dim parameters1 As Parameters
Set parameters1 = part1.Parameters
'* the following lines of Code enable the script to Get the Active CATPart Name
'* and populate the parameters1.Item Name argument
strTemp = CATIA.ActiveDocument.Part.Name
'msgbox strTemp ' FOR TEST PURPOSES
strTemp2 = strTemp & "\Mass" ' using the Pre Existing Mass parameter name
'msgbox strTemp2 ' FOR TEST PURPOSES
Dim dimension1 As Parameter
Set dimension1 = parameters1.Item(strTemp2)
Dim theBody As AnyObject
set theBody = CATIA.ActiveDocument.Part.mainBody
'msgbox (typename(theBody)) ' FOR TEST PURPOSES
'msgbox theBody.name ' FOR TEST PURPOSES
'*==========================================================
'* I cannot get the GetNameToUseInRelation method to function so have resorted to using
'* a Renaming Operation option to enable the "CreateFormula" method to succeed
'*==========================================================
strBodyNameOld = theBody.name ' Capture existing MainBody Name
strBodyTemp = "PARTBODY"
theBody.name = strBodyTemp ' Assign Temporary name to the MainBody to use in the Creation of the Formula
Dim formula1 As Formula
Set formula1 = relations1.CreateFormula("Formula.1", "", dimension1, "Density * smartVolume(PARTBODY) ")
formula1.Rename "Formula_For_Mass_Calc"
theBody.name = strBodyNameOld ' Rename the MainBody back to it's Original Name
part1.Update
End Sub
Many thanks
Ian