workshop_net_plugins
workshop_net_plugins
NET PLUGIN
CREATION GUIDE
Important Note
This Software and Related Documentation are proprietary to Siemens Industry Software Inc.
Introduction .............................................................................................................................................. 3
A More Complex Plugin: Requesting Services and Adding New Behaviour ........................................... 11
The Parasolid Workshop.Net Plugin Creation Guide provides information to help you extend
Workshop.Net by using its ‘plugin’ mechanism. It also explains how to use this mechanism in other
applications.
This guide is for experienced users of C# and Visual Studio who are interested in using
Workshop.Net’s extensibility mechanism, either to extend Workshop.Net or to create further
applications. Less experienced C# users may need to refer to standard reference material for C# and
Visual Studio in addition to this guide.
WHY SHOULD I WRITE A PLUGIN?
By writing a plugin, you can extend Workshop.Net’s functionality. You can create a self-contained,
custom component which is fully integrated with existing Workshop.Net features such as model
display and entity selection. You can easily re-use many of the capabilities of Workshop.Net’s source
code, such as gathering and managing information about Parasolid models.
The plugin mechanism is powerful and flexible but is not the only way to extend Workshop.Net. You
might alternatively choose to add your functionality simply by adding code to existing plugins or other
components of the Workshop.Net source.
Plugin 2
Plugin 3
Plugin 1
Main
Application
A plugin is typically built in its own DLL, separate from the main application executable and other
plugins. Plugins, then, have the advantage of being self-contained and potentially optional
components of an application. Creating a plugin involves writing code to support plugin messaging,
however, which is not needed if you add functionality in other ways.
• Re-use of the application framework from the Workshop.Net source to build a new
application
• Extension of Workshop.Net where the extension must build as a separate .Net assembly and
be an optional component
• Integrating functionality in various versions of Workshop.Net with minimal effort (so that
you need as few code-changes as possible when you install the latest release of
Workshop.Net)
Adding code without making a new plugin may be more efficient when:
Implementation Advice: When deciding whether to create a plugin, you should also
consider application testing. Plugins often communicate with the rest of the application
via events, rather than calling functions directly, which means that certain coding faults
become evident at run time rather than build time. So interactive testing is particularly
important for plugins.
CODE WALKTHROUGH FOR A SIMPLE PLUGIN
The following code walkthrough describes the source code for a simple Workshop.Net plugin and
shows how a plugin operates. For more formal documentation of the plugin mechanism (glossary and
details of the plugin lifecycle), see Appendices A and B of this guide.
The source code of the Example Plugin is in the Workshop.Net solution (project ‘Workshop.Example’).
You may wish to examine the code as you read this walkthrough.
The Example Plugin provides a simple service: it reports the number of faces on the model in
Workshop.Net’s active display window.
APPLICATION STARTUP
On application startup, the plugin mechanism calls the method ‘RegisterPlugin’ in the class which
implements the interface ‘IFrameworkPlugin’. (See PluginRegistration.cs for the code of this method.)
The rest of the code in PluginRegistration.cs implements the delegates and the constructor of the
‘RegisterPlugin’ class.
Key Concepts
When the constructor of Workshop.Net’s main user interface runs, it sends the plugin event
‘StandardPluginEvents.CreateMenuStripItems’ to the plugin manager (see constructor in ‘MainGUI.cs’
in project ‘Workshop.GUI’). The plugin manager then calls all the delegates associated with this event.
Since the ‘CreateMenuStripItems’ function of the Example Plugin is one of these delegates, it gets
called when the user interface is created. The ‘CreateMenuStripItems’ function adds an appropriate
menu item to the Workshop.Net menu strip.
Key Concepts
• There are predefined standard plugin events and you can find these in the
Workshop.Net source code
• When the application code sends a plugin event (message) to the plugin
manager, the plugin manager calls all delegates which have been registered
for that event
PROVIDING A SERVICE
The Example Plugin provides a service in response to the user’s clicking on ‘Example Plugin->Example
Menu Item’. The code in ‘CreateMenuItems’ sets up this menu item to send the plugin event
“ExampleMenuItem_Click” when the item is clicked. So, when the item is clicked, the delegate
‘ExampleMenuItem_Click’ gets called because it has been registered to handle that plugin event. This
delegate displays the Example Plugin’s form. When the user clicks the ‘OK’ button on the form, the
plugin reports the number of faces on the body in the active display window.
HOW DO I ADD A PLUGIN TO WORKSHOP.NET?
1. Open the Parasolid Workshop.Net solution from the Workshop.Net sub-directory of the
source directory. This solution contains a set of plugins which implement Workshop.Net’s
specific functionality. It also references Framework projects, including the definition of the
plugin mechanism itself and many Parasolid-specific and general utilities.
2. Add a new Class Library Project to the solution. Name your new Workshop plugin
“Workshop.XXX” and use the “PLMComponents.Parasolid.Workshop.XXX” namespace for
your plugin code. The plugin mechanism does not restrict the name or namespace but this
naming scheme is recommended. A Class Library project should build as a DLL.
3. Add a post-build event to your plugin project, to ensure that your plugin DLL is copied to
Workshop.Net’s central build area. You can do this via the ‘Project Properties’ for your
plugin project. This post-build event is needed because the plugin DLL needs to be in the
‘Plugins’ subdirectory of the Workshop.Net executable’s working directory in order to be
detected on application startup. To allow debugging, the pdb file for the plugin must also be
in this subdirectory. You can use the following code for the event and you should set it to run
“Only when the build updates the Project output”.
xcopy /y /f /e /r "$(TargetPath)"
"$(SolutionDir)Build\$(ConfigurationName)\Plugins\"
if “$(ConfigurationName)”==”Debug” xcopy /y /f /e /r
"$(TargetDir)$(TargetName).pdb"
4. Add references in your plugin project to Framework.PluginManager and
"$(SolutionDir)Build\$(ConfigurationName)\Plugins\"
Framework.ExceptionManager. In order to re-use existing code from other Framework or
Workshop.Net projects, add references to these projects as required.
5. Create a class which implements the IFrameworkPlugin interface. This interface is defined
in PLMComponents.Parasolid.Framework.Plugins. It is essential since it allows Workshop.Net
to recognise your DLL as a plugin. Your class must have a constructor and must implement a
RegisterPlugin function which returns void and accepts no arguments. This function should
register any delegates to handle plugin events. See code walkthroughs and source code for
examples.
6. When you have built Workshop.Net including your plugin, you should be able to run and
debug the application according to the instructions in the Parasolid Workshop.Net Source
Code Overview. The information in the ‘Overview’ about copying the Parasolid library and
schemas to the build directory and setting the correct executable for debugging is
particularly relevant.
7. Add code to your plugin to implement your new functionality. You should take into account
the information from the code walkthroughs and other sections of this guide.
The basic sequence of operations, on application startup and when responding to events, is the same
for all plugins. Certain features of a more complex plugin than the Example Plugin are of particular
interest, however.
The source code for the following discussion of a more complex plugin, the Boolean Plugin, is in the
Workshop.Net solution (project ‘Workshop.Boolean’).
The Boolean Plugin supports Boolean operations on Parasolid models: unite, subtract and intersect,
with various options for merging topology. To allow the user to set up these operations, this plugin
uses existing application functionality via plugin messages.
The following discussion concerns the Boolean Plugin’s use of the plugin mechanism, rather than the
details of the modelling operations (which are implemented using standard Parasolid coding
practices).
SENDING MESSAGES TO REQUEST SERVICES
One of the actions which this more complex plugin takes is to request services from other parts of the
application by sending plugin messages. You can see an example of this action in the function
‘SelectedEntity’ in the file BooleanControl.cs. The function sends the standard plugin events
“UnselectEntity” and “SelectEntity”, to unselect an entity in the current display window and select the
whole body instead. (In the following picture, the user has clicked on a face of the target body – if the
Boolean Plugin were not operating then only the face would be selected, but since the Boolean Plugin
is active the whole body is selected and is shown in red.)
You can pass arguments to the event handler using one of the constructors of the ‘PluginEvent’
structure, as in the following code. The following sample code sends both an event and a string
argument containing information for any delegates associated with the event.
Plugin Delegate
•Receives
• Sends event and
PluginEventArgument
PluginEventArgument
•Provides service
• Receives
and/or returns
PluginEventResults
PluginEventResults
ADDING NEW BEHAVIOUR AND MAINTAINING EXISTING FUNCTIONALITY
One of the delegates in the Boolean Plugin responds to the event for selection of an entity (see the
delegate ‘EntitySelected’ in this plugin’s PluginRegistration.cs file). This standard plugin event is raised
by the Workshop.Net GUI when the user clicks on an entity within a Parasolid model. This event has
delegates associated with it already, to provide Workshop.Net’s default selection behaviour. So, while
the Boolean Plugin’s delegate needs to provide plugin-specific behaviour while the plugin is active, it
must also preserve the default behaviour when the Boolean Plugin is not in use.
When the Boolean plugin’s form is visible, the Boolean Plugin’s delegate overrides the standard
behaviour of Workshop.Net for the “StandardPluginEvents.EntitySelected” event: it selects the whole
body rather than an entity on the body. When the Boolean Plugin’s form is not shown, however, this
specific behaviour should not override the standard behaviour, so the Boolean Plugin’s delegate does
nothing. (The delegate checks for the Boolean Plugin’s form being set to ‘null’ in order to determine
whether the form is visible: other code within the plugin ensures that this value is null when the form
is invisible – see function ‘BooleanCtrl_VisibleChanged’ for details.)
The following picture shows the behaviour of the application when the Boolean Plugin’s form has
been closed: the user has selected a face on the model and only the face is highlighted in red, rather
than the whole body as would be selected for a Boolean operation.
Key Concepts
• A plugin can send messages and other information to the rest of the
application code in order to use services or get information
• The functions for sending messages are ‘SendSynchronousMessage’ and
‘SendAsynchronousMessage’
• A plugin’s delegates may override the application’s default functionality if they
respond to standard plugin events.
Key Concepts Summary
To make a new application by using your own GUI with the Framework, write a plugin which responds
to the message ‘StandardPluginEvents.CreateGUI’. Then the Framework calls your delegate for this
message in a new thread during application startup. In this scenario you should start with an empty
Framework solution, rather than with the Parasolid Workshop.Net solution. The Framework solution
contains only Framework projects (not the plugins which implement Workshop.Net) and is in the
Framework sub-directory of the installed Workshop.Net source directory. Add a new Class Library
project to begin coding your plugin, as in the preceding section “How Do I Add a Plugin to
Workshop.Net?”.
In your event handler, you should create and display the appropriate GUI elements (e.g. Windows
Forms). This event handler should also return a result to indicate that the GUI has successfully started.
For example:
Application.Run(new Form1());
You are free to adjust existing Workshop.Net functionality when adding a plugin, but you may find the
following recommendations useful if you wish to keep the existing behaviour.
A simple approach for adding plugins to Workshop.Net is to use the source code of existing plugins as
a model. This approach ensures that your plugin delegates operate only when your plugin is visibly
active and ensures that your plugin does not store data unnecessarily.
This approach is useful but is not imposed by the plugin mechanism: by default, plugins have the
flexibility to operate differently. You could define an extended plugin interface if you require plugins
to conform to a more formal standard.
MANAGING MODELS AND WINDOWS
The ‘Active’ Window The display window which has been most recently
selected by the user is considered to be ‘active’.
When there is only one display window, it is always
the active window.
Application Framework A C# codebase providing the basis for a new application, including its
main executable. The Framework comprises the
PLMComponents.Parasolid.Framework namespace. (Workshop.Net is
an application built using the Application Framework.)
Plugin Message Handler A function which is registered with the Plugin Manager to respond to
a particular message. A Message Handler accepts arguments and
returns a response to the application. Both the arguments and the
response can be an object of any type.
Plugin Manager The Plugin Manager is the base functionality in the Application
Framework which starts and runs plugins. The Plugin Manager also
delivers messages to plugins. All plugins should register message
handlers with the Plugin Manager to identify the messages to which
they can respond and to associate each message handler a specific
message.
APPENDIX B - THE PLUGIN LIFECYCLE
The plugin mechanism involves passing messages between various components in the Framework.
The components register delegate functions for specific event messages. When an event occurs, the
plugin mechanism calls the registered delegate functions for that event and passes to the delegate
functions any arguments associated with the event. The plugin lifecycle is:
2. The Plugin Manager searches a pre-defined directory (‘Plugins’) for .Net assemblies, loads
the assemblies and searches within them for any class which implements the
IFrameworkPlugin interface.
3. The Plugin Manager executes the RegisterPlugin method of any class which implements the
IFrameworkPlugin interface.
5. At some point during its execution, the application raises a plugin event with the Plugin
Manager.
6. The Plugin Manager examines the list of all the plugins currently registered with that event
and calls all the delegate functions associated with the raised event. (These functions may be
from any plugin and may be called in any order.)
7. When a delegate function has finished executing, it returns a result to the Plugin Manager.
8. The Plugin Manager collates the results from all the delegate functions that it called in
response to the raised event and passes these back to the calling function (i.e. the function
which raised the event).
9. When the application is shut down, any plugin assemblies that are in memory are unloaded
with the rest of the application.