Convert To Sheet Metal Sample

Description

Demonstrates finding the thickness of a body at a face and converting a BRepBody to sheet metal.

Code Samples

import adsk.core, adsk.fusion, traceback

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

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

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

        # Get the root component of the active design.
        rootComp = design.rootComponent

        # Create a sketch rectangle on the YZ plane.
        sketch = rootComp.sketches.add(rootComp.yZConstructionPlane)
        sketchLines = sketch.sketchCurves.sketchLines
        startPoint = adsk.core.Point3D.create(0, 0, 0)
        endPoint = adsk.core.Point3D.create(5.0, 5.0, 0)
        sketchLines.addTwoPointRectangle(startPoint, endPoint)

        # Extrude the profile as a solid slab (0.25 cm = 2.5 mm thick).
        prof = sketch.profiles.item(0)
        extrudes = rootComp.features.extrudeFeatures
        extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        distance = adsk.core.ValueInput.createByReal(0.25)
        extInput.setDistanceExtent(False, distance)
        extInput.isSolid = True
        ext = extrudes.add(extInput)

        # Get the body.
        body = ext.bodies.item(0)

        # Find the largest planar face on the body.
        largestFace = None
        maxArea = 0.0
        for face in body.faces:
            if face.geometry.surfaceType == adsk.core.SurfaceTypes.PlaneSurfaceType:
                if face.area > maxArea:
                    maxArea = face.area
                    largestFace = face

        # Find the thickness of the body at the selected face.
        found, thickness = body.findThicknessAtFace(largestFace)

        # Search the library sheet metal rules for one whose thickness
        # matches the detected body thickness.
        libraryRules = design.librarySheetMetalRules
        matchingRule = None
        for i in range(libraryRules.count):
            rule = libraryRules.item(i)
            ruleThickness = rule.thickness.value
            if abs(ruleThickness - thickness) < 1e-6:
                matchingRule = rule
                break

        # If no matching rule was found, fall back to the first available rule.
        if matchingRule is None:
            matchingRule = libraryRules.item(0)

        # Convert the body to sheet metal using the chosen rule.
        body.convertToSheetMetal(largestFace, matchingRule)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#include <Core/Application/Application.h>
#include <Core/Application/Document.h>
#include <Core/Application/Documents.h>
#include <Core/Application/Product.h>
#include <Core/Application/ValueInput.h>
#include <Core/Geometry/Point3D.h>
#include <Core/Geometry/Surface.h>
#include <Core/UserInterface/UserInterface.h>

#include <Fusion/BRep/BRepBodies.h>
#include <Fusion/BRep/BRepBody.h>
#include <Fusion/BRep/BRepFace.h>
#include <Fusion/BRep/BRepFaces.h>
#include <Fusion/Components/Component.h>
#include <Fusion/Construction/ConstructionPlane.h>
#include <Fusion/Features/ExtrudeFeature.h>
#include <Fusion/Features/ExtrudeFeatureInput.h>
#include <Fusion/Features/ExtrudeFeatures.h>
#include <Fusion/Features/Features.h>
#include <Fusion/Fusion/Design.h>
#include <Fusion/SheetMetal/SheetMetalRule.h>
#include <Fusion/SheetMetal/SheetMetalRules.h>
#include <Fusion/SheetMetal/SheetMetalValue.h>
#include <Fusion/Sketch/Profile.h>
#include <Fusion/Sketch/Profiles.h>
#include <Fusion/Sketch/Sketch.h>
#include <Fusion/Sketch/SketchCurves.h>
#include <Fusion/Sketch/SketchLines.h>
#include <Fusion/Sketch/Sketches.h>

