Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to actually use cast function when writing add ins

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
oyvindTMNSU
408 Views, 2 Replies

How to actually use cast function when writing add ins

How to correctly use cast function to get code hints everywhere? I tried this code but when casting userInterface the program just don't recognise the message box line in the end.
Can someone please explain me the right way to this?

 

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        # ui = adsk.core.UserInterface.cast(app) // // alternative writing to get ui. This gives code hints when creating the       message box in the end of the code. But when I run the code, the message box does not show up. if I commect out this line and use ui = app.userInterface instead, the program runs, but i dont get code hints inside ui. 

        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        rootComp = design.rootComponent

        # Create sketch
        sketches = rootComp.sketches
        sketch1 = sketches.add(rootComp.xYConstructionPlane)
        sketch1.name = "test sketch"

        #draw on sketch
        # sketchCurves = adsk.fusion.SketchCurves.cast(sketch1)
        sketchCurves = sketch1.sketchCurves
        sketchCircles = sketchCurves.sketchCircles
        circle1 = sketchCircles.addByCenterRadius(adsk.core.Point3D.create(2,3,0),5)
        circle2 = sketchCircles.addByCenterRadius(adsk.core.Point3D.create(5,2,0),5)
        circleCount = sketchCircles.count
        
        ui.messageBox("Hello")

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

def stop(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('Stop addin')

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

 

Tags (2)
Labels (3)
2 REPLIES 2
Message 2 of 3
BrianEkins
in reply to: oyvindTMNSU

First, the reason the assignment to ui using cast isn't working is that you have an error in that line.  Here's your current line:

ui = adsk.core.UserInterface.cast(app)

 

and here's what it should be:

ui = adsk.core.UserInterface.cast(app.userInterface)

 

This is a good lead-in to help describe what cast does though.  It actually does two things.  First, it checks the argument to see if it is the same type as the type the cast function is being called on.  In this case, it's checking to see if the return of app.userInterface is of type adsk.core.UserInterface.  If it is, then the variable ui will have a reference to the object.  If it's not the correct type then ui will be None.

 

The second thing that cast accomplishes is what you already mentioned.  Because the cast call is describing a type, the VS Code is able to figure out that the variable ui is of the type adsk.core.UserInterface and as a result can show code hints.

 

Many of the existing samples use cast but since most of those samples were written Python has added support for type hints.  Using this new (since Python 3.5) capability in your case is:

ui: adsk.core.UserInterface = app.userInterface

 

This style of providing type hints to Python is more flexible because you can also use it to type the parameters of functions and the function return type. I think it's also easier to read because it's more like typing in other languages. This style of typing is completely ignored by the Python interpreter and it only exists to help the IDE understand the types and provides code hints.

 

Also, this style does not check that the type is correct.  For example, in your original line where you were passing in the Application object and casting it to a UserInterface object that was failing and was returning None. Using this other style, the ui variable would end up referencing the Application object. The assignment is made regardless of the type.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3
oyvindTMNSU
in reply to: oyvindTMNSU

Thank you so much @BrianEkins , it was very useful explanation!

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

Post to forums  

Autodesk Design & Make Report