SEARCH
TOOLBOX
LANGUAGES
ComponentCreation

ComponentCreation

From SOFAWiki

Jump to: navigation, search
Basic instructions to create a new Component

A Sofa Component is a class deriving, directly or not, from sofa::core::objectmodel::BaseObject.

Contents

File Creation in the good directory

Create the skeleton of your components.

  • The fast and dirty way, is to place your files next to the other components, in Sofa/modules/sofa/components/ and the subdirectory corresponding to the type of your component (forcefield, constraint, linearsolver, ...).
  • To be cleaner, and make it easier to distribute your creation, you'd rather put your files inside a directory in Sofa/applications/projects/ . Then you just need to create/edit a file projects-local.cfg to be placed in Sofa/applications/projects/ and add :

SUBDIRS += name_of_your_directory

Put as many SUBDIRS lines as projects you want to add to the compilation of Sofa.


Link to the Sofa Factory

You have to declare, and link your components to the Object Factory. To do so, Sofa provide some macros to help you. You will find these macros in all our components, so don't hesitate to take a look.

  • Add a : SOFA_DECL_CLASS(NewComponent) in your .cpp file, NewComponent being the class name of your component.
  • Register the component: it is generally done in the .cpp of your new component class.

if your component is not template:

 int NewComponentClass = core::RegisterObject("Description of your component")
.add< NewComponentClass >();

if your component is template with Vec3dTypes and Vec3fTypes (for instance)

 int NewComponentClass = core::RegisterObject("Description of your component")
.add< NewComponent<Vec3dTypes> >()
.add< NewComponent<Vec3fTypes> >()
;
  • Add a : SOFA_LINK_CLASS(NewComponent) in the init file.
    • If you chose to put your components directly inside sofa directories (fast and dirty), you will find a file called initNameCategoryComponent.cpp with NameCategoryComponent being the category of your component: ForceField, Constraint, Mapping ...
    • If you have a separated project, you have to create the files initMyProject.h and initMyProject.cpp. Basically, this is only a file containing all the existing components, being declared using the macro SOFA_LINK_CLASS(NewComponent). You can find examples in Sofa/modules/sofa/component/forcefield/initForceField.cpp, or Sofa/modules/sofa/component/constraint/initConstraint.cpp, ...


If you don't do one of these steps correctly, an error will occur during the launch of your simulation, saying : Object type "NewComponent" creation Failed

Modifications of the .pro file

  • If you chose to put your components directly inside sofa directories (fast and dirty), open the .pro file in the same directory as your component
  • If you have a separated project, then you should already have a .pro file (take example on one of our projects), open this .pro file
  • Modify the opened .pro file by
    • Adding in the HEADERS section, the list of the new .h and .inl files
    • Adding in the SOURCES section, the list of the new .cpp files

Recreate the project

  1. For windows users, you have to run projectVC8.bat or projectVC9.bat depending on your version of Microsoft Visual Studio C++.
  2. For the others, simply run qmake at the Sofa main directory

Build Sofa

Launch the compilation. A good way to verify that your component's creation went well, is to launch the Modeler, and try to find it in the Library. If it doesn't appear, it means that you failed the Link to the Sofa Factory step.

Documentation

A scene has to be created, with the same name as your component, and placed inside Sofa/examples/Components and its good subdirectory. Finally, you can write a small paragraph for the sofa documentation placed in Sofa/doc, and a link to a publication, if any exists.