SHAPER  9.12.0
How to create custom features or plugins

A SHAPER module consists of one or several plug-ins which provide implementation of Module features. To extend the application functionality, developers are able to add their own features into existent plugins. Also, it is possible to create a custom plugin, if necessary.
This document describes the basic principles of plugin/feature system and shows how to use the API for writing a feature or plugin. Currently, the API is available for C++ and Python languages. Plugin, written in C++ is a shared object (dll); For Python, it is regular python module, with *.py extension.

XML configuration of the module

By default, all application's plugins are stored in the /plugins folder. However, it is possible to change this path in preferences: "Preferences >> Module|Plugins >> Default Path".
The plugins directory have to contain plugins.xml file, which declares plugins included in the module and describes their parameters, like this:

<plugin library="FooPlugin" configuration="plugin-Foo.xml"/>

or, for python plugin:

<plugin script="PythonicPlugin" configuration="plugin-Pythonic.xml"/>

First example declares FooPlugin, which is library (FooPlugin.dll or FooPlugin.so) with "plugin-Foo.xml" configuration file. The second - a Python module, PythonicPlugin.py. All the declared libraries, scripts and configuration files should be placed in the same directory as the plugins.xml. Note also, that library and script attributes should not contain any extensions (*.py, *.dll); However, the configuration attribute doesn't have any pattern and just have to match name of the plugin configuration file.

XML configuration of a plugin

The plugin configuration files contains description of features:

  1. Position in the main menu: workbench and group.
  2. Visual representation of feature's button: text, icon, tooltip
  3. Representation feature's in the property panel (widgets)

Here is example configuration for a feature for storing and editing a double value:

<plugin>
<workbench id="FooTab">
<group "Counters">
<feature id="CppDoubleCounter" title="Counter">
<doublevalue id="DoubleCounterData" label="Value" default="0"/>
</feature>
</group>
</workbench>
</plugin>

Now we will show how to implement a plugin with this feature using the C++ and Python APIs.

Creating a plugin

First of all, you should create/modify all configuration files (*.xml) in your plugin directory, as shown in examples before. For plugin-Pythonic.xml you may use the same content as in the plugin-Foo.xml, just change the name of the feature, in example, to PythonDoubleCounter. In this case you will get two features in one workbench and group, despite the fact that they are from different plugins.
Secondly, you should create a subclass of ModelAPI_Plugin. Constructor on the subclass should register itself in the application. In C++ it is:

#include <ModelAPI_Plugin.h>
//...
class FooPlugin : public ModelAPI_Plugin {
FooPlugin() // Constructor
{
ModelAPI_Session::get()->registerPlugin(this);
}
//...
Interface common for any plugin: allows to use plugin by the plugins manager.
Definition: ModelAPI_Plugin.h:34
static std::shared_ptr< ModelAPI_Session > get()
Returns the real implementation (the alone instance per application) of the plugin manager.
Definition: ModelAPI_Session.cpp:68

And in Python:

import ModelAPI
#...
class PythonicPlugin(ModelAPI.ModelAPI_Plugin)
def __init__(self): #constructor
ModelAPI.ModelAPI_Plugin.__init__(self) # call ancestor's constructor
aSession = ModelAPI.ModelAPI_Session.get()
aSession.registerPlugin(self) # register itself
#...
Object that knows (from the initial XML file) which plugin contains which feature,...
Definition: ModelAPI_Session.h:42

Furthermore, your class must have implementation of the createFeature(...) method, which should create corresponding feature object by the given id:

FeaturePtr FooPlugin::createFeature(std::string theFeatureID)
{
if (theFeatureID == "CppDoubleCounter") {
return FeaturePtr(new CppDoubleCounter);
} else {
return FeaturePtr()
}
}

It is a bit more tricky for Python - you should pass the ownership of created object from Python to the application by calling the __disown__() method:

def createFeature(self, theFeatureID):
if theFeatureID == "PythonDoubleCounter":
return PythonDoubleCounter().__disown__() # passing the ownership
else:
return None

Now your plugin is able to create features, declared in its configuration file. However, to register the plugin properly, its constructor must be called on loading of the library (script), like this:

static FooPlugin* MY_FOOPLUGIN_INSTANCE = new FooPlugin();

Please note, that some linux platforms required unique names for static variables.
For Python, note that this code should be in the module's namespace (has no leading spaces):

plugin = PythonicPlugin()
plugin.__disown__()

Plugin is created, lets pass over to the feature's implementation.

Creating a feature

Like a plugin, feature has its own base class - ModelAPI_Feature.

#include <ModelAPI_Feature.h>
//...
class CppDoubleCounter : public ModelAPI_Feature { //...
Feature function that represents the particular functionality of this operation.
Definition: ModelAPI_Feature.h:39

Python:

import ModelAPI
#...
class PythonDoubleCounter(ModelAPI.ModelAPI_Feature):
def __init__(self):
ModelAPI.ModelAPI_Feature.__init__(self) # default constructor;

And like a plugin implements a functionality to create 'feature' objects by string id, feature defines which type of data should be stored in model and how this data should be processed. The initAttributes(...) method links feature's widget with data model:

{
data()->addAttribute("DoubleCounterData", ModelAPI_AttributeDouble::typeId());
}
virtual void initAttributes()
Request for initialization of data model of the feature: adding all attributes.
Definition: ConstructionPlugin_Point.cpp:56
static MODELAPI_EXPORT std::string typeId()
Returns the type of this class of attributes.
Definition: ModelAPI_AttributeDouble.h:71
virtual std::shared_ptr< ModelAPI_Data > data() const
Returns the data manager of this object: attributes.
Definition: ModelAPI_Object.cpp:45

Python:

def initAttributes(self):
self.data().addAttribute("DoubleCounterData", ModelAPI.ModelAPI_AttributeDouble.typeId())
Attribute that contains real value with double precision.
Definition: ModelAPI_AttributeDouble.h:34

As you may notice, this method defines that feature has a widget with "DoubleCounterData" id, which has Double type. Therefore, if your feature uses, in example, three widgets, the initAttributes() method should 'init' three attributes.
Sometimes, it is not enough just to get data (ModelAPI_Data) from user input, and an internal logic (how to process the data) is required. The execute() method gives ability to implement it. In our example, we will just print the data from the input to console:

void CppDoubleCounter::execute()
{
double d = data()->real("DoubleCounterData")->value();
std::cout << "User input: " << d << std::endl;
}

Python:

def execute(self):
d = data().real("DoubleCounterData").value()
print "User input: ", d

Creation of a Qt panel and custom controls in the plugin

Plugin allows creating of a specific property panel content and a custom widget. The panel content will be shown instead of controls container in the application property panel. The custom widget will be shown among other standard and custom widgets in the Property Panel.

To provide this SHAPER has a widget creator interface and a factory of the creators. Each plugin which creates panels or custom widgets should implement own creator and register it in this factory by some unique string-ID. This creator will be obtained by this name and create new controls. Steps to create the Qt property panel content are the following:

