Autodesk Inventor - Change parameter units with ilogic rule

Autodesk Inventor - Change parameter units with ilogic rule

MarcoMoura9852
Enthusiast Enthusiast
977 Views
2 Replies
Message 1 of 3

Autodesk Inventor - Change parameter units with ilogic rule

MarcoMoura9852
Enthusiast
Enthusiast

Hello All,

 

We have thousands of part files where a specific parameter, called DENS1, is setup as UNITLESS unit type.

 

I need to change these units on all these files from UNITLESS to g/cm^3.

 

I've tried a ilogic code that creates this parameter, sets a value and it's unit type: g/cm^3. 

 

oMyParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters

oParameter = oMyParameter.AddByExpression("DENS1", "8", "g/cm^3")

However, it only works on files that don't have the parameter DENS1. (this rules creates it)

 

Can someone help us to get a  code that can change the units on the files where the parameter already exists?

 

Thank you for your attention.

 

 

0 Likes
Accepted solutions (1)
978 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @MarcoMoura9852.  I don't know what version of Inventor you may be using, so I don't know if ModelStates are involved, but I have assumed that is the case here.  If that is not the case, then simply switch "FactoryDocument" to "Document".  You can try this code below for this task.

 

If Not ThisApplication.FileManager.IsInventorComponent(ThisDoc.Document) Then Exit Sub
Dim oDoc As Document = ThisDoc.FactoryDocument
If oDoc.IsModifiable = False Then Exit Sub
Dim oUParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
Dim oUParam As UserParameter = Nothing
Try
	oUParam = oUParams.Item("DENS1")
Catch 'if it was not found
	oUParam = oUParams.AddByExpression("DENS1", "8", "g/cm^3")
End Try
'if it was found, but its value/Expression or Units do not match what you want
If oUParam.Units <> "g/cm^3" Then oUParam.Units = "g/cm^3"
If oUParam.Expression <> "8" Then oUParam.Expression = "8"
If oDoc.RequiresUpdate Then oDoc.Update2(True)
'If oDoc.Dirty Then oDoc.Save2(False)

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 3

MarcoMoura9852
Enthusiast
Enthusiast
Thank you! It works great!!
0 Likes