Unfold Feature Sample

Description

Demonstrates creating a sheet metal unfold feature using BRepBody.getBendFaces.

Code Samples

"""Unfold Feature API sample — creates an unfold feature on a sheet metal body.

Requires a design with a sheet metal body that has at least one bend.
Sample data: Neutron/Test/APITest/APISampleSheetMetalUnFold.f3d
"""

import traceback

import adsk.core
import adsk.fusion

app = adsk.core.Application.get()

STATIONARY_FACE_IDX = 34


def run(_context):
    try:
        design = adsk.fusion.Design.cast(app.activeProduct)
        root_comp = design.rootComponent

        body = root_comp.bRepBodies.item(0)
        if body is None or not body.isSheetMetal:
            app.log("UnfoldSample skipped: no sheet metal body in the active design.")
            return

        found, inner_bends, outer_bends = body.getBendFaces()
        if not found or not inner_bends or not outer_bends:
            app.log("UnfoldSample skipped: no bend faces on sheet metal body.")
            return

        stationary_face = body.faces.item(STATIONARY_FACE_IDX)
        if stationary_face is None:
            app.log(f"UnfoldSample skipped: no stationary face at index {STATIONARY_FACE_IDX}.")
            return

        unfold_features = root_comp.features.unfoldFeatures
        inp = unfold_features.createInput(stationary_face)
        inp.isUnfoldAllBends = False

        all_bends = []
        for inner, outer in zip(inner_bends, outer_bends):
            all_bends.append(inner)
            all_bends.append(outer)
        inp.bendFaces = all_bends

        feature = unfold_features.add(inp)
        assert feature is not None

        app.log(f"UnfoldSample completed: unfold '{feature.name}'.")

    except Exception:
        app.log(f"UnfoldSample failed:\n{traceback.format_exc()}")
// Unfold Feature API sample — creates an unfold feature on a sheet metal body.
//
// Requires a design with a sheet metal body that has at least one bend.
// Sample data: Neutron/Test/APITest/APISampleSheetMetalUnFold.f3d

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

#include <vector>

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

namespace
{
Ptr<Application> app;

const size_t STATIONARY_FACE_IDX = 34;

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

} // namespace

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

    auto product = app->activeProduct();
    CHECK_PTR(product);
    auto design = product->cast<Design>();
    CHECK_PTR(design);
    auto rootComp = design->rootComponent();
    CHECK_PTR(rootComp);

    auto bodies = rootComp->bRepBodies();
    CHECK_PTR(bodies);
    auto body = bodies->item(0);
    CHECK_PTR(body);
    CHECK(body->isSheetMetal());

    std::vector<Ptr<BRepFace>> innerBends;
    std::vector<Ptr<BRepFace>> outerBends;
    if (!body->getBendFaces(innerBends, outerBends))
    {
        app->log("UnfoldSample skipped: no bend faces on sheet metal body.");
        return true;
    }

    auto faces = body->faces();
    CHECK_PTR(faces);
    auto stationaryFace = faces->item(STATIONARY_FACE_IDX);
    CHECK_PTR(stationaryFace);

    auto features = rootComp->features();
    CHECK_PTR(features);
    auto unfoldFeatures = features->unfoldFeatures();
    CHECK_PTR(unfoldFeatures);

    auto inp = unfoldFeatures->createInput(stationaryFace);
    CHECK_PTR(inp);
    CHECK(inp->isUnfoldAllBends(false));

    std::vector<Ptr<BRepFace>> allBends;
    allBends.reserve(innerBends.size() + outerBends.size());
    for (size_t i = 0; i < innerBends.size(); ++i)
    {
        allBends.push_back(innerBends[i]);
        allBends.push_back(outerBends[i]);
    }
    CHECK(inp->bendFaces(allBends));

    auto feature = unfoldFeatures->add(inp);
    CHECK_PTR(feature);

    app->log("UnfoldSample completed: unfold '" + feature->name() + "'.");
    return true;
}