Message 1 of 9
iLogic rule to create custom user parameters on the fly in ipt and iam files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all,
I wanted to share a little rule I made to help me create user parameters in ipt and iam files on the fly. I use it as an external rule so I can make use of it in old or new files.
Whilst I'm not new to Inventor or iLogic, I am what you might call a novice at coding. This rule is mostly built from googling bits and bobs (Thanks mostly to all of you who ask the questions in here, and to those who answer them!), plus a basic bit of coding knowledge (Conditional statements, very basic vb.net,general ilogic), that got me what I needed to proceed. Now I've done it, I have a much better Idea of how to make use of the API in Inventor, so that's good!
Here's the code; Use it, change it, enjoy.
SyntaxEditor Code Snippet
Sub Main() paramselect() End Sub Function tryDoc() 'function determines and defines part or assembly document parameters odoc = ThisDoc.Document Dim oParams As Parameters If odoc.documenttype=Inventor.DocumentTypeEnum.kPartDocumentObject Then Dim oPartDoc as PartDocument = odoc Dim oPartCompDef As PartComponentDefinition = oPartdoc.ComponentDefinition oParams = oPartCompDef.Parameters ElseIf odoc.documenttype=Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then Dim oAssdoc as AssemblyDocument= odoc Dim oAssCompDef As AssemblyComponentDefinition = oAssdoc.ComponentDefinition oParams = oAssCompDef.Parameters End If Return oParams End Function Function textfix(oBVName As String) ' function modifies text to fit confines of inventor parameter names. oBVName = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(oBVName)' Capitalise all starting letters Dim returntext As String = oBVName.Replace(" ", "_") ' remove spaces and replace with underscore Return returntext End Function Sub paramselect() 'create a list parameter that displays in selection window, used to add the type of parameter required Dim iparamname As String = "iParameters" ' the created parameter name (Change it here to what ever you like) oParams = tryDoc() Dim oUserParams As UserParameters = oParams.UserParameters 'test for exsisting multivalue parameter, if not, creates it and populates it. Try oparamselect = oParams(iparamname) Catch Dim newParam As UserParameter = oUserParams.AddByValue(iparamname, "Numerical", UnitsTypeEnum.kTextUnits) MultiValue.SetList(iparamname, "Numerical", "Text", "Boolean") End Try 'display window for selecting values from the created multivalue parameter above. oSelection = InputListBox("Select one:", MultiValue.List(iparamname), _ Parameter(iparamname), "iLogic", "Pick a parameter Type:") If oSelection = "" Then Return ' Exits on window close Else oBoxValues(oSelection) 'Trigger input boxes End If End Sub Sub oBoxValues(oSelection As String) 'Define Values for Input boxes: parameter names and values Line1: Dim oBVNameFix As String Dim oBVName As String = InputBox("Enter a parameter name", "Custom Parameter", "") 'Test for empty boxes and strings starting with numbers If oBVName = "" Then MessageBox.Show("Nothing was entered. Operation Cancelled", "Error") Return Else 'Test first character for numeric character Dim oBVNameAsArray = oBVName.ToCharArray() If IsNumeric(oBVNameAsArray(0)) Then MessageBox.Show("Parameters must start with text", "Error") Goto Line1 'If starts with number, re-enter value in box Else oBVNameFix = textfix(oBVName) 'String is sent to be corrected to inventor acceptable format 'Defines Conditions for Adding a value to the parameter If oSelection = "Numerical" Then question1 = MessageBox.Show("Add a value to the parameter?", "iLogic",MessageBoxButtons.YesNo) If question1 = vbYes Then Line2: oBoxParamVal = InputBox("Add a Value then", "iLogic", "") 'Check numeric text is actually numeric, or for empty string If oBoxParamVal = "" Then oBoxParamVal = 1 Else Dim oBoxParamValAsdouble As Double If Double.TryParse(oBoxParamVal,oBoxParamValAsdouble)Then Else MessageBox.Show("You must enter a number", "iLogic Error") Goto Line2 End If End If ElseIf question1 = vbNo Then oBoxParamVal = 1 End If ElseIf oSelection = "Text" Then question1 = MessageBox.Show("Add a value to the parameter?", "iLogic",MessageBoxButtons.YesNo) If question1 = vbYes Then oBoxParamVal = InputBox("Add a Value then", "iLogic", "") ElseIf question1 = vbNo Then oBoxParamVal = "Empty" End If End If End If End If 'Triggers correct subroutine and passes parameters through If oSelection = "Numerical" Then numparam(oBVNameFix,oBoxParamVal) ElseIf oSelection = "Text" Then textparam(oBVNameFix,oBoxParamVal) ElseIf oSelection = "Boolean" Then booleanparam(oBVNameFix) End If End Sub Sub numparam(oBVNameFix As String,oBoxParamVal As String) oParams = tryDoc() 'Identifies ipt or iam Dim oUserParams As UserParameters = oParams.UserParameters 'Identifies user parameters in doc Dim newParam As UserParameter = oUserParams.AddByExpression(oBVNameFix, oBoxParamVal, "mm") ' Creates Custom parameter orepeat() End Sub Sub textparam(oBVNameFix As String,oBoxParamVal As String) oParams = tryDoc()'Identifies ipt or iam Dim oUserParams As UserParameters = oParams.UserParameters 'Identifies user parameters in doc Dim newParam As UserParameter = oUserParams.AddByValue(oBVNameFix, oBoxParamVal, "Text") ' Creates Custom parameter orepeat() End Sub Sub booleanparam(oBVNameFix As String) oParams = tryDoc()'Identifies ipt or iam Dim oUserParams As UserParameters = oParams.UserParameters 'Identifies user parameters in doc Dim newParam As UserParameter = oUserParams.AddByValue(oBVNameFix,True,"BOOLEAN") ' Creates Custom parameter orepeat() End Sub Sub orepeat() 'repeat add parameters questionAdd = MessageBox.Show("Create another parameter?", "iLogic",MessageBoxButtons.YesNo) If questionAdd = vbYes Then paramselect() ' Go back to selecting a parameter ElseIf questionAdd = vbNo Then Return End If End Sub