Replicating ChainSelection

Replicating ChainSelection

joshM57CN
Contributor Contributor
748 Views
4 Replies
Message 1 of 5

Replicating ChainSelection

joshM57CN
Contributor
Contributor

I am trying to find a way to select a series of connected edges (tangent and nontangent) similar to how edges are selected in Manufacturing. Pick one and the whole loop is selected.

 

So far, I've been able to get tangent edges by setting my selection filter to "Edges" and in my PreSelectHandler using: eventArgs->additionalEntities(edge->tangentiallyConnectedEdges());

 

Is there a similar, or effective way to do this other than picking them one by one? Utlimately, I'm planning on passing the collection of edges into a Path to do other things with down the road. 

 

joshM57CN_0-1688675794667.png

 

0 Likes
Accepted solutions (1)
749 Views
4 Replies
Replies (4)
Message 2 of 5

kandennti
Mentor
Mentor

Hi @joshM57CN -San.

 

The following sample selects the edge of a face that shares the selected edge.

# Fusion360API Python script

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

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

        msg: str = 'Select Edge'
        selFilter: str = 'Edges'
        sel: core.Selection = select_ent(msg, selFilter)
        if not sel:
            return

        edge: fusion.BRepEdge = sel.entity
        sels: core.Selections = ui.activeSelections

        for idx, face in enumerate(edge.faces):
            sels.clear()
            edges: fusion.BRepEdges = face.edges
            [sels.add(e) for e in edges]
            ui.messageBox(f"BRepFace.Edges Index:{idx} Count:{edges.count}")

        # for idx, coEdge in enumerate(edge.coEdges):
        #     sels.clear()
        #     edges: fusion.BRepEdges = coEdge.loop.edges
        #     [sels.add(e) for e in edges]
        #     ui.messageBox(f"BRepCoEdge.Loop.Edges Index:{idx} Count:{edges.count}")

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


def select_ent(
    msg: str,
    filter: str
) -> core.Selection:

    try:
        app: core.Application = core.Application.get()
        ui: core.UserInterface = app.userInterface
        sel = ui.selectEntity(msg, filter)
        return sel
    except:
        return None

 

Normally, for a solid edge, you would see two MessageBoxes in a loop because the two faces share the edge.

1.png

0 Likes
Message 3 of 5

joshM57CN
Contributor
Contributor

Oh that's very helpful actually! I think coedges and choosing the right index was the piece I was missing.

 

As a follow up, is there away to do something similar within a SelectionCommandInput handler?

 

Thanks Again!

 

Message 4 of 5

kandennti
Mentor
Mentor
Accepted solution

@joshM57CN -San.

 

I used CustomGraphics to display the desired Edges.

 

 

# 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 = []

_edgeIpt: core.SelectionCommandInput = None
_idxIpt: core.BoolValueCommandInput = None

CMD_INFO = {
    'id': 'kantoku_test',
    'name': 'test',
    'tooltip': 'test'
}


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

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

            onPreview = MyPreviewHandler()
            cmd.executePreview.add(onPreview)
            _handlers.append(onPreview)

            global _edgeIpt
            _edgeIpt = inputs.addSelectionInput(
                "_edgeIptId",
                "Edge",
                "Select Edge",
            )
            _edgeIpt.addSelectionFilter("Edges")

            global _idxIpt
            _idxIpt = inputs.addBoolValueInput(
                "_idxIptId",
                "Other Side",
                True,
            )

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


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

    def notify(self, args: core.CommandEventArgs):
        global _edgeIpt
        edge: fusion.BRepEdge = _edgeIpt.selection(0).entity

        global _idxIpt
        idx = 0 if _idxIpt.value else -1
        edges: fusion.BRepEdges = edge.faces[idx].edges

        draw_cg_edges(edges)


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

def draw_cg_edges(edges: fusion.BRepEdges) -> None:
    blue = fusion.CustomGraphicsSolidColorEffect.create(
        core.Color.create(
            0, 0, 255, 255
        )
    )

    app: core.Application = core.Application.get()
    des: fusion.Design = app.activeProduct
    root: fusion.Component = des.rootComponent

    cgGroup: fusion.CustomGraphicsGroup = root.customGraphicsGroups.add()
    cgGroup.depthPriority = 1

    edge: fusion.BRepEdge = None
    for edge in edges:
        cgCrv: fusion.CustomGraphicsCurve = cgGroup.addCurve(
            edge.geometry
        )
        cgCrv.color = blue
        cgCrv.weight = 2

 

 

0 Likes
Message 5 of 5

joshM57CN
Contributor
Contributor

That's fantastic. Thank you so much!

0 Likes