Traverse Bodies for Join Feature. Inconsistent results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I guess I have been looking at this for too long, I don't see problems clearly anymore.
The goal is to create a SINGLE body out of a multi components assembly which can be complex. It "seems" that it works, if there are not too many components, if the ROOT is selected. BUT, I would like this to work also if I select a component, not always the root, as some assemblies are too big/complex to always start from the root.
Here is the code so far :
from typing import NewType
import adsk.core, adsk.fusion, traceback
lst = [] #body list
def traverse(occurences, bodies):
# recursive method to get all bodies from components and sub-components
for occ in occurences:
for bod in occ.bRepBodies:
if (bod.isVisible):
lst.append(bod)
if occ.childOccurrences:
traverse(occ.childOccurrences,bodies)
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
# Get active design
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
if not design:
ui.messageBox('It is not supported in current workspace, please change to MODEL workspace and try again.')
return
# History must be captured for this to work
if design.designType == adsk.fusion.DesignTypes.DirectDesignType:
design.designType = adsk.fusion.DesignTypes.ParametricDesignType
# Get root component in this design
rootComp = design.rootComponent
selectedComp = design.activeComponent
# Get all Components below root
occs = selectedComp.occurrences
r_occs = rootComp.occurrences
bodies = selectedComp.bRepBodies
# add all bodies from all components in the collection
traverse(occs, bodies)
ui.messageBox('Num bodies: ' + str(len(bodies)))
# create the new component , adding "for STL Export" to the name
newComp = r_occs.addNewComponent(adsk.core.Matrix3D.create())
newComp.component.name = selectedComp.name + " - ONE BODY"
#num = len(lst) // STEP
cpyBodies = newComp.component.features.copyPasteBodies
sourceBodies = adsk.core.ObjectCollection.create()
# modif pour tacher d'agglutiner tout ça, même si on voit tout. On verrra après pour base feature, si possible.
sourceBodies.add(lst[000])
cpyBodies.add(sourceBodies)
#adsk.doEvents()
sourceBodies.clear()
for bod in lst:
adsk.doEvents()
sourceBodies.add(bod)
#cpyBodies.add(sourceBodies)
firstBody = newComp.component.bRepBodies.item(0)
#firstBody.name = newComp.name
combines = rootComp.features.combineFeatures
combine_input = combines.createInput(firstBody,sourceBodies)
combine_input.operation = adsk.fusion.FeatureOperations.JoinFeatureOperation
combine_input.isKeepToolBodies = False
combine_input.isNewComponent = False
returnValue = combines.add(combine_input)
adsk.doEvents()
returnValue.timelineObject.rollTo(True) #nécessaire, sinon erreur
firstBody = returnValue
returnValue.timelineObject.rollTo(False) #nécessaire, sinon erreur
On the F3D example sent, if I select (activate) "Flanc Gauche:1" component instead of the root, I get this error, which I really don't understand, as I don't get the path problem with bod.isVisible
I am sure this is "buggy", but as I said, I end up not "seeing" properly anymore. Also, I would like to learn how to do this using a base Feature, which seems to be a better way.