Message 1 of 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am absolutely NOT familiar with the API for DirectDesign, BUT the script below works, just awfully slow. So, I would love to learn how I should do the same, using DirectDesign instead.
#Author-Le Bear
#Description-Get all the VISIBLE bodies from a selected assembly, to combine them all into one single body. The goal is to make cinematic studies easier.
from typing import NewType
import adsk.core, adsk.fusion, traceback
#lst = [] #body list
sourceBodies = adsk.core.ObjectCollection.create()
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 and bod.isSolid) :
sourceBodies.add(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 SOLID workspace and try again.')
return
# Bon, on va essayer d'ajouter des barres de progression.
progressDialog = ui.createProgressDialog()
progressDialog.cancelButtonText = 'Cancel'
progressDialog.isBackgroundTranslucent = False
progressDialog.isCancelButtonShown = True
progressDialog.hide()
# 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)
# show created dialog
progressDialog.show('Copy Bodies Progress', 'Percentage: %p, Current Value: %v, Total steps: %m', 0,sourceBodies.count, 1)
# display progress bar only after we have all bodies
# create the new component , adding " - ONE BODY" to the name
newComp = r_occs.addNewComponent(adsk.core.Matrix3D.create())
newComp.component.name = selectedComp.name + " - ONE BODY"
cpyBodies = newComp.component.features.copyPasteBodies
# Paste that first body to the new component
cpyBodies.add(sourceBodies.item(0))
# ui.messageBox('Num bodies: ' + str(len(sourceBodies)))
# I need an object collection as parameter for CreateInput, even if it contains only one body
followingBody = adsk.core.ObjectCollection.create()
# as it seems to crash if I have a few hundredth of bodies; I'll do this in a loop, one per one. Slow, but should not crash (fingers crossed !)
i = 1 # we start at 1, as 0 is already copied into the new component
cancelledFlag = False
while i < len(sourceBodies):
# firstBody MUST be a BRepBody, NOT an item from a collection. Inside the loop, it should always be the last result of combine
firstBody = newComp.component.bRepBodies.item(0)
followingBody.add(sourceBodies.item(i))
combines = rootComp.features.combineFeatures
combine_input = combines.createInput(firstBody,followingBody)
combine_input.operation = adsk.fusion.FeatureOperations.JoinFeatureOperation
combine_input.isKeepToolBodies = False
combine_input.isNewComponent = False
#returnValue = combines.add(combine_input)
combines.add(combine_input)
followingBody.clear()
i += 1
progressDialog.progressValue = i
if progressDialog.wasCancelled:
cancelledFlag = True
break
adsk.doEvents()
#combines.add(combine_input)
returnValue = newComp.component.bRepBodies.item(0)
returnValue.timelineObject.rollTo(True) #nécessaire, sinon erreur
#firstBody = returnValue
returnValue.timelineObject.rollTo(False) #nécessaire, sinon erreur
progressDialog.hide()
if cancelledFlag == False :
ui.messageBox("La combinaison des bodies en un seul est terminée avec succès.")
else:
ui.messageBox("La combinaison des bodies a été interrompue.")
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Thanks for your help,
Bernard
Solved! Go to Solution.