  1. append Qt library dependencies in plugin project
  2. write the custom panel content. It should be a child of a QWidget. It is necessary to put it in the application Property Panel.
  3. define a unique panel name. Put this name in the "property_panel_id" section of the feature. If this XML section is filled, XML sections of feature attributes should not be defined, they will be ignored. The following example creates a custom panel:
    <plugin>
    <workbench id="FooTab">
    <group id="Counters">
    <feature id="CppDoubleCounter" title="Counter" property_panel_id = "CountersPanel">
    </feature>
    </group>
    </workbench>
    </plugin>
  4. write a widget creator, which will create an instance of a panel content by the unique name. This creator must inherit SHAPER widget creator interface. It will implement a virtual method of a panel creation.
  5. create an instance of the widget creator and register it in the widget creator factory.

SamplePanelPlugin_Plugin is an example plugin to create a custom property panel.

Steps to create a custom widget are the following:

  1. append Qt library dependencies in plugin project
  2. write the custom widget. It should be a child of a SHAPER model widget. It provides a set of standard Qt controls and methods to apply these controls content into the model and back. So, the next virtual methods should be implemented:
    1. provides all internal Qt widgets (to let application to listen "value changed" Qt events, etc.)
    2. store of the values from controls to data model by request
    3. fill Qt widgets with the values from data model by request
  3. define a unique key value for the custom widget, add a section with this key in the XML, for an example:
    <plugin>
    <workbench id="FooTab">
    <group id="Counters">
    <feature id="CppDoubleCounter" title="Counter">
    <sample_combo_box id="ComboValue" values="Value_1, Value_2"/>
    </feature>
    </group>
    </workbench>
    </plugin>
  4. write a widget creator, which will create an instance of a custom widget by the unique name. This creator must inherit SHAPER widget creator interface. It will implement a virtual method of a widget creation. In this method some information is accessible, which can be useful in this widget. This is API of an XML configuration of this widget and a workshop instance to obtain some information about application state if needed.
  5. create an instance of the widget creator and register it in the widget creator factory.

To sum everything up, we:

  1. Declared two custom plugins in the plugins.xml
  2. Declared a feature for each plugin
  3. Created a custom plugin, by subclassing from ModelAPI_Plugin and implementation of createFeature(...) method
  4. Created a custom feature, by subclassing from ModelAPI_Feature and implementation of initAttributes() and execute() methods

If you writing a C++ plugin you should compile and link all sources in a dynamic link library (shared object). In Windows, do not forget to export symbols.