Calculate Mass of parts in an assembly

Calculate Mass of parts in an assembly

sei.bun
Enthusiast Enthusiast
2,449 Views
7 Replies
Message 1 of 8

Calculate Mass of parts in an assembly

sei.bun
Enthusiast
Enthusiast

Hello,

I'm fairly new and have an problem to solve with.

I suppose to calculate the total mass of parts or assemblies whose iProperties.Value("Custom", "title") = tray.

 

I found posts which were similar with my problem but I have no idea how to modify their ilogic to solve my case.

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/ilogic-sum-of-a-custom-iproperty-wit... 

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/calculating-mass-of-selected-parts-a... 

 

Any help would be appreciated

QW

0 Likes
Accepted solutions (1)
2,450 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @sei.bun.  When you say calculate mass of 'selected parts & assemblies', do you mean selected files in a file explorer window, or do you mean selected components within an already open assembly document, that represent parts and sub-assemblies?  Also, are you wanting to pre-select a set of files or components before running the rule, or select them while the rule is running with Pick function?  Also, do you want to get fresh Mass reading from each; or have you already stored a Mass value you want to use within each one to a custom iProperty within each, and you want to use the value in that custom iProperty?  How do you want the end results (just display overall mass in a pop-up message, write it to a Parameter somewhere, write it to an iProperty somewhere, write it to something external, etc)?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 8

benny.deswert
Enthusiast
Enthusiast

Hi

I have a macro that calculates the mass of all selected parts or assemblys inside an assembly.

It is a macro but shouldnt be to hard to translate to ilogic.

 

Please be kind, It's one of my first and oldest macros 😉

 

Public Sub A_gewichten()          ' Display weights of all components selected and the total
    Dim odoc As Document
   ' Dim opropset As Document
   ' Dim odoctype As DocumentTypeEnum
    'Dim oAssemblydoc As AssemblyDocument
   ' Dim o As PartDocument
    Dim gewichttotaal As Single
    Dim oOcc As ComponentOccurrence
    Dim gewichttest As String
    Dim oOccurrences As ObjectCollection
    Dim counter As Integer
    Dim oMassProps As MassProperties
    Dim oMaterial As Material
    Dim omaterial1 As String
    Dim gewicht As Single
 '   Dim volume As Single
    

    
   counter = 0
   gewichttotaal = 0
   Set odoc = ThisApplication.ActiveDocument
   gewichttest = ("")
    
Set oOccurrences = ThisApplication.TransientObjects.CreateObjectCollection
    

For Each oOcc In odoc.SelectSet
    oOccurrences.Add oOcc
  
  
Next
    
    For Each oOcc In oOccurrences
 
    counter = (counter + 1)
    
        Set oMassProps = odoc.SelectSet.Item(counter).MassProperties ' reading mass propertys
        oMassProps.Accuracy = k_VeryHigh

            gewicht = oMassProps.Mass
            
            gewicht = Format(gewicht, "####0.0##")
         '   volume = oMassProps.volume
          '  volume = Format(volume, "####0.0##")
    
  '  On Error Resume Next        ' reading material name from the custom propertys
 '       Set oMaterial = oDoc.SelectSet.Item(counter).Definition.Document.PropertySets("User Defined Properties").Item("material")
  '          omaterial1 = oMaterial.value
    
  '  Set Material = oDoc.SelectSet.Item(counter).ComponentDefinition.Material
   ' MsgBox (omaterial1)

    
    
    gewichttest = gewichttest & vbCrLf & gewicht & " - " & omaterial1
    gewichttotaal = gewichttotaal + gewicht

   
    Next
    MsgBox ("Total weight: " & vbCrLf & gewichttotaal & vbCrLf & vbCrLf & "Individuals : " & gewichttest)
 '   MsgBox (volume)
    Set MyData = New DataObject  ' copy total weigth to clipboard
    MyData.SetText gewichttest 'gewichttotaal
    MyData.PutInClipboard
    GoTo einde
errorhandler: MsgBox ("Please select parts and/or assembly")
einde:
End Sub
0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor

