Change tools through API

Change tools through API

j_zander
Contributor Contributor
1,323 Views
6 Replies
Message 1 of 7

Change tools through API

j_zander
Contributor
Contributor

Hi! Im trying to make a script or an Api to change the tool_descriptions to a unique number (kind of ID) through a script.

I'm able to read everything from the tools I want to change but it seems like it's not possible to change the value. I can change the variables but it is never displayed inside the Library.

Can anyone help me?

 

Here is the code I use:

#Author-
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback
import json
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        # Get a reference to the CAMManager object.
        camMgr = adsk.cam.CAMManager.get()

        # Get the ToolLibraries object.
        toolLibs = camMgr.libraryManager.toolLibraries

        # Get the URL for the local libraries.
        localLibLocationURL = toolLibs.urlByLocation(adsk.cam.LibraryLocations.LocalLibraryLocation)

        # Get the URL of the folder, which will be for the "CustomTools" folder.
        f360FolderURLs = toolLibs.childFolderURLs(localLibLocationURL)
        BuildToolsFolderURL = f360FolderURLs[0]
        

       # ui.messageBox("DIE URL lautet {}" .format(BuildToolsFolderURL))

        # Get the "Millingtools" library.
        f360LibraryURLs = toolLibs.childAssetURLs(BuildToolsFolderURL)
        toolLib = None
        for libURL in f360LibraryURLs:
            if 'Millingtools' in libURL.leafName:
                toolLib = toolLibs.toolLibraryAtURL(libURL)

        # Find a specific tool.
        for tool in toolLib:
            
            tooldesc = tool.parameters.itemByName('tool_description')
            tooldesc.expression = "123456"
            
            
           
           
            
            ui.messageBox("New toolname is {}".format(tooldesc.expression))
         
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Accepted solutions (1)
1,324 Views
6 Replies
Replies (6)
Message 2 of 7

kandennti
Mentor
Mentor

Hi @j_zander -San.

 

I think it is not possible to change the tool settings in ToolLibrary.


The only tool settings that can be changed are those that are called into the document.
I made a simple sample.

# Fusion360API Python script

import adsk.core, adsk.fusion, adsk.cam, traceback
def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        cam: adsk.cam.CAM = app.activeProduct
        toolLib: adsk.cam.DocumentToolLibrary = cam.documentToolLibrary

        for tool in toolLib:
            tooldesc = tool.parameters.itemByName('tool_description')
            tooldesc.expression = "123456"

            toolLib.update(tool, True)

        ui.messageBox("Done")

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

 

The DocumentToolLibrary object has an update method, and executing this will confirm the change.
However, the ToolLibrary object does not have an update method.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-C0A61669-1C70-4A68-8CAA-158108844FC7 

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-1C9482B1-CEAA-4D48-83ED-DDA9637372D2 

 

As a side note, I pointed out earlier that the description of the update method in Help was incorrect, but it has not been fixed yet.
Please take a look at this reply and the next one and you will see what I mean.

https://forums.autodesk.com/t5/fusion-api-and-scripts/manufacturing-cam-api-feedback/m-p/11876509#M1... 

0 Likes
Message 3 of 7

jeff.pek
Community Manager
Community Manager

The ToolLibraries object has an updateToolLibrary method, which should be able to be used to save changes to non-document-based tools.

 

Jeff

Message 4 of 7

kandennti
Mentor
Mentor
0 Likes
Message 5 of 7

j_zander
Contributor
Contributor

@kandennti  this works for me very well, thanks a lot. I think it is the easiest way to setup a new tool in the documentlibrary, give it the ID via script and copy it to my allmillingtools lib manually.

 

@jeff.pek  i found that method but I couldn't figure out how to use it. I don't understand which attributes to set. Maybe you can help me with that.

 

Jan

Message 6 of 7

kandennti
Mentor
Mentor
Accepted solution

@j_zander -San.

 

The first your code worked with the following modification.

・・・
        # Get the "Millingtools" library.
        f360LibraryURLs = toolLibs.childAssetURLs(BuildToolsFolderURL)
        toolLib = None
        toolLibURL = ""
        for libURL in f360LibraryURLs:
            if 'Millingtools' in libURL.leafName:
                toolLib = toolLibs.toolLibraryAtURL(libURL)
                toolLibURL = libURL

        # Find a specific tool.
        for tool in toolLib:
            
            tooldesc = tool.parameters.itemByName('tool_description')
            tooldesc.expression = "123456"
            
            # ui.messageBox("New toolname is {}".format(tooldesc.expression))

        # update toollib.
        toolLibs.updateToolLibrary(toolLibURL, toolLib)
・・・
0 Likes
Message 7 of 7

j_zander
Contributor
Contributor
thank you very much!