]> SALOME platform Git repositories - modules/geom.git/commitdiff
Salome HOME
CCAR: add plugins to GEOM module
authorcaremoli <caremoli>
Mon, 29 Mar 2010 07:21:31 +0000 (07:21 +0000)
committercaremoli <caremoli>
Mon, 29 Mar 2010 07:21:31 +0000 (07:21 +0000)
1- GUI plugin manager located in GEOMGUI (see beginning of geom_pluginsmanager.py for explanantions)
2- commands plugin manager located in GEOM_SWIG (see end of geompyDC.py for explanations)
3- 2 new methods for GEOM_Object (GetDescription, SetDescription) to manage Python dump for
macro commands defined in Python

idl/GEOM_Gen.idl
src/GEOM/GEOM_Object.cxx
src/GEOM/GEOM_Object.hxx
src/GEOMGUI/GeometryGUI.cxx
src/GEOMGUI/Makefile.am
src/GEOMGUI/geom_pluginsmanager.py [new file with mode: 0644]
src/GEOM_I/GEOM_Object_i.cc
src/GEOM_I/GEOM_Object_i.hh
src/GEOM_SWIG/Makefile.am
src/GEOM_SWIG/geom_plugins.py [new file with mode: 0644]
src/GEOM_SWIG/geompyDC.py

index a93cdb751991f792e7b4086dc11e81a78686dd82..6ae925f61b3db27ba93c5d2c45e79d9238462e11 100644 (file)
@@ -304,6 +304,15 @@ module GEOM
      *  Return list of notebook variables used for object creation separated by ":" symbol
      */
     string GetParameters();
+
+    /*!
+     *  Return description associated to the object
+     */
+    string GetDescription();
+    /*!
+     *  Set the description associated to the object
+     */
+    void SetDescription (in string theDescription);
   };
 
 
index fc2170a8612e7ca8c488741c3a90da6023f3ada3..965081b291dbd3b491a0434ad9e44468911c53c6 100644 (file)
@@ -697,3 +697,29 @@ const Handle(GEOM_Object) Handle(GEOM_Object)::DownCast(const Handle(Standard_Tr
 }
 
 
+//=============================================================================
+/*!
+ *  SetDescription
+ */
+//=============================================================================
+void GEOM_Object::SetDescription(const TCollection_AsciiString& theDescription)
+{
+  Handle(GEOM_Function) aFunction = GetFunction(1);
+  if(aFunction.IsNull() ) return;
+  aFunction->SetDescription(theDescription);
+}
+
+//=============================================================================
+/*!
+ *  GetDescription
+ */
+//=============================================================================
+TCollection_AsciiString GEOM_Object::GetDescription()
+{
+  Standard_Integer nb = GetNbFunctions();
+  Handle(GEOM_Function) aFunction = GetFunction(1);
+  if(aFunction.IsNull() ) return "";
+  TCollection_AsciiString aDescr = aFunction->GetDescription();
+  return aDescr;
+}
+
index c79f827e1de78bc5459d5da8e37182acd793ebfd..0fb442a775b85c95b7c15163aa87b1f13d02c989 100644 (file)
@@ -248,6 +248,12 @@ class GEOM_Object : public MMgt_TShared
   //Get a notebook variables used for object creation
   Standard_EXPORT TCollection_AsciiString GetParameters() const;
 
+  //Set an object associated description
+  Standard_EXPORT void SetDescription(const TCollection_AsciiString& theDescription);
+
+  //Get an object associated description
+  Standard_EXPORT TCollection_AsciiString GetDescription();
+
   //###########################################################
   // Sub shape methods
   //###########################################################
index f15fec4dbf77aea371720cc364ac0e4dc3920bb7..e9014be605643179ecb7d6376049bb35601ea1dc 100644 (file)
@@ -23,6 +23,7 @@
 // File   : GeometryGUI.cxx
 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
 //
+#include "Python.h"
 #include "GeometryGUI.h"
 #include "GeometryGUI_Operations.h"
 #include "GEOMGUI_OCCSelector.h"
@@ -1098,6 +1099,21 @@ bool GeometryGUI::activateModule( SUIT_Study* study )
   setMenuShown( true );
   setToolShown( true );
 
+  // import Python module that manages GEOM plugins (need to be here because SalomePyQt API uses active module)
+  PyGILState_STATE gstate = PyGILState_Ensure();
+  PyObject* pluginsmanager=PyImport_ImportModule((char*)"geom_pluginsmanager");
+  if(pluginsmanager==NULL)
+    PyErr_Print();
+  else
+    {
+      PyObject* result=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"");
+      if(result==NULL)
+        PyErr_Print();
+      Py_XDECREF(result);
+    }
+  PyGILState_Release(gstate);
+  // end of GEOM plugins loading
+
   connect( application()->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
           this, SLOT( onWindowActivated( SUIT_ViewWindow* ) ) );
 
