Creating parameters with script problem

Creating parameters with script problem

brad.bylls
Collaborator Collaborator
1,812 Views
7 Replies
Message 1 of 8

Creating parameters with script problem

brad.bylls
Collaborator
Collaborator

My first try at scripting so please bear with me.

I have gone through the forum and tried lots of different examples, but nothing seem to work.

I am reading a csv file and then trying to create some user parameters.

I am reading the file ok and get the correct values, but when I try to create the parameters, BOOM:

 

 
 
 
This is the code in question:
cnt = 0
file = open('C:/Users/bradb/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Scripts/SHCStest/SHCS-IMPERIAL.csv')
for line in file:
   # Get the values from the csv file.
    pieces = line.split(',')

    if pieces[0] == '1/2-13':
        ScrewDiameter = pieces[1]
        HeadDiameter = pieces[2]
        HexSize = pieces[4]
        TapDrill = pieces[5]
        HeadFillet = pieces[6]            
        
        # Create user parameters
        # Set the parameters.

        ScrewDiameterParam = design.userParameters
        ScrewDiameterParam.add('ScrewDiameter', ScrewDiameter, 'in''')
 
Being new I am not sure how to track down the proper syntax, arguments, etc  in the documentation.
Any help is appreciated.
Thank you in advance.
 
Brad
Brad Bylls
0 Likes
Accepted solutions (1)
1,813 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

Assuming that the ScrewDiameter is a number, you need to use ValueInput.createByReal to create the value that you use to save the parameter. You can’t store the number directly.


Something like:

 

ScrewDiameterParam.add('ScrewDiameter', ValueInput.createByReal(ScrewDiameter), 'in', '')

 

Here’s some code that I’m using for something similar.

0 Likes
Message 3 of 8

brad.bylls
Collaborator
Collaborator

There was supposed to be a picture showing the error message. Here is the error:

 

in method 'UserParameters_add', argument 3 of type 'adsk::core::Ptr< adsk::core::ValueInput > const &'
File "c:\users\bradb\appdata\local\autodesk\webdeploy\production\d2d901e2ef5ae5c384b4ac001a48c4e37589dc43\...", line 31754, in add return _fusion.UserParameters_add(self, *args) File "c:\Users\bradb\AppData\Roaming\Autodesk\Autodesk Fusion 360\API\Scripts\SHCStest\SHCStest.py", line 30, in <module> ScrewDiameterParam.add('ScrewDiameter', ScrewDiameter, 'in', '')
Brad Bylls
0 Likes
Message 4 of 8

brad.bylls
Collaborator
Collaborator

Thank you.

I tried it, but an error message said 'ValueInput not created'

I also looked at your code and found nothing similar.

I am a newbe and must be missing something.

Brad Bylls
0 Likes
Message 5 of 8

Anonymous
Not applicable

I wasn't thinking when I replied to you. I'm guessing that when you are reading the file, the ScrewDiameter is being read as a string, in which case, you need to use ValueInput.createByString instead of createByReal.

 

Try this:

 

cnt = 0
file = open('C:/Users/bradb/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Scripts/SHCStest/SHCS-IMPERIAL.csv')
for line in file:
   # Get the values from the csv file.
    pieces = line.split(',')

    if pieces[0] == '1/2-13':
        ScrewDiameter = pieces[1]
        HeadDiameter = pieces[2]
        HexSize = pieces[4]
        TapDrill = pieces[5]
        HeadFillet = pieces[6]            
        
        # Create user parameters
        # Set the parameters.

        ScrewDiameterParam = design.userParameters
        ScrewDiameterParam.add('ScrewDiameter', ValueInput.createByString(ScrewDiameter), 'in', '')
 
0 Likes
Message 6 of 8

brad.bylls
Collaborator
Collaborator
Accepted solution

That didn't work, but I finally tracked down some code that did.

https://help.autodesk.com/view/fusion360/ENU/?caas=caas/discussion/t5/Fusion-360-API-and-Scripts/csv...

Thank you for the input.

Brad Bylls
0 Likes
Message 7 of 8

MattPerez314
Advocate
Advocate

Since this was the closest answer to a question I struggled with I thought I would add to it.  The api example reading from the CSV works if you want to do that, but I was wanting to just create a parameter directly.  Without getting into user inputs here is the basic code.

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        design = adsk.fusion.Design.cast(app.activeProduct)

        #retParam = design.userParameters.itemByName('Size')
        #ui.messageBox(retParam.expression)
        widthValueInput = 5.0
        inputUnits = 'mm'
        widthValuePass = widthValueInput / 10
        widthValue = adsk.core.ValueInput.createByReal(widthValuePass)
        design.userParameters.add('width',widthValue,inputUnits,'')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

  I left the retParam in there commented out because I used that to find if a parameter was in the design and then I returned the expression from that back to the user.  I thought it might be helpful for anyone trying to do this.  I did leave an input value(even though the code isn't prompting the user) and the widthValuePass is converting it from cm to mm before using ValueInput.createByReal.

 

Hope it helps someone!

0 Likes
Message 8 of 8

brad.bylls
Collaborator
Collaborator

Thanks for the input.

This is what I ended up doing.

# Create user parameters
	userParams = des.userParameters
	MOLDparam = userParams.add('Mold_Exists', adsk.core.ValueInput.createByString('Exists'), '', 'Exists')
	TCPparam = userParams.add('TCP_thickness', adsk.core.ValueInput.createByReal(numPlateTCPthickness), 'in', 'TCP Plate Thickness')
	Aparam = userParams.add('A_thickness', adsk.core.ValueInput.createByReal(numPlateAthickness), 'in', 'A Plate Thickness')

 And then used this to update the model parameters using the User Parameters

# Modify model parameters
	# TCP plate
	extent_1 = plateTCPext.extentOne
	extent_2 = plateTCPext.extentTwo
	distanceMP_1 = extent_1.distance
	distanceMP_2 = extent_2.distance
	distanceMP_1.expression = 'TCP_thickness + A_thickness'
	distanceMP_2.expression = 'A_thickness * -1'
Brad Bylls
0 Likes