CylindricalJointMotion API Sample

Description

Demonstrates creating a joint with cylindrical joint motion.

Code Samples

#Author-
#Description-
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 sketch in root component
        sketches = rootComp.sketches
        sketch = sketches.add(rootComp.xZConstructionPlane)
        sketchPts = sketch.sketchPoints
        point = adsk.core.Point3D.create(1, 0, 1)
        sketchPt = sketchPts.add(point)
        sketchLines = sketch.sketchCurves.sketchLines
        point1 = adsk.core.Point3D.create(1, 0, 2)
        point2 = adsk.core.Point3D.create(2, 0, 1)
        point1SketchSpace = sketch.modelToSketchSpace(point1)
        point2SketchSpace = sketch.modelToSketchSpace(point2)
        lines = sketchLines.addTwoPointRectangle(point1SketchSpace, point2SketchSpace)
        
        # Get the profile defined by the circle
        prof = sketch.profiles.item(0)

        # Create an extrusion input and make sure it's in a new component
        extrudes = rootComp.features.extrudeFeatures
        extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewComponentFeatureOperation)
        
        # Set the extrusion input
        distance = adsk.core.ValueInput.createByReal(5)
        extInput.setDistanceExtent(True, distance)
        extInput.isSolid = True

        # Create the extrusion
        ext = extrudes.add(extInput)
        
        # Get the end face of the created extrusion body
        endFace = ext.endFaces.item(0)
        
        # Get the occurrence of the new component
        occ = rootComp.occurrences.item(0)
        
        # Create a new sketch in the occurrence
        sketchInOcc = sketches.add(endFace, occ)
        
        # Get the sketch curve projected to the sketch
        curve = sketchInOcc.sketchCurves.item(0)
        
        # Create the first joint geometry with the sketch curve
        geo0 = adsk.fusion.JointGeometry.createByCurve(curve, adsk.fusion.JointKeyPointTypes.StartKeyPoint)
        
        # Create the second joint geometry with sketch point
        geo1 = adsk.fusion.JointGeometry.createByPoint(sketchPt)
        
        # Create joint input
        joints = rootComp.joints
        jointInput = joints.createInput(geo0, geo1)
        
        # Set the joint input
        jointInput.setAsCylindricalJointMotion(adsk.fusion.JointDirections.CustomJointDirection, curve.createForAssemblyContext(occ))
        
        # Create the joint
        joint = joints.add(jointInput)
		
        cylindricalMotion = joint.jointMotion
        rotLimits = cylindricalMotion.rotationLimits
        rotLimits.isRestValueEnabled = True
        rotLimits.restValue = 3.14 / 3
        slideLimits = cylindricalMotion.slideLimits
        slideLimits.isMinimumValueEnabled = True
        slideLimits.minimumValue = 0.1
    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/Geometry/Point3D.h>
#include <Core/UserInterface/UserInterface.h>
#include <Fusion/BRep/BRepFace.h>
#include <Fusion/BRep/BRepFaces.h>
#include <Fusion/Components/Component.h>
#include <Fusion/Components/Joint.h>
#include <Fusion/Components/JointGeometry.h>
#include <Fusion/Components/JointInput.h>
#include <Fusion/Components/JointLimits.h>
#include <Fusion/Components/Joints.h>
#include <Fusion/Components/CylindricalJointMotion.h>
#include <Fusion/Components/Occurrence.h>
#include <Fusion/Components/Occurrences.h>
#include <Fusion/Construction/ConstructionPlane.h>
#include <Fusion/Features/Features.h>
#include <Fusion/Features/ExtrudeFeature.h>
#include <Fusion/Features/ExtrudeFeatures.h>
#include <Fusion/Features/ExtrudeFeatureInput.h>
#include <Fusion/Fusion/Design.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/SketchCurve.h>
#include <Fusion/Sketch/SketchCurves.h>
#include <Fusion/Sketch/SketchLine.h>
#include <Fusion/Sketch/SketchLines.h>
#include <Fusion/Sketch/SketchLineList.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<SketchCurves> sketchCurves = sketch->sketchCurves();
    if (!sketchCurves)
        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<SketchLines> sketchLines = sketchCurves->sketchLines();
    if (!sketchLines)
        return false;
    Ptr<Point3D> point1 = Point3D::create(1, 0, 2);
    if (!point1)
        return false;
    Ptr<Point3D> point2 = Point3D::create(2, 0, 1);
    if (!point2)
        return false;
    Ptr<Point3D> point1SketchSpace = sketch->modelToSketchSpace(point1);
    if (!point1SketchSpace)
        return false;
    Ptr<Point3D> point2SketchSpace = sketch->modelToSketchSpace(point2);
    if (!point2SketchSpace)
        return false;
    Ptr<SketchLineList> lines = sketchLines->addTwoPointRectangle(point1SketchSpace, point2SketchSpace);
    if (!lines)
        return false;

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

    // Create an extrusion 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(prof, FeatureOperations::NewComponentFeatureOperation);
    if (!extInput)
        return false;

    // Set the extrusion input
    Ptr<ValueInput> distance = ValueInput::createByReal(5);
    if (!distance)
        return false;
    extInput->setDistanceExtent(true, distance);
    extInput->isSolid(true);

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

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

    // Get the occurrence of the new component
    Ptr<Occurrences> occs = rootComp->occurrences();
    if (!occs)
        return false;
    Ptr<Occurrence> occ = occs->item(0);
    if (!occ)
        return false;

    // Create a new sketch in the occurrence
    Ptr<Sketch> sketchInOcc = sketches->add(endFace, occ);
    if (!sketchInOcc)
        return false;

    // Get the sketch curve projected to the sketch
    Ptr<SketchCurves> sketchCurvesInOcc = sketchInOcc->sketchCurves();
    if (!sketchCurvesInOcc)
        return false;
    Ptr<SketchCurve> curve = sketchCurvesInOcc->item(0);
    if (!curve)
        return false;

    // Create the first joint geometry with the sketch curve
    Ptr<JointGeometry> geo0 = JointGeometry::createByCurve(curve, JointKeyPointTypes::StartKeyPoint);
    if (!geo0)
        return false;

    // Create the second joint geometry with sketch point
    Ptr<JointGeometry> geo1 = JointGeometry::createByPoint(sketchPt);
    if (!geo1)
        return false;

    // Create joint input
    Ptr<Joints> joints = rootComp->joints();
    if (!joints)
        return false;
    Ptr<JointInput> jointInput = joints->createInput(geo0, geo1);
    if (!jointInput)
        return false;

    // Set the joint input
    Ptr<SketchLine> line = curve->cast<SketchLine>();
    if (!line)
        return false;
    Ptr<SketchLine> lineProxy = line->createForAssemblyContext(occ);
    if (!lineProxy)
        return false;
    jointInput->setAsCylindricalJointMotion(JointDirections::CustomJointDirection, lineProxy);

    // Create the joint
    Ptr<Joint> joint = joints->add(jointInput);
    if (!joint)
        return false;

    Ptr<CylindricalJointMotion> cylindricalMotion = joint->jointMotion();
    if (!cylindricalMotion)
        return false;
    Ptr<JointLimits> rotLimits = cylindricalMotion->rotationLimits();
    if (!rotLimits)
        return false;
    rotLimits->isRestValueEnabled(true);
    rotLimits->restValue(3.14 / 3);
    Ptr<JointLimits> slideLimits = cylindricalMotion->slideLimits();
    if (!slideLimits)
        return false;
    slideLimits->isMinimumValueEnabled(true);
    slideLimits->minimumValue(0.1);

    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