Draft Feature API Sample

Description

Demonstrates creating a new draft feature.

Code Samples

import adsk.core, adsk.fusion, traceback

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)
        
        # Get root component in this design
        rootComp = design.rootComponent

        # Get the first face of box
        face1 = rootComp.bRepBodies.item(0).faces.item(0)
        
        # Get all faces which connect to the first face
        connectedFaces = [];
        for edge in face1.edges:
            for face in edge.faces:
                if face1 != face:
                    connectedFaces.append(face)
        
        # Create draft feature
        drafts = rootComp.features.draftFeatures
        draftFeatInput = drafts.createInput(connectedFaces, face1, True)
        draftFeatInput.isDirectionFlipped = False
        angle = adsk.core.ValueInput.createByString("10 deg")
        draftFeatInput.setSingleAngle(True, angle)
        drafts.add(draftFeatInput)       
        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <Cam/CamAll.h>

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

Ptr<Application> app;
Ptr<UserInterface> ui;

extern "C" XI_EXPORT bool run(const char* context)
{
    // Get the application
    app = Application::get();
    if (!app)
        return false;

    // Get the UserInterface
    ui = app->userInterface();
    if (!ui)
        return false;

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

    // Get the design which is the active product
    Ptr<Design> design = product;
    if (!design)
        return false;

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

    // Get the first face of box
    Ptr<BRepFace> face1 = rootComp->bRepBodies()->item(0)->faces()->item(0);
    if (!face1)
        return false;

    // Get all faces which connect to the first face
    std::vector<Ptr<BRepFace>> connectedFaces;
    Ptr<BRepEdges> edges = face1->edges();
    for (size_t i = 0; i < edges->count(); ++i)
    {
        Ptr<BRepEdge> edge = edges->item(i);
        Ptr<BRepFaces> faces = edge->faces();
        for (size_t j = 0; j < faces->count(); ++j)
        {
            Ptr<BRepFace> face = faces->item(j);
            if (face != face1)
                connectedFaces.push_back(face);
        }
    }

    // Create draft feature collection
    Ptr<DraftFeatures> drafts = rootComp->features()->draftFeatures();
    if (!drafts)
        return false;

    // Create the input for the draft feature. The first argument is the list of faces to draft, the second is the face
    // that defines the direction of the draft, and the last argument is whether to draft outward (true) or inward
    // (false).
    Ptr<DraftFeatureInput> draftFeatInput = drafts->createInput(connectedFaces, face1, true);
    if (!draftFeatInput)
        return false;

    // Set the direction of the draft to be flipped
    draftFeatInput->isDirectionFlipped(false);
    Ptr<ValueInput> angle = ValueInput::createByString("10 deg");
    draftFeatInput->setSingleAngle(true, angle);

    // Create the draft feature
    Ptr<DraftFeature> draftFeature = drafts->add(draftFeatInput);
    if (!draftFeature)
        return false;


    return true;
}
/**
 * Draft Feature API Sample
 * Demonstrates creating a new draft feature.
 */

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 design = app.activeProduct as adsk.fusion.Design

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

  // Get the first face of box
  const face1 = rootComp.bRepBodies.item(0).faces.item(0)

  // Get all faces which connect to the first face
  const connectedFaces = [];

  for(let i= 0; i < face1.edges.count; i++){
    const edge = face1.edges.item(i)
    for(let j= 0; j < edge.faces.count; j++){
      const face = edge.faces.item(j)
      if (face1?.tempId != face?.tempId){
        connectedFaces.push(face)
      }
    }
  }

  // Create draft feature
   const drafts = rootComp.features.draftFeatures
   const draftFeatInput = drafts.createInput(connectedFaces, face1, true)
   draftFeatInput.isDirectionFlipped = false
   const angle = adsk.core.ValueInput.createByString("10 deg")
   draftFeatInput.setSingleAngle(true, angle)
   drafts.add(draftFeatInput)

}

run();