Refold Feature Sample

Description

Demonstrates creating a new sheet metal refold feature.

Code Samples

"""Refold Feature API sample — creates a refold for the active unfold session via the public API.

Requires a design with at least one unfold feature that does not yet have an associated refold.
Sample data: Neutron/Test/APITest/APISampleSheetMetalRefold.f3d
"""

import traceback

import adsk.core
import adsk.fusion

app = adsk.core.Application.get()


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

        unfold_features = features.unfoldFeatures
        refold_features = features.refoldFeatures

        if unfold_features.count == 0:
            app.log("RefoldSample skipped: no unfold features in this design.")
            return

        unfold = unfold_features.item(0)
        assert unfold is not None

        refold = unfold.refoldFeature
        if refold is None:
            refold_input = refold_features.createInput(unfold)
            assert refold_input is not None
            refold = refold_features.add(refold_input)
            assert refold is not None

        assert refold is not None
        associated_unfold = refold.unfoldFeature
        assert associated_unfold is not None
        assert associated_unfold == unfold

        app.log(
            f"RefoldSample completed: unfold '{unfold.name}' <-> refold '{refold.name}'."
        )

    except Exception:
        app.log(f"Failed:\n{traceback.format_exc()}")
// Refold Feature API sample — creates a refold for an existing unfold via the public API.
//
// Requires a design with at least one unfold feature that does not yet have an associated refold.
// Sample data: Neutron/Test/APITest/APISampleSheetMetalUnFold.f3d (create unfold first, or use a doc with unfold-only).

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

#include <string>

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

namespace
{
Ptr<Application> app;

#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 features = rootComp->features();
    CHECK_PTR(features);

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

    auto refoldFeatures = features->refoldFeatures();
    CHECK_PTR(refoldFeatures);

    if (unfoldFeatures->count() == 0)
    {
        app->log("RefoldSample skipped: no unfold features in this design.");
        return true;
    }

    Ptr<UnfoldFeature> unfold = unfoldFeatures->item(0);
    CHECK_PTR(unfold);

    Ptr<RefoldFeature> refold = unfold->refoldFeature();
    if (!refold)
    {
        auto refoldInput = refoldFeatures->createInput(unfold);
        CHECK_PTR(refoldInput);
        refold = refoldFeatures->add(refoldInput);
        CHECK_PTR(refold);
    }

    CHECK_PTR(refold);
    auto associatedUnfold = refold->unfoldFeature();
    CHECK_PTR(associatedUnfold);
    CHECK(associatedUnfold.get() == unfold.get());

    app->log("RefoldSample completed: unfold '" + unfold->name() + "' <-> refold '" + refold->name() + "'.");

    return true;
}