Sheet Metal Rules Sample

Description

Demonstrates creating a new sheet metal rule.

Before running: No open document is required; the script creates a new design (or additional documents) as it runs.

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)

        # get active design and components
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        # Get the sheet metal rules
        sheetMetalRules = design.librarySheetMetalRules

        # Get the steel (default) rule then create a copy of it
        steelRule = sheetMetalRules.item(0)
        steelCopy = sheetMetalRules.addByCopy(steelRule, 'Steel Two (mm)')

        # Make some changes to the copied rule
        # First we can make some direct changes
        steelCopy.kFactor = 0.5
        steelCopy.twoBendReliefShape = adsk.fusion.TwoBendReliefShapes.RoundTwoBendReliefShape
        steelCopy.twoBendReliefPlacement = adsk.fusion.TwoBendReliefPlacements.TangentTwoBendReliefPlacement

        # Some changes need to be changed by value and can't be made directly on the rule
        thicknessValue = steelCopy.thickness
        thicknessValue.value = 0.02
        gapValue = steelCopy.gap
        gapValue.value = 0.03

        # You can also delete rules, create a test and delete it
        ruleToDelete = sheetMetalRules.addByCopy(steelCopy, 'Steel Three (mm)')
        ruleToDelete.deleteMe()

    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/ValueInput.h>
#include <Core/UserInterface/UserInterface.h>

#include <Fusion/SheetMetal/SheetMetalRule.h>
#include <Fusion/SheetMetal/SheetMetalRules.h>
#include <Fusion/SheetMetal/SheetMetalValue.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;

    // Create a document.
    Ptr<Documents> documents = app->documents();

    Ptr<Document> doc = documents->add(DocumentTypes::FusionDesignDocumentType);

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

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

    Ptr<SheetMetalRules> sheetMetalRules = design->librarySheetMetalRules();
    if (!sheetMetalRules->item(0))
        return false;

    // Get the first sheet metal rule, it should be named "Steel (mm)" and should be in the library by default.
    Ptr<SheetMetalRule> steelRule = sheetMetalRules->item(0);
    if (!steelRule)
        return false;

    // Create a rule by copying the "Steel (mm)" rule.
    Ptr<SheetMetalRule> steelRuleCopy = sheetMetalRules->addByCopy(steelRule, "Steel Two (mm)");
    if (!steelRuleCopy)
        return false;

    // Some properties can be changed directly.

    // Others need to be changed by value.
    Ptr<SheetMetalRuleValue> thicknessValue = steelRuleCopy->thickness();
    Ptr<SheetMetalRuleValue> gapValue = steelRuleCopy->gap();

    // Example of creating a third rule, then deleting it.
    Ptr<SheetMetalRule> ruleToDelete = sheetMetalRules->addByCopy(steelRuleCopy, "Steel Three (mm)");


    return true;
}
/**
 * Sheet Metal Rules Sample
 * Demonstrates copying a library sheet metal rule, editing it both directly
 * and by value, and deleting a rule.
 */

import { adsk } from '@adsk/fusion';

function run() {

  // Get the Fusion API's application object
  const app = adsk.core.Application.get();
  if (!app) throw Error("No adsk.core.Application.");

  // Create a document.
  const doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType);
  if (!doc) throw Error("No document.");

  // Get active design
  const product = app.activeProduct;
  if (!product) throw Error("No active product.");

  const design = product as adsk.fusion.Design;
  if (!design) throw Error("No active design.");

  // Get the sheet metal rules
  const sheetMetalRules = design.librarySheetMetalRules;
  if (!sheetMetalRules) throw Error("No library sheet metal rules.");

  // Get the steel (default) rule then create a copy of it
  const steelRule = sheetMetalRules.item(0);
  const steelCopy = sheetMetalRules.addByCopy(steelRule, 'Steel Two (mm)');

  // Make some changes to the copied rule.
  // First we can make some direct changes.
  steelCopy.kFactor = 0.5;
  steelCopy.twoBendReliefShape = adsk.fusion.TwoBendReliefShapes.RoundTwoBendReliefShape;
  steelCopy.twoBendReliefPlacement = adsk.fusion.TwoBendReliefPlacements.TangentTwoBendReliefPlacement;

  // Some changes need to be made by value and can't be set directly on the rule.
  const thicknessValue = steelCopy.thickness;
  thicknessValue.value = 0.02;
  const gapValue = steelCopy.gap;
  gapValue.value = 0.03;

  // You can also delete rules. Create one then delete it.
  const ruleToDelete = sheetMetalRules.addByCopy(steelCopy, 'Steel Three (mm)');
  ruleToDelete.deleteMe();
}

run();