Calculating Mass of selected Parts and/or Assemblies with Ilogic

Calculating Mass of selected Parts and/or Assemblies with Ilogic

alexander.duell
Contributor Contributor
3,349 Views
9 Replies
Message 1 of 10

Calculating Mass of selected Parts and/or Assemblies with Ilogic

alexander.duell
Contributor
Contributor

Hello,

 

I'm fairly new to ilogic and I have an idea for ilogic that I need some help with.

 

As the title suggests I would like an Ilogic rule that shows me the mass of selected Parts and Assemblies. 

 

So it should give the total mass of all selected files regardless if it's a part or assembly.

 

The help would be very appreciated. 

 

Best Regards

Alex

 

 

Accepted solutions (2)
3,350 Views
9 Replies
Replies (9)
Message 2 of 10

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @alexander.duell 

As I understand it you want to be able to select a bunch of parts/subassemblies within an assembly, then run a rule and get the total mass of those occurrences?

Try this 🙂

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oSelSet As SelectSet = oAsm.SelectSet
Dim UoM As UnitsOfMeasure = oAsm.UnitsOfMeasure
Dim totMass As Double = 0
Dim oList As New List(Of ComponentOccurrence)
For Each oObj As Object In oSelSet
	If TypeOf (oObj) Is ComponentOccurrence Then oList.Add(oObj)
Next
For Each oOcc As ComponentOccurrence In oList
	Dim parentSelected As Boolean = False
	'Make sure something isn't selected twice (subassembly and occurrence in subassembly both selected)
	For Each oParentOcc As ComponentOccurrence In oOcc.OccurrencePath
		If oList.OfType(Of ComponentOccurrence).Where(Function(x As ComponentOccurrence) x.Definition Is oParentOcc.Definition _
		AndAlso x.Definition IsNot oOcc.Definition).Count > 0 Then parentSelected = True
	Next
	'--------------------------------------------------------------------------------------------------
	If parentSelected = False Then totMass = totMass + oOcc.MassProperties.Mass
Next

MessageBox.Show("Total mass of selected occurrences" & vbCrLf & UoM.GetStringFromValue(totMass, UoM.MassUnits), _
"Total mass", MessageBoxButtons.OK, MessageBoxIcon.Information)
Message 3 of 10

alexander.duell
Contributor
Contributor

Yes, it work like a charm,

 

Thou, I figured, how would the rule look like if one should add:

 

IF nothing is marked, it shows the mass of visible parts & sub assemblies in the current assembly? By adding this there is a possibility to easily calculate the total mass of certain representation views? 

 

But, the code you gave me does exactly that i asked for. Just perfect. 

 

 

0 Likes
Message 4 of 10

JhoelForshav
Mentor
Mentor
Accepted solution

@alexander.duell 

I'm glad it works for you. I have updated the code to handle the case if nothing is selected as requested 🙂

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oSelSet As SelectSet = oAsm.SelectSet
Dim UoM As UnitsOfMeasure = oAsm.UnitsOfMeasure
Dim totMass As Double = 0
Dim oList As New List(Of ComponentOccurrence)
For Each oObj As Object In oSelSet
	If TypeOf (oObj) Is ComponentOccurrence Then oList.Add(oObj)
Next
Dim noSelection As Boolean = False
If oList.Count = 0
noSelection = True
For Each oOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences.AllLeafOccurrences
	If oOcc.Visible Then totMass = totMass + oOcc.MassProperties.Mass
Next
Else
For Each oOcc As ComponentOccurrence In oList
	Dim parentSelected As Boolean = False
	'Make sure something isn't selected twice (subassembly and occurrence in subassembly both selected)
	For Each oParentOcc As ComponentOccurrence In oOcc.OccurrencePath
		If oList.OfType(Of ComponentOccurrence).Where(Function(x As ComponentOccurrence) x.Definition Is oParentOcc.Definition _
		AndAlso x.Definition IsNot oOcc.Definition).Count > 0 Then parentSelected = True
	Next
	'--------------------------------------------------------------------------------------------------
	If parentSelected = False Then totMass = totMass + oOcc.MassProperties.Mass
