Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic change units to grams

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
stephengibson76
1864 Views, 7 Replies

iLogic change units to grams

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

Stephen Gibson



View stephen gibson's profile on LinkedIn


7 REPLIES 7
Message 2 of 8

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   

 

Message 3 of 8

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	

 

Message 4 of 8

Hi
 
That was really helpful! Thank you.
 
Pramod
 
 
 
Message 5 of 8

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... 🤔

Message 6 of 8

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 

Message 7 of 8

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!



Johnson Shiue (johnson.shiue@autodesk.com)
Software Test Engineer
Message 8 of 8

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 

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

Post to forums  

Autodesk Design & Make Report