Array of Fusion objects in C++

<?xml encoding=”UTF-8″>By Adam Nagy

In languages like JavaScript it’s very easy to use an array:

var objects = [];
objects[1] = object1;
objects[2] = object2;
// etc.

In C++ it’s not too difficult either but takes a bit more code to create and delete such an array:

app = Application::get();
if (!app)
  return false;

ui = app->userInterface();
if (!ui)
  return false;

// Get selected entities
Ptr<Selections> sel = ui->activeSelections();
int selCount = sel->count();

// Create array for storing objects
Ptr<Base> * objects = new Ptr<Base>[selCount];

// Store the objects in the array
for( int i = 0; i < selCount; i++)
{
  objects[i] = sel->item(i)->entity();
}

// Retrieve items from the array
for(int i = 0; i < selCount; i++)
{
  Ptr<ConstructionPoint> pt = objects[i];
  const char * ot = objects[i]->objectType();
  if (pt)
  {
    // Do something…
  }
}

// Delete array
for(int i = 0; i < selCount; i++)
  objects[i].detach();
delete objects;

An even nicer solution could be using a vector:

std::vector<Ptr<Base>> objects(selCount);

for(int i = 0; i < selCount; i++)
{
  Ptr<Base> obj = sel->item(i)->entity();
  if (obj)
    objects[i] = obj;
}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading