Loading a Xml scene with SFE

14 Apr Loading a Xml scene with SFE

Loading a SOFA Xml scene with SFE is quite straightforward and can be done in only a few lines of code.

First, you have to include the SFE header. Note that depending on your version of SFE, the name of the file to include might change.

1
#include <sfe/Simulation.h>

Then you have to create the simulation object and ask it to load your xml file, in this case, “cubes.scn”.

2
3
4
5
6
int main ( int argc, char** argv )
{
   sfe::Simulation simulation = sfe::getSimulation();
   simulation.loadFile( "cubes.scn" );
}

And that’s it!

To be convinced that your scene is actually loaded, you can display the scene graph in the console. For instance by calling the following method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void print_node_subgraph( sfe::Node node, const std::string& tab )
{
  // Print the name of the current node
  std::cout << tab << node.name() << std::endl;
  std::string newtab = tab + "\t";
 
  // Print the name of all its objects
  for ( auto object : node.objects() )
     std::cout << newtab << object.name() << std::endl;
 
  // Process of all its children
  for ( auto child : node.children() )
     print_node_subgraph( child, newtab );
}
No Comments

Post A Comment