Drop down box in API

Drop down box in API

paul.slaterMZZFE
Advocate Advocate
1,990 Views
9 Replies
Message 1 of 10

Drop down box in API

paul.slaterMZZFE
Advocate
Advocate

Hi -I am approx 4 hrs into API's and I want to do some simple user parameter manipulation.

I have copied the below script from a Brian Ekins post back in 2015

 

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

def run(contextšŸ˜ž
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        des = adsk.fusion.Design.cast(app.activeProduct)
        userParams = des.userParameters
        
        if ui.messageBox('Change to configuration one?''Parameter Edit', adsk.core.MessageBoxButtonTypes.YesNoButtonType, adsk.core.MessageBoxIconTypes.QuestionIconType) == adsk.core.DialogResults.DialogYes:
            userParams.itemByName('Length').expression = '50'
            userParams.itemByName('Height').expression = '30'
        else:
            userParams.itemByName('Length').expression = '30'
            userParams.itemByName('Height').expression = '20'
            
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))   
   

unfortunately I am getting an error " 'none type' object has no attribute expression"
FYI I have created the two parameters in Fusion user parameters as in the above and made them favourites (not sure if that matters)


Your help would be greatly appreciated. Kind regards Paul

 

 

 

0 Likes
Accepted solutions (2)
1,991 Views
9 Replies
Replies (9)
Message 2 of 10

zachesenbock
Contributor
Contributor

Have you printed a list of UserParameters to make sure that "Length" and "Height" are valid? The error looks like it is caused by the itemByName method not returning a valid parameter.

 

for param in userParams:
    print(param.name)
0 Likes
Message 3 of 10

paul.slaterMZZFE
Advocate
Advocate

zachesenbock -thank you for your response. As I mentioned I am right at the very beginning of the learning curve, do I start another blank script and just put only the code in that you have posted? FYI I have used the two variables to dimension a rectangle in a sketch and they work fine.  Thanks Paul

0 Likes
Message 4 of 10

zachesenbock
Contributor
Contributor

 

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

def main():
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        

# REST OF THE CODE HERE except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) main()

 

I would personally put that code in a new "testing" script. It's also good practice to use the framework above (or something similar) to make it easier to find bugs in your code.

 

Also I noticed that the code in the original post was not formatted to create a drop down menu. The Fusion API has the capability of creating a drop down menu on its own, but I personally use/recommend going with the Python library tkinter for most inputs that don't require manual selection in the Fusion user interface (selecting models, sketches, lines, etc.)

 

from tkinter import *

def backBone(root):
# gather the input and destroy the window
desiredFont = textStr.get()
runMultiple = mpCheck.get()
root.destroy()
availableFonts = ["Romans.shx","Yada","Yada"," Another Option"] root = Tk() # use width x height + x_offset + y_offset (no spaces!) root.geometry("%dx%d+%d+%d" % (250, 125, 1400, 300)) root.title("Options") root.configure(bg="DeepSkyBlue") mpCheck = IntVar() Checkbutton(root, text="- Run Multiple Plates", variable=mpCheck, bg="DeepSkyBlue").grid(row=2, column=2, padx=(60, 0), pady=(20, 20)) textStr = StringVar(root) # initial value textStr.set('Romans.shx') option = OptionMenu(root, textStr, *availableFonts) option.config(bg = "DeepSkyBlue") # Set background color to green option.grid(row=3, column=2, padx=(0, 10), pady=(0, 10)) # when the button is pressed the function backBone is called with root being passed as a parameter (used to kill the window later on) button = Button(root, text="Run", command=lambda:backBone(root), bg="DeepSkyBlue") button.grid(row=3, column=4, padx=(0, 10), pady=(0, 10)) root.mainloop()

Here's just a simple (and pretty visually unappealing) UI I made for one of my add-ins. Feel free to adjust it to your liking. If you want to have your inputs be both height and length in one drop down option...

 

options = ["30/40","40/50"]

# get the selected string ex. "30/40"
selectedOption = inputVar.get()

# Split the string with the slash
selectedOption = selectionOption.split("/")

# This will return ["30","40"]
# Allowing you to do something like this.

height = selectedOption[0] # The first number in the list
length = selectedOption[1] # Second item

# The "/" could be replaced with anything

 

Message 5 of 10

paul.slaterMZZFE
Advocate
Advocate

zachesenbock -thank you. I will try this tomorrow (I am in UK) I will need time to try and digest the data as  I am not a coder and it is all new to me. Thank you fore your help. Kind regards Paul.

0 Likes
Message 6 of 10

Nilesh.Mohite
Autodesk
Autodesk
Accepted solution

Hi @paul.slaterMZZFE ,

Your script works fine if you have added the parameters to the Fusion User parameter box as shown in screenshot attachedScreenshot 2019-11-28 at 10.07.32 AM.png

You can try with the test model attached 


Nilesh Mohite
LinkedIn

If my answer helped you, please use  ACCEPT SOLUTION .
Also be generous with Likes!  Thank you and enjoy!

0 Likes
Message 7 of 10

paul.slaterMZZFE
Advocate
Advocate
Accepted solution

Nilesh -thank you. I realised this morning what I had done, embarasingly I had not used CAPITALS on the user  parameter name definitions. Kind regards Paul.

Message 8 of 10

Nilesh.Mohite
Autodesk
Autodesk

You are welcome @paul.slaterMZZFE .

Please let us know if you need more help.


Nilesh Mohite
LinkedIn

If my answer helped you, please use  ACCEPT SOLUTION .
Also be generous with Likes!  Thank you and enjoy!

0 Likes
Message 9 of 10

paul.slaterMZZFE
Advocate
Advocate

Nilesh -the file as promised.

Kind regards Paul.

0 Likes
Message 10 of 10

brad.bylls
Collaborator
Collaborator

It was reported in another thread that Tkinter does not work well on a Mac machine and it would be advisable to just use the API if you are going to make it available to everyone. 

Brad Bylls
0 Likes