Minimal Part 21 C++ example

Based on P21read

Contents

Important Classes

  • Registry (contains information about types present in the current schema)
  • InstMgr (holds instances that have been created or that have been loaded from a file)
  • STEPfile (takes care of reading and writing Part 21 files, and creates an SDAI_Application_instance for every instance read)
  • SDAI_Application_instance (the base class for every type of instance that STEP deals with)

Code

 1 #include <sc_cf.h>
 2 extern void SchemaInit( class Registry & );
 3 #include <STEPfile.h>
 4 #include <sdai.h>
 5 #include <STEPattribute.h>
 6 #include <ExpDict.h>
 7 #include <Registry.h>
 8 #include <errordesc.h>
 9 #ifdef HAVE_UNISTD_H
10 # include <unistd.h>
11 #endif
12 
13 int main( int argc, char * argv[] ) {
14 
15     // the registry contains information about types present in the current schema; SchemaInit 
16     // is a function in the schema-specific SDAI library
17     Registry  registry( SchemaInit );
18 
19     //the InstMgr holds instances that have been created or that have been loaded from a file
20     InstMgr   instance_list;
21 
22     // STEPfile takes care of reading and writing Part 21 files
23     STEPfile  sfile( registry, instance_list, "", false );
24 
25     // read a file, using the name from the command line
26     sfile.ReadExchangeFile( argv[1] );
27 
28     // check for errors; exit if they are particularly severe
29     if( sfile.Error().severity() < SEVERITY_USERMSG ) {
30         sfile.Error().PrintContents( cout );
31     }
32     if ( sfile.Error().severity() <= SEVERITY_INCOMPLETE ) {
33         exit(1);
34     }
35 
36     /**************************************************
37     ** do something with the data here
38     ***************************************************/
39 
40     // write to "file.out", then check for write errors. The write operation overwrites any
41     // errors caused by previous operations.
42     sfile.WriteExchangeFile( "file.out" );
43     if( sfile.Error().severity() < SEVERITY_USERMSG ) {
44         sfile.Error().PrintContents( cout );
45     }
46 }

Include Paths

Stepcode’s includes are not relative the top level of the stepcode include directory, so multiple include paths are needed to compile this example:

  • include/stepcode
  • include/stepcode/cldai
  • include/stepcode/cleditor
  • include/stepcode/clstepcore
  • include/stepcode/clutils

Linking

Your executable will need to link to various libraries, likely including a generated library, specific to the schema you are interested in. Once generated, this lib can be found in build/lib with a prefix of libsdai_.

It will also need to link with cldai, cleditor, clstepcore, and clutils - all found in build/lib.

If you get stuck, consider examining the compile/link commands used for one of the p21read executables. This can be done with make VERBOSE=1_:

make VERBOSE=1 p21read_sdai_ap214e3