Wednesday, February 17, 2010

Modlr plugins

We added a plugin interface for Gaffr and ECO. We want to enable you to extend Modlr so that you can implement a special file format import, or export, or what have you.

This is how it works:

Given these two interfaces…

   1: namespace Modlr.Plugins
   2: {
   3:     public interface IModlrPlugin
   4:     {
   5:         string GetName();
   6:         List<IModlrPluginMenuOperation> ProvideOperations();
   7:     }
   8:  
   9:     public interface IModlrPluginMenuOperation
  10:     {
  11:         string GetName();
  12:         void CheckEnableOrExecute(IEcoServiceProvider esp, bool execute, 
                        IEcoObject maincontext, IEcoObject subcontext,out bool enabled );
  13:     }
  14: }

…you can implement a plugin in an assembly like this:

   1: public class Class1 : IModlrPlugin
   2: {
   3:     #region IModlrPlugin Members
   4:  
   5:     public string GetName()
   6:     {
   7:         return "Demo plugin";
   8:     }
   9:  
  10:  
  11:     public List<IModlrPluginMenuOperation> ProvideOperations()
  12:     {
  13:         List<IModlrPluginMenuOperation> ops = new List<IModlrPluginMenuOperation>();
  14:         ops.Add(new OperationShowDialog());
  15:         ops.Add(new OperationActOnClass());
  16:  
  17:         return ops;
  18:     }   
  19:  
  20:     #endregion
  21: }
  22:  

The code above installs two Operations; OperationShowDialog and OperationActOnClass.

Looking closer on the operation ActOnClass:

   1: public class OperationActOnClass : IModlrPluginMenuOperation
   2: {
   3:     #region IModlrPluginMenuAction Members
   4:  
   5:     public void CheckEnableOrExecute(IEcoServiceProvider esp,
                      bool execute, Eco.ObjectRepresentation.IEcoObject maincontext,
                      Eco.ObjectRepresentation.IEcoObject subcontext, out bool enabled)
   6:     {
   7:         enabled = false;
   8:         if (maincontext != null && maincontext.AsIObject().AsObject is Eco.ModelLayer.Class)
   9:         {
  10:             enabled = true;
  11:             if (execute)
  12:             {
  13:                 (maincontext.AsIObject().AsObject as Eco.ModelLayer.Class).Name += "X";
  14:             }
  15:         }
  16:     }
  17:  
  18:     public string GetName()
  19:     {
  20:         return "Operation on class";
  21:     }
  22:  
  23:     #endregion
  24: }

The code checks that the context is a class, and if it is the code says that the operation should be enabled. If this was an “execute” call we perform the desired logic – in this case I just add an “X” to the back of the class name.

You can do a lot of stuff in a plugin. You get access to the IEcoServerProvider for the EcoModelLayer. The Modlr application is a standard ECO-built-application so you can use the IOCLService to get to any object, of any class that builds up your model content. You can access Diagrams, PlacedClasses, StateMachines etc. You can inspect and set TaggedValues, you can run thru every Attribute and check for spelling errors. You can create reports that you share with other ECO/Gaffr users…

So even if the interface is very small and simple, it is very powerful.

When you build your Plugin you will want to reference the following assemblies (found in the eco or gaffr install directory):

image

Compile your plugin and put the assembly in the folder %ProgramData%/CapableObjects/Plugins

Restart Gaffr.net or VisualStudio and you will find your plugin operations in the context menu:

image

Simple and powerful.

3 Comments:

Anonymous Alois said...

Customizing the Modlr... more productivity... sounds really cool!
Can we also get an interface for interacting with the model data (Diagrams, PlacedClasses, Statemachine...) at runtime? Thinking of a extended Autoform with statemachine animation.

Kind regards

February 19, 2010 at 5:42 AM  
Blogger Hans Karlsen said...

The diagrams are not transformed into the runtime model. But having the Modlr plugin you can access all the diagram information - and turn it into a format you can read at runtime.

February 20, 2010 at 10:10 AM  
Blogger Hans Karlsen said...

How to debug and troubleshoot plugin loading?
- Make sure your plugin outputpath is set to the same directory you get when pasting in this on the start menu: %ProgramData%/CapableObjects/Plugins
- In the plugin project set "Start external program" to Gaffr.exe, or Visual Studio for ECO.
- Check the VS output window for error info "Plugin loaded C:\ProgramData\CapableObjects\Plugins\ModlrPlugin.dll Demo plugin"

February 21, 2010 at 11:17 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 
Contact Us | Terms of Use | Privacy Statement © 2009 CapableObjects