Hi @sei.bun.  Even though I am still not sure what you want, without having the answers to my earlier questions, here is an iLogic rule example I had on hand for getting the total mass of pre-selected components in the active assembly document.  It even converts the units for you, in case the database units for mass do not match your document units for mass, then rounds it off to 3 decimal places.

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 oSS As SelectSet = oADoc.SelectSet
	If oSS.Count = 0 Then Exit Sub
	Dim oMassOfSelected As Double
	For Each oObj In oSS
		If TypeOf oObj Is ComponentOccurrence Then
			Dim oOcc As ComponentOccurrence = oObj
			If oOcc.Suppressed Then Continue For
			If TypeOf oOcc.Definition Is VirtualComponentDefinition Then Continue For
			Dim oMass As Double = oOcc.MassProperties.Mass
			oMassOfSelected = oMassOfSelected + oMass
		End If
	Next
	'convert units if needed (retrieved in 'database units', not necessarily document units)
	Dim oUOM As UnitsOfMeasure = oADoc.UnitsOfMeasure
	Dim oDocMassUnits As UnitsTypeEnum = oUOM.MassUnits
	Dim oDBMassUnits As UnitsTypeEnum = UnitsTypeEnum.kDatabaseMassUnits
	If oDocMassUnits <> oDBMassUnits Then
		oMassOfSelected = oUOM.ConvertUnits(oMassOfSelected, oDBMassUnits, oDocMassUnits)
	End If
	'round value off to 3 decimal places
	oMassOfSelected = Round(oMassOfSelected, 3)
	MsgBox("Total Mass of all selected components = " & oMassOfSelected, vbInformation, "Total Mass Of Selected")
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 5 of 8

sei.bun
Enthusiast
Enthusiast
Hi, WCrihfield.
Thanks for your reply.
Sorry for my description. It was wrong.
I suppose to calculate the total mass of parts or assemblies whose iProperties.Value("Custom", "title") = tray, not selected parts & assemblies.
Thanks.

0 Likes
Message 6 of 8

sei.bun
Enthusiast
Enthusiast
Hi, Benny Deswert.
Thanks for your reply.
Sorry for my description. It was wrong.
I suppose to calculate the total mass of parts or assemblies whose iProperties.Value("Custom", "title") = tray, not selected parts & assemblies.
Thanks.
0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Hi @sei.bun.  I changed my code to suit your latest specifications.  It now recursively iterates all components, in all levels of the assembly, looking for components that have that custom iProperty with that value, then when it finds them, it gets their Mass and adds it to a running Total Mass value.  Then, as before, it converts the units, if needed, then shows you the result in a message.  Since I am not sure what the term 'tray' represents, I am not sure if I am using the correct syntax when comparing it to the value of a custom iProperty, so I put a message in there for when it finds that custom iProperty, but the value does not match.

See if this code works better for you.

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
	Dim oTotalMass As Double
	'run our custom Sub routine
	RecurseComponents(oOccs, oTotalMass)
	'convert units if needed (retrieved in 'database units', not necessarily document units)
	Dim oUOM As UnitsOfMeasure = oADoc.UnitsOfMeasure
	Dim oDocMassUnits As UnitsTypeEnum = oUOM.MassUnits
	Dim oDBMassUnits As UnitsTypeEnum = UnitsTypeEnum.kDatabaseMassUnits
	If oDocMassUnits <> oDBMassUnits Then
		oTotalMass = oUOM.ConvertUnits(oTotalMass, oDBMassUnits, oDocMassUnits)
	End If
	'round value off to 3 decimal places
	oTotalMass = Round(oTotalMass, 3)
	MsgBox("Total Mass filtered components = " & oTotalMass, vbInformation, "Total Mass")
End Sub

Sub RecurseComponents(oComps As ComponentOccurrences, ByRef oTotalMass As Double)
	If IsNothing(oComps) 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
		If TypeOf oComp.Definition Is AssemblyComponentDefinition Then
			RecurseComponents(oComp.Definition.Occurrences, oTotalMass)
		End If
		Dim oCompDoc As Document = oComp.ReferencedDocumentDescriptor.ReferencedDocument
		Dim oCProps As PropertySet = oCompDoc.PropertySets.Item("Inventor User Defined Properties")
		If oCProps.Count = 0 Then Continue For
		Dim oCProp As Inventor.Property = Nothing
		For Each oProp As Inventor.Property In oCProps
			If oProp.Name = "title" Then
				oCProp = oProp
				Exit For
			End If
		Next
		If IsNothing(oCProp) Then Continue For
		'I don't know what the term 'tray' represents, so I don't know if this is correct syntax
		If oCProp.Value = tray Then
			Dim oMass As Double = oComp.MassProperties.Mass
			oTotalMass = oTotalMass + oMass
		Else
			MsgBox("The Custom iProperty was found, but it had different value.", , "")
			'Logger.Debug("The Custom iProperty was found, but it had different value.")
		End If
	Next
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 8

sei.bun
Enthusiast
Enthusiast
@WCrihfield Thank you!
This works perfect!
0 Likes