Any suggestions for running an ilogic rule to change the units of parts to grams if they are less than 1 kg in an assembly?
Our part template is set to kg which we want for the majority, but when working on small component assemblies, we want the units in grams and its a ball ache going into each one to change. We have a sketched symbol to pull the mass of the part that shows part unit
Solved! Go to Solution.
Solved by Curtis_Waguespack. Go to Solution.
Hi stephengibson76,
Here is a quick iLogic rule that you can run in an assembly and it will look at each part file in the assembly and change it's mass unit to grams if it is less than 1 kg.
Please test this a bit before relying on it too much, as I didn't have much time to test it myself.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
'Define the open document
Dim openDoc As Document
openDoc = ThisDoc.Document
'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In openDoc.AllReferencedDocuments
Dim oPartDoc As PartDocument
oPartDoc = docFile
'Set a reference to the mass properties object.
Dim oMassProps As MassProperties
oMassProps = oPartDoc.ComponentDefinition.MassProperties
'get the mass in the default units of kg
oMass= oMassProps.Mass
'check to see if less than 1 kg
If oMass < 1 'kg Then
'change the unit of measure to grams
'kKilogramMassUnits = 11283
'kGramMassUnits = 11284
docFile.unitsofmeasure.MassUnits = 11284
End If
Next
Hi stephengibson76,
Here's another version that you should be able to run in an assembly or in a part file.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
'Define the open document Dim openDoc As Document openDoc = ThisDoc.Document 'set mass units for the top level 'if run on a part rather than assembly 'this will look at just that part 'check to see if less than 1 kg If iProperties.Mass < 1 'kg Then 'change the unit of measure to grams 'kKilogramMassUnits = 11283 'kGramMassUnits = 11284 openDoc.unitsofmeasure.MassUnits = 11284 End If 'Look at all of the files referenced in the open document Dim docFile As Document For Each docFile In openDoc.AllReferencedDocuments Dim oPartDoc As PartDocument oPartDoc = docFile 'Set a reference to the mass properties object. Dim oMassProps As MassProperties oMassProps = oPartDoc.ComponentDefinition.MassProperties 'get the mass in the default units of kg oMass= oMassProps.Mass 'check to see if less than 1 kg If oMass < 1 'kg Then 'change the unit of measure to grams 'kKilogramMassUnits = 11283 'kGramMassUnits = 11284 docFile.unitsofmeasure.MassUnits = 11284 End If Next
Awesome! Can confirm this still works as of today with Inventor 2022.
UPDATE: Oh, well, most of the times, at least. I have no idea what the issue is when nothing happens... 🤔
Hi Curtis
thanks for the code
This runs and does what's expected but throws this error
Unable to cast COM object of type 'System.__ComObject' to interface type 'Inventor.PartDocument'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{29F0D463-C114-11D2-B77F-0060B0F159EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
can this be dealt with , what do i need to do ?
keith
Hi! Curtis' rule has to run within an assembly, not a part. Please open an assembly and copy&paste the rule an iLogic rule. It should run without a problem.
Many thanks!
The code was run in an assembly environment and worked as expected
I was enquiring about the error message that appeared and what could be done to stop the error