I've had some success with this - but still not satisfactory.
I think the key piece of information lies in the guide for selectionSets.add method:
https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-35C13BD1-39BA-48CA-BFC0-DB6BBCDE3B70
When describing the array that forms an argument to the function:
An array of entities that will be in the created selection set. All entities must be in the context of the root component. This means if the entity isn't directly owned by the root component, it must be a proxy.
Referring to the attached model - the root component contains a sketch and a body.
There are other components (and their occurrences) under the root of course.
I am able to create an empty selectionSet, and I can create a selectionSet of the bodies under the root.
I am still failing to create a selectionSet containing an occurrence (or anything under an occurrence).
import adsk.core
import adsk.fusion
import traceback
app = None
ui = None
design:adsk.fusion.Design = None
def isDesignLoaded():
root = design.rootComponent
occs = root.allOccurrences
if len(occs) == 0:
return False
else:
return True
def run(context):
global app, ui, design
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
root:adsk.fusion.Component = design.rootComponent
if not isDesignLoaded():
ui.messageBox('There are no components in this design.')
return
newEmptyEntity:adsk.core.Base = []
newBodiesEntity:adsk.core.Base = []
newOccsEntity:adsk.core.Base = []
adsk.doEvents()
design.selectionSets.add(newEmptyEntity,'NewEmpty')
for body in root.bRepBodies:
newBodiesEntity.append(body)
design.selectionSets.add(newBodiesEntity,'NewBodies')
for occ in root.occurrences:
newOccsEntity.append(occ)
design.selectionSets.add(newOccsEntity,'NewOccurrences')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
design.selectionSets.add(newOccsEntity,'NewOccurrences') fails with:
RuntimeError: 2 : InternalValidationError : owningCompOfEntity
is there something I need to do with "createForAssemblyContext" ?