Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Feature Request? : API for creating and managing "Group"s in browser

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
nnikbin
865 Views, 9 Replies

Feature Request? : API for creating and managing "Group"s in browser

I was not able to find any API related to creation and management of "Group"s in browser (picture below). Is there such API available? I think having access to groups will be beneficial to the add-ins that create lots of bodies.

 

26.gif

Tags (1)
Labels (1)
9 REPLIES 9
Message 2 of 10
kandennti
in reply to: nnikbin

Hi @nnikbin .

 

When I tried, I was able to create the group itself.

        # selections
        sels :adsk.core.Selections = ui.activeSelections
        sels.clear()

        # select root bodies
        bodies :adsk.fusion.BRepBodies = root.bRepBodies
        sels.add(bodies)

        # Create SurfaceGroup
        _app.executeTextCommand(u'Commands.Start FusionCreateSurfaceGroupCommand')

 

However, I looked for a way to access the group that was created, but could not find it.
I used this one as a reference.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/access-section-analysis/m-p/9700491#M11052 

 

I also selected the group in the GUI and ran the text command here.

ObjectPaths.Show

https://github.com/kantoku-code/Fusion360_Small_Tools_for_Developers/blob/master/TextCommands/TextCo... 

 

The result is here.

ObjectPaths.Show 
1 selections
Selection 0 has 4 elements
  ComponentBaseInstance (Unsaved) linkName: , (referenced component: FusionComponent - (Unsaved)), (CommonData CommonBaseEntData (base ManagedEntity (base Entity type ComponentInstanceMetaType id: 57 Subject count: 1 [EntityId: 3] ) : HasParent: 0) HasClientObject: 0)
  FusionComponent - (Unsaved)
  SurfaceGroups (base CommonBaseEntData (base ManagedGroupEntity (base ManagedEntity (base Entity type SurfaceGroupsMetaType id: 21 ) : HasParent: 1) children : 1) HasClientObject: 0)
  ManagedGroupEntity (base ManagedEntity (base Entity type SurfaceGroupMetaType id: 256 ) : HasParent: 1) children : 0

I have a feeling it has something to do with SurfaceGroups, but I don't know how to get here.

 

We also know that the command ID for the "MOVE TO GROUP" command is "FUSIONMoveToSurfaceGroupCommand".

Message 3 of 10
nnikbin
in reply to: kandennti

Hi @kandennti ,

Awesome!, Thank you for your reply. Is it possible to rename the created group? (I know that you mentioned you were not able to find a solution for getting access to created group). I need creating groups with some dynamic names.

 

And a general question: Is it possible to pass arguments to commands using executeTextCommand or all of the commands only use the current context?

Message 4 of 10
kandennti
in reply to: nnikbin

We have made some progress.
The group could be renamed to any name.

        # selections
        sels :adsk.core.Selections = ui.activeSelections
        sels.clear()

        # select root bodies
        bodies :adsk.fusion.BRepBodies = root.bRepBodies
        sels.add(bodies)

        # Create SurfaceGroup
        _app.executeTextCommand(u'Commands.Start FusionCreateSurfaceGroupCommand')

        # get SurfaceGroups Properties
        # SurfaceGroups(SurfaceGroupsMetaType id: 21)
        surfaceGroupsProp = _app.executeTextCommand(u'PEntity.Properties 21')

        # Convert to json
        import json
        surfaceGroups = json.loads(surfaceGroupsProp)

        # SurfaceGroup count
        surfaceGroups_count = len(surfaceGroups['children'])
        print(str(surfaceGroups_count))

        # get Target Id
        targetId = surfaceGroups['children'][surfaceGroups_count - 1]['entityId']

        # Rename SurfaceGroup
        newGroupName = 'hoge'
        _app.executeTextCommand(u'PInterfaces.Rename {} {}'.format(targetId, newGroupName))

1.png

Thanks to @thomasa88 !

Message 5 of 10
nnikbin
in reply to: kandennti

Wonderful workaround! Tanks a lot @kandennti and @thomasa88 !

Message 6 of 10
kandennti
in reply to: nnikbin

Limited, but seems to have reached the goal.
(The root component is only)

 

The following sample creates 10 bodies and divides them into two groups.

# Fusion360API Python script-sample_CreateBodyGroup
# Fusion360 ver2.0.9313
import adsk.core, adsk.fusion, traceback

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)

