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: 

Fusion 360 API units

7 REPLIES 7
Reply
Message 1 of 8
yebyps
1115 Views, 7 Replies

Fusion 360 API units

I'm a complete newbie to API.  I found a script, and got it to run with a bit of badgering, and also discovered how to stepover in the debugging mode.  What the script does is the wrong scale for me and I want to change probable cm to mm as in my normal unit mode.  What I have at the beginning of the (python) code is 

 

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

def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        mm = design.unitsManager.defaultLengthUnits
        
        des = adsk.fusion.Design.cast(app.activeProduct)
        root = des.rootComponent
 
the addition of mm = design.unitsManager.defaultLengthUnits  to an otherwise functioning code results in total silence when I try to run the code.  When I step in debug mode, it drops out at that line.  Obviously I'm missing some previous declaration, but I'm too new to the grammar to figure that out.  Any ideas please. 
 
 
 
7 REPLIES 7
Message 2 of 8
JesusFreke
in reply to: yebyps

A few things:

 

First, on the problematic line:

mm = design.unitsManager.defaultLengthUnits

 

you are trying to access the "unitsManager" member of the object contained in the design variable - but the design variable has not been defined yet -- but you retrieve the design just a couple of lines down:

des = adsk.fusion.Design.cast(app.activeProduct)

 Although you assign it to the "des" variable, not the design variable.

 

Also, I'm not sure what your intention is with the line

mm = design.unitsManager.defaultLengthUnits

I'm guessing that you're trying to change the defaultLengthUnits to mm, but that line doesn't do that. Instead, it is simply retrieving the existing value of defaultLengthUnits and assigning it to a variable with the name "mm". However this is just a local variable with no intrinsic meaning. The value of the "mm" variable after this line would typically be either the string "in" or the string "cm", depending on which is the default in the current design. defaultLengthUnits is a read-only property, so you can't use it to change the default.

 

And finally, it's generally a good practice to wrap your script with a try/except block, and either print the exception or show a dialog with the exception. In this case, an exception is being thrown at the line I previously mentioned, and the error message would tell you what was wrong. It would likely be something along the lines of "accessing undefined variable 'design'".

 

For example, something like:

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

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

        mm = design.unitsManager.defaultLengthUnits
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

I'm not positive, but I'm not aware of a way to change the default units to mm. Although I do agree with you in that I prefer to work in mm instead of cm/in. I typically do everything as if the units were mm, and then just scale the end result by 1/10 at the end.

 

 

Message 3 of 8
goyals
in reply to: JesusFreke

Please take a look at https://forums.autodesk.com/t5/fusion-360-api-and-scripts/change-units-with-api/td-p/8208839



Shyam Goyal
Sr. Software Dev. Manager
Message 4 of 8
BrianEkins
in reply to: yebyps

You might also find this useful.

http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A81B295F-984A-4039-B1CF-B449BEE893D7

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 5 of 8
samputzi
in reply to: BrianEkins

Hi thanks for the great resources

 

maybe i can‘t see it but the example codes on this link: http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A81B295F-984A-4039-B1CF-B449BEE893D7

are just in javascript and don‘t show either python or c++. Do you know of another ressource with python code?

Message 6 of 8
BrianEkins
in reply to: samputzi

You're right.  That's some very old content that somehow slipped through when the JavaScript API was retired.  It needs to be updated.  Here's a version of the first snippet in Python.

    design = adsk.fusion.Design.cast(app.activeProduct)
    unitsMgr = adsk.core.UnitsManager.cast(design.unitsManager)

    # Prompt the user for a string and validate it's valid.
    isValid = False
    default = '1 in'  # The initial default value.
    value = ''
    while not isValid:
        # Get a string from the user.
        (value, isCancelled) = ui.inputBox('Enter a distance', 'Distance', default);
            
        # Exit the program if the dialog was cancelled.
        if (isCancelled):
            return
            
        # Check that a valid length was entered.
        if unitsMgr.isValidExpression(value, unitsMgr.defaultLengthUnits):
            isValid = True
        else:
            # Invalid expression so display an error and set the flag to allow them
            # to enter a value again.
            ui.messageBox('"' + value + '" is not a valid length expression.', 'Invalid entry', 
                            adsk.core.MessageBoxButtonTypes.OKButtonType, 
                            adsk.core.MessageBoxIconTypes.CriticalIconType)
        
    # Use the value for something.
    realValue = unitsMgr.evaluateExpression(value, unitsMgr.defaultLengthUnits)
    ui.messageBox('input: ' + value + ', result: ' + str(realValue))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 7 of 8
buginaframe
in reply to: BrianEkins

Hello, newbie here.

 

1. So from my understanding and what I've tried, this code changes the default unit of the current document

unitsMgr = design.fusionUnitsManager

unitsMgr.distanceDisplayUnits = adsk.fusion.DistanceUnits.InchDistanceUnits

 

It works great to change the default units, but how can I change the unit system of the API? What I mean, if for instance, I create a sketch with a 1x1 square in it, regardless of my default unit of the design, it'll always be 1cm x 1cm. Can I change that and if yes, how? Below is how I did my 1in x 1in square, but is there a more elegant way of doing it?

 

 

        cmToInches = 2.54
        usLength = 3
        usWidth = 4
        usHeight = 1

        edgLength = cmToInches * usLength
        edgWidth = cmToInches  * usWidth
        edgHeight = cmToInches  * usHeight

        edges = sketch.sketchCurves.sketchLines
        cubEdges = edges.addCenterPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(edgLength/2, edgWidth/2, 0))
        cubEdges.item(2)
        cubProfile = sketch.profiles.item(0)

 

 

Thank you for your help

Message 8 of 8
BrianEkins
in reply to: buginaframe

The API is a layer over the Fusion internals that tries to be as thin as possible.  Internally, length units within Fusion core are _ALWAYS_ in centimeters.  The API reflects that so there's not any way to change it.  In most programs, the only time you have to care about units is when you get values from the user and when you display values.  In both of those cases, you can use utilities in the API to convert the user input to internal units and to create a string in the current document units to display.

 

Also, if you're not commenting on an existing topic, it's best to create a new one for your new question, even if it is related to a previous question.

 

Welcome to the Fusion 360 API.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report