Sketch fillet and offset API Sample

Description

Demonstrates the creation of a fillet in a sketch and offset a set of curves.

Code Samples

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = adsk.fusion.Design.cast(app.activeProduct)
        
        # Get the root component of the active design.
        rootComp = design.rootComponent

        # Create a new sketch on the xy plane.
        sketches = rootComp.sketches
        xyPlane = rootComp.xYConstructionPlane
        sketch = sketches.add(xyPlane)

        # Draw two connected lines.
        lines = sketch.sketchCurves.sketchLines
        line1 = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(3, 1, 0))
        line2 = lines.addByTwoPoints(line1.endSketchPoint, adsk.core.Point3D.create(1, 4, 0))
        
        # Add a fillet.
        arc = sketch.sketchCurves.sketchArcs.addFillet(line1, line1.endSketchPoint.geometry, line2, line2.startSketchPoint.geometry, 1)

        # Add the geometry to a collection. This uses a utility function that
        # automatically finds the connected curves and returns a collection.
        curvesColl = sketch.findConnectedCurves(line1)
               
        # Create the offset.
        offsetVal = adsk.core.ValueInput.createByReal(0.25)
        offsetInput = sketch.geometricConstraints.createOffsetInput(list(curvesColl), offsetVal)
        offsetCurves = sketch.geometricConstraints.addOffset2(offsetInput)

        app.log(f'Sketch offset resulted in {len(offsetCurves.childCurves)} curves.')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#include <Core/Application/Application.h>
#include <Core/Application/Documents.h>
#include <Core/Application/Document.h>
#include <Core/Application/Product.h>
#include <Core/UserInterface/UserInterface.h>
#include <Core/Geometry/Point3D.h>
#include <Core/Geometry/Line3D.h>
#include <Core/Application/ObjectCollection.h>
#include <Fusion/Fusion/Design.h>
#include <Fusion/Components/Component.h>
#include <Fusion/Sketch/Sketches.h>
#include <Fusion/Sketch/Sketch.h>
#include <Fusion/Sketch/SketchCurves.h>
#include <Fusion/Sketch/SketchLines.h>
#include <Fusion/Sketch/SketchLine.h>
#include <Fusion/Sketch/SketchPoint.h>
#include <Fusion/Sketch/SketchArcs.h>
#include <Fusion/Sketch/SketchArc.h>
#include <Fusion/Construction/ConstructionPlane.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> documents = app->documents();
    if (!documents)
        return false;

    Ptr<Document> doc = documents->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 new sketch on the xy plane.
    Ptr<Sketches> sketches = rootComp->sketches();
    if (!sketches)
        return false;
    Ptr<ConstructionPlane> xyPlane = rootComp->xYConstructionPlane();
    if (!xyPlane)
        return false;
    Ptr<Sketch> sketch = sketches->add(xyPlane);
    if (!sketch)
        return false;

    // Draw two connected lines.
    Ptr<SketchCurves> curves = sketch->sketchCurves();
    if (!curves)
        return false;
    Ptr<SketchLines> lines = curves->sketchLines();
    if (!lines)
        return false;
    Ptr<SketchLine> line1 =
        lines->addByTwoPoints(adsk::core::Point3D::create(0, 0, 0), adsk::core::Point3D::create(3, 1, 0));
    if (!line1)
        return false;
    Ptr<SketchLine> line2 = lines->addByTwoPoints(line1->endSketchPoint(), adsk::core::Point3D::create(1, 4, 0));
    if (!line2)
        return false;

    // Add a fillet.
    Ptr<SketchArcs> arcs = curves->sketchArcs();
    if (!arcs)
        return false;
    Ptr<SketchArc> arc =
        arcs->addFillet(line1, line1->endSketchPoint()->geometry(), line2, line2->startSketchPoint()->geometry(), 1);
    if (!arc)
        return false;

    // Add the geometry to a collection. This uses a utility function that
    // automatically finds the connected curves and returns a collection.
    Ptr<ObjectCollection> connectedCurves = sketch->findConnectedCurves(line1);
    if (!connectedCurves)
        return false;

    // Create the offset.
    Ptr<Point3D> dirPoint = adsk::core::Point3D::create(0, 0.5, 0);
    if (!dirPoint)
        return false;
    Ptr<ObjectCollection> offsetCurves = sketch->offset(connectedCurves, dirPoint, 0.25);
    if (!offsetCurves)
        return false;

    return true;
}
/**
 * Sketch fillet and offset API Sample
 * Demonstrates the creation of a fillet in a sketch and offset a set of curves.
 */

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

//Params
// {
//     "useCurrentDocument": false,
//     "saveAsNewDocument": false,
//     "message": "",
//     "hubId": "wip1fqaautodesk4298",
//     "fileURN": "urn:adsk.wipqa:dm.lineage:WlSCDh7RQN6zD2btrrO4SA"
// }

