Add a calculated total to a custom iProperty

Add a calculated total to a custom iProperty

ober2558
Enthusiast Enthusiast
325 Views
4 Replies
Message 1 of 5

Add a calculated total to a custom iProperty

ober2558
Enthusiast
Enthusiast

I have a code that calculates the total weight of all components within an assembly (Based on custom iproperties within each component not the mass calculation) I would like this total value to be added to the top level assembly as a custom property. I am not very experienced in coding so any help you can provide would be greatly appreciated.

0 Likes
Accepted solutions (1)
326 Views
4 Replies
Replies (4)
Message 2 of 5

Andrii_Humeniuk
Advisor
Advisor

Hi @ober2558 . This code must be run in an assembly. Replace dCalcTotal (123456) with your amount and in line 4 write the name of your property "Total value".

Dim dCalcTotal As Double = 123456
Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oCustom As PropertySet = oDoc.PropertySets("Inventor User Defined Properties")
Dim sNameProp As String = "Total value"
Dim oProp As Inventor.Property
Try : oProp = oCustom(sNameProp) : oProp.Value = dCalcTotal
Catch : oCustom.Add(dCalcTotal, sNameProp) : End Try

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 3 of 5

ober2558
Enthusiast
Enthusiast

How would that fit into this code?

 

Class ThisRule
	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.Document
		Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
		oWeightList = New List(Of Double)
		oLabels = New List(Of String)
		oLabels.Add("Profile1")
		oLabels.Add("Profile2")
		oLabels.Add("Profile3")
		oLabels.Add("Profile4")
		oLabels.Add("Profile5")
		oLabels.Add("Profile6")
		oLabels.Add("Profile7")
		oLabels.Add("Profile8")
		oLabels.Add("Profile9")
		oLabels.Add("Profile10")
		oLabels.Add("Profile11")
		RecursivelyProcessComponents(oOccs)
		Dim oTotaliPartsWeight As Double = oWeightList.Sum
		oTotaliPartsWeight = Round(oTotaliPartsWeight, 3, MidpointRounding.AwayFromZero)
		MsgBox("Total Tool Weight = " & oTotaliPartsWeight & " kg", vbInformation, "Total Tool Weight")
	End Sub
	
	Dim oWeightList As List(Of Double)
	Dim oLabels As List(Of String)
	
	Sub RecursivelyProcessComponents(oComps As ComponentOccurrences)
		If oComps Is Nothing OrElse oComps.Count = 0 Then Exit Sub
		For Each oComp As ComponentOccurrence In oComps
			If oComp.Suppressed Then Continue For
			If TypeOf oComp.Definition Is VirtualComponentDefinition Then Continue For
			If TypeOf oComp.Definition Is WeldsComponentDefinition Then Continue For
			Dim oCompDoc As Document = Nothing
			Try : oCompDoc = oComp.Definition.Document : Catch : End Try
			If oComp.IsiAssemblyMember Then
				Dim oMember As iAssemblyMember = oComp.Definition.iAssemblyMember
				oWeightList.Add(CDbl(oMember.Row.Item("Weight [Custom]").Value))
			ElseIf oComp.IsiPartMember Then
				Dim oMember As iPartMember = oComp.Definition.iPartMember
				oWeightList.Add(CDbl(oMember.Row.Item("Weight [Custom]").Value))
			Else
				Dim bCCMember As Boolean = False
				Try : bCCMember = oComp.Definition.IsContentMember : Catch : End Try
				If bCCMember = True Then
					Dim bCheck As Boolean = False
					For Each sLabel In oLabels
						If oCompDoc.DisplayName.Contains(sLabel) Then bCheck = True
					Next
					If bCheck = True Then
						oWeightList.Add(oComp.MassProperties.Mass)
					Else 
						Dim oPropValue As Object = Nothing
						Try : oPropValue = oCompDoc.PropertySets.Item(4).Item("Weight").Value : Catch : End Try
						Try : oWeightList.Add(CDbl(oPropValue)) : Catch : End Try
					End If
				Else 
					Dim Result As Integer
					Result = MessageBox.Show("Would You Like To Input a Temporary Weight For This Part?", _
					"No Part Weight Detected for '" & oComp.Name & "'", MessageBoxButtons.YesNoCancel, _
					MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
					If Result = 6 Then
						Dim Returnmyparam As String = InputBox("Input Weight in kg ", "Weight Input Prompt for '" & _
						oComp.Name & "'", "")
						Dim oWeightCProp As Inventor.Property = Nothing
						Try
							oWeightCProp = oCompDoc.PropertySets.Item(4).Item("Weight")
						Catch
							oWeightCProp = oCompDoc.PropertySets.Item(4).Add(CDbl(Returnmyparam), "Weight")
						End Try
						oWeightList.Add(CDbl(Returnmyparam))
					ElseIf Result = 7 Then
					ElseIf Result = 2 Then
						Return
					End If
				End If
			End If
		Next
	End Sub
End Class
0 Likes
Message 4 of 5

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Please try this code:

Class ThisRule
	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.Document
		Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
		oWeightList = New List(Of Double)
		oLabels = New List(Of String)
		oLabels.Add("Profile1")
		oLabels.Add("Profile2")
		oLabels.Add("Profile3")
		oLabels.Add("Profile4")
		oLabels.Add("Profile5")
		oLabels.Add("Profile6")
		oLabels.Add("Profile7")
		oLabels.Add("Profile8")
		oLabels.Add("Profile9")
		oLabels.Add("Profile10")
		oLabels.Add("Profile11")
		RecursivelyProcessComponents(oOccs)
		Dim oTotaliPartsWeight As Double = oWeightList.Sum
		oTotaliPartsWeight = Round(oTotaliPartsWeight, 3, MidpointRounding.AwayFromZero)
		MsgBox("Total Tool Weight = " & oTotaliPartsWeight & " kg", vbInformation, "Total Tool Weight")
        Dim oCustom As PropertySet = oADoc.PropertySets("Inventor User Defined Properties")
        Dim sNameProp As String = "Total Tool Weight"
        Dim oProp As Inventor.Property        
        Try : oProp = oCustom(sNameProp) : oProp.Value = oTotaliPartsWeight & " kg"
        Catch : oCustom.Add(oTotaliPartsWeight & " kg", sNameProp) : End Try
	End Sub
	
	Dim oWeightList As List(Of Double)
	Dim oLabels As List(Of String)
	
	Sub RecursivelyProcessComponents(oComps As ComponentOccurrences)
		If oComps Is Nothing OrElse oComps.Count = 0 Then Exit Sub
		For Each oComp As ComponentOccurrence In oComps
			If oComp.Suppressed Then Continue For
			If TypeOf oComp.Definition Is VirtualComponentDefinition Then Continue For
			If TypeOf oComp.Definition Is WeldsComponentDefinition Then Continue For
			Dim oCompDoc As Document = Nothing
			Try : oCompDoc = oComp.Definition.Document : Catch : End Try
			If oComp.IsiAssemblyMember Then
				Dim oMember As iAssemblyMember = oComp.Definition.iAssemblyMember
				oWeightList.Add(CDbl(oMember.Row.Item("Weight [Custom]").Value))
			ElseIf oComp.IsiPartMember Then
				Dim oMember As iPartMember = oComp.Definition.iPartMember
				oWeightList.Add(CDbl(oMember.Row.Item("Weight [Custom]").Value))
			Else
				Dim bCCMember As Boolean = False
				Try : bCCMember = oComp.Definition.IsContentMember : Catch : End Try
				If bCCMember = True Then
					Dim bCheck As Boolean = False
					For Each sLabel In oLabels
						If oCompDoc.DisplayName.Contains(sLabel) Then bCheck = True
					Next
					If bCheck = True Then
						oWeightList.Add(oComp.MassProperties.Mass)
					Else 
						Dim oPropValue As Object = Nothing
						Try : oPropValue = oCompDoc.PropertySets.Item(4).Item("Weight").Value : Catch : End Try
						Try : oWeightList.Add(CDbl(oPropValue)) : Catch : End Try
					End If
				Else 
					Dim Result As Integer
					Result = MessageBox.Show("Would You Like To Input a Temporary Weight For This Part?", _
					"No Part Weight Detected for '" & oComp.Name & "'", MessageBoxButtons.YesNoCancel, _
					MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
					If Result = 6 Then
						Dim Returnmyparam As String = InputBox("Input Weight in kg ", "Weight Input Prompt for '" & _
						oComp.Name & "'", "")
						Dim oWeightCProp As Inventor.Property = Nothing
						Try
							oWeightCProp = oCompDoc.PropertySets.Item(4).Item("Weight")
						Catch
							oWeightCProp = oCompDoc.PropertySets.Item(4).Add(CDbl(Returnmyparam), "Weight")
						End Try
						oWeightList.Add(CDbl(Returnmyparam))
					ElseIf Result = 7 Then
					ElseIf Result = 2 Then
						Return
					End If
				End If
			End If
		Next
	End Sub
End Class

 

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 5 of 5

ober2558
Enthusiast
Enthusiast

Perfect! Thank you!

0 Likes