iLogic to Update Custom Mass Property with Model States

iLogic to Update Custom Mass Property with Model States

arkelec
Collaborator Collaborator
652 Views
2 Replies
Message 1 of 3

iLogic to Update Custom Mass Property with Model States

arkelec
Collaborator
Collaborator

I have some code to iterate through the members of an iPart, to push the 'MassProperties.Mass' value to a custom parameter & populate a column in the iPart Excel table.

 

I have some code to do the same for the selected instance of an equivalent Part with Model States, but only for that selected Model State instance.  

 

Is there a way of using iLogic to iterate though each Model Stare in the Part, to push the mass value to the custom iProperty?

 

Also, is the same possible for Assembly files?

0 Likes
Accepted solutions (2)
653 Views
2 Replies
Replies (2)
Message 2 of 3

bradeneuropeArthur
Mentor
Mentor
Accepted solution
Then you need for each modelstate a Iproperty like:
Modelstate 1 = wms1
Modelstate 2 = wms2
Etc
Is that ok?

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

Message 3 of 3

arkelec
Collaborator
Collaborator
Accepted solution

Thanks @bradeneuropeArthur.

 

A bit of further scouting found this thread: https://forums.autodesk.com/t5/inventor-programming-ilogic/calculate-mass-of-parts-in-an-assembly/td... 

 

I used some of the code that @WCrihfield posted, so thanks for that.

 

In the event that my code helps anyone else, here it is:

 

Sub Main()
'-------------------------------------------------------------------------
' Notes:
'
'-------------------------------------------------------------------------
' Vars:
'-------------------------------------------------------------------------
'
Dim oMass As Double
Dim strMass As String
' set number of decimal places:
Dim strDP As String = InputBox("Enter Number for DP " & strDP & "", "Set Decimal Places")
'
Dim intDP As Integer = (Convert.toInt32(strDP))
'
'-------------------------------------------------------------------------
' Get the active document:
'-------------------------------------------------------------------------
' 
Dim oDocType = ThisDoc.Document.DocumentType
'
'=========================================================================
' Code for PART:
'=========================================================================
'
If oDocType = DocumentTypeEnum.kPartDocumentObject Then
	Dim oPartDoc As PartDocument = ThisDoc.Document
	Dim oPartDef As PartComponentDefinition = oPartDoc.ComponentDefinition
'
Dim oMSs As ModelStates = oPartDoc.ComponentDefinition.ModelStates
	If oMSs.MemberEditScope <> MemberEditScopeEnum.kEditActiveMember Then
		oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
	End If
'
'-------------------------------------------------------------------------
' Iterate through Model States:
'-------------------------------------------------------------------------
'
Dim oOrigMS As ModelState = oMSs.ActiveModelState
'
	For Each oMS As ModelState In oMSs
		oMS.Activate
		Dim oMSDoc As PartDocument = oMS.FactoryDocument
		'oMSDoc.Rebuild2(True) 'helps ensure all updated, but may not be necessary (may take extra time)
		oMass = oPartDoc.ComponentDefinition.MassProperties.Mass
		oMass = oPartDoc.UnitsOfMeasure.ConvertUnits(oMass, oPartDoc.UnitsOfMeasure.MassUnits, Inventor.UnitsTypeEnum.kKilogramMassUnits)
		oMass = Round(oMass, intDP)
		'
		Dim c_Mass As String = fnPropGetVal("c_Mass", "")
		iProperties.Value("Custom", "c_Mass") = oMass & " kg"
	Next 'oMS
	oOrigMS.Activate
	If oPartDoc.RequiresUpdate Then oPartDoc.Update2(True)
'
'=========================================================================	
' Code for ASSEMBLY:
'=========================================================================
'
ElseIf oDocType = DocumentTypeEnum.kAssemblyDocumentObject Then
	Dim oAssemblyDoc As AssemblyDocument = ThisDoc.Document
	Dim oAssemblyDef As AssemblyComponentDefinition = oAssemblyDoc.ComponentDefinition
'
Dim oMSs As ModelStates = oAssemblyDoc.ComponentDefinition.ModelStates
	If oMSs.MemberEditScope <> MemberEditScopeEnum.kEditActiveMember Then
		oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
	End If
'
'-------------------------------------------------------------------------
' Iterate through Model States:
'-------------------------------------------------------------------------
'
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)
		oMass = oAssemblyDoc.ComponentDefinition.MassProperties.Mass
		oMass = oAssemblyDoc.UnitsOfMeasure.ConvertUnits(oMass, oAssemblyDoc.UnitsOfMeasure.MassUnits, Inventor.UnitsTypeEnum.kKilogramMassUnits)
		oMass = Round(oMass, intDP)
		'
		Dim c_Mass As String = fnPropGetVal("c_Mass", "")
		iProperties.Value("Custom", "c_Mass") = oMass & " kg"
	Next 'oMS
	oOrigMS.Activate
	If oAssemblyDoc.RequiresUpdate Then oAssemblyDoc.Update2(True)
'
'=========================================================================	
'
	End If
'
End Sub
'
'======================================================================
'
'Use:		Check for missing user defined property types
'Params:	PropName - passed in name to check
'			MissingSetVal - optional value to pass to the selected property set by default to empty string
'			Doc - Name of the document to search for the attached property type defined as PropName
'Returns:  	A value held by the selected PropName object as an object - this could be text, bool or numeric.
'On Error:  Returns -1. this can be tested and signalled to the user for further investigation.
'----------------------------------------------------------------------
Function fnPropGetVal(PropName As String, 
	Optional oMissingSetVal As Object = "", 
	Optional Doc As Inventor.Document = Nothing) As Object
	'
	Dim oReturnVal As Object
	'
	If Doc Is Nothing Then Doc = ThisDoc.Document 'set to current doc..
	'
	'define a property set object from the API
	Dim CustomPropSet As Inventor.PropertySet = Doc.PropertySets.Item("Inventor User Defined Properties")
	'
		'does the requested object exist?
			Try
			oReturnVal = CustomPropSet.Item(PropName).Value
			Catch
			'not found so create this object defined as PropName in the selected document and apply a value..
			Dim oTransaction As Inventor.Transaction
			Try
			'Modify the current database and add a new param to user defined properties..
			oTransaction = ThisApplication.TransactionManager.StartTransaction(Doc,"Create property """ & PropName & """")
			CustomPropSet.Add(oMissingSetVal, PropName)
			oTransaction.End
			'
			oReturnVal = oMissingSetVal
			'Successfully created the object and set its value..
			Catch
			'failed to create - may be a permissions error..
			If Not oTransaction Is Nothing Then oTransaction.Abort
			oReturnVal = -1 'Unable to create.
			End Try
			End Try
			'all done so..
			Return oReturnVal
			End Function
'
'======================================================================
0 Likes