Delete Empty Components

Description

Deletes empty components from the active design.

Code Samples

/**
 * Delete Empty Components
 * Deletes empty components from the active design.
 */

import { adsk } from '@adsk/fusion';

function run() {

  // Get the Fusion API's application object
  const app = adsk.core.Application.get();
  if (!app) throw Error("No adsk.core.Application.");

  // Create a document.
  const doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

  // Create a document.
  const design = app.activeProduct as adsk.fusion.Design

  if (!design) {
    adsk.log('No active Fusion design')
    return
  }
  const components = design.allComponents

  // Find all of the empty components.
  // It is empty if it has no occurrences, bodies, featres, sketches, or construction.
  const rootComp = design.rootComponent
  const trans = adsk.core.Matrix3D.create()

  // Create empty components
  const numberOfEmptyComponents = 3
  for (let i = 0; i < numberOfEmptyComponents; i++) {
    rootComp.occurrences.addNewComponent(trans)
  }
  const componentsToDelete: adsk.fusion.Component[] = []

  for (const component of components) {
    //Skip the root component.
    if (rootComp == component) {
      continue
    }

    if (component.occurrences.count == 0
      && component.bRepBodies.count == 0
      && component.features.count == 0
      && component.sketches.count == 0
      && component.constructionPlanes.count == 0
      && component.constructionAxes.count == 0
      && component.constructionPoints.count == 0) {

      componentsToDelete.push(component)
    }
  }
  // Delete all immediate occurrences of the empty components.
  const deletedComponents: string[] = []
  for (let component of componentsToDelete) {

    // Get the name first because deleting the final Occurrence will delete the Component.
    const name = component.name

    // Build a list of unique immediate occurrences of the component.
    const occurrences = rootComp.allOccurrencesByComponent(component)
    const uniqueOccurrences: adsk.fusion.Occurrence[] = []

    for (const occurrence of occurrences) {
      let index = 0

      for (let k = 0; k < uniqueOccurrences.length; k++) {
        if (occurrence === uniqueOccurrences[k]) {
          break
        }
        index = k + 1
      }
      if (index == uniqueOccurrences.length) {
        uniqueOccurrences.push(occurrence)
      }

    }

    // Delete them.
    for (let uniqueOccurrencesI of uniqueOccurrences) {
      uniqueOccurrencesI.deleteMe()
    }
    deletedComponents.push(name)
  }

  let msg = ""
  if (deletedComponents.length == 0) {
    msg = 'No empty components found.'
  }
  else {
    if (deletedComponents.length > 1) {
      msg = String(deletedComponents.length) + ' empty component' + 's'
    }
    else {
      msg = String(deletedComponents.length) + ' empty component' + ' deleted'

    }
    msg += '\n\n'
    for (let deletedComponentI of deletedComponents) {
      msg += '\n' + deletedComponentI
    }

  }
  adsk.log(msg)

}
run();
#Author-Autodesk Inc.
#Description-Delete empty components from an assembly.

import adsk, adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        
        # Get all components in the active design.
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        title = 'Delete Empty Components'
        if not design:
            ui.messageBox('No active Fusion design', title)
            return

        components = design.allComponents

        # Find all of the empty components.
        # It is empty if it has no occurrences, bodies, featres, sketches, or construction.
        root = design.rootComponent
        componentsToDelete = []

        for component in components:

            # Skip the root component.
            if root == component:
                continue

            if component.occurrences.count == 0 \
                and component.bRepBodies.count == 0 \
                and component.features.count == 0 \
                and component.sketches.count == 0 \
                and component.constructionPlanes.count == 0 \
                and component.constructionAxes.count == 0 \
                and component.constructionPoints.count == 0:

                componentsToDelete.append(component)

        # Delete all immediate occurrences of the empty components.
        deletedComponents = []
        for component in componentsToDelete:

            # Get the name first because deleting the final Occurrence will delete the Component.
            name = component.name

            # Build a list of unique immediate occurrences of the component.
            occurrences = root.allOccurrencesByComponent(component)
            uniqueOccurrences = []
            for occurrence in occurrences:
                index = 0
                for k in range(0, len(uniqueOccurrences)):
                    if occurrence is uniqueOccurrences[k]:
                        break
                    index = k+1
                if index == len(uniqueOccurrences):
                    uniqueOccurrences.append(occurrence)

            # Delete them.
            for uniqueOccurrencesI in uniqueOccurrences:
                uniqueOccurrencesI.deleteMe()

            deletedComponents.append(name)

        if len(deletedComponents) == 0:
            msg = 'No empty components found.'
        else:
            if len(deletedComponents) > 1:
                msg = str(len(deletedComponents)) + ' empty component' + 's'
            else:
                msg = str(len(deletedComponents)) + ' empty component' + ' deleted'
            msg += '\n\n'
            for deletedComponentI in deletedComponents:
                msg += '\n' + deletedComponentI

        ui.messageBox(msg, title)

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