I recently made a mistake. While working on a project, I should have marked some User Parameters to Export as iProperty. Unfortunately, I didn't realize this until I was about 300 parts in. I sure didn't want to open up each part and manually check those boxes.
For those unsure of what I'm referring to, in Inventor you can export Parameters as iProperties. This is a very easy and simple way of getting model data onto your Bill Of Materials or Parts Lists. You accomplish this by checking the box in the Export Parameter column of the Parameters dialog box.
Once you've marked one or more parameters to Export, you can right mouse click on one of the marked Parameters and choose Custom Property Format from the Contextual Menu.
As I mentioned, I sure wasn't going to open each part and export the necessary Parameters and set their format. Automation to the rescue!
The Inventor Parameter API object has a Property called ExposedAsProperty. This is set to a Boolean that controls whether or not the parameter is exposed as an iProperty. Assuming I've already declared and set the oUserParam object:
oUserParam.ExposedAsProperty = True
Once that property has been set to True, you can set all the options for the formatting thru the Property called CustomPropertyFormat.
oUserParam.CustomPropertyFormat.PropertyType = kTextPropertyType
oUserParam.CustomPropertyFormat.Precision = kThreeDecimalPlacesPrecision
oUserParam.CustomPropertyFormat.ShowLeadingZeros = False
oUserParam.CustomPropertyFormat.ShowTrailingZeros = True
oUserParam.CustomPropertyFormat.ShowUnitsString = True
oUserParam.CustomPropertyFormat.Units = "in"
The PropertyType Property determines if the iProperty will be a Number or a Text Property. It uses the CustomPropertyTypeEnum.
The Precision Property determines the precision of the iProperty. It uses the CustomPropertyPrecisionEnum.
The ShowLeadingZeros and ShowTrailing Zeros determine if leading or trailing zeros are included in the iProperty. These are set to Booleans.
The ShowUnitsString Property determines if the unit string is shown in the iProperty.
The Units Property determines the units to use in the iProperty.
You can look up the enumerators in the Inventor Programming/API Help for more information on them.
In my case, I had three specific Parameters (Height, Length, Width) in each part that I wanted to expose. Admittedly, there are many ways I could have done this. I chose to loop thru all the User Parameters and if the Parameter name matched what I wanted, I exported it and set its format to the way I needed it.
Below is the final code:
Dim oPart As PartDocument
oPart = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition
oCompDef = oPart.ComponentDefinition
Dim oUserParams As UserParameters
oUserParams = oCompDef.Parameters.UserParameters
' Loop thru parameters and set specific parameters to export as iProperty
For i = 1 to oUserParams.Count
sName = oUserParams.Item(i).Name
Select Case sName
Case "Height", "Length", "Width"
oUserParams.Item(i).ExposedAsProperty = True
oUserParams.Item(i).CustomPropertyFormat.PropertyType = kTextPropertyType
oUserParams.Item(i).CustomPropertyFormat.Precision = kThreeDecimalPlacesPrecision
oUserParams.Item(i).CustomPropertyFormat.ShowLeadingZeros = False
oUserParams.Item(i).CustomPropertyFormat.ShowTrailingZeros = True
oUserParams.Item(i).CustomPropertyFormat.ShowUnitsString = True
oUserParams.Item(i).CustomPropertyFormat.Units = "in"
I ran this code as an external rule. I used a custom application that I wrote that allows me to run rules against a large group of parts. I accomplished in a few minutes what would have taken me hours to do....and I know it's all correct!
If you have any questions, be sure to post in the comments below.