Create Sheet Metal Component Sample

Description

Demonstrates creating new sheet metal components programmatically, covering both the local case (Occurrences.addNewSheetMetalComponent) and the external / X-Ref case (Occurrences.addNewExternalSheetMetalComponent). Each method takes a SheetMetalRule (picked from the library) and a transform.

Code Samples

import adsk.core, adsk.fusion, traceback

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

        # Create a new design document.
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        rootComp = design.rootComponent
        occs = rootComp.occurrences

        # Place the new occurrence at the origin.
        transform = adsk.core.Matrix3D.create()

        # Create a local sheet metal component and its occurrence at the root.
        # The design's active sheet metal rule is applied automatically.
        smOcc = occs.addNewSheetMetalComponent(transform)
        smOcc.component.name = 'MySheetMetalComponent'

        # Create a second local sheet metal component offset along Z so the two don't overlap.
        offset = adsk.core.Matrix3D.create()
        offset.translation = adsk.core.Vector3D.create(0.0, 0.0, 5.0)
        occs.addNewSheetMetalComponent(offset)

        # Create a new external (X-Ref) sheet metal component saved into the active project.
        # This requires a signed-in user with an active Fusion Team project. When running
        # offline or without a project the external step is skipped.
        activeProject = app.data.activeProject
        if activeProject is not None:
            externalOffset = adsk.core.Matrix3D.create()
            externalOffset.translation = adsk.core.Vector3D.create(0.0, 0.0, 10.0)
            occs.addNewExternalSheetMetalComponent(
                'MyExternalSheetMetalComponent',
                activeProject.rootFolder,
                externalOffset)


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#include <Core/Application/Application.h>
#include <Core/Application/Data.h>
#include <Core/Application/DataFolder.h>
#include <Core/Application/DataProject.h>
#include <Core/Application/Document.h>
#include <Core/Application/Documents.h>
#include <Core/Application/Product.h>
#include <Core/Geometry/Matrix3D.h>
#include <Core/Geometry/Vector3D.h>
#include <Core/UserInterface/UserInterface.h>

#include <Fusion/Components/Component.h>
#include <Fusion/Components/Occurrence.h>
#include <Fusion/Components/Occurrences.h>
#include <Fusion/Fusion/Design.h>


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();
    if (!ui)
        return false;

    Ptr<Documents> docs = app->documents();
    if (!docs)
        return false;

    // Create a new design document.
    Ptr<Document> doc = docs->add(DocumentTypes::FusionDesignDocumentType);

    Ptr<Product> product = app->activeProduct();

    Ptr<Design> design = product;

    Ptr<Component> rootComp = design->rootComponent();

    Ptr<Occurrences> occs = rootComp->occurrences();

    // Create a local sheet metal component and its occurrence at the root.
    // The design's active sheet metal rule is applied automatically.
    Ptr<Matrix3D> transform = Matrix3D::create();
    Ptr<Occurrence> smOcc = occs->addNewSheetMetalComponent(transform);

    // Create a second local sheet metal component offset along Z so the two don't overlap.
    Ptr<Matrix3D> offset = Matrix3D::create();
    offset->translation(Vector3D::create(0.0, 0.0, 5.0));

    // Create a new external (X-Ref) sheet metal component saved into the active project.
    // This requires a signed-in user with an active Fusion Team project. When running
    // offline or without a project the external step is skipped.
    Ptr<DataProject> activeProject = app->data()->activeProject();
    if (activeProject)
    {
        Ptr<DataFolder> targetFolder = activeProject->rootFolder();
        if (targetFolder)
        {
            Ptr<Matrix3D> externalOffset = Matrix3D::create();
            externalOffset->translation(Vector3D::create(0.0, 0.0, 10.0));
            occs->addNewExternalSheetMetalComponent("MyExternalSheetMetalComponent", targetFolder, externalOffset);
        }
    }


    return true;
}