Fold Feature Sample

Description

Demonstrates creating a new sheet metal fold feature.

Before running: Open or create a sheet metal design in the DESIGN workspace, unless the script builds its own sheet metal body.

Code Samples

"""Fold Feature API sample — demonstrates creating and configuring fold features.

The document must contain:
  - A sheet metal body at bRepBodies.item(0)
  - A flat face suitable as the stationary face
  - A sketch with at least two sketch lines for bend lines

Adjust STATIONARY_FACE_IDX, LINE0_IDX, and LINE1_IDX to match your document.
"""

import traceback
import math
import adsk.core
import adsk.fusion

app = adsk.core.Application.get()

BEND_ANGLE_DEG = 90.0
BEND_ANGLE_RAD = math.radians(BEND_ANGLE_DEG)

STATIONARY_FACE_IDX = 9
LINE0_IDX = 0
LINE1_IDX = 1


def clearFoldFeatures(features):
    """Delete all fold features so indices are stable."""
    ff = features.foldFeatures
    while ff.count > 0:
        ff.item(0).deleteMe()
        ff = features.foldFeatures
    return ff


def run(_context):
    """Documentation sample: Create a fold feature with bend lines."""
    try:
        design = adsk.fusion.Design.cast(app.activeProduct)
        rootComp = design.rootComponent

         Get the sheet metal body — located in the first child component
        occurrence = rootComp.occurrences.item(0)
        if occurrence is None:
            app.log("FoldSample: No child component found.")
            return
        comp1 = occurrence.component
        if comp1 is None:
            app.log("FoldSample: Could not get component from occurrence.")
            return

         Clean up any existing fold features
        features = comp1.features
        clearFoldFeatures(features)

         Get the sheet metal body
        body = comp1.bRepBodies.item(0)
        if body is None:
            app.log("FoldSample: No body found.")
            return

         Select the stationary face — the face that doesn't move when folding
        stationary_face = body.faces.item(STATIONARY_FACE_IDX)
        if stationary_face is None:
            app.log(f"FoldSample: No face at index {STATIONARY_FACE_IDX}.")
            return

         Get the sketch lines that define bend lines
        sketch = rootComp.sketches.item(0)
        if sketch is None:
            app.log("FoldSample: No sketch found.")
            return

        sketch_lines = sketch.sketchCurves.sketchLines
        line0 = sketch_lines.item(LINE0_IDX)
        line1 = sketch_lines.item(LINE1_IDX)
        if line0 is None or line1 is None:
            app.log(f"FoldSample: Could not get sketch lines at indices {LINE0_IDX}, {LINE1_IDX}.")
            return

         Create a fold feature
        foldFeatures = features.foldFeatures
        inp = foldFeatures.createInput(stationary_face)
        inp.isUseCornerRelief = True

         Add bend lines — each line specifies a fold location and angle
        bendLines = inp.bendLines
        angleVal = adsk.core.ValueInput.createByReal(BEND_ANGLE_RAD)
        bendLines.add(
            line0,
            angleVal,
            adsk.fusion.FoldBendLinePositionTypes.StartFoldBendLinePositionType,
            True)
        bendLines.add(
            line1,
            angleVal,
            adsk.fusion.FoldBendLinePositionTypes.StartFoldBendLinePositionType,
            True)

         Create the fold feature
        feature = foldFeatures.add(inp)
        app.log(f'FoldSample completed: fold feature "{feature.name}" created.')

    except Exception:
        app.log(f'FoldSample failed:\n{traceback.format_exc()}')
// Fold Feature API sample — demonstrates creating a fold feature on a sheet
// metal body.
//
// The document must contain:
//   component1 (first child occurrence of the root component):
//     - a sheet metal body at bRepBodies.item(0)
//     - faces.item(STATIONARY_FACE_IDX) suitable as the stationary face
//   root component:
//     - a sketch at sketches.item(0) with at least two sketch lines
//
// Sample data: Neutron/Test/APITest/APISampleSheetMetalFold.f3d
// Adjust STATIONARY_FACE_IDX, LINE0_IDX, and LINE1_IDX to match your document.

