Hi @WCrihfield,
Thank you for your reply, it works very well
Would it be possible to add to this rule, the update of the mass according to the model state?
I tried to use one of the codes you made in the article:
https://forums.autodesk.com/t5/inventor-programming-ilogic/get-mass-properties-from-every-model-stat...
ub Main
Dim oDoc As Document = ThisDoc.FactoryDocument
'If oDoc Is Nothing Then Return 'will return Nothing if it was a DrawingDocument
Dim oMSs As ModelStates = oDoc.ComponentDefinition.ModelStates
If oMSs.Count < 2 Then Return 'if no custom ModelStates, exit rule
Dim oOrigMS As ModelState = oMSs.ActiveModelState 'record originally active ModelState
oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
Dim oParams As Inventor.Parameters = oDoc.ComponentDefinition.Parameters
If oParams.Count = 0 Then Return 'if no parameters, exit rule
Dim oParam As Inventor.Parameter = Nothing
'record param names & Expressions of current ModelState, so we can copy the values to other ModelStates
Dim oPartNumber As String = iProperties.Value("Project", "Part Number")
Dim oVendor As String = iProperties.Value("Project", "Vendor")
Dim oMap As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
For Each oParam In oParams
oMap.Add(oParam.Name, oParam.Expression)
Next
If oMap.Count = 0 Then Return 'if nothing recorded, exit rule
'copy that recorded data to other ModelStates
'For M As Integer = 1 To 1
For Each oMS As ModelState In oMSs
oMS.Activate
iProperties.Value("Project", "Part Number") = oPartNumber
iProperties.Value("Project", "Vendor")=oVendor
Dim oMSDoc As PartDocument = oMS.FactoryDocument
oMSDoc.Rebuild2(True)
Dim oMP As MassProperties = oMS.ComponentDefinition.MassProperties
TryCreateUpdateCustomProperty(oMSDoc, oMS.Name & "_Mass", oMP.Mass)
For Each oParam In oParams
For i As Integer = 1 To oMap.Count
If oParam.Name = oMap.Name(i) Then
Try
Dim sExpression As String = oMap.Item(i).ToString
oParam.Expression = sExpression
Catch 'what to do if that fails
Logger.Error("Error copying Expression to Parameter named " & oParam.Name _
& " in ModelState named " & oMS.Name)
End Try
End If
Next
Next
Next 'oMS
'Next 'M
If oMSs.ActiveModelState IsNot oOrigMS Then
oOrigMS.Activate 'restore originally active ModelState to the currently active one
End If
If oDoc.RequiresUpdate Then oDoc.Update2(True)
'If oDoc.Dirty Then oDoc.Save2(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
But I get the following error:
Error on line 27 of rule: Identical values, in document: ---
The 'ComponentDefinition' public member of type 'ModelState' cannot be found.
Thank you in advance and have a nice day