VB.net Output combobox value to inventor Model parameters

VB.net Output combobox value to inventor Model parameters

BromanSillito
Advocate Advocate
1,231 Views
8 Replies
Message 1 of 9

VB.net Output combobox value to inventor Model parameters

BromanSillito
Advocate
Advocate

Hey,

 

I'm creating a form in VB.net that controls parameters in an Inventor 3D model. Here is the sub routine I am using to pass the parameters:

 

 

Public Sub ChangeParameter(paramName As String, setValue As Double)

'Make Sure Inventor is Running

CheckInventor()

Try

'set the flag to false

Dim paramFound As Boolean = False

Dim oParameter As Parameter = Nothing

Dim oParameters As Inventor.Parameters = oInventorApp.ActiveDocument.ComponentDefinition.Parameters

'loop through all of the parameters

For Each oParameter In oParameters

If UCase(paramName) = UCase(oParameter.Name) Then

'set the flag

paramFound = True

'change the parameter and make sure it is specified in inches

oParameter.Expression = System.Convert.ToString(setValue) & " in"

'update the drawing

oInventorApp.ActiveDocument.Update()

'since the parameter was found, exit the loop

Exit For

End If

Next

'alert the user if the specified parameter was not found

If paramFound = False Then

Beep()

MsgBox("Parameter name: " & paramName & " was not found.", vbOKOnly, "Parameter not found")

End If  

Catch ex As Exception

'alert the user if there is a problem

MsgBox(ex.Message & -"Error: ChangeParameter")

End Try

End Sub

 

 

 

I am accessing the sub-routine from the form using the following:

 

 

 

Private Sub cboChmbrSzChmbr_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboChmbrSzChmbr.SelectedIndexChanged

'Update Chamber Size in Inventor

CheckInventor()

ChangeParameter("SHELL_PIPE_SIZE", cboChmbrSzChmbr.SelectedItem)

End Sub

 

 

 

Here is the sub used to check if Inventor is open:

 

 

 

Public Sub CheckInventor()

oInventorApp = GetObject(, "Inventor.Application")

If Err.Number Then

MsgBox("Inventor must be running.")

End

End If

If oInventorApp.Documents.Count = 0 Then

MsgBox("An assembly document must be active.")

End

End If

'if you get to this point, then make the active document the

'oassemblyDocument

oAssemblyDocument = oInventorApp.ActiveDocument

 

 

 

 

However, I get the following error when I run the program & select an item from the cboChmbrSzChmbr combobox:

 

combo error.JPG

 

 

Can anyone help me out with this?

0 Likes
Accepted solutions (1)
1,232 Views
8 Replies
Replies (8)
Message 2 of 9

ekinsb
Alumni
Alumni

The problem is that the value you're getting back from the cboChmbrSzChmbr.SelectedItem call is a String and the setValue argument in your ChangeParameter Sub is typed as a Double.  You can also simplify how you find the parameter.  Below is a new version of your Sub.

 

Public Sub ChangeParameter(paramName As String, setValue As Double)
    'Make Sure Inventor is Running
    CheckInventor()
    Try
        Dim oParameters As Parameters = oInventorApp.ActiveDocument.ComponentDefinition.Parameters

        ' Get the parameter.
        Dim oParameter As Parameter
        Try
            oParameter = oParameters.Item(paramName)
        Catch ex As Exception
            Beep()
            MsgBox("Parameter name: " & paramName & " was not found.", vbOKOnly, "Parameter not found")
            Return
        End Try

        ' Change the parameter using inches as the unit.
        oParameter.Expression = setValue & " in"

        'update the drawing
        oInventorApp.ActiveDocument.Update()
    Catch ex As Exception
        'alert the user if there is a problem
        MsgBox(ex.Message & " - Error: ChangeParameter")
    End Try
End Sub

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 9

BromanSillito
Advocate
Advocate

Thank you for the response ekinsb! I tried replacing my code with the code that you posted & running the program, but now I get the following error when picking an item from the combobox:

 

combo error2.JPG

 

Do you have any other thoughts?

 

 

0 Likes
Message 4 of 9

BromanSillito
Advocate
Advocate
I forgot to mention, the Model I am modifying is an Assembly document. Not sure if that helps.
0 Likes
Message 5 of 9

ekinsb
Alumni
Alumni

I was having a similar problem when I was first testing your code.  I couldn't see any problems and finally just retyped that line and then it worked.  I think maybe there is an invisible bad character somehow that's resulted from copying and pasting from the discussion group.

 

Try retyping the line below and see if that gets rid of the problem.

 

Dim oParameters As Parameters = oInventorApp.ActiveDocument.ComponentDefinition.Pa​rameters


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 6 of 9

BromanSillito
Advocate
Advocate

Thank you again for the help, ekinsb. I retyped that line and even tried re-creating the whole module and re-typed everything in, but I the following error shows up now:

 

combo error3.JPG

 

Do you have any ideas why?

0 Likes
Message 7 of 9

ekinsb
Alumni
Alumni

That's different than the previous error so I suspect what you did fixed that.  That error's getting displayed when anything wrong happens in that function so I don't know which line it's complaining about.  You can step through your code to see where it jumps to the catch handler.  Just put a break point at the top of that function and then run your program and once it hits the break point you can start stepping through a line at a time.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 8 of 9

BromanSillito
Advocate
Advocate

Okay, I tried stepping through it and this is where the error occurred:

 

combo error4.JPG

 

 

One thing I hadn't realized is that the parameter in Inventor that I'm modifying is a "Text" parameter and it is unitless, so I modified the following parts of the code:

 

Public Sub ChangeParameter(paramName As String, setValue As String)

 

'and...

 

oParameter.Expression = setValue '& " in"

 

 

This produced the same errors as before, however.

 

 

0 Likes
Message 9 of 9

BromanSillito
Advocate
Advocate
Accepted solution

Okay, after doing some searching on how to handle "Text" type properties, I found that I have to change this:

 

'Change the parameter using inches as the unit.

oParameter.Expression =

 

 

 

to this:

 

'Change the parameter using inches as the unit.

oParameter.Value =

 

 

This seems to fix the problem & update the Text parameter as desired. Thanks for all the help ekinsb!