Custom iProperty off total mass of parts in assembly containing a certain phrase

Custom iProperty off total mass of parts in assembly containing a certain phrase

helouise
Enthusiast Enthusiast
287 Views
3 Replies
Message 1 of 4

Custom iProperty off total mass of parts in assembly containing a certain phrase

helouise
Enthusiast
Enthusiast

Good day , I hope all are well!

 

I am figuring out this iLogic bit by bit. I have an assembly that has a sub assembly and anywhere from 2 to a 100 parts that all have the word "Liner" in them.

 

helouise_0-1698766196768.png

or

helouise_2-1698766613413.png

 

I would like to run a rule in this assembly that calculates the sum of all parts that include the word "Liner" and adds it to a custom property "Liner mass" in main assembly so that I can include this in my drawings. At the moment I manually get the total mass and subtract the sub assembly mass (which gives me the liner mass) and write it in my drawing each time. This takes time and can cause mistakes if I forget to check it after I may have made any changes.

 

Even if the iLogic rule can rather subtract the sub assembly mass from total mass and add that to custom I property it would have the same result.

 

The sub assembly name always differs but parts will always contain Liner in their name if that helps.

 

I was wondering if it is possible and if so, to get some help please.

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

WCrihfield
Mentor
Mentor
Accepted solution

Hi @helouise.  Here is an iLogic rule you could try out for that task.  I customized it to iterate through all levels of the assembly, and if the component's name contains "Liner", it will get its Mass, and add it to a total mass.  Then it attempts to write that total mass to a custom iProperty in the main assembly named "Liner mass".  The variable named dTotalMass is declared between routines, so that all routines can access it, but that, and several other things could be done differently also.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	RecurseComponents(oOccs, AddressOf ProcessComponent)
	Dim oCProps As Inventor.PropertySet = oADoc.PropertySets.Item(4)
	Dim oCProp As Inventor.Property = Nothing
	Try
		oCProp = oCProps.Item("Liner mass")
	Catch
		oCProp = oCProps.Add(dTotalMass, "Liner mass")
	End Try
	If oCProp IsNot Nothing AndAlso oCProp.Value <> dTotalMass Then
		oCProp.Value = dTotalMass
	End If
End Sub

Dim dTotalMass As Double

Sub RecurseComponents(oComps As ComponentOccurrences, ComponentProcess As Action(Of ComponentOccurrence))
	If oComps Is Nothing OrElse oComps.Count = 0 Then Exit Sub
	For Each oComp As ComponentOccurrence In oComps
		ComponentProcess(oComp)
		If oComp.Suppressed = False AndAlso _
			oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			RecurseComponents(oComp.Definition.Occurrences, ComponentProcess)
		End If
	Next
End Sub

Sub ProcessComponent(oComp As ComponentOccurrence)
	If oComp Is Nothing OrElse oComp.Suppressed Then Exit Sub
	If oComp.Name.Contains("Liner") Then
		Dim dMass As Double = 0.0
		Try : dMass = oComp.MassProperties.Mass : Catch : End Try
		dTotalMass = dTotalMass + dMass
	End If
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 3 of 4

helouise
Enthusiast
Enthusiast

@WCrihfield Thank you so much you are amazing!!

 

Can you please help me to round the number off with no decimals?

This is how it is used in our drawings.

helouise_0-1698816388025.png

0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Sure.  You would just need to add this following single line of code:

dTotalMass = Round(dTotalMass)

...between these two existing lines of code, within the Sub Main area of the rule:

RecurseComponents(oOccs, AddressOf ProcessComponent)
Dim oCProps As Inventor.PropertySet = oADoc.PropertySets.Item(4)

There area many variations of the System.Math.Round method, so you can customize it further if needed.  The simplest form (shown here) will attempt to round to the nearest whole number (Integer), but when the value is at the mid point, it rounds towards the nearest even number by default.  If that does not sound like the best fit for your needs, you can include a second input after the the current one, to specify a variation of the system.midpointrounding Enum. 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)