#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>

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

static const size_t STATIONARY_FACE_IDX = 9;
static const size_t LINE0_IDX = 0;
static const size_t LINE1_IDX = 1;
static const double BEND_ANGLE_RAD = 1.5707963267948966; // 90 degrees

Ptr<Application> app;

#define CHECK(expr)                                                                                                    \
    if (!(expr))                                                                                                       \
    {                                                                                                                  \
        app->log("FAIL: " #expr);                                                                                      \
        return false;                                                                                                  \
    }
#define CHECK_PTR(ptr) CHECK((ptr) != nullptr)

// Delete any existing fold features so face and sketch indices are stable.
static bool clearFoldFeatures(const Ptr<Features>& features)
{
    auto foldFeatures = features->foldFeatures();
    CHECK_PTR(foldFeatures);
    while (foldFeatures->count() > 0)
    {
        auto feat = foldFeatures->item(0);
        CHECK_PTR(feat);
        CHECK(feat->deleteMe());
        foldFeatures = features->foldFeatures();
        CHECK_PTR(foldFeatures);
    }
    CHECK(foldFeatures->count() == 0);
    return true;
}

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

    auto product = app->activeProduct();
    if (!product)
        return false;
    auto design = product->cast<Design>();
    if (!design)
        return false;
    auto rootComp = design->rootComponent();
    if (!rootComp)
        return false;

     The body and sketch live in component1, the first child occurrence of root
    auto occurrences = rootComp->occurrences();
    if (!occurrences || occurrences->count() == 0)
        return false;
    auto occurrence = occurrences->item(0);
    if (!occurrence)
        return false;
    auto comp1 = occurrence->component();
    if (!comp1)
        return false;

     Get the sheet metal body
    auto bodies = comp1->bRepBodies();
    if (!bodies || bodies->count() == 0)
        return false;
    auto body = bodies->item(0);
    if (!body)
        return false;

     Select the stationary face — the face that will not move when folding
    auto faces = body->faces();
    if (!faces)
        return false;
    auto stationaryFace = faces->item(STATIONARY_FACE_IDX);
    if (!stationaryFace)
        return false;

     Get the sketch lines that define bend lines
    auto sketches = rootComp->sketches();
    if (!sketches || sketches->count() == 0)
        return false;
    auto sketch = sketches->item(0);
    if (!sketch)
        return false;

    auto sketchLines = sketch->sketchCurves()->sketchLines();
    if (!sketchLines || sketchLines->count() < 2)
        return false;
    auto line0 = sketchLines->item(LINE0_IDX);
    auto line1 = sketchLines->item(LINE1_IDX);
    if (!line0 || !line1)
        return false;

    auto features = comp1->features();
    if (!features)
        return false;

     Clean up any existing fold features
    clearFoldFeatures(features);

     Create a fold feature
    auto foldFeatures = features->foldFeatures();
    if (!foldFeatures)
        return false;

    auto inp = foldFeatures->createInput(stationaryFace);
    if (!inp)
        return false;
    inp->isUseCornerRelief(true);

     Add bend lines — each line specifies a fold location and angle
    auto bendLines = inp->bendLines();
    if (!bendLines)
        return false;

    auto angleVal = ValueInput::createByReal(BEND_ANGLE_RAD);
    auto bl0 = bendLines->add(line0, angleVal, FoldBendLinePositionTypes::StartFoldBendLinePositionType, true);
    if (!bl0)
        return false;

    auto bl1 = bendLines->add(line1, angleVal, FoldBendLinePositionTypes::StartFoldBendLinePositionType, true);
    if (!bl1)
        return false;

     Create the fold feature
    auto feature = foldFeatures->add(inp);
    if (!feature)
        return false;

    app->log("FoldSample completed: fold feature '" + feature->name() + "' created.");
    return true;
}