KOLMAN info   http://www.kolmanl.info

Skripty, makra, programování, API - C++


publikováno:20.10.2019, vydavatel a autor není-li uvedeno jinak: Libor Kolman, kolmanl@volny.cz
na http://kolmanl.info, v sekci Fusion 360, na stránce http://www.kolmanl.info/index.php?show=f360_scripts

Napsat první program pro řízení Fusion 360 v C++, když drtivá většina zveřejněných příkladů je v Pythonu a když s C++ teprve začínáte a zápasíte se syntaxí jazyka, není úplně jednoduché.

image

Využil jsem nemoci a zkusil jsem v posteli přepsat tento jednoduchý příklad v Pythonu:


import adsk.core, adsk.fusion, adsk.cam, traceback
def run(context):
  ui = None
  try:
    app = adsk.core.Application.get()
    ui = app.userInterface
    design = app.activeProduct
    rootComp = design.rootComponent
    sketches = rootComp.sketches
    xyPlane = rootComp.xYConstructionPlane
    sketch = sketches.add(xyPlane)
    lines = sketch.sketchCurves.sketchLines
    point0 = adsk.core.Point3D.create(0, 0, 0)
    point1 = adsk.core.Point3D.create(0, 1, 0)
    point2 = adsk.core.Point3D.create(1, 1, 0)
    point3 = adsk.core.Point3D.create(1, 0, 0)
    lines.addByTwoPoints(point0, point1)
    lines.addByTwoPoints(point1, point2)
    lines.addByTwoPoints(point2, point3)
    lines.addByTwoPoints(point3, point0)
    profile = sketch.profiles.item(0)
    extrudes = rootComp.features.extrudeFeatures
    ext_input = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    distance = adsk.core.ValueInput.createByReal(1)
    ext_input.setDistanceExtent(False, distance)
    ext_input.isSolid = True
    extrudes.add(ext_input)
  except:
    if ui:
      ui.messageBox('Failed:
{}'.format(traceback.format_exc()))

Program vykreslí krychli tak, jak je vidět na obrázku výše.

A takto to vypadá v mém podání v C++:


#include 
#include 
#include 

using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;

Ptr app;
Ptr ui;

extern "C" XI_EXPORT bool run(const char* context)
{
  app = Application::get();
  if (!app)
    return false;
  
  ui = app->userInterface();
  if (!ui)
    return false;
  try
  {
    Ptr design = app->activeProduct();
    Ptr rootComp = design->rootComponent();
    Ptr	sketches = rootComp->sketches();
    Ptr xyPlane = rootComp->xYConstructionPlane();
    Ptr sketch = sketches->add(xyPlane);
    Ptr lines = sketch->sketchCurves()->sketchLines();
    Ptr point0 = Point3D::create(0, 0, 0);
    Ptr point1 = Point3D::create(0, 1, 0);
    Ptr point2 = Point3D::create(1, 1, 0);
    Ptr point3 = Point3D::create(1, 0, 0);
    lines->addByTwoPoints(point0, point1);
    lines->addByTwoPoints(point1, point2);
    lines->addByTwoPoints(point2, point3);
    lines->addByTwoPoints(point3, point0);
    Ptr profile = sketch->profiles()->item(0);
    Ptr extrudes = rootComp->features()->extrudeFeatures();
    Ptr ext_input = extrudes->createInput(profile, FeatureOperations::NewBodyFeatureOperation);
    Ptr distance = ValueInput::createByReal(1);
    ext_input->setDistanceExtent(false, distance);
    bool vysl = ext_input->isSolid(true);
    extrudes->add(ext_input);
  }
  catch (std::bad_exception& e)
  {
    if (ui)
    	ui->messageBox("Chyba :");
  }
  return true;
}

#ifdef XI_WIN

#include 

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


http://www.kolmanl.info