#include <cmath>


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 document.
    Ptr<Document> doc = docs->add(DocumentTypes::FusionDesignDocumentType);
    if (!doc)
        return false;

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

    Ptr<Design> design = product;
    if (!design)
        return false;

    // Get the root component of the active design.
    Ptr<Component> rootComp = design->rootComponent();
    if (!rootComp)
        return false;

    // Create a sketch rectangle on the YZ plane.
    Ptr<Sketches> sketches = rootComp->sketches();
    if (!sketches)
        return false;

    Ptr<ConstructionPlane> yzPlane = rootComp->yZConstructionPlane();
    if (!yzPlane)
        return false;

    Ptr<Sketch> sketch = sketches->add(yzPlane);
    if (!sketch)
        return false;

    Ptr<SketchCurves> sketchCurves = sketch->sketchCurves();
    if (!sketchCurves)
        return false;

    Ptr<SketchLines> sketchLines = sketchCurves->sketchLines();
    if (!sketchLines)
        return false;

    Ptr<Point3D> startPoint = Point3D::create(0, 0, 0);
    Ptr<Point3D> endPoint = Point3D::create(5.0, 5.0, 0);
    sketchLines->addTwoPointRectangle(startPoint, endPoint);

    // Get the profile.
    Ptr<Profiles> profs = sketch->profiles();
    if (!profs)
        return false;

    Ptr<Profile> prof = profs->item(0);
    if (!prof)
        return false;

    // Extrude the profile as a solid slab (0.25 cm = 2.5 mm thick).
    Ptr<Features> feats = rootComp->features();
    if (!feats)
        return false;

    Ptr<ExtrudeFeatures> extrudes = feats->extrudeFeatures();
    if (!extrudes)
        return false;

    Ptr<ExtrudeFeatureInput> extInput = extrudes->createInput(prof, FeatureOperations::NewBodyFeatureOperation);

    Ptr<ValueInput> distance = ValueInput::createByReal(0.25);
    extInput->setDistanceExtent(false, distance);
    extInput->isSolid(true);

    Ptr<ExtrudeFeature> ext = extrudes->add(extInput);
    if (!ext)
        return false;

    // Get the body.
    Ptr<BRepBodies> bodies = ext->bodies();
    if (!bodies)
        return false;

    Ptr<BRepBody> body = bodies->item(0);
    if (!body)
        return false;

    // Find the largest planar face on the body.
    Ptr<BRepFaces> faces = body->faces();
    if (!faces)
        return false;

    Ptr<BRepFace> largestFace;
    double maxArea = 0.0;
    for (size_t i = 0; i < faces->count(); ++i)
    {
        Ptr<BRepFace> face = faces->item(i);
        if (!face)
            continue;
        Ptr<Surface> geom = face->geometry();
        if (geom && geom->surfaceType() == SurfaceTypes::PlaneSurfaceType)
        {
            double faceArea = face->area();
            if (faceArea > maxArea)
            {
                maxArea = faceArea;
                largestFace = face;
            }
        }
    }

    // Find the thickness of the body at the selected face.
    double thickness = 0.0;
    bool found = body->findThicknessAtFace(largestFace, thickness);

    // Search the library sheet metal rules for one whose thickness
    // matches the detected body thickness.
    Ptr<SheetMetalRules> libraryRules = design->librarySheetMetalRules();
    if (!libraryRules)
        return false;

    Ptr<SheetMetalRule> matchingRule;
    for (size_t i = 0; i < libraryRules->count(); ++i)
    {
        Ptr<SheetMetalRule> rule = libraryRules->item(i);
        if (!rule)
            continue;
        Ptr<SheetMetalRuleValue> ruleThickness = rule->thickness();
        if (ruleThickness && std::fabs(ruleThickness->value() - thickness) < 1e-6)
        {
            matchingRule = rule;
            break;
        }
    }

    // If no matching rule was found, fall back to the first available rule.
    if (!matchingRule)
        matchingRule = libraryRules->item(0);

    // Convert the body to sheet metal using the chosen rule.
    bool success = body->convertToSheetMetal(largestFace, matchingRule);


    return true;
}