Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Creating a formula for Mass = Volume x Density using Parameters

Status
Not open for further replies.

Ian Cuz

Aerospace
Dec 6, 2017
18
0
0
GB
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
Mass_Formula_Starter_Model_zufme6.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
 
Replies continue below

Recommended for you

Property Volume( ) As double (Read Only)

Returns the volume.
Example:
This example retrieves the volume of NewMeasurable measure.
The volume unit given by oVolume is m^3
Dim AVolume As double
AVolume = NewMeasurable.Volume

 
Hi LucasC
Thank you for your response.
I was hoping for advise related to how to get the Reference Object value ofthe MainBody (regardless of its actual name) to use in the SmartVolume expression.

But I'll certainly have a play around your suggestion.
Cheers
Ian
 
Give this a try instead, it's the measurement method I got to work. Don't judge me if I mesesed up the unit conversions, it's late! [sleeping2]

Question, have you considered just applying a catalog material and using inertia measure instead of doing this manual conversion?

Code:
Dim PartDoc1 As Part
Set PartDoc1 = CATIA.ActiveDocument.Part

Dim Mass1
    Set Mass1 = PartDoc1.Parameters.Item("Mass")
    
Dim Density1
    Set Density1 = PartDoc1.Parameters.Item("Density")

Dim VolRef As Reference
    Set VolRef = PartDoc1.CreateReferenceFromObject(PartDoc1.MainBody)
    
Dim SPAWB As Workbench
    Set SPAWB = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
    
Dim VolMeas As Measurable
    Set VolMeas = SPAWB.GetMeasurable(VolRef)
        
'MSGBox Output
    
MsgBox VolMeas.Volume * 10 ^ 6 & " cm^3" 'change this based on your desired unit of measurement

MsgBox Density1.Value & " kg/m^3"

MsgBox ("Mass = " & ((Density1.Value) * (VolMeas.Volume)) & "kg")

'Set Parameter Value

Mass1.Value = (Density1.Value) * (VolMeas.Volume)

End Sub
 
Hi LucasC (all)
The code above does give me the Mass value.
But I am coding a Starter Model and require Dynamic Update of the Mass as the Design matures
Which is why I was trying to used smartVolume.
I may have to write a separate macro using your code to populate the Mass at the end of the design or via Manual Update at specific stages
Thanks again for the help
 
Hi All,
Having taken your advice and lots of other guidance from other forums I managed to achieve what I wanted
As it took me quite a while I thought I'd share my final code to assist other with the same requirements
So what did I do ?
1) I created a ParameterSet within the RootParameterSet to group my two new Parameters
2) I created two Parameters directly within this New ParameterSet (no need for Copy /Paste)
3) Created a Formula linking a User entered Density directly to the MainBody - which updates dynamically as the Solid evolves
The resulting Spec Tree
Capture_amo20z.jpg


The full code =

'* 1) Get Active Document
'* 2) Declare Document related Object Items
'* 3) Declare Parameter related Objects

Dim objPartDocument As Document
Set objPartDocument = CATIA.ActiveDocument

Dim objPart As Part
Set objPart = objPartDocument.Part

Dim objParameters As Parameters
' Set objParameters = objPart.Parameters

Dim objParameterSet As ParameterSet
' Set objParameterSet = objParameters.RootParameterSet

Dim objParameterSets As ParameterSets
' Set objParameterSets = objParameterSet.ParameterSets

Dim objNewParamSet As ParameterSet

Dim objParameterSet2 As CATBaseDispatch
Dim objParameterSet3 As CATBaseDispatch
Dim objParameterSet4 As CATBaseDispatch

Dim objRelations As Relations
Dim formula1 As Formula

Dim strDensity As String
Dim strTargetParam As String
Dim strDensityFull As String
Dim strDensityTrimmed As String
Dim strTrimmedFormulaBody As String
Dim strPNOriginal As String
Dim strPNTemp As String

'*=============================================================================
'* START MAIN PROGAM CODE
'*=============================================================================

Sub CATMain()

'* Set Object Declarations

Set objParameters = objPart.Parameters
Set objRelations = objPart.Relations
Set objParameterSet = objParameters.RootParameterSet
Set objParameterSets = objParameterSet.ParameterSets

strPNOriginal = objPart.MainBody.Name
strPNTemp = "PARTBODY"

'* Temporarily suspend On Screen Update during the Creation of the Parameters and ParameterSets
CATIA.RefreshDisplay = False

'*=============================================================================
'* START PARAMETER AND PARAMETERSET CREATION
'*=============================================================================

'* Create Another New ParameterSet Object within the RootParameterSet Object
Set objNewParamSet = objParameterSets.CreateSet("COMPONENT_SPECIFIC_PARAMETERS")

'* Get Object related to the Newly Cerated ParameterSet
Set objNewParamSet = objParameters.RootParameterSet.ParameterSets.Item("COMPONENT_SPECIFIC_PARAMETERS")

Set dblLength = objNewParamSet.DirectParameters.CreateDimension("MatlDensity", "DENSITY", 1000.000000)

Set dblLength = objNewParamSet.DirectParameters.CreateDimension("MassAsModelled" , "MASS", 0.000000)

'* Temporarily modify the Name of the MainBody object to allow the Formula Method Expression to work as required
objPart.MainBody.Name = strPNTemp

'* Code / Sub required to link Mass = Volume x Density
'* The next section of code Builds Text Strings and Declares Objects to enable the Creation of a Formula (Relation)
'* that links the Density Value to the Volume of the PartBody Solid Object
'* It has taken quite a significant amount of research and trial and error to develop the command line of code
'* to get this requirement to function correctly

' Set the Declarations for additional objects
Set objParameterSet2 = objParameterSets.GetItem("COMPONENT_SPECIFIC_PARAMETERS")
Set objParameterSet3 = objParameterSet2.DirectParameters.Item("MatlDensity")
Set objParameterSet4 = objParameterSet2.DirectParameters.Item("MassAsModelled")

'* The next sub section manipulates th Density Text String to be able to construct the FormulaBody expression

'* Get the Full System Object Name String
strDensity = objParameterSet3.Name
strTargetParam = objParameterSet4.name

'* The first part of the Systems Name is related to the Active Document "Part" part name
'* This text needs to be removed to enable the Formula Method to function corerctly
'* This stripping of the Part Name text is achieved by the use of the VB Split function
'* and using the Second and Third (array 1 & 2) text sections

strDensityFull = Split(strDensity, "\")
strDensityTrimmed = strDensityFull(1) & "\" & strDensityFull(2)

'* Develop a String for the text to be used within the FormulaBody of the Relation
strTrimmedFormulaBody = strDensityTrimmed & " * smartVolume(PARTBODY)"

'* Use the Direct Object (for the Mass Parameter) and the modified String to build the CreateFormula method arguments
Set formula1 = objRelations.CreateFormula("Formula.1", "", objParameterSet4, strTrimmedFormulaBody )

formula1.Rename "CALC_FOR_AS_MODELLED_MASS"

'* RESET the Name of the MainBody object back to its Original Name
objPart.MainBody.Name = strPNOriginal

'* CLEAR any Active Object Selection
CATIA.ActiveDocument.Selection.Clear

'* Turn On the On Screen Update Setting
CATIA.RefreshDisplay = True

'* Force Update to the CATPart Document
objPart.Update

End Sub

I hope this is helpful to other forum members :)
 
Status
Not open for further replies.
Back
Top