index 331483c78ae99c9fa56cc590281614d06e315b41..3ec76948c1b0f74f29292eb49be90f94ad946fc3 100644 (file)
@@ -29,6 +29,8 @@ include $(top_srcdir)/adm_local/unix/make_common_starter.am
 # Libraries targets
 lib_LTLIBRARIES = libGEOM.la
 
+salomepython_PYTHON = geom_pluginsmanager.py
+
 # header files 
 salomeinclude_HEADERS =        \
        GeometryGUI.h           \
diff --git a/src/GEOMGUI/geom_pluginsmanager.py b/src/GEOMGUI/geom_pluginsmanager.py
new file mode 100644 (file)
index 0000000..714dae3
--- /dev/null
@@ -0,0 +1,169 @@
+"""
+This module is imported from C++ (GeometryGUI.cxx)
+and initialized (call to initialize function)
+
+A plugins manager is created at import (singleton object).
+
+The plugins manager creates a submenu Plugins in the "New Entity" menu.
+
+The plugins manager searches all files named geom_plugins.py in special directories and executes them.
+
+The directories are :
+- $HOME/.salome/Plugins
+- $HOME/$APPLI/Plugins
+- the directory where this file is installed
+- a list of directories given by SALOME_PLUGINS_PATH environment variable (paths separated by ":")
+
+These files should contain python code that register functions into the plugins manager.
+
+geom_plugins.py example::
+
+  import geom_pluginsmanager
+
+  def about(context):
+    from PyQt4.QtGui import QMessageBox
+    QMessageBox.about(None, "About GEOM pluginmanager", "GEOM plugins manager in GEOM module ")
+
+  geom_pluginsmanager.AddFunction('AboutGEOM','About GEOM plugins manager',about)
+
+First import the python module geom_pluginsmanager
+Second write a function with one argument context (it's an object with 3 attributes)
+Third register the function with a call to AddFunction (entry in menu plugins, tooltip, function)
+
+context attributes:
+- sg : the SALOME Swig interface
+- studyId : the SALOME studyId that must be used to execute the plugin
+- study : the SALOME study object that must be used to execute the plugin
+
+"""
+
+import os,sys,traceback
+from PyQt4 import QtGui
+from PyQt4 import QtCore
+
+import salome
+
+SEP=":"
+
+# Get SALOME PyQt interface
+import SalomePyQt
+sgPyQt = SalomePyQt.SalomePyQt()
+
+# Get SALOME Swig interface
+import libSALOME_Swig
+sg = libSALOME_Swig.SALOMEGUI_Swig()
+
+def initialize():
+  plugins.activate()
+
+class Context:
+    def __init__(self,sg):
+        self.sg=sg
+        self.studyId=salome.sg.getActiveStudyId()
+        self.study= salome.myStudyManager.GetStudyByID(self.studyId)
+
+class Plugins:
+    def __init__(self):
+        self.registry={}
+        self.handlers={}
+        self.lasttime=0
+        self.plugindirs=[]
+        self.plugins_files=[]
+
+        # USER plugins directory
+        user_dir = os.path.expanduser("~/.salome/Plugins")
+        self.plugindirs.append(user_dir)
+
+        # APPLI plugins directory
+        appli=os.getenv("APPLI")
+        if appli:
+          appli_dir=os.path.join(os.path.expanduser("~"),appli,"Plugins")
+          self.plugindirs.append(appli_dir)
+
+        #SALOME_PLUGINS_PATH environment variable (list of directories separated by ":")
+        pluginspath=os.getenv("SALOME_PLUGINS_PATH")
+        if pluginspath:
+          for directory in pluginspath.split(SEP):
+            self.plugindirs.append(directory)
+
+        #Add also the directory GEOMGUI
+        self.plugindirs.append(os.path.dirname(__file__))
+
+        a=QtGui.QAction("Plugins",sgPyQt.getPopupMenu("New Entity"))
+        self.menu=QtGui.QMenu("Plugins",sgPyQt.getPopupMenu("New Entity"))
+        a.setMenu(self.menu)
+        mid=sgPyQt.createMenu(a,"New Entity")
+        self.menu.connect(self.menu,QtCore.SIGNAL("aboutToShow()"),self.importPlugins)
+
+    def AddFunction(self,name,description,script,script_type=None):
+        """ Add a plugin function
+        """
+        self.registry[name]=script,description,script_type
+
+        def handler(obj=self,script=script):
+          try:
+            script(Context(sgPyQt))
+          except:
+            s=traceback.format_exc()
+            QtGui.QMessageBox.warning(None,"Exception occured",s)
+
+        self.handlers[name]=handler
+
+    def importPlugins(self):
+        """Import the geom_plugins module that contains plugins definition """
+        if self.lasttime ==0:
+          studyId=sg.getActiveStudyId()
+          salome.salome_init(embedded=1)
+
+        lasttime=0
+
+        plugins_files=[]
+        for directory in self.plugindirs:
+          geom_plugins_file = os.path.join(directory,"geom_plugins.py")
+          if os.path.isfile(geom_plugins_file):
+            plugins_files.append((directory,geom_plugins_file))
+            lasttime=max(lasttime,os.path.getmtime(geom_plugins_file))
+
+        plugins_files.sort()
+
+        if not plugins_files:
+          self.registry.clear()
+          self.handlers.clear()
+          self.lasttime=0
+          self.menu.clear()
+          return
+
+        if self.plugins_files != plugins_files or lasttime > self.lasttime:
+          self.registry.clear()
+          self.handlers.clear()
+          self.lasttime=lasttime
+          for directory,salome_plugins_file in plugins_files:
+            if directory not in sys.path:
+              sys.path.insert(0,directory)
+            try:
+              execfile(salome_plugins_file,globals(),{})
+            except:
+              print "Error while loading plugins from file:",salome_plugins_file
+              traceback.print_exc()
+
+          self.updateMenu()
+
+    def updateMenu(self):
+        """Update the Plugins menu"""
+        self.menu.clear()
+        for name,handler in self.handlers.items():
+          act=self.menu.addAction(name,handler)
+
+    def activate(self):
+        """Add the Plugins menu to the specified menu"""
+
+    def deactivate(self):
+        """Remove the Plugins menu from Tools menu (not implemented)"""
+
+plugins=Plugins()
+
+def AddFunction(name,description,script,script_type=None):
+   """ Add a plugin function
+       Called by a user to register a function (script)
+   """
+   return plugins.AddFunction(name,description,script,script_type)
index f164b4ef4bb9dc58916730914d297300ac57a73e..c1ba2a1726d4892b0f78666829268e1828f29fab 100644 (file)
@@ -437,3 +437,12 @@ char* GEOM_Object_i::GetParameters()
   return CORBA::string_dup(_impl->GetParameters().ToCString());
 }
 
