Add in conversion option to properties dialog

Add in conversion option to properties dialog

terry_fusion
Advocate Advocate
657 Views
6 Replies
Message 1 of 7

Add in conversion option to properties dialog

terry_fusion
Advocate
Advocate

I've been doing a bit of work in creating specific sized volumes for a defined space and find that I'm taking the volume information from the property dialog box and then going to google to find a conversion table.

 

I'm not a programmer, so I have to ask.

 

Would there be an easy way to add into the dialog box a drop down menu that list a number of different types of volume conversions?  Gallon, Liter, Milliliter, etc that could be converted to the added to the results in the dialog box?

 

Maybe even add one for mass?

 

Just an idea.

0 Likes
Accepted solutions (1)
658 Views
6 Replies
Replies (6)
Message 2 of 7

kandennti
Mentor
Mentor
Accepted solution

Hi @terry_fusion -San.

 

I don't think it is possible to customize the properties dialog.

Instead, I have created a script that displays the volume of the selected solid body in multiple units.

# Fusion360API Python script

import traceback
import adsk
import adsk.core as core
import adsk.fusion as fusion

_app: core.Application = None
_ui: core.UserInterface = None
_handlers = []

CMD_INFO = {
    'id': 'kantoku_Multi_Units_Volume',
    'name': 'Multi Units Volume',
    'tooltip': 'Multi Units Volume'
}

_bodyIpt: core.SelectionCommandInput = None
_txtIpt: core.TextBoxCommandInput = None

UNITS_LIST = [
    "mm^3",
    "cm^3",
    "ml",
    "l",
    "gal",
    "qt",
    "pt",
    "cup",
    "fl_oz",
]

class MyCommandCreatedHandler(core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args: core.CommandCreatedEventArgs):
        try:
            global _handlers
            cmd: core.Command = core.Command.cast(args.command)
            inputs: core.CommandInputs = cmd.commandInputs

            cmd.isOKButtonVisible = False

            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onInputChanged = MyInputChangedHandler()
            cmd.inputChanged.add(onInputChanged)
            _handlers.append(onInputChanged)

            global _bodyIpt
            _bodyIpt = inputs.addSelectionInput(
                "_bodyIptId",
                "SolidBody",
                "Select Solid Body",
            )
            _bodyIpt.addSelectionFilter(core.SelectionCommandInput.SolidBodies)

            global _txtIpt
            _txtIpt = inputs.addTextBoxCommandInput(
                '_txtIptId',
                'Volume',
                '-',
                len(UNITS_LIST)-1,
                True
            )

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


class MyInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: adsk.core.InputChangedEventArgs):
        global _bodyIpt
        if args.input != _bodyIpt: return

        global _txtIpt
        if _bodyIpt.selectionCount < 1:
            _txtIpt.text = "-"
            return

        body: fusion.BRepBody = _bodyIpt.selection(0).entity
        volume = body.volume # cm^3
        print(volume)

        app: core.Application = core.Application.get()
        des: fusion.Design = app.activeProduct
        unitsMgr: core.UnitsManager = des.unitsManager

        msgLst = [unitsMgr.formatInternalValue(volume, u, True) for u in UNITS_LIST]

        _txtIpt.text = "\n".join(msgLst)


class MyCommandDestroyHandler(core.CommandEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args: core.CommandEventArgs):
        adsk.terminate()


def run(context):
    try:
        global _app, _ui
        _app = core.Application.get()
        _ui = _app.userInterface

        cmdDef: core.CommandDefinition = _ui.commandDefinitions.itemById(
            CMD_INFO['id']
        )

        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition(
                CMD_INFO['id'],
                CMD_INFO['name'],
                CMD_INFO['tooltip']
            )

        global _handlers
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        cmdDef.execute()

        adsk.autoTerminate(False)

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

1.png

0 Likes
Message 3 of 7

terry_fusion
Advocate
Advocate

That is very much appreciated, thank you for taking the time to do this.

 

I felt it was something that could be done, just wasn't sure how.

 

Thanks again! 🙂

0 Likes
Message 4 of 7

terry_fusion
Advocate
Advocate

I've been using this script a lot lately and its been a wonderful boon to my workflow.

 

One more question, is there a way I can add in an icon on the toolbar or menu? say under the inspect menu?

 

Thanks again for the help.

0 Likes
Message 5 of 7

kandennti
Mentor
Mentor

@terry_fusion -San.

 

To add an icon to the menu, it must be an add-in, not a script.
We will create an add-in later.

0 Likes
Message 6 of 7

kandennti
Mentor
Mentor

@terry_fusion -San.

 

The add-in has been created and is attached.
When you run the add-in, the command is added to the bottom of the inspect panel.

1.png

0 Likes
Message 7 of 7

terry_fusion
Advocate
Advocate

I am appreciative,  that was so kind of you to do that. 

 

I feel it will be a feature I use often.

 

Regards.

 

 

0 Likes