Wednesday 9 July 2014

iLogic Code to create Cusom Parameters

This little piece of code was created to bulk create custom parameters.

When I start with a new project, I sometimes need to create a lot of parameters in the skeleton part/s. This can sometimes be rather time consuming, so I wrote this snippet.

'GDI iLogic to create a new User Parameter
Sub Main()
Dim oNewParam as String
Dim oPartDoc as PartDocument = ThisDoc.Document

'Get the new Parameter Name
    tNewParam = InputBox("Input Name for New Parameter" & vbNewLine & "" & vbNewLine & "add '-DEG' for Angle" & vbNewLine & "E.G: xxx-DEG", "GDI - iLogic", "") ' prompt the user for a parameter name

If tNewParam <>"" Then 'Error Check if user presses the Cancel key
'Split the name If Needed
    oNewParam = Split(tNewParam, "-") (0)

 'Check for the new parameter name
 'If found then Exit to the question of create something else.
    Try
        oParam = oPartDoc.ComponentDefinition.Parameters(oNewParam)
        i1 = MessageBox.Show(oNewParam & vbNewLine & "Parameter already exists", "GDI - iLogic", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
        Call AsktoCreateAnother ' Subroutine to create another
      
        Catch     'If the parameter was not found, then create a new one.
            If tNewParam.contains ("deg") Or tNewParam.contains ("DEG")Then 'Checks the Temp name, inputted by user
                Call DegreeSUB (oNewParam) 'Pass the Variable name to the sub
            Else
                Call mmSUB (oNewParam) 'Pass the Variable name to the sub
            'Return
            End If
        End Try
    Else 'this one comes from the cancel key press
    MessageBox.Show("Cancel key pressed", "GDI - iLogic")
    Return 'exit the code if Cancel is pressed.
    End If
End Sub

Sub DegreeSUB (ByRef oNewParam as String)
Dim oPartDoc as PartDocument = ThisDoc.Document
Dim userParams As UserParameters = oPartDoc.ComponentDefinition.Parameters.UserParameters

oValue=InputBox("Add a Value?", "GDI - iLogic", 0) ' Get a Value if desired
oComment=InputBox("Add a description if desired?", "GDI - iLogic", "") ' Get a description if desired
    Dim newParam As UserParameter ' Placeholder
    Dim oFormat As CustomPropertyFormat
    newParam = userParams.AddByExpression(oNewParam, oValue, "deg") ' Create the Parameter as per above
    newParam.ExposedAsProperty=True 'Flag for Export
    oFormat=newParam.CustomPropertyFormat 'For some reason or other this line is needed to enable the following formatting
    oFormat.PropertyType=Inventor.CustomPropertyTypeEnum.kTextPropertyType
            oFormat.Precision=Inventor.CustomPropertyPrecisionEnum.koneDecimalPlacePrecision 'Set one decimal place
            'oFormat.Units="mm" 'Units
            oFormat.ShowUnitsString=False
            oFormat.ShowLeadingZeros=False
            oFormat.ShowTrailingZeros=False
    newParam.comment=oComment 'Set the Description
    Call AsktoCreateAnother (oNewParam, oComment) ' Subroutine to add another
End Sub

Sub mmSUB (ByRef oNewParam as String)
Dim oPartDoc as PartDocument = ThisDoc.Document
Dim userParams As UserParameters = oPartDoc.ComponentDefinition.Parameters.UserParameters

oValue=InputBox("Add a Value?", "GDI - iLogic", 0) ' Get a Value if desired
oComment=InputBox("Add a description if desired?", "GDI - iLogic", "") ' Get a description if desired
    Dim newParam As UserParameter ' Placeholder
    Dim oFormat As CustomPropertyFormat
    newParam = userParams.AddByExpression(oNewParam, oValue, "mm") ' Create the Parameter as per above
    newParam.ExposedAsProperty=True 'Flag for Export
    oFormat=newParam.CustomPropertyFormat 'For some reason or other this line is needed to enable the following formatting
    oFormat.PropertyType=Inventor.CustomPropertyTypeEnum.kTextPropertyType
            oFormat.Precision=Inventor.CustomPropertyPrecisionEnum.koneDecimalPlacePrecision 'Set one decimal place
            'oFormat.Units="mm" 'Units
            oFormat.ShowUnitsString=False
            oFormat.ShowLeadingZeros=False
            oFormat.ShowTrailingZeros=False
    newParam.comment=oComment 'Set the Description
    Call AsktoCreateAnother (oNewParam, oComment) ' Subroutine to add another
End Sub

Sub AsktoCreateAnother (ByRef oNewParam as String, ByRef oComment As String)
    i2 = MessageBox.Show(oNewparam & " (" & oComment & ")" & " - Created" & vbNewLine & "Add Another", "GDI - iLogic", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
    If i2 = "6" Then
    Main 'Got to the begining
    ElseIf i2 = "7" Then
    'Return
    End If
    Return
End Sub

Sub DONEAsktoCreateAnother
    i3 = MessageBox.Show("Add Another", "GDI - iLogic", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
    If i3 = "6" Then
    Main 'Got to the beginning
    ElseIf i3 = "7" Then
    'Return
    End If
    Return
End Sub

No comments:

Post a Comment