Thursday, May 19, 2011

Use iLogic to Add and Use a Custom iProperty All in One Rule

Issue:
You want to have a rule that checks for a custom iProperty and then adds it if it is not already created. Then you'd like to use that custom iProperty in the rest of the rule.



Solution:
Here is a bit of code that uses Try/Catch to query the custom property set and add the custom properties to the file if they are not found. It then goes on to use an input radio box to prompt the user to choose between two possible options for the part finish. And then finally it sets the Finish and Finish Code custom iProperties based off of the user input.


' ---------------------start of iLogic code---------------------
'check for custom iprops and add them if not found
Dim propertyName1 As String = "Finish"
Dim propertyName2 As String = "FinishCode"

customPropertySet = ThisDoc.Document.PropertySets.Item _
("Inventor User Defined Properties")

Try
          prop = customPropertySet.Item(propertyName1)
          prop = customPropertySet.Item(propertyName2)
Catch
      ' Assume error means not found
            customPropertySet.Add("", propertyName1)
          customPropertySet.Add("", propertyName2)
End Try

'output the custom iproperties and update the file
RuleParametersOutput()
InventorVb.DocumentUpdate()

'prompt user for iProperty value
booleanParam1 = InputRadioBox("Select Finish", _
"Primed for Painting", "Bright Stainless Steel", True, Title := "iLogic Finish")

'set iproperties based upon user input
if booleanParam1 = True then
iProperties.Value("Custom", "Finish") = "Primed for Painting"
iProperties.Value("Custom", "FinishCode") = "600"
else
iProperties.Value("Custom", "Finish") = "Bright Stainless Steel"
iProperties.Value("Custom", "FinishCode") = "629"
end if

'update file
iLogicVb.UpdateWhenDone = True

' ---------------------end of iLogic code ---------------------

A big THANK YOU to Mike Deck of Autodesk for the Try/Catch example for creating a custom iProperty via iLogic.





Update:
Wednesday, August 01, 2012
I ran into an issue with combining more than one custom iProperty in the Try/Catch, and have found it better to keep them separate as I show in the example below.

This example also demonstrates how to add a Text, Date, Number, or Yes/No type custom iProperty.


' ---------------------start of iLogic code---------------------

Dim propertyName1 As String = "Paint Code"
Dim propertyName2 As String = "Size"
Dim propertyName3 As String = "Galvanized"
Dim propertyName4 As String = "Completion Date"

'define custom prp[erty collection
oCustomPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")

Try
'set property value
oProp = oCustomPropertySet.Item(propertyName1)
Catch
' Assume error means not found so create it
oCustomPropertySet.Add("", propertyName1)
End Try

Try
'look for property
oProp = oCustomPropertySet.Item(propertyName2)
Catch
' Assume error means not found so create it
oCustomPropertySet.Add("", propertyName2)
End Try

Try
'look for property
oProp = oCustomPropertySet.Item(propertyName3)
Catch
' Assume error means not found so create it
oCustomPropertySet.Add("", propertyName3)
End Try

Try
oProp = oCustomPropertySet.Item(propertyName4)
Catch
' Assume error means not found so create it
oCustomPropertySet.Add("", propertyName4)
End Try

Dim strPaintCode As String
Dim dblSize As Double
Dim blGalv As Boolean
Dim dtDate As Date

'get values from user
strPaintCode = InputBox("Enter the paint code.", "iLogic", "") 'no default value
dblSize = InputBox("Enter the size.", "iLogic", 100) 'set default to 100
blGalv = InputRadioBox("Is this thing galvanized?", "Yes", "No", True, "iLogic") 'default is True (yes)
dtDate = InputBox("When was this completed?", "iLogic", Now.ToString("MM/dd/yyyy")) 'set default to today's date

'set custom property values
iProperties.Value("Custom", "Paint Code") = strPaintCode
iProperties.Value("Custom", "Size") = dblSize
iProperties.Value("Custom", "Galvanized") = blGalv
iProperties.Value("Custom", "Completion Date") = dtDate

iLogicVb.UpdateWhenDone = True

' ---------------------end of iLogic code ---------------------



Update:
August 03, 2013
Here is another version that is a bit more tidy:
 



' - - - - - - - - start of iLogic code - - - - - - - -
Dim sProp1 As String = "Destination Code"
Dim sDestCode As String
  'define custom property collection
oCustomPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")

Try
            'get current custom property value
                sDestCode = iProperties.Value("Custom", sProp1)
            'get user input
                sDestCode = InputBox("Enter the Destination Code:", "iLogic", sDestCode )
            'set custom property value
                iProperties.Value("Custom", sProp1) = sDestCode
Catch
            ' Assume error means not found so create it
                oCustomPropertySet.Add("", sProp1)
            'get user input
                sDestCode = InputBox("Enter the Destination Code:", "iLogic", sDestCode )
            'set custom property values
                iProperties.Value("Custom", sProp1) = sDestCode
End Try

iLogicVb.UpdateWhenDone = True
' - - - - - - - - end of iLogic code - - - - - - - -