iLogic rule to create custom user parameters on the fly in ipt and iam files

iLogic rule to create custom user parameters on the fly in ipt and iam files

lmc.engineering
Advocate Advocate
2,957 Views
8 Replies
Message 1 of 9

iLogic rule to create custom user parameters on the fly in ipt and iam files

lmc.engineering
Advocate
Advocate

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


  

 

 

2,958 Views
8 Replies
Replies (8)
Message 2 of 9

lmc.engineering
Advocate
Advocate

I'm not sure if you can edit a post in this forum, if you can, I don't know how! Just an update to allow the rule to catch special character inputs;

 

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    
    For Each illegalChar As Char In returntext
        Select Case illegalChar
            Case "!","","$","%","^","&","*","(",")","+","{","}","@","<",">","?","|","=","[","]",";","'",",",".","/","\","`","",""
            returntext = returntext.Replace(illegalChar, "")
        End Select
    Next
    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


Message 3 of 9

lmc.engineering
Advocate
Advocate

I have made another update that makes this rule feel a bit more complete.

 

You can now select metric or imperial when the rule is first run; if you need to change back, simply delete the multi-list parameter called "iParameters" from the parameters window.

 

It now allows Numerical, Angular, Unitless, Text, and Boolean.

 

I am considering adding a multi value option too, I'll see if I find the time. In the mean time, here's the better version of the above:

 

All the best.

 

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    
    For Each illegalChar As Char In returntext
        Select Case illegalChar
            Case "!","$","%","^","&","*","(",")","+","{","}","@","<",">","?","|","=","[","]",";","'",",",".","/","\","`"
            returntext = returntext.Replace(illegalChar, "")
        End Select
    Next
    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.
