Get mass properties from every model state

Get mass properties from every model state

rafael.barata
Enthusiast Enthusiast
1,166 Views
5 Replies
Message 1 of 6

Get mass properties from every model state

rafael.barata
Enthusiast
Enthusiast

Hi guys,

 

Can you help me creating a rule to get physical properties from each model state in an assembly and place it in custom Iprops as:

Name -> "Modelstatename_Prop"

Value -> Prop value

Type -> Text or number

 

The objective is to get those props in an .idw note to specify a multi tool robot gripper 🙂

 

Properties are: 

- Mass

- COG X coordinate

- COG Y coordinate

- COG Z coordinate

- COG Ixx

- COG Iyy

- COG Izz

 

0 Likes
Accepted solutions (1)
1,167 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @rafael.barata.  Here is something you can try out.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.FactoryDocument
	Dim oMSs As ModelStates = oADoc.ComponentDefinition.ModelStates
	If oMSs.MemberEditScope <> MemberEditScopeEnum.kEditActiveMember Then
		oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
	End If
	Dim oOrigMS As ModelState = oMSs.ActiveModelState
	For Each oMS As ModelState In oMSs
		oMS.Activate
		Dim oMSDoc As AssemblyDocument = oMS.FactoryDocument
		'oMSDoc.Rebuild2(True) 'helps ensure all updated, but may not be necessary (may take extra time)
		Dim oMP As MassProperties = oMSDoc.ComponentDefinition.MassProperties
		Dim oCOG As Inventor.Point = oMP.CenterOfMass
		Dim Ixx, Iyy, Izz, Ixy, Iyz, Ixz As Double
		oMP.XYZMomentsOfInertia(Ixx, Iyy, Izz, Ixy, Iyz, Ixz)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_Mass", oMP.Mass)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG X coordinate", oCOG.X)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Y coordinate", oCOG.Y)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Z coordinate", oCOG.Z)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Ixx", Ixx)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Iyy", Iyy)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Izz", Izz)
	Next 'oMS
	oOrigMS.Activate
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
End Sub

Sub TryCreateUpdateCustomProperty(oDoc As Inventor.Document, sPropName As String, _
oValue As Object, Optional sExpression As String = vbNullString)
	If IsNothing(oDoc) Or sPropName = "" Or oValue Is Nothing Then Exit Sub
	If oDoc.IsModifiable = False Then Exit Sub
	Dim oCProps As Inventor.PropertySet = oDoc.PropertySets.Item(4)
	Dim oCProp As Inventor.Property = Nothing
	Try
		oCProp = oCProps.Item(sPropName)
	Catch
		oCProp = oCProps.Add(oValue, sPropName)
	End Try
	If String.IsNullOrEmpty(sExpression) Then
		If oCProp.Value <> oValue Then oCProp.Value = oValue
	Else
		If oCProp.Expression <> sExpression Then oCProp.Expression = sExpression
	End If
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

rafael.barata
Enthusiast
Enthusiast

Excellent!!! There's just one little thing. It's converting the units of COGs coordinates and mass properties to cm when they're defined to mm as default. 

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @rafael.barata.  I forgot about units conversions.  Everything in Inventor having to do with measurements and units of measure, uses what is called 'database units' which is one specific set of all metric units for each measurement type (length, mass, temperature, time), and that is centimeters for length.  So most measurements you get from Inventor by code are in those database units by default, instead of in document units.  This situation has been a major pain for many years, but it seems to be the only way Autodesk can write the programming for all that numerical stuff and keep it straight.  Below is an altered version of the code that uses a built-in units conversion tool.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.FactoryDocument
	Dim oMSs As ModelStates = oADoc.ComponentDefinition.ModelStates
	If oMSs.MemberEditScope <> MemberEditScopeEnum.kEditActiveMember Then
		oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
	End If
	Dim oOrigMS As ModelState = oMSs.ActiveModelState
	Dim UOM As UnitsOfMeasure = oADoc.UnitsOfMeasure
	Dim oDocLengthUnits As UnitsTypeEnum = UOM.LengthUnits
	Dim oDBLengthUnits As UnitsTypeEnum = UnitsTypeEnum.kDatabaseLengthUnits
	For Each oMS As ModelState In oMSs
		oMS.Activate
		Dim oMSDoc As AssemblyDocument = oMS.FactoryDocument
		'oMSDoc.Rebuild2(True) 'helps ensure all updated, but may not be necessary (may take extra time)
		Dim oMP As MassProperties = oMSDoc.ComponentDefinition.MassProperties
		Dim oCOG As Inventor.Point = oMP.CenterOfMass
		Dim oCOC_X As Double = UOM.ConvertUnits(oCOG.X, oDBLengthUnits, oDocLengthUnits)
		Dim oCOC_Y As Double = UOM.ConvertUnits(oCOG.Y, oDBLengthUnits, oDocLengthUnits)
		Dim oCOC_Z As Double = UOM.ConvertUnits(oCOG.Z, oDBLengthUnits, oDocLengthUnits)
		Dim Ixx, Iyy, Izz, Ixy, Iyz, Ixz As Double
		oMP.XYZMomentsOfInertia(Ixx, Iyy, Izz, Ixy, Iyz, Ixz)
		Ixx = UOM.ConvertUnits(Ixx, oDBLengthUnits, oDocLengthUnits)
		Iyy = UOM.ConvertUnits(Iyy, oDBLengthUnits, oDocLengthUnits)
		Izz = UOM.ConvertUnits(Izz, oDBLengthUnits, oDocLengthUnits)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_Mass", oMP.Mass)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG X coordinate", oCOC_X)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Y coordinate", oCOC_Y)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Z coordinate", oCOC_Z)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Ixx", Ixx)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Iyy", Iyy)
		TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_COG Izz", Izz)
	Next 'oMS
	oOrigMS.Activate
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
End Sub

Sub TryCreateUpdateCustomProperty(oDoc As Inventor.Document, sPropName As String, _
oValue As Object, Optional sExpression As String = vbNullString)
	If IsNothing(oDoc) Or sPropName = "" Or oValue Is Nothing Then Exit Sub
	If oDoc.IsModifiable = False Then Exit Sub
	Dim oCProps As Inventor.PropertySet = oDoc.PropertySets.Item(4)
	Dim oCProp As Inventor.Property = Nothing
	Try
		oCProp = oCProps.Item(sPropName)
	Catch
		oCProp = oCProps.Add(oValue, sPropName)
	End Try
	If String.IsNullOrEmpty(sExpression) Then
		If oCProp.Value <> oValue Then oCProp.Value = oValue
	Else
		If oCProp.Expression <> sExpression Then oCProp.Expression = sExpression
	End If
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 6

rafael.barata
Enthusiast
Enthusiast

This saved and will save me a lot of work. Many thanks.

0 Likes
Message 6 of 6

m_h_hashempour1999
Observer
Observer

Hi

can you help me creating rule to get mass properties of all model1 ((raw mattrial)) of each part in an assembly and have Bom colum for them