Joint Origin Sample

Description

Demonstrates creating a new Joint Origin.

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 sub component under root component
        allOccs = rootComp.occurrences
        transform = adsk.core.Matrix3D.create()
        subOcc = allOccs.addNewComponent(transform)

        # Create an extrude in the sub component
        subComp = subOcc.component
        sketches = subComp.sketches
        sketch = sketches.add(subComp.xZConstructionPlane)
        sketchCircles = sketch.sketchCurves.sketchCircles
        centerPoint = adsk.core.Point3D.create(0, 0, 0)
        sketchCircles.addByCenterRadius(centerPoint, 2.5)

        profile = sketch.profiles.item(0)

        extrudes = subComp.features.extrudeFeatures
        extInput = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

        distance = adsk.core.ValueInput.createByString("50 mm")
        extInput.setDistanceExtent(False, distance)
        extInput.isSolid = True
        extrude = extrudes.add(extInput)

        endFaceOfExtrude = extrude.endFaces.item(0)

        # Create the joint geometry
        jointGeometry = adsk.fusion.JointGeometry.createByPlanarFace(endFaceOfExtrude, None, adsk.fusion.JointKeyPointTypes.CenterKeyPoint)

        # Create the JointOriginInput
        jointOrigins_ = subComp.jointOrigins
        jointOriginInput = jointOrigins_.createInput(jointGeometry)

        # Create the JointOrigin
        jointOrigins_.add(jointOriginInput)

        camera_ = app.activeViewport.camera
        camera_.isFitView = True
        app.activeViewport.camera = camera_

    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/Application/ValueInput.h>
#include <Core/Application/Viewport.h>
#include <Core/Application/Camera.h>
#include <Core/Geometry/Point3D.h>
#include <Core/UserInterface/UserInterface.h>
#include <Fusion/Fusion/Design.h>
#include <Fusion/BRep/BRepFace.h>
#include <Fusion/BRep/BRepFaces.h>
#include <Fusion/Construction/ConstructionPlane.h>
#include <Fusion/Components/Component.h>
#include <Fusion/Components/JointOrigins.h>
#include <Fusion/Components/JointOriginInput.h>
#include <Fusion/Components/JointOrigin.h>
#include <Fusion/Components/JointGeometry.h>
#include <Fusion/Features/Features.h>
#include <Fusion/Features/ExtrudeFeature.h>
#include <Fusion/Features/ExtrudeFeatures.h>
#include <Fusion/Features/ExtrudeFeatureInput.h>
#include <Fusion/Sketch/Profile.h>
#include <Fusion/Sketch/Profiles.h>
#include <Fusion/Sketch/Sketch.h>
#include <Fusion/Sketch/Sketches.h>
#include <Fusion/Sketch/SketchCircle.h>
#include <Fusion/Sketch/SketchCircles.h>
#include <Fusion/Sketch/SketchCurves.h>
#include <Fusion/Sketch/SketchPoint.h>
#include <Fusion/Sketch/SketchPoints.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 sketch in root component
    Ptr<Sketches> sketches = rootComp->sketches();
    if (!sketches)
        return false;
    Ptr<ConstructionPlane> xz = rootComp->xZConstructionPlane();
    if (!xz)
        return false;
    Ptr<Sketch> sketch = sketches->add(xz);
    if (!sketch)
        return false;
    Ptr<SketchPoints> sketchPts = sketch->sketchPoints();
    if (!sketchPts)
        return false;
    Ptr<Point3D> point = Point3D::create(1, 0, 1);
    if (!point)
        return false;
    Ptr<SketchPoint> sketchPt = sketchPts->add(point);
    if (!sketchPt)
        return false;
    Ptr<SketchCurves> sketchCurves = sketch->sketchCurves();
    if (!sketchCurves)
        return false;
    Ptr<SketchCircles> sketchCircles = sketchCurves->sketchCircles();
    if (!sketchCircles)
        return false;
    Ptr<Point3D> centerPoint = Point3D::create(0, 0, 0);
    if (!centerPoint)
        return false;
    Ptr<SketchCircle> circle = sketchCircles->addByCenterRadius(centerPoint, 2.5);
    if (!circle)
        return false;

    // Get the profile defined by the circle
    Ptr<Profiles> profs = sketch->profiles();
    if (!profs)
        return false;
    Ptr<Profile> profile = profs->item(0);
    if (!profile)
        return false;

    // Create an extrude input and make sure it's in a new component
    Ptr<Features> feats = rootComp->features();
    if (!feats)
        return false;
    Ptr<ExtrudeFeatures> extrudes = feats->extrudeFeatures();
    if (!extrudes)
        return false;
    Ptr<ExtrudeFeatureInput> extInput = extrudes->createInput(profile, FeatureOperations::NewComponentFeatureOperation);
    if (!extInput)
        return false;

    // Set the extrude input
    Ptr<ValueInput> distance = ValueInput::createByString("50 mm");
    if (!distance)
        return false;
    extInput->setDistanceExtent(false, distance);
    extInput->isSolid(true);

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

    // Get the end face of the created extrude
    Ptr<BRepFaces> endFaces = ext->endFaces();
    if (!endFaces)
        return false;
    Ptr<BRepFace> endFace = endFaces->item(0);
    if (!endFace)
        return false;

    // Create the joint geometry
    Ptr<JointGeometry> jointGeometry = JointGeometry::createByPlanarFace(endFace, nullptr, CenterKeyPoint);
    if (!jointGeometry)
        return false;

    // Create JointOrigininput
    Ptr<JointOrigins> jointOrigs = rootComp->jointOrigins();
    if (!jointOrigs)
        return false;
    Ptr<JointOriginInput> jointOriginInput = jointOrigs->createInput(jointGeometry);
    if (!jointOriginInput)
        return false;

    // Create the JointOrigin
    Ptr<JointOrigin> jointOrigin = jointOrigs->add(jointOriginInput);
    if (!jointOrigin)
        return false;

    // Fit to window
    Ptr<Viewport> viewPort = app->activeViewport();
    if (!viewPort)
        return false;
    Ptr<Camera> cam = viewPort->camera();
    if (!cam)
        return false;
    cam->isFitView(true);
    viewPort->camera(cam);


    return true;
}

#ifdef XI_WIN

#include <windows.h>

BOOL APIENTRY DllMain(HMODULE hmodule, DWORD reason, LPVOID reserved)
{
    switch (reason)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

#endif // XI_WIN