Dim oDimStyle As String        
    Try
    oparamselect = oParams(iparamname)
    Catch
    oStandards= InputRadioBox("Select metric or Imperial", "Metric", "Imperial", True, Title :="iLogic")
        If oStandards = True Then
        oDimStyle = "mm"            
        Else
        oDimStyle = "in"
        End If        
    Dim newParam As UserParameter = oUserParams.AddByValue(iparamname, "Numerical", UnitsTypeEnum.kTextUnits)
    MultiValue.SetList(iparamname, "Numerical","Angular","Unitless", "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,oDimStyle) 'Trigger input boxes
    End If
End Sub

Sub oBoxValues(oSelection As String,oDimStyle 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", oBVName)
    '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 = "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
            Else    
            question1 = MessageBox.Show("Add a value to the parameter?", "iLogic",MessageBoxButtons.YesNo)
                If question1 = vbYes Then
                Line2:
                oBoxParamVal = InputBox("Add a Value then", "iLogic", oBoxParamVal)
                    '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("That's not a number", "iLogic Error")
                        Goto Line2
                        End If
                    End If    
                ElseIf question1 = vbNo Then
                oBoxParamVal = 1
                End If    
            End If    
            
        End If
    End If
    'Triggers correct subroutine and passes parameters through 
    If oSelection = "Numerical" Then
    numparam(oBVNameFix,oBoxParamVal, oDimStyle)
    ElseIf oSelection = "Angular" Then
    angleparam(oBVNameFix,oBoxParamVal)
    ElseIf oSelection = "Unitless" Then
    ulparam(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,oDimStyle 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, oDimStyle) ' Creates Custom parameter
orepeat()    
End Sub

Sub angleparam(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, "deg") ' Creates Custom parameter
orepeat()    
End Sub

Sub ulparam(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, "ul") ' 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

 

Message 4 of 9

lmc.engineering
Advocate
Advocate

 Typically I'm using my rule and I find out the above doesn't quite work as intended, it gives an error if you try to input more than one parameter. The following solves that. 

 

It also allows Math functions now too!

 

Also, this thread is a bit messy with all of this script, so I apologies for that. Last update for while now, I promise 🙂

 

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    
    For Each illegalChar As Char In returntext
        Select Case illegalChar
            Case "!","$","%","^","&","*","(",")","+","{","}","@","<",">","?","|","=","[","]",";","'",",",".","/","\","`"
            returntext = returntext.Replace(illegalChar, "")
        End Select
    Next
    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)
Dim iUnits As String = "iUnits" ' the created units 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.
Dim oDimStyle As String        
    Try
    oparamselect = oParams(iparamname)    
    Catch    
    Dim newParam As UserParameter = oUserParams.AddByValue(iparamname, "Numerical", UnitsTypeEnum.kTextUnits)
    MultiValue.SetList(iparamname, "Numerical","Angular","Unitless", "Text", "Boolean")
    End Try 
    
    Try
    oUnitselect = oParams(iUnits)
    Catch
        oStandards= InputRadioBox("Select metric or Imperial", "Metric", "Imperial", True, Title :="iLogic")
        If oStandards = True Then
        oDimStyle = "mm"            
        Else
        oDimStyle = "in"
        End If 
    Dim newParam As UserParameter = oUserParams.AddByValue(iUnits, oDimStyle, "Text")
    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:")

    oUnits = Parameter(iUnits)

    If oSelection = "" Then
    Return ' Exits on window close
    Else
    oBoxValues(oSelection,oUnits) 'Trigger input boxes
    End If
End Sub

Sub oBoxValues(oSelection As String,oDimStyle 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", oBVName)
    '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 = "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
            ElseIf oSelection = "Boolean" Then
            Else    
            question1 = MessageBox.Show("Add a value to the parameter?", "iLogic",MessageBoxButtons.YesNo)
                If question1 = vbYes Then
                Line2:
                oBoxParamVal = InputBox("Add a Value then", "iLogic", oBoxParamVal)
                '----------------------------------------------------------------------------
                'Removes math characters to check if entered value is a number.     
                    If oBoxParamVal = "" Then
                    oBoxParamVal = 1
                    Else 'Removes Math characters
                    oBoxMathVal = oBoxParamVal                    
                        For Each oMathChar As Char In oBoxParamVal
                        Select Case oMathChar
                        Case "+","-","*","/","(",")"
                        oBoxMathVal = oBoxMathVal.Replace(oMathChar, "")
                        End Select
                        Next
                        Dim oBoxMathValAsdouble As Double
                        'Check numeric is actually numeric
                            If Double.TryParse(oBoxMathVal,oBoxMathValAsdouble)Then
                            Else
                            MessageBox.Show("That's not a valid Input", "iLogic Error")
                            Goto Line2
                            End If
                    End If    
                ElseIf question1 = vbNo Then
                oBoxParamVal = 1
                End If    
            End If
            '-------------------------------------------------------------------------------
        End If
    End If
    'Triggers correct subroutine and passes parameters through 
    If oSelection = "Numerical" Then
    numparam(oBVNameFix,oBoxParamVal, oDimStyle)
    ElseIf oSelection = "Angular" Then
    angleparam(oBVNameFix,oBoxParamVal)
    ElseIf oSelection = "Unitless" Then
    ulparam(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,oDimStyle 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, oDimStyle) ' Creates Custom parameter
orepeat()    
End Sub

Sub angleparam(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, "deg") ' Creates Custom parameter
orepeat()    
End Sub

Sub ulparam(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, "ul") ' 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

 

 

 

 

Message 5 of 9

Anonymous
Not applicable
Hi. I'm very new in rules but want to try and explore it. Could you tell me when your rule would be usefull? How does it help with the a work flow? Thank you
0 Likes
Message 6 of 9

lmc.engineering
Advocate
Advocate

Hi, 

 

I find this rule is particularly useful with parametric modelling, where parameters are linked and used to solve for model adjustments. An example would be when creating a sketch, I may decided that I want to have control over a particular set of dimensions, so I would run this rule, whilst in the sketch, create,name and add values to the parameters, then use the parameters to fill out the relevant dimensions. using this through-out a model gives control from my user parameters at a top level.

 

It's possible to work like this without the rule, however, adding a name, selecting unit types, then adding a value through the parameters window is slow when you need to do it for say 10 custom parameters. I can now fire off a load of them in a short space of time, and as Inventor is all about parameters, I figure this saves me time and hassle in the long run. 

 

Other things you may write a rule for would be; exporting a part/assembly to DXF,DWG,STL, etc. Printing idw's to PDF and storing them away. Auto Title blocks. Common material selector. Custom colours. The list goes on... 

 

In general though, I find the best use of iLogic is with script based modeling; giving highly configurable models that act as a kind of BIM model, but Inventor versions. Really, it's down to you how far you go with it all, but as with everything, it all requires an investment of time. Start small, mess around with things, and have fun.

 

As a side note, If you can't find help from google by searching 'iLogic' solutions, try searching for VB.net solutions instead, as iLogic is essentially just that, but with Inventor API.

 

Hope that helps, Good luck!

0 Likes
Message 7 of 9

Anonymous
Not applicable

Hi.

 

Thank you very much for your answer.

Rule is very useful after all!

 

Thanks again

0 Likes
Message 8 of 9

lmc.engineering
Advocate
Advocate

 

Alas, I was working in sheet metal and realised that this rule didn't work in that environment... so I changed it... again..

 

Now works in ipt, Iam, Sheet metal, and Weldments.

 

Now has mass parameters. In grams and lbs

 

If anyone has any suggestions, feel free to throw them at me!

 

All the best.

 

 

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.kPartDocumentObject Then
    Dim oSMdoc As PartDocument= odoc
    Dim oSMCompDef As SheetMetalComponentDefinition = oSMdoc.ComponentDefinition
    oParams = oSMCompDef.Parameters    
    ElseIf odoc.documenttype=Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
    Dim oAssdoc as AssemblyDocument= odoc
    Dim oAssCompDef As AssemblyComponentDefinition = oAssdoc.ComponentDefinition
    oParams = oAssCompDef.Parameters
	ElseIf odoc.documenttype=Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
    Dim oWMdoc as AssemblyDocument= odoc
    Dim oWMCompDef As WeldmentComponentDefinition = oWMdoc.ComponentDefinition
    oParams = oWMCompDef.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    
    For Each illegalChar As Char In returntext
        Select Case illegalChar
            Case "!","$","%","^","&","*","(",")","+","{","}","@","<",">","?","|","=","[","]",";","'",",",".","/","\","`"
            returntext = returntext.Replace(illegalChar, "")
        End Select
    Next
    Return returntext
End Function

Function MathFunction()
			question1 = MessageBox.Show("Add a value to the parameter?", "iLogic",MessageBoxButtons.YesNo)
				If question1 = vbYes Then
				Line2:
				oBoxParamVal = InputBox("Add a Value then", "iLogic", oBoxParamVal)
'---------------------------------------------------------------------------------------------------------------
				'Removes math characters to check if entered value is a number.	 
					If oBoxParamVal = "" Then
					oBoxParamVal = 1
					Else 'Removes Math characters
					oBoxMathVal = oBoxParamVal
						For Each oMathChar As Char In oBoxParamVal
        				Select Case oMathChar
            			Case "+","-","*","/","(",")"
            			oBoxMathVal = oBoxMathVal.Replace(oMathChar, "")
						End Select
						Next
						Dim oBoxMathValAsdouble As Double
						'Check numeric is actually numeric
							If Double.TryParse(oBoxMathVal,oBoxMathValAsdouble)Then
							Else
							MessageBox.Show("That's not a valid Input", "iLogic Error")
							Goto Line2
							End If		
					End If 
'---------------------------------------------------------------------------------------------------------------	
                ElseIf question1 = vbNo Then
                oBoxParamVal = 1
                End If 
				Return oBoxParamVal
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)
Dim iUnits As String = "iUnits" ' the created units 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.
Dim oDimStyle As String        
    Try
    oparamselect = oParams(iparamname)	
    Catch	
    Dim newParam As UserParameter = oUserParams.AddByValue(iparamname, "Numerical", UnitsTypeEnum.kTextUnits)
    MultiValue.SetList(iparamname, "Numerical","Angular","Unitless","Mass", "Text", "Boolean")
    End Try 
	
	Try
	oUnitselect = oParams(iUnits)
	Catch
	    oStandards= InputRadioBox("Select metric or Imperial", "Metric", "Imperial", True, Title :="iLogic")
        If oStandards = True Then
        oDimStyle = "mm"            
        ElseIf oStandards = False Then
        oDimStyle = "in"
		Else
        End If 
	Dim newParam As UserParameter = oUserParams.AddByValue(iUnits, oDimStyle, "Text")
    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:")

	oUnits = Parameter(iUnits)

    If oSelection = "" Then
    Return ' Exits on window close
    Else
    oBoxValues(oSelection,oUnits) 'Trigger input boxes
    End If
End Sub

Sub oBoxValues(oSelection As String,oUnits 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", oBVName)
    '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 = "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
			ElseIf oSelection = "Boolean" Then
            Else 
			oBoxParamVal = MathFunction()  
            End If
        End If
    End If
    'Triggers correct subroutine and passes parameters through 
    If oSelection = "Numerical" Then
    numparam(oBVNameFix,oBoxParamVal, oUnits)
    ElseIf oSelection = "Angular" Then
    angleparam(oBVNameFix,oBoxParamVal)
    ElseIf oSelection = "Unitless" Then
    ulparam(oBVNameFix,oBoxParamVal)
	ElseIf oSelection = "Mass" Then
	Massparam(oBVNameFix,oBoxParamVal, oUnits)
    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,oUnits 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, oUnits) ' Creates Custom parameter
orepeat()    
End Sub

Sub angleparam(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, "deg") ' Creates Custom parameter
orepeat()    
End Sub

Sub ulparam(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, "ul") ' Creates Custom parameter
orepeat()    
End Sub

Sub Massparam(oBVNameFix As String,oBoxParamVal As String,oUnits As String)
oParams = tryDoc() 'Identifies ipt or iam
	Dim oMassUnits As String
	If oUnits = "mm" Then
	oMassUnits = "g"
	Else
	oMassUnits = "lbmass"
	End If
Dim oUserParams As UserParameters = oParams.UserParameters    'Identifies user parameters in doc
Dim newParam As UserParameter = oUserParams.AddByExpression(oBVNameFix, oBoxParamVal, oMassUnits) ' 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

 

Message 9 of 9

cadman777
Advisor
Advisor

This is an amazing piece of work!

It's similar to what I want to accomplish.

Maybe I'll have time to scrub it up to do what I need it to do.
Thanx for posting this!

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes