- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
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)
Solved! Go to Solution.