C#ATIA

↑タイトル詐欺 主にFusion360API 偶にCATIA V5 VBA(絶賛ネタ切れ中)

クエリによる工具の検索

ツールライブラリから必要な工具を抜き出します。
中身をループして必要な工具を抜き出す・・・ではちょっとナンセンス
なので、クエリが使えるようなので、クエリでやりたいですよね?
Fusion 360 Help

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.cam as cam
import json

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

        camManager: cam.CAMManager = cam.CAMManager.get()
        libraryManager: cam.CAMLibraryManager = camManager.libraryManager
        toolLibraries: cam.ToolLibraries = libraryManager.toolLibraries

        # ツールライブラリ取得
        samplesUrl: core.URL = core.URL.create(
            'systemlibraryroot://Samples/Milling Tools (Metric).json'
        )
        toolLib: cam.ToolLibrary = toolLibraries.toolLibraryAtURL(samplesUrl)

        # クエリで工具を取得
        # フラットエンドミルでφ5.9mm~φ6.1mm
        query: cam.ToolQuery = toolLib.createQuery()
        query.criteria.add(
            'tool_type',
            core.ValueInput.createByString('flat end mill')
        )
        query.criteria.add(
            'tool_diameter.min',
            core.ValueInput.createByReal(0.59)
        )
        query.criteria.add(
            'tool_diameter.max',
            core.ValueInput.createByReal(0.61)
        )

        results: cam.ToolQueryResult = query.query()
        if len(results) < 1:
            return
        tool: cam.Tool = results[0].tool

        # 取得した工具データをjsonでダンプ
        print(f'{json.dumps(json.loads(tool.toJson()), indent=2)}')

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

該当する工具(φ6フラットエンドミル)を取得後、jsonフォーマットで
工具情報を出力しています。

{
  "BMC": "unspecified",
  "GRADE": "Mill Generic",
  "description": "6mm Flat Endmill",
  "geometry": {
    "CSP": false,
    "DC": 6,
    "HAND": true,
    "LB": 22.5,
    "LCF": 20,
    "NOF": 3,
    "NT": 1,
    "OAL": 63,
    "RE": 0,
    "SFDM": 6,
    "TA": 0,
    "TP": 0,
    "shoulder-length": 20,
    "thread-profile-angle": 60,
    "tip-diameter": 6,
    "tip-length": 0,
    "tip-offset": 0
  },
  "guid": "95592c49-3364-4b7f-873a-5a00ffdb78f1",
  "holder": {
・・・

全部は長すぎるので・・・。