import json
def createBoxesGroup(
    groupName :str,
    bodiesList :list
    ):

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

    # selections
    sels :adsk.core.Selections = ui.activeSelections
    sels.clear()

    # select root bodies
    bodies :adsk.fusion.BRepBodies = root.bRepBodies
    sels.add(bodies)

    # Create SurfaceGroup
    app.executeTextCommand(u'Commands.Start FusionCreateSurfaceGroupCommand')

    # get SurfaceGroups Properties
    # SurfaceGroups(SurfaceGroupsMetaType id: 21)
    surfaceGroupsProp = app.executeTextCommand(u'PEntity.Properties 21')

    # Convert to json
    surfaceGroups = json.loads(surfaceGroupsProp)

    # SurfaceGroup count
    surfaceGroups_count = len(surfaceGroups['children'])
    # print(str(surfaceGroups_count))

    # get Target Id
    targetId = surfaceGroups['children'][surfaceGroups_count - 1]['entityId']

    # Rename SurfaceGroup
    app.executeTextCommand(u'PInterfaces.Rename {} {}'.format(targetId, groupName))

    # select bodiesList
    [sels.add(body) for body in bodiesList]

    # select SurfaceGroup
    # https://github.com/kantoku-code/Fusion360_Small_Tools_for_Developers/blob/master/TextCommands/TextCommands_txt_Ver2_0_8176.txt#L2058
    # Except for the root component, the <Paths> need to be changed.
    app.executeTextCommand(u'Selections.Add 57:3:21:{}'.format(targetId))

    # exec FusionMoveToSurfaceGroupCommand
    app.executeTextCommand(u'Commands.Start FusionMoveToSurfaceGroupCommand')
    app.executeTextCommand(u'NuCommands.CommitCmd')


def createBoxes(
    comp :adsk.fusion.Component,
    count :int
    ):

    def createBox(
        comp :adsk.fusion.Component,
        pnt :adsk.core.Point3D
        ) -> adsk.fusion.BRepBody:

        vec3D = adsk.core.Vector3D
        lVec = vec3D.create(1.0, 0.0, 0.0)
        wVec= vec3D.create(0.0, 1.0, 0.0)

        bouBox3D = adsk.core.OrientedBoundingBox3D
        box = bouBox3D.create(pnt, lVec, wVec, 10, 10, 10)

        tmpBrMgr = adsk.fusion.TemporaryBRepManager.get()

        baseFeats = comp.features.baseFeatures
        baseFeat = baseFeats.add()
        baseFeat.startEdit()
        cube :adsk.fusion.BRepBody = comp.bRepBodies.add(tmpBrMgr.createBox(box),baseFeat)
        baseFeat.finishEdit()

        return cube

    # return boxis
    for idx in range(count):
        createBox(
            comp,
            adsk.core.Point3D.create(0.0 + float(20.0 * idx), 0.0, 0.0))


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

        des  :adsk.fusion.Design = _app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # createBoxes
        createBoxes(root, 10)

        # message
        vp :adsk.core.Viewport = _app.activeViewport
        vp.fit()
        _ui.messageBox('Group the bodies.')

        # Grouping
        groupOdd = [box for idx,box in enumerate(root.bRepBodies) if idx % 2 == 0]
        groupEven = [box for idx,box in enumerate(root.bRepBodies) if idx % 2 != 0]

        # createBoxesGroup
        createBoxesGroup('Odd', groupOdd)
        createBoxesGroup('Even', groupEven)

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

Although limited to the root component, I think it will work if you copy the createBoxesGroup function and call it with a group name and a list of bodies as parameters.

 

1.png

I could learn a lot from it. Thank you.

Message 7 of 10
nnikbin
in reply to: kandennti

Great! Thank you @kandennti 

Message 8 of 10
nnikbin
in reply to: nnikbin

I hope Autodesk publishes APIs for Groups and Selection Sets. Meanwhile we can use @kandennti 's nice solution at least for our internal apps.

Message 9 of 10
kandennti
in reply to: nnikbin

I followed the example of the president of some country and never gave up, and as a result, I was able to create a group within the component (Occurrence) as well.

 

 

It got too long, so I decided to publish it on github.

https://github.com/kantoku-code/sample_CreateBodyGroup 

Message 10 of 10
nnikbin
in reply to: kandennti

Perfect! Thank you @kandennti !

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report