How to offset a sketch circle using the Fusion360 API

How to offset a sketch circle using the Fusion360 API

EamonKung
Contributor Contributor
547 Views
3 Replies
Message 1 of 4

How to offset a sketch circle using the Fusion360 API

EamonKung
Contributor
Contributor

Hello,

How can I offset a sketch circle using the Fusion360 API? Could you please provide a brief C++ code example for reference?

0 Likes
Accepted solutions (1)
548 Views
3 Replies
Replies (3)
Message 2 of 4

BrianEkins
Mentor
Mentor
Accepted solution

Here's the full code of a C++ script that works for me.

#include <Core/CoreAll.h>
#include <Fusion/FusionAll.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;

    // Get the root component from the active design and create a sketch.
    Ptr<Design> des = app->activeProduct();
    Ptr<Component> root = des->rootComponent();
    Ptr<Sketch> sk = root->sketches()->add(root->xYConstructionPlane());

    // Draw a circle.
    Ptr<SketchCircles> circles = sk->sketchCurves()->sketchCircles();
    Ptr<SketchCircle> circle = circles->addByCenterRadius(adsk::core::Point3D::create(0, 0, 0), 5);

    // Offset the circle.
    Ptr<GeometricConstraints> constraints = sk->geometricConstraints();
    
    std::vector<Ptr<SketchCurve>> curves;
    curves.push_back(circle);
    Ptr<OffsetConstraintInput> input = constraints->createOffsetInput(curves, adsk::core::ValueInput::createByReal(1));

    Ptr<OffsetConstraint> offset = constraints->addOffset2(input);
    std::vector<Ptr<SketchCurve>> resultCurves = offset->childCurves();
    Ptr<SketchCircle> newCircle = resultCurves[0];

    ui->messageBox("The offset circle is " + std::to_string(newCircle->radius()) + " cm radius.");

    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
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 4

EamonKung
Contributor
Contributor

Thanks a lot for your response.

0 Likes
Message 4 of 4

MichaelT_123
Advisor
Advisor

Hi Mr  EamonKung,

 

It is known that an offset of a circle is also a circle, the concentric one, with changed by the offset value radius. 

Yes, it is possible to complicate things, but what for?

 

Regards

MichaelT

MichaelT
0 Likes