Hey Ladies and Gents, just wondering is anyone else had this issue and how they might have resolved it:
This crashes Inventor at this line: oParams(i).Expression = ... only when oParams(i) is a text type user parameter, works for numeric
for example in module:
...
UpdateTextParameter("DOC_TYPE", "PIPING_ASSEMBLY", PlumbingAssemblyTemp)
...
Function UpdateTextParameter(ByVal ParameterName AsString, ByVal NewExpression AsString, OptionalByVal SpecdDoc As Inventor.Document = Nothing)
If GetINV() = FalseThenReturn 0 'Gets an active Inventor instance
Dim oDoc As Inventor.Document = Nothing
If IsNothing(SpecdDoc) = True Then
oDoc = oINV.ActiveEditDocument
EndIf
If IsNothing(SpecdDoc) = False Then
oDoc = SpecdDoc
EndIf
Dim oParams As Inventor.Parameters = oDoc.ComponentDefinition.Parameters
If IsNothing(NewExpression) = TrueThen
msgbox("The new expression to be entered into the parameter """ & ParameterName & """ is null which would crash Inventor. Skipping operation.") 'I've found this issue out already
Return 0
EndIf
For i = 1 To oParams.Count
If UCase(oParams(i).Name) = UCase(ParameterName) Then
oParams(i).Expression = """" & NewExpression & """" 'crashes here always, I've also tried oParams(i).Expression = NewExpression to the same affect
Return 0
EndIf
If i = oParams.Count Then
msgbox("A parameter named: " & ParameterName & " was not found in the active document.")
EndIf
Next
Return 0
EndFunction
I got this figured, oddly enough, if i put
oParams(i).Value = NewExpression
instead of
oParams(i).Expression = NewExpression
it works and doesn;t crash inventor
The reason is the expression of a Parameter with Text units must evaluate to a text expression. For instance:
Expression = "2.0 in" <- String value that gets evaluated to a double with the Value = 5.08.
Expression = "\"Some Text \"" <- String value that evaluates to a string with the Value = "Some Text". (Notice the escaped quotes in the expression.)
It can be confusing but you have to remember whether you're working with the string value or an expression that evaluates to a string.