function run() {
  
// Read the parameters passed with the script
  const scriptParameters = JSON.parse(adsk.parameters);
  if (!scriptParameters) throw Error("Invalid parameters provided.");

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

  // const doc: adsk.core.Document = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType);
  // Create a new empty document
  const doc = getDocument(
    app,
    scriptParameters.useCurrentDocument,
    scriptParameters.hubId,
    scriptParameters.fileURN,
  );
  if (!doc) throw Error("Invalid document.");

 // Ensure we are in the Design environment.
  const design = app.activeProduct as adsk.fusion.Design;

  //Get the root component of the active design.
  const rootComp = design.rootComponent

  // Create a new sketch on the xy plane.
  const sketches = rootComp.sketches
  const xyPlane = rootComp.xYConstructionPlane
  const sketch = sketches.add(xyPlane)

  // Draw two connected lines.
  const lines = sketch.sketchCurves.sketchLines
  const line1 = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(3, 1, 0))
  const line2 = lines.addByTwoPoints(line1.endSketchPoint, adsk.core.Point3D.create(1, 4, 0))
  
  // Add a fillet.
  const arc = sketch.sketchCurves.sketchArcs.addFillet(line1, line1.endSketchPoint.geometry, line2, line2.startSketchPoint.geometry, 1)
  // Add the geometry to a collection. This uses a utility function that
  // automatically finds the connected curves and returns a collection.
  const curves = sketch.findConnectedCurves(line1)
         
  // Create the offset.
  const dirPoint = adsk.core.Point3D.create(0, .5, 0)
  const offsetCurves = sketch.offset(curves, dirPoint, 0.25)

  saveDocument(
      doc,
      scriptParameters.saveAsNewDocument,
      scriptParameters.message,
      doc.dataFile.parentFolder,
    );

    while (app.hasActiveJobs) {
    wait(2000);
  }
}
function wait(ms: number) {
  const start = new Date().getTime();
  while (new Date().getTime() - start < ms) adsk.doEvents();
}

function getDocument(
  app: adsk.core.Application,
  useCurrentDocument: boolean,
  hubId: string,
  fileURN: string,
): adsk.core.Document {
  if (useCurrentDocument === true) {
    adsk.log(`Using currently open document: ${app.activeDocument.name}.`);
    return app.activeDocument;
  }

  if (hubId) {
    // Possible hubId formats: base64 encoded string, or business:<id>,
    // or personal:<id> (deprecated)
    const hub =
      app.data.dataHubs.itemById(hubId) ||
      app.data.dataHubs.itemById(`a.${adsk.btoa(`business:${hubId}`, true)}`) ||
      app.data.dataHubs.itemById(`a.${adsk.btoa(`personal:${hubId}`, true)}`);
    if (!hub) throw Error(`Hub with id ${hubId} not found.`);
    adsk.log(`Setting hub: ${hub.name}.`);
    app.data.activeHub = hub;
  }

  const file = app.data.findFileById(fileURN);
  if (!file) throw Error(`File not found ${fileURN}.`);
  adsk.log(`Opening ${file.name}`);
  const document = app.documents.open(file, true);
  if (!document) throw Error(`Cannot open file ${file.name}.`);
  return document;
}

function saveDocument(
  doc: adsk.core.Document,
  saveAsNewDocument: boolean,
  message: string,
  destinationFolder: adsk.core.DataFolder,
): boolean {
  if (saveAsNewDocument) {
    adsk.log("Saving as new document.");
    try {
      destinationFolder.parentProject;
    } catch (e) {
      adsk.log(
        "Destination folder is not in a project, setting folder to Fusion Automation Service project.",
      );
      destinationFolder = defaultFolder(
        doc.parent,
        "Fusion Automation Service",
      );
    }
    if (
      doc.saveAs(doc.name + " Additive Ready", destinationFolder, message, "")
    ) {
      adsk.log("Document saved successfully.");
      return true;
    } else {
      adsk.log("Document failed to save.");
      return false;
    }
  }

  if (!doc.isModified) {
    adsk.log("Document not modified, not saving.");
    return true;
  }

  adsk.log(`Saving with message: "${message}".`);
  if (doc.save(message)) {
    adsk.log("Document saved successfully.");
    return true;
  } else {
    adsk.log("Document failed to save.");
    return false;
  }
}

function defaultFolder(app: adsk.core.Application, defaultProjectName: string) {
  const projects = app.data.activeHub.dataProjects;
  if (!projects) throw Error("Unable to get active hub's projects.");
  for (let i = 0; i < projects.count; ++i) {
    const project = projects.item(i)!;
    if (project.name === defaultProjectName) {
      return project.rootFolder;
    }
  }
  adsk.log(`Creating new project: ${defaultProjectName}`);
  const project = projects.add(defaultProjectName);
  if (!project) throw Error("Unable to create new project.");
  return project.rootFolder;
}

run();