Hi Martin, I don't think there's a built-in API method for checking parameter name validity. However, I wrote a couple of functions that will check if a parameter name is valid, or try to convert it to a valid name. See functions at the bottom of the post.
Here's a usage example of a program that uses the functions to test if input names are valid or can be made valid:
Sub Main()
Dim oRuleTitle As String = "Parameter Name Checker"
Dim oParamName As String = ""
Dim oInput As String = ""
Do
oInput = InputBox("Parameter Name:", oRuleTitle, oInput)
If oInput = "" Then
'User hit "Cancel"
Exit Do
Else If ParamNameIsValid(oInput) = False Then
Dim oConvertedName As String = MakeParamNameValid(oInput)
If oConvertedName = "" Then
MessageBox.Show("Could not convert name to a valid one. It may be a unit string or contain all invalid characters. Please try again.",oRuleTitle,MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
Else
MessageBox.Show("Converted to valid name: """ & oConvertedName & """",oRuleTitle,MessageBoxButtons.OK,MessageBoxIcon.Information)
oParamName = oConvertedName
End If
Else
MessageBox.Show("Name is valid.",oRuleTitle,MessageBoxButtons.OK,MessageBoxIcon.Information)
oParamName = oInput
End If
Loop
End Sub
It works by trying to create parameters with the inputted names. However, it wraps this in a transaction and aborts, it, so the document isn't actually modified.
Hope this works for you.
Functions:
Function ParamNameIsValid(oName As String) As Boolean
Dim oThisDoc As Inventor.PartDocument = ThisDoc.Document
Dim oCompDef As Inventor.PartComponentDefinition = oThisDoc.ComponentDefinition
Dim oTransaction As Inventor.Transaction = ThisApplication.TransactionManager.StartTransaction(oThisDoc,"Create dummy parameter")
Try
Dim oParam As Inventor.Parameter
Try
oParam = oCompDef.Parameters.Item(oName)
'Parameter already exists, so name is valid.
Return True
Catch
oParam = oCompDef.Parameters.UserParameters.AddByValue(oName,0,"ul")
'Parameter created, so name is valid.
oParam.Delete
Return True
End Try
Catch
'Parameter could not be created, so name is invalid.
Return False
Finally
oTransaction.Abort
End Try
End Function
Function MakeParamNameValid(oName As String) As String
Dim oThisDoc As Inventor.PartDocument = ThisDoc.Document
Dim oCompDef As Inventor.PartComponentDefinition = oThisDoc.ComponentDefinition
Dim oTransaction As Inventor.Transaction = ThisApplication.TransactionManager.StartTransaction(oThisDoc,"Create dummy parameter")
oParam = oCompDef.Parameters.UserParameters.AddByValue("dummyparam",0,"ul")
Dim oNewName As String
Dim i As Integer = 0
For Each oChar As Char In oName
i += 1
Try
Try
Dim oParam2 As Inventor.Parameter = oCompDef.Parameters.Item(oChar)
'Parameter named character exists, so character is valid.
oNewName = oNewName & oChar
Catch
If i = 1 Then
oParam.Name = oChar & oParam.Name
Else
oParam.Name = oParam.Name & oChar
End If
'Parameter renamed, so character is valid in current position.
oNewName = oNewName & oChar
End Try
Catch
'Character could not be used, so character is invalid. Replace with underscore, unless is first character.
If i = 1 Then
'Skip character and reset counter to zero.
i = 0
Else
oNewName = oNewName & "_"
End If
End Try
Next
oTransaction.Abort
If ParamNameIsValid(oNewName) Then
Return oNewName
Else
Return ""
End If
End Function