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: 

unable to find tool when add tool_type as query criteria

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
maurizio_manzi
163 Views, 3 Replies

unable to find tool when add tool_type as query criteria

Hello,

if I comment out the lines:

query.criteria.add('tool_type', adsk.core.ValueInput.createByString...
then tools are found.
With the tool_type as query criteria, no tools are found, although the tool types are present (local library of the document).
.f3d document and script are attached.
Best regards
Maurizio
 
3 REPLIES 3
Message 2 of 4
BrianEkins
in reply to: maurizio_manzi

I don't believe the "tool_type" is causing problems. I found a couple of issues with your code.

 

If you want to search for document tools, you should use the LocalLibraryLocation option.

 

 

query = toolLibs.createQuery(adsk.cam.LibraryLocations.LocalLibraryLocation)

 

 

 

For the "tool_diameter", you have a comment that you're converting from mm to cm, but the tool size is defined in inches. Here is the setup for the query that works for me.

 

query = toolLibs.createQuery(adsk.cam.LibraryLocations.LocalLibraryLocation) # Create a query object to query the local library
query.criteria.add('tool_diameter', adsk.core.ValueInput.createByReal(0.5 * 2.54)) # Convert 1/2 inch to cm
query.criteria.add('tool_cornerRadius.min', adsk.core.ValueInput.createByReal(0))
query.criteria.add('tool_cornerRadius.max', adsk.core.ValueInput.createByReal(0.25 / 10)) # / 10 for mm to Cm
query.criteria.add('tool_type', adsk.core.ValueInput.createByString('flat end mill')) 

 

 

 

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

Hello,

Thank you very much.

This was my problem:

query = toolLibs.createQuery(adsk.cam.LibraryLocations.LocalLibraryLocation)

Now, it find two tools.

The third one ("ball end mill" tool) is not found, because it's defined as diameter = 3mm (as in the query),

but if I print the diameter (ui.messageBox(str(tool.diameter)), I get 3.0000000000004

So, we have 3 != 3.0000000000004

I think, this is as software-problem.

I now this problem with float variables, but Autodesk must maybe use only 4-5 decimals for compare with the query-criteria..

I post the problem under:

 

Best regards

Maurizio

 

Message 4 of 4
kandennti
in reply to: maurizio_manzi

Hi @maurizio_manzi .

 

I created a function to find a tool with a tolerance on the query condition.

I ran it with the f3d and conditions attached here and was able to find the tool.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/manufacturing-cam-api-feedback/m-p/1194396... 

 

# Fusion360API Python script

import traceback
import adsk.core
import adsk.fusion
import adsk.cam
import json

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

        ms: adsk.core.Workspace = ui.workspaces.itemById('CAMEnvironment')
        if ms:
            ms.activate()

        camObj: adsk.cam.CAM = app.activeProduct
        toolLib: adsk.cam.ToolLibrary = camObj.documentToolLibrary

        # 'flat end mill'
        conditions: dict = {
            'tool_diameter': 0.5 / 10,
            'tool_cornerRadius.min': 0,
            'tool_cornerRadius.max': 0.25 / 10,
            'tool_type': 'flat end mill',
        }

        queryResults = find_tools_by_tolerance_query(
            toolLib,
            conditions
        )

        app.log('******')
        dump_dict(conditions)
        msg = f'found: {conditions["tool_type"]} tool'
        if len(queryResults) < 1:
            msg = 'NOT ' + msg
        app.log(msg)
        # [dump_tool(res.tool) for res in queryResults]

        # bull nose end mill
        conditions = {
            'tool_diameter': 5 / 10,
            'tool_cornerRadius.min': 2.5 / 10,
            'tool_cornerRadius.max': 2.5 / 10,
            'tool_type': 'bull nose end mill',
        }

        queryResults = find_tools_by_tolerance_query(
            toolLib,
            conditions
        )

        app.log('******')
        dump_dict(conditions)
        msg = f'found: {conditions["tool_type"]} tool'
        if len(queryResults) < 1:
            msg = 'NOT ' + msg
        app.log(msg)
        # [dump_tool(res.tool) for res in queryResults]

        # bull end mill
        conditions = {
            'tool_diameter': 3 / 10,
            'tool_cornerRadius.min': 1.5 / 10,
            'tool_cornerRadius.max': 1.5 / 10,
            'tool_type': 'ball end mill',
        }

        queryResults = find_tools_by_tolerance_query(
            toolLib,
            conditions
        )

        app.log('******')
        dump_dict(conditions)
        msg = f'found: {conditions["tool_type"]} tool'
        if len(queryResults) < 1:
            msg = 'NOT ' + msg
        app.log(msg)
        # [dump_tool(res.tool) for res in queryResults]

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


def find_tools_by_tolerance_query(
    toolLib: adsk.cam.ToolLibrary,
    conditions: dict,
    tolerance: float = 0.000001
) -> adsk.cam.ToolQueryResult:

    optionWords = ('min', 'max')

    query: adsk.cam.ToolQuery = toolLib.createQuery()
    for key in conditions:
        value = conditions[key]

        # value type string
        if type(value) is str:
            query.criteria.add(
                key,
                adsk.core.ValueInput.createByString(value)
            )
            continue

        # value type float
        keys = key.split('.')

        # has not comma
        if len(keys) < 2:
            query.criteria.add(
                key + '.min',
                adsk.core.ValueInput.createByReal(value - tolerance)
            )
            query.criteria.add(
                key + '.max',
                adsk.core.ValueInput.createByReal(value + tolerance)
            )
            continue

        # has comma
        if keys[1] not in optionWords:
            adsk.core.Application.get().log(
                f'tool query errer:{key}_{value}'
            )
            continue

        if keys[1] == 'min':
            query.criteria.add(
                key,
                adsk.core.ValueInput.createByReal(value - tolerance)
            )
        else:
            query.criteria.add(
                key,
                adsk.core.ValueInput.createByReal(value + tolerance)
            )

    return query.query()


def dump_dict(dict: dict) -> None:
    app: adsk.core.Application = adsk.core.Application.get()
    app.log(json.dumps(dict, indent=2))


def dump_tool(tool: adsk.cam.Tool) -> None:
    app: adsk.core.Application = adsk.core.Application.get()
    app.log(json.dumps(json.loads(tool.toJson()) , indent=2))

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