Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Preserve weight! Despise Weight! Iproperties!

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
ivanildo.b
379 Views, 5 Replies

Preserve weight! Despise Weight! Iproperties!

 

I need to keep the gross weight.
Despise weight of all suppress parts and references.
Routine should disregard weights of suppressed parts and referencesI

Could someone help?

Iva.btblan

5 REPLIES 5
Message 2 of 6
WCrihfield
in reply to: ivanildo.b

Hi @ivanildo.b.  Here is something very basic you can try.  It gets the 'active' document, makes sure it is an assembly, then loops through all of its 'top level' components.  If the component is Suppressed or its BOMStructure is set to Reference, it will skip that component.  Those that pass that test, it gets their Mass, then adds that to a total Mass variable.  At the end it shows you the result.  Keep in mind that the units are in 'database units' (kilogram in this case), not necessarily the mass units you have specified of your document.  So you may need to include some math or other units conversion if you need the result in other units.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
oADef = oADoc.ComponentDefinition
oOccs = oADef.Occurrences
Dim oOverallMass As Double = 0.0
For Each oOcc As ComponentOccurrence In oOccs
	If oOcc.Suppressed Or
		oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
		Continue For 'skip to next oOcc in loop
	End If
	'get its Mass
	oOccMass = oOcc.MassProperties.Mass 'is in database units, not document units
	oOverallMass = oOverallMass + oOccMass
Next
'get String for database mass units for report
oDbUnits = ThisApplication.UnitsOfMeasure.GetStringFromType(UnitsTypeEnum.kDatabaseMassUnits)
MsgBox("oOverallMass= " & oOverallMass & " " & oDbUnits,,"")

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6
WCrihfield
in reply to: ivanildo.b

Actually, after an extra test or 3, I see that a much simpler code will return the same results.

 

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
oADef = oADoc.ComponentDefinition
Dim oOverallMass As Double = oADef.MassProperties.Mass
MsgBox("oOverallMass= " & oOverallMass,,"")

 

or even this:

 

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
oADef = oADoc.ComponentDefinition
Dim oOverallMass As Double = iProperties.Mass
MsgBox("oOverallMass= " & oOverallMass,,"")

 

But this last one, which gets the value from iProperties, returns the value in document units, instead of database units.

In all three ways of getting the total assembly mass, they always reduced the overall mass anytime I suppressed any component or set any component's BOMStructure to Reference.  (I'm using Inventor 2022.1.1, just for reference.)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 6
ivanildo.b
in reply to: WCrihfield

I thank you attention!

I need the somatoria of the Bruto of each part.

I don't want the value of mass.

Iva.btblan

050.015.PNG

050.010.PNG

050.011.PNG

050.012.PNG

050.016.PNG

Message 5 of 6
WCrihfield
in reply to: ivanildo.b

OK.  After reviewing your files, I saw that you had that one component set to 'Enabled = False', instead of Suppressed or BOMStructure set to Reference.  That's why my earlier code was still including the weight for that component.  Now when I additionally include checking each component for Enabled...if Enabled = False, then skip that component.  That fixed the outcome, so it would show the final weight you were looking for.

Sub Main
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	iProperties.Value("Custom", "BRUTO") = 0
	Call Recursivo(oDoc.ComponentDefinition.Occurrences)
	oDoc.Update
	oVal = iProperties.Value("Custom", "BRUTO")
	MsgBox("oVal = " & oVal,,"")
End Sub

Sub Recursivo(ByVal Occurrences As ComponentOccurrences)
	Dim oTotal As Double = 0.0
	For Each occ As ComponentOccurrence In Occurrences
		If occ.Suppressed Or _
			occ.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Or _
			occ.Enabled = False Then
			Continue For
		End If
		If occ.DefinitionDocumentType = kPartDocumentObject Then
			Dim oOccPDoc As PartDocument = occ.Definition.Document
			Dim oOccBruto As Double = oOccPDoc.PropertySets.Item("Inventor User Defined Properties").Item("BRUTO").Value
			oTotal = oTotal + oOccBruto
		End If
		If occ.DefinitionDocumentType = kAssemblyDocumentObject Then
			Recursivo(occ.SubOccurrences)
		End If
	Next
	iProperties.Value("Custom", "BRUTO") += oTotal
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6
ivanildo.b
in reply to: WCrihfield

Excellent! It's wonderful!
Thanks for the attention!
Congratulations on your work!

 

Iva.btblan

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report