UserParameter - Set Value

UserParameter - Set Value

NachoShaw
Advisor Advisor
255 Views
2 Replies
Message 1 of 3

UserParameter - Set Value

NachoShaw
Advisor
Advisor

Hey

 

i am trying to update a parameter value. This appeared to work for a long time but now no longer works (Unless it never it worked and i only assumed it did!)

 

Anyway.

 

_BMDef is the Document ComponentDefinition

 

My function

 

 

        Friend Sub SetParams(ByVal ParamName As String, ByVal ParamValue As String)
            Dim Params As UserParameters = CType(_BMDef.Parameters.UserParameters, UserParameters)
            Dim P As UserParameter = Params.Item(ParamName)
            P.Expression = ParamValue
        End Sub

 

 

I pass in the parameter name & value to update

 

The error is on this line

 

Dim P As UserParameter = Params.Item(ParamName)

 

 

I am getting a System.ArgumentException

{"The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARGS))"}

 

The error would indicate the parameter probably doesnt exist but it does. Its the correct document with a valid existing parameter.

 

Is my code correct to access & update a user parameter?

 

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
256 Views
2 Replies
Replies (2)
Message 2 of 3

basautomationservices
Advocate
Advocate

Hi Nachito,

 

Just trying it quickly in iLogic in Inventor 2022 build 153, and it seems to work with following code. What version are you on? 

 

Dim _BMDef = ThisDoc.Document.ComponentDefinition
Dim Params As UserParameters = CType(_BMDef.Parameters.UserParameters, UserParameters)
Dim P As UserParameter = Params.Item("TestParameter")
P.Expression = "10 mm"

 

That error is indeed caused because the parameter cannot be found.. Are you sure the param is there and it is a user param? Could you upload the document?

 

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes
Message 3 of 3

tonythm
Advocate
Advocate

Please ref:

Sub UpdateParam()
    Dim oWkbk As Workbook
    Set oWkbk = ThisWorkbook
    Dim oSheet As Worksheet
    Set oSheet = oWkbk.ActiveSheet
    Dim oCell As Range
    oLastRow = oSheet.Cells(oSheet.Rows.Count, 1).End(XlDirection.xlUp).Row
    Dim oInv As inventor.Application
    Set oInv = GetObject(, "Inventor.Application")
    Dim oDoc As Document
    Set oDoc = oInv.ActiveDocument
    Dim partDoc As PartDocument
    Set partDoc = oInv.ActiveDocument
    For Each oCell In Range("A3:A" & oLastRow)
        Dim userParams As UserParameters
        Set userParams = partDoc.ComponentDefinition.Parameters.UserParameters
        Dim oUserParam As UserParameter
        For Each oUserParam In userParams
            If oUserParam.name = oCell.Value Then
            oUserParam.Expression = oCell.Offset(0, 1).Value
            End If
        Next
    Next
    MsgBox "Done!"
End Sub

levanthongeng_0-1659500179224.png

 

0 Likes