VB.net If Parameter exists do nothing

VB.net If Parameter exists do nothing

j.romo
Advocate Advocate
745 Views
8 Replies
Message 1 of 9

VB.net If Parameter exists do nothing

j.romo
Advocate
Advocate

Hello again, here I am struggling with another part of my code.

I can succesfully change the parameter name of a dimension but if the parameter name allready exists it crashes on me, cant figure out how to prevent that.

here is my code

  Public Sub ChangeAngleName(ThisApplication As Inventor.Application)
        'Declare document Active
        Dim oDoc As PartDocument
        oDoc = ThisApplication.ActiveDocument
        Dim oNumber As String
        oNumber = "Bend_" & txtbName1.Text
        If oNumber = "" Then
            MsgBox("Write a Bend Number")
        Else
            Dim oDim As Object
            oDim = oDoc.SelectSet(1)
'here it crashes if parameter name allready exists
            oDim.Parameter.Name = oNumber


        End If
0 Likes
Accepted solutions (1)
746 Views
8 Replies
Replies (8)
Message 2 of 9

Michael.Navara
Advisor
Advisor
Accepted solution

Yes you can. This sample show how to check if the parameter exists

 

Dim part As PartDocument = ThisDoc.Document

'Check if parameter exists
Dim paramName As String = "MyParam"
Dim params As Parameters = part.ComponentDefinition.Parameters
Try
	params(paramName)
	'Parameter exists

Catch
	'Parameter NOT exists

End Try
Message 3 of 9

j.romo
Advocate
Advocate

Thank you It worked like a charm even the same Try Catch Arg helps me if the user diesnt select a Dimension

the code

blic Sub ChangeAngleName(ThisApplication As Inventor.Application)
        'Declare document Active
        Dim oDoc As PartDocument
        oDoc = ThisApplication.ActiveDocument
        Dim oNumber As String
        oNumber = "Bend_" & txtbName1.Text
        If oNumber = "" Then
            MsgBox("Write a Bend Number")
        Else
            Dim oDim As Object
            Dim oParam As String = oNumber
            Dim oParams As Parameters = oDoc.ComponentDefinition.Parameters

            Try
                oDim = oDoc.SelectSet(1)
                oDim.Parameter.Name = oNumber
            Catch
                MsgBox("This Parameter already exists or select a Dimension")
                Exit Sub
            End Try
        End If
    End Sub
0 Likes
Message 4 of 9

Matthew_Policelli
Advocate
Advocate

Two things:

 

1. Wouldn't the parameter always have a name? I.e. if it's not renamed, it would be "d8" or something right? What do you get if instead of trying to rename the parameter, you just do a message box with the parameter name?

 

2. The way you have this written, the If statement would never be true, because even if your txtbName1.Text is blank, your oNumber will still be: Bend_. I don't know what you're trying to do with this, but would it work better if instead of checking that oNumber is not blank, you check that txtbName1.Text is not blank?

oNumber = "Bend_" & txtbName1.Text
        If oNumber = "" Then
            MsgBox("Write a Bend Number")
        Else

 3. As for actually getting the parameter to rename, try replacing line 13 with these 2 lines:

oDoc.ComponentDefinition.Parameters(oDim.Parameter.Name).Name = oNumber 'rename dimension name to oNumber
oDoc.Update2(True) 'update so that renamed dimension displays

 

Message 5 of 9

j.romo
Advocate
Advocate

Ill give it a try, but strangely is working as intended maybe I will see a bug later. Ill keep you posted 

thank you very much

0 Likes
Message 6 of 9

j.romo
Advocate
Advocate

You were right I changed to if the textbox is empty then send the error message.

but if I use your code on the rename it doesnt work.

 

 'Declare document Active
        Delete_list(ThisApplication)
        Dim oDoc As PartDocument
        oDoc = ThisApplication.ActiveDocument
        Dim oNumber As String
        oNumber = "Bend_" & txtbName1.Text

        If txtbName1.Text = "" Then
            MsgBox("Write a Bend Number")
        Else
            Dim oDim As Object
            Dim oParam As String = oNumber
            Dim oParams As Parameters = oDoc.ComponentDefinition.Parameters

            Try
                oDim = oDoc.SelectSet(1)
                oDim.Parameter.Name = oNumber
                'SE AGREGARON ESTAS DOS LINEAS
                'oDoc.ComponentDefinition.Parameters(oDim.Parameter.Name).Name = oNumber 'rename dimension name to oNumber
                'oDoc.Update2(True) 'update so that renamed dimension displays
                Refill_list(ThisApplication)
            Catch
                MsgBox("This Parameter already exists or select a Dimension")
                Exit Sub
            End Try

 

0 Likes
Message 7 of 9

Matthew_Policelli
Advocate
Advocate

Can you give a screenshot of what you're trying to do exactly? Both what you're trying to rename, and what you want the user to select.

0 Likes
Message 8 of 9

j.romo
Advocate
Advocate

Here is how is working at the moment

https://autode.sk/3T4hjiD

I woul like to know how to select only angular dimensions right now i can select any dimension an it would change the name but not populate the check list box.

0 Likes
Message 9 of 9

wilson.broeksema
Contributor
Contributor

sorry, wrong topic

0 Likes