I'm getting closer. I was able to make the sketch but am having trouble getting the sketch profile so the revolve feature fails. I'm getting the following error: "invalid profile(s) for Revolve Feature". I'm retrieving the profile based on the revolve sample: https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-407f1b28-fd08-11e4-a671-3417ebd3d5be (converted to C++).
Thanks for the help.
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <Cam/CamAll.h>
#include <sstream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <numeric>
#include <cmath>
#include <memory>
#include <windows.h>
using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;
// Globals
const std::string _units = "mm";
Ptr<Application> _app;
Ptr<UserInterface> _ui;
Ptr<TextBoxCommandInput> _errMessage;
bool checkReturn(Ptr<Base> returnObj)
{
if (returnObj)
return true;
else if (_app && _ui)
{
std::string errDesc;
_app->getLastError(&errDesc);
_ui->messageBox(errDesc);
return false;
}
else
return false;
}
extern "C" XI_EXPORT bool run(const char* context)
{
_app = Application::get();
if (!checkReturn(_app))
return false;
_ui = _app->userInterface();
if (!checkReturn(_ui))
return false;
double cmRadiusBase = 5.0;
double cmRadiusHole = 1.0;
double cmWidth = 2.0;
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 (!checkReturn(design))
return false;
// Get the root component of the active design
Ptr<Component> component = design->rootComponent();
if (!checkReturn(component))
return false;
// Create a new sketch.
Ptr<Sketches> sketches = component->sketches();
if (!checkReturn(sketches))
return nullptr;
Ptr<ConstructionPlane> xyPlane = component->xYConstructionPlane();
if (!checkReturn(xyPlane))
return nullptr;
Ptr<Sketch> baseSketch = sketches->add(xyPlane);
if (!checkReturn(xyPlane))
return nullptr;
// Draw a circle for the base.
baseSketch->sketchCurves()->sketchCircles()->addByCenterRadius(adsk::core::Point3D::create(0, 0, 0), cmRadiusBase);
if (!checkReturn(baseSketch))
return nullptr;
// Draw a circle for the center hole, if the value is greater than 0.
Ptr<Profile> prof = nullptr;
if (cmRadiusHole - (_app->pointTolerance() * 2) > 0)
{
Ptr<SketchCircle> circ = baseSketch->sketchCurves()->sketchCircles()->addByCenterRadius(
adsk::core::Point3D::create(0, 0, 0), cmRadiusHole);
if (!checkReturn(circ))
return nullptr;
// Find the profile that uses both circles.
for (Ptr<Profile> tempProf : baseSketch->profiles())
{
if (tempProf->profileLoops()->count() == 2)
{
prof = tempProf;
break;
}
}
}
else
{
// Use the single profile.
prof = baseSketch->profiles()->item(0);
}
if (!checkReturn(prof))
return nullptr;
// Extrude the circle to create the base of the pulley.
// Create an extrusion input to be able to define the input needed for an extrusion
// while specifying the profile and that a new component is to be created
Ptr<ExtrudeFeatures> extrudes = component->features()->extrudeFeatures();
if (!checkReturn(extrudes))
return nullptr;
Ptr<ExtrudeFeatureInput> extInput = extrudes->createInput(prof, NewBodyFeatureOperation);
if (!checkReturn(extInput))
return nullptr;
// Define that the extent is a distance extent of the pulley width.
Ptr<ValueInput> distance = adsk::core::ValueInput::createByReal(cmWidth);
if (!checkReturn(distance))
return nullptr;
bool result = extInput->setDistanceExtent(false, distance);
if (!result)
return nullptr;
// Create the extrusion.
Ptr<ExtrudeFeature> baseExtrude = extrudes->add(extInput);
if (!checkReturn(baseExtrude))
return nullptr;
// Create the revolve sketch (a rectangle)
Ptr<ConstructionPlane> yzPlane = component->yZConstructionPlane();
if (!checkReturn(yzPlane))
return nullptr;
Ptr<Sketch> sketchSideRailA = sketches->add(yzPlane);
if (!checkReturn(sketchSideRailA))
return nullptr;
auto length = cmRadiusBase + 10.0;
auto pointStart = Point3D::create(0, cmRadiusHole);
auto pointEnd = Point3D::create(1.0, length);
auto rect = sketchSideRailA->sketchCurves()->sketchLines()->addTwoPointRectangle(pointStart, pointEnd);
// Create a revolve input.
auto profile = sketchSideRailA->profiles()->item(0);
auto axisLine = sketchSideRailA->sketchCurves()->sketchLines()->addByTwoPoints(Point3D::create(0, 0, 0), Point3D::create(1.0, 0, 0));
axisLine->isConstruction(true);
auto revolveFeatures = component->features()->revolveFeatures();
auto revolveInput = revolveFeatures->createInput(profile, axisLine, FeatureOperations::NewBodyFeatureOperation);
// Set the angle for the revolve.
revolveInput->setAngleExtent(false, ValueInput::createByReal(2 * M_PI));
// Create the revolve feature.
Ptr<RevolveFeature> revolveFeature = revolveFeatures->add(revolveInput);
if (!checkReturn(revolveFeature))
{
_ui->messageBox("Failed to create revolve feature");
return false;
}
}
extern "C" XI_EXPORT bool stop(const char* context)
{
if (_ui)
{
_ui->messageBox("Stop addin");
_ui = nullptr;
}
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