+void GEOM_Object_i::SetDescription(const char* theDescription)
+{
+  _impl->SetDescription((char*)theDescription);
+}
+
+char* GEOM_Object_i::GetDescription()
+{
+  return CORBA::string_dup(_impl->GetDescription().ToCString());
+}
index b6f16c3546410cac0d2e1917385926f8ec6e1e93..7ab1f9114a5d238a8f38447f6f14067f1b5e267a 100644 (file)
@@ -98,6 +98,10 @@ class GEOM_I_EXPORT GEOM_Object_i : public virtual POA_GEOM::GEOM_Object, public
 
   Handle(GEOM_Object) GetImpl() { return _impl; }
 
+  virtual char* GetDescription();
+
+  virtual void SetDescription(const char* theDescription);
+
  private:
 
   GEOM::GEOM_Gen_var _engine;
index 11862eb9a71248d12f05113d90d0c6a5e03ed8b9..6cfc0eb6495530f447c642c84f7bd6d56ad527b1 100644 (file)
@@ -35,6 +35,8 @@ include $(top_srcdir)/adm_local/unix/make_common_starter.am
 # ===============================================================
 #
 
+salomepython_PYTHON=geom_plugins.py
+
 # Scripts to be installed.
 dist_salomescript_DATA =               \
        geompy.py                       \
