Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VB.Net API find user parameter

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
j.romoYDW7Q
235 Views, 2 Replies

VB.Net API find user parameter

Hello I want to add some user parameters but if the parameter exists skip the creation of such parameters

My code is like this

 Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
     Dim userParams As UserParameters = oPartDoc.ComponentDefinition.Parameters.UserParameters
     Dim newParam As UserParameter ' Placeholder
     Dim oFormat As CustomPropertyFormat
     Dim oParam As Parameters
     Dim oCantidad As String = cantidadTXTBox.Text

     If oCantidad Is Nothing Then
         MsgBox("Poner numero de piezas en textbox")
         Exit Sub
     End If
     'LENGTH
     Try
         oParam = oPartDoc.ComponentDefinition.Parameters.UserParameters("Cantidad")

     Catch   'If the parameter was not found, then create a new one.
         newParam = userParams.AddByExpression("Cantidad", oCantidad, "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.kZeroDecimalPlacePrecision 'Set one decimal place
         'oFormat.Units="mm" 'Units
         oFormat.ShowUnitsString = False
         oFormat.ShowLeadingZeros = False
         oFormat.ShowTrailingZeros = False

         MsgBox("Cantidad agregada")

     End Try
 End Sub

but it creates the parameter if it doesent exist but if it exists it creates a nother one the try catch is not working.

"By the way is there another way to create parameters easier?.

 

thanks in advance

 

 Screenshot 2024-03-26 131939.jpg

2 REPLIES 2
Message 2 of 3
A.Acheson
in reply to: j.romoYDW7Q

Hi @j.romoYDW7Q 

Your issue begins in the Try statement. oParam is declared as parameters collection so more than 1. Then in the try statement you set oParam as a single parameter. Silly Example userParams is the dozen eggs where as userParam is the egg. 

 

Also you can be more precise and declare oParam as a userParameter

Incorrect

Dim oParam As Parameters
     Try
         oParam = oPartDoc.ComponentDefinition.Parameters.UserParameters("Cantidad")
     Catch
         newParam = userParams.AddByExpression("Cantidad", oCantidad, "mm") ' Create the Parameter as per above
         
End Try

 Correct

     Dim userParams As UserParameters = oPartDoc.ComponentDefinition.Parameters.UserParameters
     Dim userParam As UserParameter ' Placeholder
     Dim oCantidad As String = cantidadTXTBox.Text

     Try
         userParam = userParams.Item ("Cantidad")

     Catch   'If the parameter was not found, then create a new one.
         userParam = userParams.AddByExpression("Cantidad", oCantidad, "mm") ' Create the 

     End Try

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
Message 3 of 3
j.romoYDW7Q
in reply to: A.Acheson

thanks @A.Acheson 

The solution was so simple

 

Kudos Man

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report