How can I move an Occurrence to the root component?

How can I move an Occurrence to the root component?

Anonymous
Not applicable
1,068 Views
4 Replies
Message 1 of 5

How can I move an Occurrence to the root component?

Anonymous
Not applicable

I'd like to move the selected object to the root component in a Python script.

However, the moveToComponent method of an occurrence requires an Occurrence argument.

The root component doesn't seem to have an occurrence, so how would this be done?

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

BrianEkins
Mentor
Mentor
Accepted solution

That is an oversight in the API.  I don't think it's possible to do what you want.  What should be done is the API enhanced so that you can use None as occurrence argument and it would assume you mean the root component in that case.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 5

bruce9PYQ9
Explorer
Explorer
Any word on this? I'm having the same issue in 2022? Doesn't seem like too substantial a change...
0 Likes
Message 4 of 5

kandennti
Mentor
Mentor

Hi @bruce9PYQ9 .

 

I tried to find an alternative.
I tried the Occurrences.addExistingComponent method, but it was not what I wanted.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-a2efccd6-ac40-465b-811d-e5d0266d1c62 

 

I tried other methods, but this was the one that seemed to work now.

# 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
        des: adsk.fusion.Design = app.activeProduct
        root: adsk.fusion.Component = des.rootComponent

        targetOcc: adsk.fusion.Occurrence = root.allOccurrences.itemByName('Part_C:1')
        if targetOcc:
            moveToRootComponent(targetOcc)

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


def moveToRootComponent(
    occ: adsk.fusion.Occurrence) -> adsk.fusion.Occurrence:

    app: adsk.core.Application = adsk.core.Application.get()
    ui = app.userInterface

    sels: adsk.core.Selections = ui.activeSelections
    sels.clear()
    sels.add(occ)
    app.executeTextCommand(u'NuCommands.CutCmd')

    root: adsk.fusion.Component = app.activeProduct.rootComponent
    sels.clear()
    sels.add(root)
    app.executeTextCommand(u'NuComponents.PasteCmd')
    app.executeTextCommand(u'NuCommands.CommitCmd')

    return root.occurrences[-1]
0 Likes
Message 5 of 5

bruce9PYQ9
Explorer
Explorer

@kandennti 

 

Your approach worked great! A little slow but I'll take correctness any day.

 

This works better than my previous approach of using "AddExistingComponent" on the root. That function did a copy and paste, but didn't do the "cut" part that your approach does. I didn't know you could inline UI commands like that, thanks for the technique!