Analyze Interference API Sample

Description

Demonstrates analyzing the interference between components. This uses a direct modeling design because the ability to create bodies that represent the interference volume is only supported in a direct modeling design.

Code Samples

/**
 * Analyze Interference API Sample
 * Demonstrates analyzing the interference between components. This uses a direct modeling design because the ability to create bodies that represent the interference volume is only supported in a direct modeling 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)

  const design = app.activeProduct as adsk.fusion.Design;

  // Set the design type to DirectDesignType (for non-parametric modelling)
  design.designType = adsk.fusion.DesignTypes.DirectDesignType

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

  // Create the first component - containing a box
  const occurrences = rootComp.occurrences
  const matrix = adsk.core.Matrix3D.create()
  const firstComponentOccurrence = occurrences.addNewComponent(matrix) as adsk.fusion.Occurrence
  if (!firstComponentOccurrence) throw new Error("firstComponentOccurrence is null");

  // Create sketch
  const sketches0 = firstComponentOccurrence.component.sketches
  const sketch0 = sketches0.add(rootComp.xZConstructionPlane) as adsk.fusion.Sketch

  // Create a rectangle
  const sketchLines0 = sketch0.sketchCurves.sketchLines
  const startPoint0 = adsk.core.Point3D.create(0, 0, 0) as adsk.core.Point3D
  const endPoint0 = adsk.core.Point3D.create(5, 5, 0) as adsk.core.Point3D
  sketchLines0.addTwoPointRectangle(startPoint0, endPoint0)

  // Get the profile defined by the rectangle
  const prof0 = sketch0.profiles.item(0) as adsk.fusion.Profile

  // Create an extrusion input for the profile.
  const features0 = firstComponentOccurrence.component.features
  const extrudes0 = features0.extrudeFeatures
  const extInput0 = extrudes0.createInput(prof0, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) as adsk.fusion.ExtrudeFeatureInput

  // Define that the extent of the extrusion is a distance extent of 5 cm.
  const distance0 = adsk.core.ValueInput.createByReal(5) as adsk.core.ValueInput
  const distanceExtentDef0 = adsk.fusion.DistanceExtentDefinition.create(distance0) as adsk.fusion.DistanceExtentDefinition
  extInput0.setOneSideExtent(distanceExtentDef0, adsk.fusion.ExtentDirections.PositiveExtentDirection)

  // Create the extrusion.
  const ext0 = extrudes0.add(extInput0)

  // Create the second component - containing a box that overlaps the box in the first component
  const secondComponentOccurrence = occurrences.addNewComponent(matrix) as adsk.fusion.Occurrence

  // Create sketch
  const sketches1 = secondComponentOccurrence.component.sketches
  const sketch1 = sketches1.add(rootComp.xZConstructionPlane) as adsk.fusion.Sketch

  // Create a rectangle
  const sketchLines1 = sketch1.sketchCurves.sketchLines
  const startPoint1 = adsk.core.Point3D.create(3, 3, 0) as adsk.core.Point3D
  const endPoint1 = adsk.core.Point3D.create(8, 8, 0) as adsk.core.Point3D
  sketchLines1.addTwoPointRectangle(startPoint1, endPoint1)
  // Get the profile defined by the rectangle
  const prof1 = sketch1.profiles.item(0) as adsk.fusion.Profile

  // Create an extrusion input for the profile.
  const features1 = secondComponentOccurrence.component.features
  const extrudes1 = features1.extrudeFeatures
  const extInput1 = extrudes1.createInput(prof1, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) as adsk.fusion.ExtrudeFeatureInput

  // Define that the extent of the extrusion is a distance extent of 5 cm.
  const distance1 = adsk.core.ValueInput.createByReal(5) as adsk.core.ValueInput
  const distanceExtentDef1 = adsk.fusion.DistanceExtentDefinition.create(distance1) as adsk.fusion.DistanceExtentDefinition
  extInput1.setOneSideExtent(distanceExtentDef1, adsk.fusion.ExtentDirections.PositiveExtentDirection)


  // Create the extrusion.
  const ext1 = extrudes1.add(extInput1)

  // Create the third component - containing a cylinder that overlaps the box in the second component
  const thirdComponentOccurrence = occurrences.addNewComponent(matrix) as adsk.fusion.Occurrence

  // Create sketch
  const sketches2 = thirdComponentOccurrence.component.sketches
  const sketch2 = sketches2.add(rootComp.xZConstructionPlane) as adsk.fusion.Sketch

  // Create a circle
  const sketchCircles2 = sketch2.sketchCurves.sketchCircles
  const centerPoint2 = adsk.core.Point3D.create(8, 8, 0) as adsk.core.Point3D
  sketchCircles2.addByCenterRadius(centerPoint2, 2)

  // Get the profile defined by the circle
  const prof2 = sketch2.profiles.item(0) as adsk.fusion.Profile

  // Create an extrusion input for the profile.
  const features2 = thirdComponentOccurrence.component.features
  const extrudes2 = features2.extrudeFeatures
  const extInput2 = extrudes2.createInput(prof2, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) as adsk.fusion.ExtrudeFeatureInput

  // Define that the extent of the extrusion is a distance extent of 5 cm.
  const distance2 = adsk.core.ValueInput.createByReal(5) as adsk.core.ValueInput
  const distanceExtentDef2 = adsk.fusion.DistanceExtentDefinition.create(distance2) as adsk.fusion.DistanceExtentDefinition
  extInput2.setOneSideExtent(distanceExtentDef2, adsk.fusion.ExtentDirections.PositiveExtentDirection)

  // Create the extrusion.
  const ext2 = extrudes2.add(extInput2)

  // Create a collection of the components to check for interference
  const inputOccurrences = adsk.core.ObjectCollection.create()
  inputOccurrences.add(firstComponentOccurrence)
  inputOccurrences.add(secondComponentOccurrence)
  inputOccurrences.add(thirdComponentOccurrence)

  // Create the interferenceInput object and run the analysis.
  const interferenceInput = design.createInterferenceInput(inputOccurrences) as adsk.fusion.InterferenceInput
  interferenceInput.areCoincidentFacesIncluded = false
  const results = design.analyzeInterference(interferenceInput)

  // Create bodies for every intersection.This is not supported in Parametric designs.
  const interferenceBodies = results.createBodies(true) as adsk.core.ObjectCollection

  // Activate the Intersections component created by Fusion that stores the interference bodies
  const resultsOccurrence = occurrences.item(occurrences.count - 1) as adsk.fusion.Occurrence
  resultsOccurrence.activate()

  // Fit the view
  const viewport = app.activeViewport
  viewport.fit()

  // Report the results
  for (let i = 0; i < results.count; i++) {
    const res = results.item(i) as adsk.fusion.InterferenceResult
    const comp1Name = (res.entityOne as adsk.fusion.BRepBody).parentComponent.name
    const comp2Name = (res.entityTwo as adsk.fusion.BRepBody).parentComponent.name
    const bodyVolume = res.interferenceBody.volume.toFixed(2);
    const body = interferenceBodies.item(i) as adsk.fusion.BRepBody;
    body.name = 'Interference between ' + comp1Name + ' & ' + comp2Name
    adsk.log('There is interference between ' + comp1Name + ' and ' + comp2Name + ' with a volume of ' + bodyVolume + ' cubic centimeters')

  }
}
run();
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <sstream>
#include <iomanip>

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();

    // Create a new document.
    Ptr<Documents> documents = app->documents();
    if (!documents)
        return false;
    Ptr<Document> doc = documents->add(DocumentTypes::FusionDesignDocumentType);
    if (!doc)
        return false;

    Ptr<Product> product = app->activeProduct();
    if (!product)
        return false;

    Ptr<Design> design = product;
    if (!design)
        return false;

    // Set the design type to DirectDesignType (for non-parametric modelling)
    design->designType(adsk::fusion::DesignTypes::DirectDesignType);

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

    // Create the first component - containing a box
    Ptr<Occurrences> occurrences = rootComp->occurrences();
    if (!occurrences)
        return false;
    Ptr<Matrix3D> matrix = adsk::core::Matrix3D::create();
    if (!matrix)
        return false;
    Ptr<Occurrence> firstComponentOccurrence = occurrences->addNewComponent(matrix);
    if (!firstComponentOccurrence)
        return false;

    // Create sketch
    Ptr<Sketches> sketches = firstComponentOccurrence->component()->sketches();
    if (!sketches)
        return false;
    Ptr<Sketch> sketch = sketches->add(rootComp->xZConstructionPlane());
    if (!sketch)
        return false;

    // Create a rectangle
    Ptr<SketchLines> sketchLines = sketch->sketchCurves()->sketchLines();
    if (!sketchLines)
        return false;
    Ptr<Point3D> startPoint = adsk::core::Point3D::create(0, 0, 0);
    if (!startPoint)
        return false;
    Ptr<Point3D> endPoint = adsk::core::Point3D::create(5, 5, 0);
    if (!endPoint)
        return false;
    sketchLines->addTwoPointRectangle(startPoint, endPoint);

    // Get the profile defined by the rectangle
    Ptr<Profiles> profiles = sketch->profiles();
    if (!profiles)
        return false;
    Ptr<Profile> prof = profiles->item(0);
    if (!prof)
        return false;

    // Create an extrusion input for the profile.
    Ptr<Features> features = firstComponentOccurrence->component()->features();
    if (!features)
        return false;
    Ptr<ExtrudeFeatures> extrudes = features->extrudeFeatures();
    if (!extrudes)
        return false;
    Ptr<ExtrudeFeatureInput> extInput =
        extrudes->createInput(prof, adsk::fusion::FeatureOperations::NewBodyFeatureOperation);
    if (!extInput)
        return false;

    // Define that the extent of the extrusion is a distance extent of 5 cm.
    Ptr<ValueInput> distance = adsk::core::ValueInput::createByReal(5);
    if (!distance)
        return false;
    extInput->setDistanceExtent(false, distance);

    // Create the extrusion.
    Ptr<ExtrudeFeature> ext = extrudes->add(extInput);
    if (!ext)
        return false;

    // Create the second component - containing a box that overlaps the box in the first component
    Ptr<Occurrence> secondComponentOccurrence = occurrences->addNewComponent(matrix);
    if (!secondComponentOccurrence)
        return false;

    // Create sketch
    sketches = secondComponentOccurrence->component()->sketches();
    sketch = sketches->add(rootComp->xZConstructionPlane());

    // Create a rectangle
    sketchLines = sketch->sketchCurves()->sketchLines();
    startPoint = adsk::core::Point3D::create(3, 3, 0);
    endPoint = adsk::core::Point3D::create(8, 8, 0);
    sketchLines->addTwoPointRectangle(startPoint, endPoint);

    // Get the profile defined by the rectangle
    prof = sketch->profiles()->item(0);

    // Create an extrusion input for the profile.
    features = secondComponentOccurrence->component()->features();
    extrudes = features->extrudeFeatures();
    extInput = extrudes->createInput(prof, adsk::fusion::FeatureOperations::NewBodyFeatureOperation);

    // Define that the extent of the extrusion is a distance extent of 5 cm.
    distance = adsk::core::ValueInput::createByReal(5);
    extInput->setDistanceExtent(false, distance);

    // Create the extrusion.
    ext = extrudes->add(extInput);

    // Create the third component - containing a cylinder that overlaps the box in the second component
    Ptr<Occurrence> thirdComponentOccurrence = occurrences->addNewComponent(matrix);
    if (!thirdComponentOccurrence)
        return false;

    // Create sketch
    sketches = thirdComponentOccurrence->component()->sketches();
    sketch = sketches->add(rootComp->xZConstructionPlane());

    // Create a circle
    Ptr<SketchCircles> sketchCircles = sketch->sketchCurves()->sketchCircles();
    if (!sketchCircles)
        return false;
    Ptr<Point3D> centerPoint = adsk::core::Point3D::create(8, 8, 0);
    if (!centerPoint)
        return false;
    sketchCircles->addByCenterRadius(centerPoint, 2);

    // Get the profile defined by the circle
    prof = sketch->profiles()->item(0);

    // Create an extrusion input for the profile.
    features = thirdComponentOccurrence->component()->features();
    extrudes = features->extrudeFeatures();
    extInput = extrudes->createInput(prof, adsk::fusion::FeatureOperations::NewBodyFeatureOperation);

    // Define that the extent of the extrusion is a distance extent of 5 cm.
    distance = adsk::core::ValueInput::createByReal(5);
    extInput->setDistanceExtent(false, distance);

    // Create the extrusion.
    ext = extrudes->add(extInput);

    // Create a collection of the components to check for interference
    Ptr<ObjectCollection> inputOccurrences = adsk::core::ObjectCollection::create();
    if (!inputOccurrences)
        return false;
    inputOccurrences->add(firstComponentOccurrence);
    inputOccurrences->add(secondComponentOccurrence);
    inputOccurrences->add(thirdComponentOccurrence);

    // Create the interferenceInput object and run the analysis.
    Ptr<InterferenceInput> interferenceInput = design->createInterferenceInput(inputOccurrences);
    if (!interferenceInput)
        return false;
    interferenceInput->areCoincidentFacesIncluded(false);
    Ptr<InterferenceResults> results = design->analyzeInterference(interferenceInput);
    if (!results)
        return false;

    // Create bodies for every intersection.  This is not supported in Parametric designs.
    Ptr<ObjectCollection> interferenceBodies = results->createBodies(true);
    if (!interferenceBodies)
        return false;

    Ptr<Occurrence> resultsOccurrence = occurrences->item(occurrences->count() - 1);
    if (!resultsOccurrence)
        return false;
    resultsOccurrence->activate();

    // Fit the view
    Ptr<Viewport> viewport = app->activeViewport();
    if (!viewport)
        return false;
    viewport->fit();

    // Report the results
    int bodCount = 0;
    for (Ptr<InterferenceResult> result : results)
    {
        Ptr<BRepBody> entOneBody = result->entityOne();
        if (!entOneBody)
            return false;
        std::string comp1Name = entOneBody->parentComponent()->name();

        Ptr<BRepBody> entTwoBody = result->entityTwo();
        if (!entTwoBody)
            return false;
        std::string comp2Name = entTwoBody->parentComponent()->name();

        double bodyVolume = result->interferenceBody()->volume();
        std::stringstream bodyVolumeString;
        bodyVolumeString << std::setprecision(2) << std::fixed << bodyVolume;

        Ptr<BRepBody> interferenceBody = interferenceBodies->item(bodCount);
        if (!interferenceBody)
            return false;
        bodCount++;
        interferenceBody->name("Interference between " + comp1Name + " & " + comp2Name);
        ui->messageBox(
            "There is interference between " + comp1Name + " and " + comp2Name + " with a volume of " +
            bodyVolumeString.str() + " cubic centimeters");
    }

    return true;
}
import adsk.core, adsk.fusion, traceback

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

        # Create a new document.
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
 
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        
        # Set the design type to DirectDesignType (for non-parametric modelling)        
        design.designType = adsk.fusion.DesignTypes.DirectDesignType

        # Get the root component of the active design
        rootComp = design.rootComponent
        
        # Create the first component - containing a box
        occurrences = rootComp.occurrences
        matrix = adsk.core.Matrix3D.create()
        firstComponentOccurrence = occurrences.addNewComponent(matrix)                
        
        # Create sketch
        sketches = firstComponentOccurrence.component.sketches
        sketch = sketches.add(rootComp.xZConstructionPlane)        
        
        # Create a rectangle
        sketchLines = sketch.sketchCurves.sketchLines
        startPoint = adsk.core.Point3D.create(0, 0, 0)
        endPoint = adsk.core.Point3D.create(5, 5, 0)
        sketchLines.addTwoPointRectangle(startPoint, endPoint)
        
        # Get the profile defined by the rectangle
        prof = sketch.profiles.item(0)
        
        # Create an extrusion input for the profile.
        features = firstComponentOccurrence.component.features
        extrudes = features.extrudeFeatures
        extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        
        # Define that the extent of the extrusion is a distance extent of 5 cm.
        distance = adsk.core.ValueInput.createByReal(5)
        extInput.setDistanceExtent(False, distance)
        
        # Create the extrusion.
        ext = extrudes.add(extInput)

        # Create the second component - containing a box that overlaps the box in the first component
        secondComponentOccurrence = occurrences.addNewComponent(matrix)                
        
        # Create sketch
        sketches = secondComponentOccurrence.component.sketches
        sketch = sketches.add(rootComp.xZConstructionPlane)        
        
        # Create a rectangle
        sketchLines = sketch.sketchCurves.sketchLines
        startPoint = adsk.core.Point3D.create(3, 3, 0)
        endPoint = adsk.core.Point3D.create(8, 8, 0)
        sketchLines.addTwoPointRectangle(startPoint, endPoint)
        # Get the profile defined by the rectangle
        prof = sketch.profiles.item(0)
        
        # Create an extrusion input for the profile.
        features = secondComponentOccurrence.component.features
        extrudes = features.extrudeFeatures
        extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        
        # Define that the extent of the extrusion is a distance extent of 5 cm.
        distance = adsk.core.ValueInput.createByReal(5)
        extInput.setDistanceExtent(False, distance)
        
        # Create the extrusion.
        ext = extrudes.add(extInput)
        
        # Create the third component - containing a cylinder that overlaps the box in the second component
        thirdComponentOccurrence = occurrences.addNewComponent(matrix)                
        
        # Create sketch
        sketches = thirdComponentOccurrence.component.sketches
        sketch = sketches.add(rootComp.xZConstructionPlane)        
        
        # Create a circle
        sketchCircles = sketch.sketchCurves.sketchCircles
        centerPoint = adsk.core.Point3D.create(8, 8, 0)
        sketchCircles.addByCenterRadius(centerPoint, 2)
        
        # Get the profile defined by the circle
        prof = sketch.profiles.item(0)
        
        # Create an extrusion input for the profile.
        features = thirdComponentOccurrence.component.features
        extrudes = features.extrudeFeatures
        extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        
        # Define that the extent of the extrusion is a distance extent of 5 cm.
        distance = adsk.core.ValueInput.createByReal(5)
        extInput.setDistanceExtent(False, distance)
        
        # Create the extrusion.
        ext = extrudes.add(extInput)    
        
        # Create a collection of the components to check for interference
        inputOccurrences = adsk.core.ObjectCollection.create()
        inputOccurrences.add(firstComponentOccurrence)
        inputOccurrences.add(secondComponentOccurrence)
        inputOccurrences.add(thirdComponentOccurrence)
        
        # Create the interferenceInput object and run the analysis.
        interferenceInput = design.createInterferenceInput(inputOccurrences)
        interferenceInput.areCoincidentFacesIncluded = False
        results = design.analyzeInterference(interferenceInput)    
        
        # Create bodies for every intersection.  This is not supported in Parametric designs.
        interferenceBodies = results.createBodies(True)
        
        # Activate the Intersections component created by Fusion that stores the interference bodies
        resultsOccurrence = occurrences.item(occurrences.count-1)
        resultsOccurrence.activate()
        
        # Fit the view        
        viewport = app.activeViewport
        viewport.fit()
        
        # Report the results
        bod = 0        
        for res in results:
            comp1Name = res.entityOne.parentComponent.name
            comp2Name = res.entityTwo.parentComponent.name
            bodyVolume = str(round(res.interferenceBody.volume, 2))
            interferenceBodies.item(bod).name = 'Interference between ' + comp1Name + ' & ' + comp2Name
            ui.messageBox('There is interference between ' + comp1Name + ' and ' + comp2Name + ' with a volume of ' + bodyVolume + ' cubic centimeters')
            bod += 1
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))