Break Link API Sample

Description

Iterates over all top-level occurrences and if it's a referenced component, it will break the link.

Code Samples

/**
 * Break Link API Sample
 * Iterates over all top-level occurrences and if it's a referenced component, it will break the link.
 */

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

function createComponents(rootComp: adsk.fusion.Component) {

  const trans = adsk.core.Matrix3D.create()
  const occ1 = rootComp.occurrences.addNewComponent(trans)
  // Get the associated component.
  const newComp = occ1.component

  // Create a new sketch on the xy plane and draw a circle.
  const sketches = newComp.sketches
  const xyPlane = newComp.xYConstructionPlane
  const sketch = sketches.add(xyPlane)
  sketch.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 5.0)

  // Create an extrusion.
  const extInput = newComp.features.extrudeFeatures.createInput(sketch.profiles.item(0), adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
  // Define that the extent is a distance extent of 5 cm
  const distance = adsk.core.ValueInput.createByReal(10.0)

  // Set the distance extent
  const distanceExtentDef = adsk.fusion.DistanceExtentDefinition.create(distance)
  extInput.setOneSideExtent(distanceExtentDef, adsk.fusion.ExtentDirections.PositiveExtentDirection)
  const ext = newComp.features.extrudeFeatures.add(extInput)

  trans.setCell(0, 3, 15.0)
  const newOcc = rootComp.occurrences.addExistingComponent(newComp, trans)
}

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

  // Get the root component of the active design.
  const rootComp = design.rootComponent

  createComponents(rootComp)

  // Build a list of occurrences because breaking a link will cause the
  // collection to be modified and causes a problem iterating over the colection.
  const occs: adsk.fusion.Occurrence[] = []
  for (let i = 0; i < rootComp.occurrences.count; i++) {
    const occ = rootComp.occurrences.item(i)
    occs.push(occ)
  }
  // Iterate through the top - level occurrences to see if any of them are external references.
  for (let occ of occs) {
    if (occ.isReferencedComponent) {
      occ.breakLink()
    }
  }
}

run();
#include <Core/Application/Application.h>
#include <Core/UserInterface/UserInterface.h>
#include <Fusion/Fusion/Design.h>
#include <Fusion/Components/Occurrence.h>
#include <Fusion/Components/Occurrences.h>
#include <Fusion/Components/Component.h>

using namespace adsk::core;
using namespace adsk::fusion;

Ptr<UserInterface> ui;

extern "C" XI_EXPORT bool run(const char* context)
{
    Ptr<Application> app = Application::get();
    if (!app)
        return false;

    ui = app->userInterface();
    if (!ui)
        return false;

    Ptr<Design> design = app->activeProduct();
    if (!design)
        return false;

    // Get the root component of the active design
    Ptr<Component> rootComp = design->rootComponent();
    if (!rootComp)
        return false;

    // Get the occurrences from the root component.
    Ptr<Occurrences> occurrences = rootComp->occurrences();
    if (!occurrences)
        return false;

    // Build an array of occurrences because breaking a link will cause the
    // collection to be modified and causes a problem iterating over the colection.
    std::vector<Ptr<Occurrence>> occs;
    for (int i = 0; i < occurrences->count(); ++i)
    {
        if (Ptr<Occurrence> occ = occurrences->item(i))
            occs.push_back(occ);
    }

    // Iterate through the top-level occurrences to see if any of them are external references.
    for (Ptr<Occurrence> occ : occs)
    {
        if (occ->isReferencedComponent())
        {
            occ->breakLink();
        }
    }

    return true;
}
# Author-
# Description-
import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        # Get the root component of the active design.
        rootComp = design.rootComponent

        # Build a list of occurrences because breaking a link will cause the
        # collection to be modified and causes a problem iterating over the colection.
        occs = []
        for occ in rootComp.occurrences:
            occs.append(occ)

        # Iterate through the top-level occurrences to see if any of them are external references.
        occ: adsk.fusion.Occurrence
        for occ in occs:
            if occ.isReferencedComponent:
                occ.breakLink()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))