Selection filter for custom graphics groups

Selection filter for custom graphics groups

j.han97
Advocate Advocate
554 Views
3 Replies
Message 1 of 4

Selection filter for custom graphics groups

j.han97
Advocate
Advocate

Hi all,

 

In my add-in, several groups of faces have been identified and added to custom graphics groups with distinct colors, so that users can see them clearly.

 

Now for the following actions, users will select one of the groups and decide which operation to be applied on that group. I was considering creating a selection input in a command dialog, and setting the filter of the selection input such that only custom graphics groups can be selected. 

 

However, I found that there is no 'CustomGraphicsGroup' in the list of 'Selection Filters'. Is it possible to achieve this objective or I have to do this another way?

 

I hope that I made it clear. Thank you!

 

 

0 Likes
Accepted solutions (1)
555 Views
3 Replies
Replies (3)
Message 2 of 4

kandennti
Mentor
Mentor

Hi @j.han97 .

 

My understanding is that custom graphics should only be used for display purposes.

If you still want to do some filtering, you can use the Command.preSelect event to do the same.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-E3D24F04-8576-420E-B9AD-44746A6B12EE 

0 Likes
Message 3 of 4

kandennti
Mentor
Mentor
Accepted solution

@j.han97 .

 

The selection filters are only the main ones.
Two samples were created.

 

The first is a sample that creates a BRepBody and a CustomGraphicsBRepBody.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

def run(context):
    ui: adsk.core.UserInterface = None
    try:
        app: adsk.core.Application = adsk.core.Application.get()
        ui = app.userInterface
        app.documents.add(
            adsk.core.DocumentTypes.FusionDesignDocumentType
        )
        des: adsk.fusion.Design = app.activeProduct
        des.designType = adsk.fusion.DesignTypes.ParametricDesignType

        root: adsk.fusion.Component = des.rootComponent
        bodies: adsk.fusion.BRepBodies = root.bRepBodies
        tmpMgr: adsk.fusion.TemporaryBRepManager = adsk.fusion.TemporaryBRepManager.get()

        # brep
        sphere: adsk.fusion.BRepBody = tmpMgr.createSphere(
            adsk.core.Point3D.create(-1,0,0),
            1
        )
        baseFeat: adsk.fusion.BaseFeature = root.features.baseFeatures.add()
        baseFeat.startEdit()
        bodies.add(sphere, baseFeat)
        baseFeat.finishEdit()

        # CustomGraphics
        sphere: adsk.fusion.BRepBody = tmpMgr.createSphere(
            adsk.core.Point3D.create(1,0,0),
            1
        )
        cgGroup: adsk.fusion.CustomGraphicsGroup = root.customGraphicsGroups.add()
        cgBody:adsk.fusion.CustomGraphicsBRepBody = cgGroup.addBRepBody(sphere)

        cgBody.color = adsk.fusion.CustomGraphicsBasicMaterialColorEffect.create(
            adsk.core.Color.create(0, 255, 0,255),
            adsk.core.Color.create(255, 255, 0, 255),
            adsk.core.Color.create(0, 0, 255, 255),
            adsk.core.Color.create(0, 0, 0, 255),
            10,
            0.5
        )

        app.activeViewport.fit()

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

 

1.png

 

Then, a sample selection filter that only allows CustomGraphicsBRepBody.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

_app: adsk.core.Application = None
_ui: adsk.core.UserInterface = None

_handlers = []


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

        cmdDef: adsk.core.CommandDefinition = _ui.commandDefinitions.itemById(
            'test_cmd_id'
        )

        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition(
                'test_cmd_id',
                'test',
                'test'
            )

        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()))


class MyPreSelectHandler(adsk.core.SelectionEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: adsk.core.SelectionEventArgs):
        adsk.core.Application.get().log(args.firingEvent.name)

        # Selection of the element under the mouse cursor.
        sel: adsk.core.Selection = args.selection
        ent = sel.entity

        # Filtering by classType.
        if ent.classType() != adsk.fusion.CustomGraphicsBRepBody.classType():
            # If not CustomGraphicsBRepBody, disallow selection.
            args.isSelectable = False


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

    def notify(self, args: adsk.core.CommandCreatedEventArgs):
        adsk.core.Application.get().log(args.firingEvent.name)
        try:
            global _handlers

            cmd: adsk.core.Command = adsk.core.Command.cast(args.command)
            # cmd.isOKButtonVisible = False

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

            onPreSelect = MyPreSelectHandler()
            cmd.preSelect.add(onPreSelect)
            _handlers.append(onPreSelect)

            # inputs
            inputs: adsk.core.CommandInputs = cmd.commandInputs

            selIpt: adsk.core.SelectionCommandInput = inputs.addSelectionInput(
                'selIpt',
                'select',
                'select'
            )
            # No filters are set here.

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


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

    def notify(self, args: adsk.core.CommandEventArgs):
        adsk.core.Application.get().log(args.firingEvent.name)

        adsk.terminate()

 

Create a SelectionCommandInput in MyCommandCreatedHandler, but do not specify a selection filter.

Instead, the selection permission is determined within MyPreSelectHandler.

 

This method requires a dialog and is not available in the UserInterface.selectEntity method.

Message 4 of 4

j.han97
Advocate
Advocate

Thanks @kandennti, this will be an elegant solution.  

 

 

0 Likes