Fusion360 APIのテーブル入力ダイアログ

2021/10/16 categories:Fusion360| tags:Fusion360|Fusion360 API|Python|

Fusion360 APIでテーブル入力のダイアログに子コンポーネントの名前と材質を表示してみました。

子コンポーネントの名前と材質をテーブルに追加

開いているデザインの子コンポーネントを重複なしで全て取得して、テーブルに入力します。処理は下記のように行いました。

app = adsk.core.Application.get()
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
children = self.get_children(design.rootComponent)

for row, component in enumerate(children):
    name_input     =  tableCommandInputs.addStringValueInput('table_name{}'.format(row), 'String', component.name)
    if component.material is None:
        material_input =  tableCommandInputs.addStringValueInput('table_material{}'.format(row), 'String', '')
    else:
        material_input =  tableCommandInputs.addStringValueInput('table_material{}'.format(row), 'String', component.material.name)
    tableInput.addCommandInput(name_input,     row, 0)
    tableInput.addCommandInput(material_input, row, 1)

def get_children(self, component):
    components = []
    for occurrence in component.occurrences:
        child = occurrence.component
        if not child in components:
            components.append(child)
            child_components = self.get_children(child)
            components.extend(child_components)
    return components

実行結果

スクリプトを実行すると以下の通りに子コンポーネントの名前と材質がテーブルとして表示されます。OKとキャンセルはどちらもそのまま処理を終了します。

テーブル入力のコンポーネントは、エクセルなどのような表が表示されるのを期待していましたが、文字列入力のコンポーネントなどが表のように羅列されるようで、期待している表の入力とは異なるような感じでした。エクセルみたいな表の入力をFusion360APIのGUIで出来なさそうなので、例えばPyQtのような他のライブラリを使用してFusion360APIでエクセルのような表の表示が出来ないか調べてみようと思います。

ソースコード

import adsk.core, adsk.fusion, adsk.cam, traceback
import re

handlers = []
app = adsk.core.Application.get()
if app:
    ui = app.userInterface

class CommandExcuteHandler(adsk.core.CommandEventHandler):
    def notify(self, args):
        try:
            adsk.terminate()
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class CommandDestroyHandler(adsk.core.CommandEventHandler):
    def notify(self, args):
        try:
            adsk.terminate()
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def notify(self, args):
        try:
            cmd = args.command
            cmd.setDialogInitialSize(300, 300)
            cmd.setDialogMinimumSize(300, 300)
            cmd.isRepeatable = False
            onExecute = CommandExcuteHandler()
            cmd.execute.add(onExecute)
            onDestroy = CommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            
            handlers.append(onExecute)
            handlers.append(onDestroy)

            inputs = cmd.commandInputs
            tableInput = inputs.addTableCommandInput('table1', 'table2', 0, 'a')
            tableCommandInputs = adsk.core.CommandInputs.cast(tableInput.commandInputs)

            product = app.activeProduct
            design = adsk.fusion.Design.cast(product)
            children = self.get_children(design.rootComponent)

            for row, component in enumerate(children):
                name_input     =  tableCommandInputs.addStringValueInput('table_name{}'.format(row), 'String', component.name)
                if component.material is None:
                    material_input =  tableCommandInputs.addStringValueInput('table_material{}'.format(row), 'String', '')
                else:
                    material_input =  tableCommandInputs.addStringValueInput('table_material{}'.format(row), 'String', component.material.name)
                tableInput.addCommandInput(name_input,     row, 0)
                tableInput.addCommandInput(material_input, row, 1)

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

    def get_children(self, component):
        components = []
        for occurrence in component.occurrences:
            child = occurrence.component
            if not child in components:
                components.append(child)
                child_components = self.get_children(child)
                components.extend(child_components)
        return components

def run(context):
    try:
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        if not design:
            ui.messageBox('It is not supported in current workspace, please change to MODEL workspace and try again.')
            return
        commandDefinitions = ui.commandDefinitions
        
        cmdDef = commandDefinitions.itemById('TestTableCommandInput')
        if not cmdDef:
            cmdDef = commandDefinitions.addButtonDefinition('TestTableCommandInput', 'TestTableCommandInput', 'TestTableCommandInput')

        onCommandCreated = CommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        handlers.append(onCommandCreated)
        inputs = adsk.core.NamedValues.create()
        cmdDef.execute(inputs)

        adsk.autoTerminate(False)

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

Share post

Related Posts

Comments

comments powered by Disqus