<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Fusion 360 API units in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9412694#M12308</link>
    <description>&lt;P&gt;You might also find this useful.&lt;/P&gt;&lt;P&gt;&lt;A href="http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A81B295F-984A-4039-B1CF-B449BEE893D7" target="_blank"&gt;http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A81B295F-984A-4039-B1CF-B449BEE893D7&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 31 Mar 2020 17:09:08 GMT</pubDate>
    <dc:creator>BrianEkins</dc:creator>
    <dc:date>2020-03-31T17:09:08Z</dc:date>
    <item>
      <title>Fusion 360 API units</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9411489#M12305</link>
      <description>&lt;P&gt;I'm a complete newbie to API.&amp;nbsp; I found a script, and got it to run with a bit of badgering, and also discovered how to stepover in the debugging mode.&amp;nbsp; 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.&amp;nbsp; What I have at the beginning of the (python) code is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;adsk.core,&amp;nbsp;adsk.fusion,&amp;nbsp;traceback,&amp;nbsp;math&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;def&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;run&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;context&lt;/SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ui&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;None&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;try&lt;/SPAN&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;app&amp;nbsp;=&amp;nbsp;adsk.core.Application.get()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ui&amp;nbsp;&amp;nbsp;=&amp;nbsp;app.userInterface&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mm&amp;nbsp;=&amp;nbsp;design.unitsManager.defaultLengthUnits&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;des&amp;nbsp;=&amp;nbsp;adsk.fusion.Design.cast(app.activeProduct)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;root&amp;nbsp;=&amp;nbsp;des.rootComponent&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;the addition of&amp;nbsp;mm&amp;nbsp;=&amp;nbsp;design.unitsManager.defaultLengthUnits&amp;nbsp; to an otherwise functioning code results in total silence when I try to run the code.&amp;nbsp; When I step in debug mode, it drops out at that line.&amp;nbsp; Obviously I'm missing some previous declaration, but I'm too new to the grammar to figure that out.&amp;nbsp; Any ideas please.&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 31 Mar 2020 08:08:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9411489#M12305</guid>
      <dc:creator>yebyps</dc:creator>
      <dc:date>2020-03-31T08:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: Fusion 360 API units</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9411543#M12306</link>
      <description>&lt;P&gt;A few things:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First, on the problematic line:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;mm = design.unitsManager.defaultLengthUnits&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;des = adsk.fusion.Design.cast(app.activeProduct)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Although you assign it to the "des" variable, not the design variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I'm not sure what your intention is with the line&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;mm = design.unitsManager.defaultLengthUnits&lt;/LI-CODE&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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'".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, something like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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()))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Mar 2020 08:35:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9411543#M12306</guid>
      <dc:creator>JesusFreke</dc:creator>
      <dc:date>2020-03-31T08:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: Fusion 360 API units</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9411896#M12307</link>
      <description>&lt;P&gt;Please take a look at&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/fusion-360-api-and-scripts/change-units-with-api/td-p/8208839" target="_blank"&gt;https://forums.autodesk.com/t5/fusion-360-api-and-scripts/change-units-with-api/td-p/8208839&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Mar 2020 11:44:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9411896#M12307</guid>
      <dc:creator>goyals</dc:creator>
      <dc:date>2020-03-31T11:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: Fusion 360 API units</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9412694#M12308</link>
      <description>&lt;P&gt;You might also find this useful.&lt;/P&gt;&lt;P&gt;&lt;A href="http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A81B295F-984A-4039-B1CF-B449BEE893D7" target="_blank"&gt;http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A81B295F-984A-4039-B1CF-B449BEE893D7&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Mar 2020 17:09:08 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9412694#M12308</guid>
      <dc:creator>BrianEkins</dc:creator>
      <dc:date>2020-03-31T17:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: Fusion 360 API units</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9444073#M12309</link>
      <description>&lt;P&gt;Hi thanks for the great resources&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;maybe i can‘t see it but the example codes on this link:&amp;nbsp;&lt;A href="http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A81B295F-984A-4039-B1CF-B449BEE893D7" target="_blank"&gt;http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A81B295F-984A-4039-B1CF-B449BEE893D7&lt;/A&gt;&lt;/P&gt;&lt;P&gt;are just in javascript and don‘t show either python or c++. Do you know of another ressource with python code?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 09:18:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9444073#M12309</guid>
      <dc:creator>samputzi</dc:creator>
      <dc:date>2020-04-15T09:18:06Z</dc:date>
    </item>
    <item>
      <title>Re: Fusion 360 API units</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9445339#M12310</link>
      <description>&lt;P&gt;You're right.&amp;nbsp; That's some very old content that somehow slipped through when the JavaScript API was retired.&amp;nbsp; It needs to be updated.&amp;nbsp; Here's a version of the first snippet in Python.&lt;/P&gt;&lt;LI-CODE lang="general"&gt;    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))&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 15 Apr 2020 16:18:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9445339#M12310</guid>
      <dc:creator>BrianEkins</dc:creator>
      <dc:date>2020-04-15T16:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: Fusion 360 API units</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9448736#M12311</link>
      <description>&lt;P&gt;&lt;FONT color="#000000"&gt;Hello, newbie here.&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;1. So from my understanding and what I've tried, this code changes the default unit of the current document&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;unitsMgr = design.fusionUnitsManager&lt;/P&gt;&lt;P&gt;unitsMgr.distanceDisplayUnits = adsk.fusion.DistanceUnits.InchDistanceUnits&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;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?&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;        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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Thank you for your help&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 20:18:26 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9448736#M12311</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-16T20:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: Fusion 360 API units</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9448854#M12312</link>
      <description>&lt;P&gt;The API is a layer over the Fusion internals that tries to be as thin as possible.&amp;nbsp; Internally, length units within Fusion core are _ALWAYS_ in centimeters.&amp;nbsp; The API reflects that so there's not any way to change it.&amp;nbsp; 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.&amp;nbsp; 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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Welcome to the Fusion 360 API.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 20:20:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/fusion-360-api-units/m-p/9448854#M12312</guid>
      <dc:creator>BrianEkins</dc:creator>
      <dc:date>2020-04-16T20:20:27Z</dc:date>
    </item>
  </channel>
</rss>