Next
End If
MessageBox.Show(If (noSelection, "Total mass of visible components", "Total mass of selected components") _
& vbCrLf & UoM.GetStringFromValue(totMass, UoM.MassUnits), _
"Total mass", MessageBoxButtons.OK, MessageBoxIcon.Information)
Message 5 of 10

alexander.duell
Contributor
Contributor

This was even better than i anticipated. 

 

This is a great rule!

 

Thank you

Best Regards

Alexander

0 Likes
Message 6 of 10

j_weber
Mentor
Mentor

Hi @JhoelForshav 

 

your code works great. 

I just try to grab also the material of the selected part and show it in the dialog. 

But I have no success. 




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





0 Likes
Message 7 of 10

JhoelForshav
Mentor
Mentor

@j_weber 

That's a more complex thing to do since all the selected parts can have different materials. Also if a subassembly is selected you'd have to traverse it and then if it contains a subassembly you'd have to traverse that as well to get all the part occurrences and their materials.

 

I added it to the code.

A warning though, if too many occurrences are selected the entire message box will not fit on the screen 😄

Sub Main
Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oMsg As String = "Components:"
Dim oSelSet As SelectSet = oAsm.SelectSet
Dim UoM As UnitsOfMeasure = oAsm.UnitsOfMeasure
Dim totMass As Double = 0
Dim oList As New List(Of ComponentOccurrence)
For Each oObj As Object In oSelSet
	If TypeOf (oObj) Is ComponentOccurrence Then oList.Add(oObj)
Next
Dim noSelection As Boolean = False
If oList.Count = 0
noSelection = True
For Each oOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences.AllLeafOccurrences
	If oOcc.Visible
		totMass = totMass + oOcc.MassProperties.Mass
		oMsg = getLeafOccs(oOcc, oMsg, UoM) 
	End If
Next
Else
For Each oOcc As ComponentOccurrence In oList
	Dim parentSelected As Boolean = False
	'Make sure something isn't selected twice (subassembly and occurrence in subassembly both selected)
	For Each oParentOcc As ComponentOccurrence In oOcc.OccurrencePath
		If oList.OfType(Of ComponentOccurrence).Where(Function(x As ComponentOccurrence) x.Definition Is oParentOcc.Definition _
		AndAlso x.Definition IsNot oOcc.Definition).Count > 0 Then parentSelected = True
	Next
	'--------------------------------------------------------------------------------------------------
	If parentSelected = False
		totMass = totMass + oOcc.MassProperties.Mass
		oMsg = getLeafOccs(oOcc, oMsg, UoM) 
	End If
Next
End If
MessageBox.Show(If (noSelection, "Total mass of visible components", "Total mass of selected components") _
& vbCrLf & UoM.GetStringFromValue(totMass, UoM.MassUnits) & vbCrLf & vbCrlf & oMsg, _
"Total mass", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Function getLeafOccs(oOcc As ComponentOccurrence, oMsg As String, UoM As UnitsOfMeasure) As String
If TypeOf (oOcc.Definition) Is PartComponentDefinition
	oMsg = oMsg & vbCrLf & oOcc.Name & ":  " & oOcc.Definition.Document.ActiveMaterial.DisplayName _
	& " - " & UoM.GetStringFromValue(oOcc.MassProperties.Mass, UoM.MassUnits)
Else
	For Each subOcc As ComponentOccurrence In oOcc.SubOccurrences
		oMsg = getLeafOccs(subOcc, oMsg, UoM)
	Next
End If
Return oMsg
End Function
Message 8 of 10

j_weber
Mentor
Mentor

Hallo @JhoelForshav 

 

thanks a lot. I'll test it tomorrow when I'm back in the office. 

 

 




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





0 Likes
Message 9 of 10

Thomas.DosSantos
Community Visitor
Community Visitor

hi @JhoelForshav,

Very useful thank you.

I would like to group the weights by materials, can you do that?

 

0 Likes
Message 10 of 10

TGibson7A8HD
Enthusiast
Enthusiast

is it possible to expand on this code so that any parts in the assembly that return a 0 are automatically supressed? 

 

The only way I know how to do it, is by adding a code for every individual item based on its file name which wouldnt work as a rule to use on multiple assemblies

0 Likes