2 \page first_feature_help How to create custom features or plugins
4 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.
6 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.
8 <h3>XML configuration of the module</h3>
9 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".
11 The plugins directory have to contain `plugins.xml` file, which declares plugins included in the module and describes their parameters, like this:
13 <plugin library="FooPlugin" configuration="plugin-Foo.xml"/>
15 or, for python plugin:
17 <plugin script="PythonicPlugin" configuration="plugin-Pythonic.xml"/>
19 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.
21 <h3>XML configuration of a plugin</h3>
22 The plugin configuration files contains description of features:
24 <li>Position in the main menu: workbench and group.</li>
25 <li>Visual representation of feature's button: text, icon, tooltip</li>
26 <li>Representation feature's in the property panel (widgets)</li>
28 Here is example configuration for a feature for storing and editing a double value:
31 <workbench id="FooTab">
33 <feature id="CppDoubleCounter" title="Counter">
34 <doublevalue id="DoubleCounterData" label="Value" default="0"/>
40 Now we will show how to implement a plugin with this feature using the C++ and Python APIs.
42 <h3>Creating a plugin</h3>
43 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.
45 Secondly, you should create a subclass of ModelAPI_Plugin. Constructor on the subclass should register itself in the application. In C++ it is:
47 #include <ModelAPI_Plugin.h>
49 class FooPlugin : public ModelAPI_Plugin {
51 FooPlugin() // Constructor
53 ModelAPI_Session::get()->registerPlugin(this);
61 class PythonicPlugin(ModelAPI.ModelAPI_Plugin)
63 def __init__(self): #constructor
64 ModelAPI.ModelAPI_Plugin.__init__(self) # call ancestor's constructor
65 aSession = ModelAPI.ModelAPI_Session.get()
66 aSession.registerPlugin(self) # register itself
69 Furthermore, your class must have implementation of the `createFeature(...)` method, which should create corresponding feature object by the given id:
71 FeaturePtr FooPlugin::createFeature(std::string theFeatureID)
73 if (theFeatureID == "CppDoubleCounter") {
74 return FeaturePtr(new CppDoubleCounter);
80 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:
82 def createFeature(self, theFeatureID):
83 if theFeatureID == "PythonDoubleCounter":
84 return PythonDoubleCounter().__disown__() # passing the ownership
88 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:
90 static FooPlugin* MY_FOOPLUGIN_INSTANCE = new FooPlugin();
92 Please note, that some linux platforms required unique names for static variables.
94 For Python, note that this code should be in the module's namespace (has no leading spaces):
96 plugin = PythonicPlugin()
99 Plugin is created, lets pass over to the feature's implementation.
101 <h3>Creating a feature</h3>
102 Like a plugin, feature has its own base class - ModelAPI_Feature.
104 #include <ModelAPI_Feature.h>
106 class CppDoubleCounter : public ModelAPI_Feature { //...
112 class PythonDoubleCounter(ModelAPI.ModelAPI_Feature):
115 ModelAPI.ModelAPI_Feature.__init__(self) # default constructor;
117 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:
119 void ConstructionPlugin_Point::initAttributes()
121 data()->addAttribute("DoubleCounterData", ModelAPI_AttributeDouble::typeId());
126 def initAttributes(self):
127 self.data().addAttribute("DoubleCounterData", ModelAPI.ModelAPI_AttributeDouble.typeId())
129 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.
131 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:
133 void CppDoubleCounter::execute()
135 double d = data()->real("DoubleCounterData")->value();
136 std::cout << "User input: " << d << std::endl;
142 d = data().real("DoubleCounterData").value()
143 print "User input: ", d
146 <h3>Creation of a Qt panel and custom controls in the plugin</h3>
147 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.
149 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.
150 Steps to create the Qt property panel content are the following:
152 <li>append Qt library dependencies in plugin project</li>
153 <li>write the custom panel content. It should be a child of a QWidget. It is necessary to put it in the application Property Panel.</li>
154 <li>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:</li>
157 <workbench id="FooTab">
158 <group id="Counters">
159 <feature id="CppDoubleCounter" title="Counter" property_panel_id = "CountersPanel">
165 <li>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.</li>
166 <li>create an instance of the widget creator and register it in the widget creator factory.</li>
168 SamplePanelPlugin_Plugin is an example plugin to create a custom property panel.
171 Steps to create a custom widget are the following:
173 <li>append Qt library dependencies in plugin project</li>
174 <li>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:</li>
176 <li>provides all internal Qt widgets (to let application to listen "value changed" Qt events, etc.)</li>
177 <li>store of the values from controls to data model by request</li>
178 <li>fill Qt widgets with the values from data model by request</li>
180 <li>define a unique key value for the custom widget, add a section with this key in the XML, for an example:</li>
183 <workbench id="FooTab">
184 <group id="Counters">
185 <feature id="CppDoubleCounter" title="Counter">
186 <sample_combo_box id="ComboValue" values="Value_1, Value_2"/>
192 <li>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.</li>
193 <li>create an instance of the widget creator and register it in the widget creator factory.</li>
196 <h3>To sum everything up, we:</h3>
198 <li>Declared two custom plugins in the plugins.xml</li>
199 <li>Declared a feature for each plugin</li>
200 <li>Created a custom plugin, by subclassing from ModelAPI_Plugin and implementation of `createFeature(...)` method</li>
201 <li>Created a custom feature, by subclassing from ModelAPI_Feature and implementation of `initAttributes()` and `execute()` methods</li>
203 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 <a href="https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx">export</a> symbols.