Demonstrates measure-related API functions.
Before running: No open document is required; the script creates a new design (or additional documents) as it runs.
#Author- #Description- import adsk.core, adsk.fusion, adsk.cam, traceback def run(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface # Create a new 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 sketches = rootComp.sketches sketch1 = sketches.add(rootComp.yZConstructionPlane) # Create sketch lines sketchLines = sketch1.sketchCurves.sketchLines # Create some 3D points point1 = adsk.core.Point3D.create(0.0, 0.0, 0.0) point2 = adsk.core.Point3D.create(5.0, 5.0, 0.0) # Create sketch rectangle sketchLines.addTwoPointRectangle(point1, point2) # Get the profile profile = sketch1.profiles.item(0) # Create an extrusion input extrudes = rootComp.features.extrudeFeatures extrudeInput = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) # Set extrude distance is 6 cm distance = adsk.core.ValueInput.createByReal(6.0) # Set the distance extent extrudeInput.setDistanceExtent(False, distance) # Set the extrude type is solid extrudeInput.isSolid = True # Create the extrude extrude = extrudes.add(extrudeInput) # Get the extrude brepbody brepBody = extrude.bodies.item(0) # Measure minimum distance app.log("*** Measure minimum distance.") sketchLine = sketchLines.item(0) brepEdge = brepBody.edges.item(0) measureResult = app.measureManager.measureMinimumDistance(sketchLine, brepEdge) minDistance = measureResult.value app.log('Minimum distance value is ' + str(minDistance)) position1 = measureResult.positionOne position2 = measureResult.positionTwo app.log('positionOne is (' + str(position1.x) + ',' + str(position1.y) + ',' + str(position1.z) + ')' ) app.log('positionTwo is (' + str(position2.x) + ',' + str(position2.y) + ',' + str(position2.z) + ')' ) # Measure minimum distance using transient Point3D objects app.log("*** Measure minimum distance between Point3D objects.") pointStart = adsk.core.Point3D.create(0.0, 0.0, 0.0) pointEnd = adsk.core.Point3D.create(3.0, 4.0, 0.0) measureResult = app.measureManager.measureMinimumDistance(pointStart, pointEnd) minDistance = measureResult.value app.log('Minimum distance value is ' + str(minDistance)) # Measure minimum distance from a transient Point3D to a body app.log("*** Measure minimum distance between a Point3D and a body.") pointToBody = adsk.core.Point3D.create(0.0, 5.0, 0.0) measureResult = app.measureManager.measureMinimumDistance(pointToBody, brepBody) minDistance = measureResult.value app.log('Minimum distance value is ' + str(minDistance)) # Measure angle for three point app.log("*** Measure angle for three point.") constructionPoint = rootComp.originConstructionPoint vertex = brepBody.vertices.item(0) measureResult = app.measureManager.measureAngle(constructionPoint, point2, vertex) angle = measureResult.value app.log('Angle value is ' + str(angle)) position1 = measureResult.positionOne position2 = measureResult.positionTwo position3 = measureResult.positionThree app.log('positionOne is (' + str(position1.x) + ',' + str(position1.y) + ',' + str(position1.z) + ')' ) app.log('positionTwo is (' + str(position2.x) + ',' + str(position2.y) + ',' + str(position2.z) + ')' ) app.log('positionThree is (' + str(position3.x) + ',' + str(position3.y) + ',' + str(position3.z) + ')' ) # Measure angle for two object app.log("*** Measure angle for two objects.") brepFace = brepBody.faces.item(1) brepEdge = brepBody.edges.item(0) measureResult = app.measureManager.measureAngle(brepFace, brepEdge) angle = measureResult.value app.log('Angle value is ' + str(angle)) # Get oriented bounding box app.log("*** Get oriented bounding box.") vector1 = adsk.core.Vector3D.create(0.0, 0.0, 1.0) vector2 = adsk.core.Vector3D.create(0.0, 1.0, 0.0) boundingBox = app.measureManager.getOrientedBoundingBox(brepBody, vector1, vector2) # Get the bounding box length, width and height length = boundingBox.length app.log("Bounding box length is " + str(length)) width = boundingBox.width app.log("Bounding box width is " + str(width)) height = boundingBox.height app.log("Bounding box height is " + str(height)) # Get the bounding box width direction widthDirection = boundingBox.widthDirection heightDirection = boundingBox.heightDirection lengthDirection = boundingBox.lengthDirection app.log("Bounding box width direction is " + "(" + str(widthDirection.x) + ", " + str(widthDirection.y) + ", " + str(widthDirection.z) + ")" ) # Get the bounding contain a point or not pointA= adsk.core.Point3D.create(-2.0, 0.5, 2.0) isContain = boundingBox.contains(pointA) app.log("Does the bounding box contain pointA? " + str(isContain)) pointB = adsk.core.Point3D.create(0.0, 0.0, 0.0) isContain = boundingBox.contains(pointB) app.log("Does the bounding box contain pointB? " + str(isContain)) # Copy the bounding box boundingBoxCopy = boundingBox.copy() length = boundingBoxCopy.length app.log("The copied Bounding box length is " + str(length)) # Set bounding box height boundingBoxCopy.height = 10.0 length = boundingBoxCopy.height app.log("The new set length of the copied bounding box is " + str(length)) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#include <Core/Application/Application.h>
#include <Core/UserInterface/UserInterface.h>
#include <Core/Application/Document.h>
#include <Core/Application/Documents.h>
#include <Fusion/Components/Component.h>
#include <Fusion/Fusion/Design.h>
#include <Fusion/Sketch/Sketch.h>
#include <Fusion/Sketch/Sketches.h>
#include <Fusion/Construction/ConstructionPlane.h>
#include <Fusion/Construction/ConstructionPoint.h>
#include <Fusion/Sketch/SketchCurves.h>
#include <Fusion/Sketch/SketchLines.h>
#include <Fusion/Sketch/SketchLine.h>
#include <Core/Geometry/Point3D.h>
#include <Fusion/Sketch/Profiles.h>
#include <Fusion/Sketch/Profile.h>
#include <Fusion/Features/Features.h>
#include <Fusion/Features/ExtrudeFeatures.h>
#include <Fusion/Features/ExtrudeFeatureInput.h>
#include <Fusion/Features/ExtrudeFeature.h>
#include <Core/Application/ValueInput.h>
#include <Fusion/BRep/BRepBodies.h>
#include <Fusion/BRep/BRepBody.h>
#include <Fusion/BRep/BRepEdges.h>
#include <Fusion/BRep/BRepEdge.h>
#include <Fusion/BRep/BRepVertices.h>
#include <Fusion/BRep/BRepVertex.h>
#include <Fusion/BRep/BRepFaces.h>
#include <Fusion/BRep/BRepFace.h>
#include <Core/Application/MeasureManager.h>
#include <Core/Application/MeasureResults.h>
#include <Core/Geometry/Vector3D.h>
#include <Core/Geometry/OrientedBoundingBox3D.h>
using namespace adsk::core;
using namespace adsk::fusion;
Ptr<Application> app;
Ptr<UserInterface> ui;
extern "C" XI_EXPORT bool run(const char* context)
{
app = Application::get();
if (!app)
return false;
ui = app->userInterface();
if (!ui)
return false;
// Create a new document
Ptr<Documents> docs = app->documents();
if (!docs)
return false;
Ptr<Document> doc = docs->add(DocumentTypes::FusionDesignDocumentType);
if (!doc)
return false;
Ptr<Design> design = app->activeProduct();
if (!design)
return false;
// Get the root component of active design
Ptr<Component> rootComp = design->rootComponent();
if (!rootComp)
return false;
// Create a new sketch on the yz plane
Ptr<Sketches> sketches = rootComp->sketches();
if (!sketches)
return false;
Ptr<ConstructionPlane> yzPlane = rootComp->yZConstructionPlane();
if (!yzPlane)
return false;
Ptr<Sketch> sketch1 = sketches->add(yzPlane);
if (!sketch1)
return false;
// Create sketch lines
Ptr<SketchCurves> sketchCurves = sketch1->sketchCurves();
if (!sketchCurves)
return false;
Ptr<SketchLines> sketchLines = sketchCurves->sketchLines();
if (!sketchLines)
return false;
// Create sketch rectangle
Ptr<Point3D> point1 = Point3D::create(0.0, 0.0, 0.0);
Ptr<Point3D> point2 = Point3D::create(5.0, 5.0, 0.0);
sketchLines->addTwoPointRectangle(point1, point2);
// Get the profile
Ptr<Profiles> profiles = sketch1->profiles();
if (!profiles)
return false;
Ptr<Profile> profile = profiles->item(0);
if (!profile)
return false;
// Create an extrusion input
Ptr<Features> features = rootComp->features();
if (!features)
return false;
Ptr<ExtrudeFeatures> extrudes = features->extrudeFeatures();
if (!extrudes)
return false;
Ptr<ExtrudeFeatureInput> extrudeInput = extrudes->createInput(profile, FeatureOperations::NewBodyFeatureOperation);
// Set extrude distance is 6 cm
Ptr<ValueInput> distance = ValueInput::createByReal(6.0);
// Set the distance extent
extrudeInput->setDistanceExtent(false, distance);
// Set the extrude type to be solid
extrudeInput->isSolid(true);
// Create the extrude
Ptr<ExtrudeFeature> extrude = extrudes->add(extrudeInput);
if (!extrude)
return false;
// Get the extrude brepbody
Ptr<BRepBodies> brepBodies = extrude->bodies();
if (!brepBodies)
return false;
Ptr<BRepBody> brepBody = brepBodies->item(0);
if (!brepBody)
return false;
// Get one sketch line
Ptr<SketchLine> sketchLine = sketchLines->item(0);
if (!sketchLine)
return false;
// Get one brepEdge
Ptr<BRepEdges> brepEdges = brepBody->edges();
if (!brepEdges)
return false;
Ptr<BRepEdge> brepEdge = brepEdges->item(0);
if (!brepEdge)
return false;
// Get measure manager
Ptr<MeasureManager> measureMgr = app->measureManager();
if (!measureMgr)
return false;
// Measure minimum distance
Ptr<MeasureResults> results = measureMgr->measureMinimumDistance(sketchLine, brepEdge);
if (!results)
return false;
double minDistance = results->value();
Ptr<Point3D> position1 = results->positionOne();
if (!position1)
return false;
Ptr<Point3D> position2 = results->positionTwo();
if (!position2)
return false;
// Measure minimum distance using transient Point3D objects
Ptr<Point3D> pointStart = Point3D::create(0.0, 0.0, 0.0);
Ptr<Point3D> pointEnd = Point3D::create(3.0, 4.0, 0.0);
results = measureMgr->measureMinimumDistance(pointStart, pointEnd);
if (!results)
return false;
minDistance = results->value();
// Measure minimum distance from a transient Point3D to a body
Ptr<Point3D> pointToBody = Point3D::create(0.0, 5.0, 0.0);
results = measureMgr->measureMinimumDistance(pointToBody, brepBody);
if (!results)
return false;
minDistance = results->value();
// Get root component origin point
Ptr<ConstructionPoint> constructionPoint = rootComp->originConstructionPoint();
if (!constructionPoint)
return false;
// Get a brep Vertex
Ptr<BRepVertices> brepVertices = brepBody->vertices();
if (!brepVertices)
return false;
Ptr<BRepVertex> brepVertex = brepVertices->item(0);
if (!brepVertex)
return false;
// Measure angle for three point
results = measureMgr->measureAngle(constructionPoint, point2, brepVertex);
if (!results)
return false;
double angle = results->value();
position1 = results->positionOne();
if (!position1)
return false;
position2 = results->positionTwo();
if (!position2)
return false;
Ptr<Point3D> position3 = results->positionThree();
if (!position3)
return false;
// Get a brep face
Ptr<BRepFaces> brepFaces = brepBody->faces();
if (!brepFaces)
return false;
Ptr<BRepFace> brepFace = brepFaces->item(1);
if (!brepFace)
return false;
// Measure angle for two objects
results = measureMgr->measureAngle(brepFace, brepEdge);
if (!results)
return false;
angle = results->value();
position1 = results->positionOne();
if (!position1)
return false;
position2 = results->positionTwo();
if (!position2)
return false;
position3 = results->positionThree();
if (!position3)
return false;
// Get brep body's oriented bounding box
Ptr<Vector3D> vector1 = Vector3D::create(0.0, 0.0, 1.0);
Ptr<Vector3D> vector2 = Vector3D::create(0.0, 1.0, 0.0);
Ptr<OrientedBoundingBox3D> boundingBox = measureMgr->getOrientedBoundingBox(brepBody, vector1, vector2);
if (!boundingBox)
return false;
// Get the bounding box length, width and height
double length = boundingBox->length();
double width = boundingBox->width();
double height = boundingBox->height();
// Get the bounding box width direction
Ptr<Vector3D> widthDir = boundingBox->widthDirection();
if (!widthDir)
return false;
// Get the bounding box length direction
Ptr<Vector3D> lengthDir = boundingBox->lengthDirection();
if (!lengthDir)
return false;
// Get the bounding box height direction
Ptr<Vector3D> heightDir = boundingBox->heightDirection();
if (!heightDir)
return false;
// Get the bounding contain a point or not
Ptr<Point3D> pointA = Point3D::create(-2.0, 0.5, 2.0);
bool isContain = boundingBox->contains(pointA);
Ptr<Point3D> pointB = Point3D::create(0.0, 0.0, 0.0);
isContain = boundingBox->contains(pointB);
// Copy the bounding box
Ptr<OrientedBoundingBox3D> boundingBoxCopy = boundingBox->copy();
if (!boundingBoxCopy)
return false;
// Set bounding box height
boundingBoxCopy->height(10.0);
double newHeight = boundingBoxCopy->height();
return true;
}
/** * Measure Sample * Measure related functions */ 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) // Create a document. const design = app.activeProduct as adsk.fusion.Design const rootComp = design.rootComponent // Create a sketch const sketches = rootComp.sketches const sketch1 = sketches.add(rootComp.yZConstructionPlane) // Create sketch lines const sketchLines = sketch1.sketchCurves.sketchLines // Create some 3D points const point1 = adsk.core.Point3D.create(0.0, 0.0, 0.0) const point2 = adsk.core.Point3D.create(5.0, 5.0, 0.0) // Create sketch rectangle sketchLines.addTwoPointRectangle(point1, point2) // Get the profile const profile = sketch1.profiles.item(0) // Create an extrusion input const extrudes = rootComp.features.extrudeFeatures const extrudeInput = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) // Set extrude distance is 6 cm const distance = adsk.core.ValueInput.createByReal(6.0) // Set the distance extent const distanceExtentDef = adsk.fusion.DistanceExtentDefinition.create(distance) extrudeInput.setOneSideExtent(distanceExtentDef, adsk.fusion.ExtentDirections.PositiveExtentDirection) // Set the extrude type is solid extrudeInput.isSolid = true // Create the extrude const extrude = extrudes.add(extrudeInput) // Get the extrude brepbody const brepBody = extrude.bodies.item(0) // Measure minimum distance adsk.log("*** Measure minimum distance.") const sketchLine = sketchLines.item(0) let brepEdge = brepBody.edges.item(0) let measureResult = app.measureManager.measureMinimumDistance(sketchLine, brepEdge) const minDistance = measureResult.value adsk.log('Minimum distance value is ' + String(minDistance)) let position1 = measureResult.positionOne let position2 = measureResult.positionTwo adsk.log('positionOne is (' + String(position1.x) + ',' + String(position1.y) + ',' + String(position1.z) + ')' ) adsk.log('positionTwo is (' + String(position2.x) + ',' + String(position2.y) + ',' + String(position2.z) + ')' ) // Measure minimum distance using transient Point3D objects adsk.log("*** Measure minimum distance between Point3D objects.") const pointStart = adsk.core.Point3D.create(0.0, 0.0, 0.0) const pointEnd = adsk.core.Point3D.create(3.0, 4.0, 0.0) measureResult = app.measureManager.measureMinimumDistance(pointStart, pointEnd) adsk.log('Minimum distance value is ' + String(measureResult.value)) // Measure minimum distance from a transient Point3D to a body adsk.log("*** Measure minimum distance between a Point3D and a body.") const pointToBody = adsk.core.Point3D.create(0.0, 5.0, 0.0) measureResult = app.measureManager.measureMinimumDistance(pointToBody, brepBody) adsk.log('Minimum distance value is ' + String(measureResult.value)) // Measure angle for three point adsk.log("*** Measure angle for three point.") const constructionPoint = rootComp.originConstructionPoint const vertex = brepBody.vertices.item(0) measureResult = app.measureManager.measureAngle(constructionPoint, point2, vertex) let angle = measureResult.value adsk.log('Angle value is ' + String(angle)) position1 = measureResult.positionOne position2 = measureResult.positionTwo const position3 = measureResult.positionThree adsk.log('positionOne is (' + String(position1.x) + ',' + String(position1.y) + ',' + String(position1.z) + ')' ) adsk.log('positionTwo is (' + String(position2.x) + ',' + String(position2.y) + ',' + String(position2.z) + ')' ) adsk.log('positionThree is (' + String(position3.x) + ',' + String(position3.y) + ',' + String(position3.z) + ')' ) // Measure angle for two object adsk.log("*** Measure angle for two objects.") const brepFace = brepBody.faces.item(1) brepEdge = brepBody.edges.item(0) measureResult = app.measureManager.measureAngle(brepFace, brepEdge) angle = measureResult.value adsk.log('Angle value is ' + String(angle)) // Get oriented bounding box adsk.log("*** Get oriented bounding box.") const vector1 = adsk.core.Vector3D.create(0.0, 0.0, 1.0) const vector2 = adsk.core.Vector3D.create(0.0, 1.0, 0.0) const boundingBox = app.measureManager.getOrientedBoundingBox(brepBody, vector1, vector2) // Get the bounding box length, width and height length = boundingBox.length adsk.log("Bounding box length is " + String(length)) const width = boundingBox.width adsk.log("Bounding box width is " + String(width)) const height = boundingBox.height adsk.log("Bounding box height is " + String(height)) // Get the bounding box width direction const widthDirection = boundingBox.widthDirection const heightDirection = boundingBox.heightDirection const lengthDirection = boundingBox.lengthDirection adsk.log("Bounding box width direction is " + "(" + String(widthDirection.x) + ", " + String(widthDirection.y) + ", " + String(widthDirection.z) + ")" ) // Get the bounding contain a point or not const pointA = adsk.core.Point3D.create(-2.0, 0.5, 2.0) let isContain = boundingBox.contains(pointA) adsk.log("Does the bounding box contain pointA? " + String(isContain)) const pointB = adsk.core.Point3D.create(0.0, 0.0, 0.0) isContain = boundingBox.contains(pointB) adsk.log("Does the bounding box contain pointB? " + String(isContain)) // Copy the bounding box const boundingBoxCopy = boundingBox.copy() length = boundingBoxCopy.length adsk.log("The copied Bounding box length is " + String(length)) // Set bounding box height boundingBoxCopy.height = 10.0 length = boundingBoxCopy.height adsk.log("The new set length of the copied bounding box is " + String(length)) } run();