Activating a component

Activating a component

edjohnson100
Contributor Contributor
710 Views
2 Replies
Message 1 of 3

Activating a component

edjohnson100
Contributor
Contributor

I'm struggling with trying to activate a newly created occurrence. I have a "base component" that is activated by the user that contains a number of bodies. I have code that creates a new occurrence and moves some bodies (based on the name of the body) from the active component into the newly created component. That all works fine.

 

What I can't figure out is the proper syntax or method to make the new occurrence/component active so I can create components from the bodies I just moved.

 

I suspect there is a more elegant way than the brute force method I have going here, but I'd really like to learn how I can get the activate method to work. I've found examples that helped me get this far, but haven't found any examples using the activate method that helps me with this last hurdle.

 

I'm also wondering if there more is a more efficient way to move a body to a new occurrence and create a new component from that body in the same loop. There is a "target" parameter for the moveToComponent method. I don't find an equivalent when creating components from bodies.

 

Here is a couple of screenshots from my simple test. 

 

Activate Component Screenshots.jpg

 

Thanks in advance for any help. 

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = app.activeProduct
        root = design.rootComponent
        activeComp = design.activeComponent

        # Create a new occurrence.	
        newComp = adsk.core.Matrix3D.create()
        occ = root.occurrences.addNewComponent(newComp)
        # Get the associated component.
        allPartsComp = occ.component
        # Name the component
        allPartsComp.name = 'AllParts'

        stopNumber = activeComp.bRepBodies.count
        # only move the "Part[n]" bodies
        for i in range(1, stopNumber):
            body = activeComp.bRepBodies.itemByName('Part' + str(i))
            body.moveToComponent(occ)
        
        # this is where I'm stuck
        # the newly created occurrence fails to activate
        occ.activate

        stopNumber = activeComp.bRepBodies.count
        for i in range(1, stopNumber + 1):
            body = activeComp.bRepBodies.itemByName('Part' + str(i))
            body.createComponent
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# -------------------------------------------------------------------------
# Functions
# -------------------------------------------------------------------------
def DebugPrint(message):
    """Display a message in the Fusion 360 Text Commands window."""
    # Get the palette that represents the TEXT COMMANDS window
    app = adsk.core.Application.get()
    ui = app.userInterface
    textPalette = ui.palettes.itemById('TextCommands')

    # Make sure the palette is visible
    if not textPalette.isVisible:
        textPalette.isVisible = True

    # Write the message
    textPalette.writeText(message)

 

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

tykapl.breuil
Advocate
Advocate
Accepted solution

According to the documentation here you need to call the activate function, so use occ.activate(), same thing for the createComponent method. Moreover you should probably add an if condition to your final loop to avoid the error message after running the script, something like : 

if body:
    body.createComponent()

 

Message 3 of 3

edjohnson100
Contributor
Contributor

@tykapl.breuil,

Thank you, thank you! Fixing the syntax error on occ.activate() got me over the hump. The other piece I was missing was redefining the activeComponent variable after successfully activating the occurrence and before creating the components from the bodies. This code now gives me the desired results.

 

Activate Component Success.jpg

 

I need to be less stubborn and learn to ask for help sooner. I stared at that code and searched for solutions for most of the last 2 days. Your answer was just what I needed for the "ah ha!" moment.

 

Thanks again.

 

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = app.activeProduct
        root = design.rootComponent
        activeComp = design.activeComponent

        # Create a new occurrence.	
        newComp = adsk.core.Matrix3D.create()
        occ = root.occurrences.addNewComponent(newComp)
        # Get the associated component.
        allPartsComp = occ.component
        # Name the component
        allPartsComp.name = 'AllParts'

        stopNumber = activeComp.bRepBodies.count
        # only move the "Part[n]" bodies
        for i in range(1, stopNumber):
            body = activeComp.bRepBodies.itemByName('Part' + str(i))
            body.moveToComponent(occ)
        
        # this is where I was stuck
        # all working as expected
        success = occ.activate()
        DebugPrint(str(success))
        if not success:
            ui.messageBox('The occurrence was not activated', 'Oops!')
            return

        activeComp = design.activeComponent
        DebugPrint(allPartsComp.name)

        stopNumber = activeComp.bRepBodies.count
        for i in range(1, stopNumber + 1):
            body = activeComp.bRepBodies.itemByName('Part' + str(i))
            if body:
                body.createComponent()

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


# -------------------------------------------------------------------------
# Functions
# -------------------------------------------------------------------------
def DebugPrint(message):
    """Display a message in the Fusion 360 Text Commands window."""
    # Get the palette that represents the TEXT COMMANDS window
    app = adsk.core.Application.get()
    ui = app.userInterface
    textPalette = ui.palettes.itemById('TextCommands')

    # Make sure the palette is visible
    if not textPalette.isVisible:
        textPalette.isVisible = True

    # Write the message
    textPalette.writeText(message)