Create a User parameter using VB.net in a API

Create a User parameter using VB.net in a API

j.romo
Advocate Advocate
457 Views
2 Replies
Message 1 of 3

Create a User parameter using VB.net in a API

j.romo
Advocate
Advocate

Hello Guys Im finally moving from Ilogic to VB.net and I have a problem that I cant find a sloution.

Im trying to use this code but keep getting the Null error. can you guys help me undertand the problem?

 Public Sub SampleCommandFunction2()
        Dim oPartDoc As PartDocument = ThisApplication
        Dim userParams As UserParameters = oPartDoc.ComponentDefinition.Parameters.UserParameters
        Dim newParam As UserParameter ' Placeholder
        Dim oFormat As CustomPropertyFormat
        Dim oParam As Parameters

        'LENGTH
        Try
            oParam = oPartDoc.ComponentDefinition.Parameters("LENGTH")
        Catch   'If the parameter was not found, then create a new one.
            newParam = userParams.AddByExpression("LENGTH", 0, "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
        End Try
End Sub
0 Likes
Accepted solutions (1)
458 Views
2 Replies
Replies (2)
Message 2 of 3

A.Acheson
Mentor
Mentor
Accepted solution

Your part document object has no document reference. It should be ThisApplication.ActiveDocument or you can use the ilogic version ThisDoc.Document if your running it in event triggers.

Your oParam object  is referenced to the parameters collection "Parameters" . Either rewrite it like Inventor.Parameter or [Parameter]

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 3

j.romo
Advocate
Advocate

Thank you, I have used this as a Ilogic rule and it works, but now im trying to make my first AddIn in Visual studio. and I really have a hard time passing from VBA to .Net. I have solved it by using the Inventor Application Object, you were right...

Public Sub SampleCommandFunction2()
        Dim oPartDoc As PartDocument = g_inventorApplication.ActiveDocument
        Dim userParams As UserParameters = oPartDoc.ComponentDefinition.Parameters.UserParameters
        Dim newParam As UserParameter ' Placeholder
        Dim oFormat As CustomPropertyFormat
        Dim oParam As Parameters

        'LENGTH
        Try
            oParam = oPartDoc.ComponentDefinition.Parameters("LENGTH")
        Catch   'If the parameter was not found, then create a new one.
            newParam = userParams.AddByExpression("LENGTH", 0, "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
        End Try

  Thanks 

0 Likes