Home Hierarchy Members Alphabetical Related Pages

Loading a VRML file

Loading a VRML file is two lines of code in the first place:
 Scene scene;
 scene.load("myfile.wrl");
The main element you access from a file is therefore a wrl::Scene. A Scene can be loaded from a file using the load member function, and contains a list of nodes that correspond to the top nodes present in the file. Here is for example how to display the name of the top nodes:
 for (unsigned int i=0;i<scene.nodes.size();++i)
 {
   cout<<"top node "<<i<<" is a "<<scene.nodes[i]->typeName()<<endl;
 }

Handling errors

Your VRML file might contain errors, or (very unlikely ;-) the parser can contain some bugs. If it is the case, the load function throws an exception that you have to catch at some point. Here is a simple example of a program that check that a VRML file can be parsed.
#include <xdkwrl/scene.h>

#include <iostream>
#include <stdexcept>
#include <string>

using namespace std;

int
main(int char** argv)
{
  string fileName(argv[1]); // should do better checks!!  
  try
  {
    wrl::Scene scene;
    scene.load(fileName);
    cout<<fileName<<" can be parsed correctly"<<endl;
  }
  catch (runtime_error& e)
  {
    cerr<<fileName<<" *cannot* be parsed correctly\n"
        <<e.what()<<endl;
  }
}

The two flavors of the parser

If you look at the documentation of wrl::Scene::load(), you will see that it can be passed two extra parameters. The first one is a verbose flag that indicates to the parser whether it should output some information while parsing the file. It can be useful if you want to see how much can be parsed in the case of a file that causes a parsing error.

The second flag is more involved. It indicates which flavor of the parser to use. There exists two implementations of the parser: one is generated by ANTLR from the grammar and is therefore trustworthy. However, it is a bit slow and is therefore referred to as the "slow" flavor. The second implementations overidde some of the functions of the first parser in order to achieve faster parsing (and is therefore refered to as the "fast" flavor"). It has been thoroughly tested and works fine. So you probably want to use that one in general and this is why the default value for the fast flag is true. However, if you encounter a file for which the fast parsing fails, and for which you are pretty sure the file is a valid VRML one, you can use the slow version to try to still parse the file. If it fails too, then you can send me an e-mail with the file so I can fix the parser.

Below is a complete example of a program that can accept different command line arguments to perform the different combinations of flags and parsing. It uses the argstream tool bundled with the library for command line parsing.

#include <xdkwrl/scene.h>
#include <xdkwrl/tools/argstream.h>

#include <iostream>
#include <stdexcept>

using namespace std;

int
main(int argc,char** argv)
{
  bool verbose;
  bool slow; 
  string file;
  
  argstream as(argc,argv);
  as>>option('v',"verbose",verbose,"")
    >>option('s',"slow",slow,"")
    >>values<string>(&file),"files to parse",1)
    >>help();
  as.defaultErrorHandling();

  try
  {
      wrl::Scene scene;
      scene.load(file,verbose,!slow);
      cout<<fileName<<" can be parsed correctly"<<endl;
    }
  }
  catch (runtime_error& e)
  {
    cerr<<fileName<<" *cannot* be parsed correctly\n"
        <<e.what()<<endl;
  }
}


Generated on 24 Feb 2005 with doxygen version 1.3.9.1. Valid HTML 4.0! Valid CSS!