diff --git a/src/GEOM_SWIG/geom_plugins.py b/src/GEOM_SWIG/geom_plugins.py
new file mode 100644 (file)
index 0000000..33a40e8
--- /dev/null
@@ -0,0 +1,9 @@
+
+import geom_pluginsmanager
+
+def about(context):
+    from PyQt4.QtGui import QMessageBox
+    QMessageBox.about(None, "About GEOM pluginmanager", """GEOM plugins manager in GEOM module """)
+
+geom_pluginsmanager.AddFunction('AboutGEOM','About GEOM plugins manager',about)
+
index 88c5cc42fd4999a4a1f78730534e6731254c1676..1e228327db22e3a3ff28e1d57d37b64f27c290b3 100644 (file)
@@ -76,6 +76,7 @@
 
 ## @}
 
+import os
 import salome
 salome.salome_init()
 from salome import *
@@ -4186,6 +4187,42 @@ class geompyDC(GEOM._objref_GEOM_Gen):
             RaiseIfFailed("AddTexture", self.InsertOp)
             return ID
 
+
+#Try to load commands that are found in command plugins
+# command plugins are python files named geom_commands.py that are found in special directories.
+# These directories are :
+# $HOME/.salome/Plugins
+# $HOME/$APPLI/Plugins
+# The directory where this file is installed
+# a list of directories given by SALOME_PLUGINS_PATH environment variable (paths separated by ":")
+SEP=":"
+plugindirs=[]
+# USER plugins directory
+plugindirs.append(os.path.expanduser("~/.salome/Plugins"))
+# APPLI plugins directory
+appli=os.getenv("APPLI")
+if appli:
+  plugindirs.append(os.path.join(os.path.expanduser("~"),appli,"Plugins"))
+#This file directory
+plugindirs.append(os.path.dirname(__file__))
+#SALOME_PLUGINS_PATH environment variable (list of directories separated by ":")
+pluginspath=os.getenv("SALOME_PLUGINS_PATH")
+if pluginspath:
+  for directory in pluginspath.split(SEP):
+    plugindirs.append(directory)
+
+for directory in plugindirs:
+  commands_file = os.path.join(directory,"geom_commands.py")
+  if os.path.isfile(commands_file):
+    if directory not in sys.path:
+      sys.path.insert(0,directory)
+    try:
+      execfile(commands_file)
+    except:
+      print "Error while loading plugins from file:",commands_file
+      import traceback
+      traceback.print_exc()
+
 import omniORB
 #Register the new proxy for GEOM_Gen
 omniORB.registerObjref(GEOM._objref_GEOM_Gen._NP_RepositoryId, geompyDC)