Salome HOME
Issue 0020165: implement sample of DumpPython functionality for Python module (PYHELLO)
authorvsr <vsr@opencascade.com>
Thu, 26 Feb 2009 15:58:25 +0000 (15:58 +0000)
committervsr <vsr@opencascade.com>
Thu, 26 Feb 2009 15:58:25 +0000 (15:58 +0000)
idl/PYHELLO_Gen.idl
src/PYHELLO/Makefile.am
src/PYHELLO/PYHELLO.py
src/PYHELLO/PYHELLO_utils.py [new file with mode: 0644]
src/PYHELLOGUI/PYHELLOGUI.py

index 87cbfcc57efa606b274bd0228771d3149e470e99..129d7f127d0e136d2f9c410d9c33d77f6d4a6469 100644 (file)
 //
 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+// ---
+// File   : PYHELLOGUI.py
+// Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
+// ---
+//
+
 #ifndef __PYHELLO_GEN__
 #define __PYHELLO_GEN__
 
@@ -30,8 +36,16 @@ module PYHELLO_ORB
 {
   interface PYHELLO_Gen : Engines::Component, SALOMEDS::Driver
   {
-     string makeBanner(in string name)
-         raises (SALOME::SALOME_Exception);
+    string makeBanner(in string name)
+      raises (SALOME::SALOME_Exception);
+    
+    void createObject(in SALOMEDS::Study theStudy,
+                     in string name)
+      raises (SALOME::SALOME_Exception);
+
+    TMPFile DumpPython(in Object theStudy,
+                       in boolean isPublished,
+                       out boolean isValidScript);
   };
 };
 
index bfde71ae74a00dbb5205d7991a97bda84012e4b9..93a89381e70d1d179d5d45ef7094e238324f06c2 100755 (executable)
 #  Author : , CEA
 #  Modified by : Alexander BORODIN (OCN) - autotools usage
 #
+
 include $(top_srcdir)/adm_local/unix/make_common_starter.am
 
 # Scripts to be installed
 dist_salomescript_SCRIPTS = \
-       PYHELLO.py
+       PYHELLO.py \
+       PYHELLO_utils.py
index 98108fe174061574a1b22badfd3a96180341ccbc..3135e2beb1e3c2b812d66f77adeae4b926834f4b 100644 (file)
 #
 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
+# ---
+# File   : PYHELLOGUI.py
+# Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
+# ---
+#
+
 import PYHELLO_ORB__POA
 import SALOME_ComponentPy
 import SALOME_DriverPy
 
+from PYHELLO_utils import *
+
 class PYHELLO(PYHELLO_ORB__POA.PYHELLO_Gen,
               SALOME_ComponentPy.SALOME_ComponentPy_i,
               SALOME_DriverPy.SALOME_DriverPy_i):
     """
-        Pour etre un composant SALOME cette classe Python
-        doit avoir le nom du composant et heriter de la
-        classe PYHELLO_Gen issue de la compilation de l'idl
-        par omniidl et de la classe SALOME_ComponentPy_i
-        qui porte les services generaux d'un composant SALOME
+    Construct an instance of PYHELLO module engine.
+    The class PYHELLO implements CORBA interface PYHELLO_Gen (see PYHELLO_Gen.idl).
+    It is inherited from the classes SALOME_ComponentPy_i (implementation of
+    Engines::Component CORBA interface - SALOME component) and SALOME_DriverPy_i
+    (implementation of SALOMEDS::Driver CORBA interface - SALOME module's engine).
     """
     def __init__ ( self, orb, poa, contID, containerName, instanceName, 
                    interfaceName ):
-        print "PYHELLO.__init__: ", containerName, ';', instanceName
         SALOME_ComponentPy.SALOME_ComponentPy_i.__init__(self, orb, poa,
                     contID, containerName, instanceName, interfaceName, 0)
         SALOME_DriverPy.SALOME_DriverPy_i.__init__(self, interfaceName)
-        # On stocke dans l'attribut _naming_service, une reference sur
-        # le Naming Service CORBA
+        #
         self._naming_service = SALOME_ComponentPy.SALOME_NamingServicePy_i( self._orb )
+        #
+        pass
 
+    """
+    Generate hello banner.
+    """
     def makeBanner( self, name ):
         banner = "Hello %s!" % name
         return banner
+
+    """
+    Create object.
+    """
+    def createObject( self, study, name ):
+        builder = study.NewBuilder()
+        father  = findOrCreateComponent( study )
+        object  = builder.NewObject( father )
+        attr    = builder.FindOrCreateAttribute( object, "AttributeName" )
+        attr.SetValue( name )
+        attr    = builder.FindOrCreateAttribute( object, "AttributeLocalID" )
+        attr.SetValue( objectID() )
+        pass
+
+    """
+    Dump module data to the Python script.
+    """
+    def DumpPython( self, study, isPublished ):
+        abuffer = []
+        abuffer.append( "def RebuildData( theStudy ):" )
+        names = []
+        father = study.FindComponent( moduleName() )
+        if father:
+            iter = study.NewChildIterator( father )
+            while iter.More():
+                name = iter.Value().GetName()
+                if name: names.append( name )
+                iter.Next()
+                pass
+            pass
+        if names:
+            abuffer += [ "  from batchmode_salome import lcc" ]
+            abuffer += [ "  import PYHELLO_ORB" ]
+            abuffer += [ "  " ]
+            abuffer += [ "  pyhello = lcc.FindOrLoadComponent( 'FactoryServerPy', '%s' )" % moduleName() ]
+            abuffer += [ "  " ]
+            abuffer += [ "  pyhello.createObject( theStudy, '%s' )" % name for name in names ]
+            pass
+        abuffer += [ "  " ]
+        abuffer.append( "  pass" )
+        abuffer.append( "\0" )
+        return ("\n".join( abuffer ), 1)
diff --git a/src/PYHELLO/PYHELLO_utils.py b/src/PYHELLO/PYHELLO_utils.py
new file mode 100644 (file)
index 0000000..03186fc
--- /dev/null
@@ -0,0 +1,190 @@
+#  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+#
+#  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+#  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+#
+#  This library is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU Lesser General Public
+#  License as published by the Free Software Foundation; either
+#  version 2.1 of the License.
+#
+#  This library is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#  Lesser General Public License for more details.
+#
+#  You should have received a copy of the GNU Lesser General Public
+#  License along with this library; if not, write to the Free Software
+#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+#  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+# ---
+# File   : PYHELLO_utils.py
+# Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
+# ---
+#
+
+__all__ = [
+    "moduleID",
+    "objectID",
+    "unknownID",
+    "moduleName",
+    "modulePixmap",
+    "verbose",
+    "getORB",
+    "getNS",
+    "getLCC",
+    "getStudyManager",
+    "getEngine",
+    "findOrCreateComponent",
+    "getObjectID",
+    ]
+
+from omniORB import CORBA
+from SALOME_NamingServicePy import SALOME_NamingServicePy_i
+from LifeCycleCORBA import LifeCycleCORBA
+import SALOMEDS
+import SALOMEDS_Attributes_idl
+import PYHELLO_ORB
+
+###
+# Get PYHELLO module's ID
+###
+def moduleID():
+    MODULE_ID = 1000
+    return MODULE_ID
+
+###
+# Get PYHELLO object's ID
+###
+def objectID():
+    OBJECT_ID = 1010
+    return OBJECT_ID
+
+###
+# Get unknown ID
+###
+def unknownID():
+    FOREIGN_ID = -1
+    return FOREIGN_ID
+
+###
+# Get PYHELLO module's name
+###
+def moduleName():
+    return "PYHELLO"
+
+###
+# Get module's pixmap name
+###
+def modulePixmap():
+    return "PYHELLO_small.png"
+
+###
+# Get verbose level
+### 
+__verbose__ = None
+def verbose():
+    global __verbose__
+    if __verbose__ is None:
+        try:
+            __verbose__ = int( os.getenv( 'SALOME_VERBOSE', 0 ) )
+        except:
+            __verbose__ = 0
+            pass
+        pass
+    return __verbose__
+
+###
+# Get ORB reference
+###
+__orb__ = None
+def getORB():
+    global __orb__
+    if __orb__ is None:
+        __orb__ = CORBA.ORB_init( [''], CORBA.ORB_ID )
+        pass
+    return __orb__
+
+###
+# Get naming service instance
+###
+__naming_service__ = None
+def getNS():
+    global __naming_service__
+    if __naming_service__ is None:
+        __naming_service__ = SALOME_NamingServicePy_i( getORB() )
+        pass
+    return __naming_service__
+
+##
+# Get life cycle CORBA instance
+##
+__lcc__ = None
+def getLCC():
+    global __lcc__
+    if __lcc__ is None:
+        __lcc__ = LifeCycleCORBA( getORB() )
+        pass
+    return __lcc__
+
+##
+# Get study manager
+###
+__study_manager__ = None
+def getStudyManager():
+    global __study_manager__
+    if __study_manager__ is None:
+        obj = getNS().Resolve( '/myStudyManager' )
+        __study_manager__ = obj._narrow( SALOMEDS.StudyManager )
+        pass
+    return __study_manager__
+
+###
+# Get PYHELLO engine
+###
+__engine__ = None
+def getEngine():
+    global __engine__
+    if __engine__ is None:
+        __engine__ = getLCC().FindOrLoadComponent( "FactoryServerPy", moduleName() )
+        pass
+    return __engine__
+
+###
+# Find or create PYHELLO component object in a study
+###
+def findOrCreateComponent( study ):
+    father = study.FindComponent( moduleName() )
+    if father is None:
+        builder = study.NewBuilder()
+        father = builder.NewComponent( moduleName() )
+        attr = builder.FindOrCreateAttribute( father, "AttributeName" )
+        attr.SetValue( moduleName() )
+        attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
+        attr.SetPixMap( modulePixmap() )
+        attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
+        attr.SetValue( moduleID() )
+        try:
+            builder.DefineComponentInstance( father, getEngine() )
+            pass
+        except:
+            pass
+        pass
+    return father
+
+###
+# Get object's ID
+###
+def getObjectID( study, entry ):
+    ID = unknownID()
+    if study and entry:
+        sobj = study.FindObjectID( entry )
+        if sobj is not None:
+            test, anAttr = sobj.FindAttribute( "AttributeLocalID" )
+            if test: ID = anAttr._narrow( SALOMEDS.AttributeLocalID ).Value()
+            pass
+        pass
+    return ID
+    
index 297eb4b317129a92d2a49e56a7a5040254bb86a7..86a86dee78babbd352cb67a355af18cd0427aac1 100644 (file)
 # Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
 # ---
 #
+
 import traceback
 import os
 from PyQt4.QtGui import *
 from PyQt4.QtCore import *
 
-from omniORB import CORBA
-from SALOME_NamingServicePy import *
-from LifeCycleCORBA import *
-import SALOMEDS
-import SALOMEDS_Attributes_idl
-
-import PYHELLO_ORB
+from PYHELLO_utils import *
 
 ################################################
 # GUI context class
@@ -43,14 +38,6 @@ import PYHELLO_ORB
 ################################################
 
 class GUIcontext:
-    # module name
-    MODULE_NAME      = "PYHELLO"
-    # module icon
-    MODULE_PIXMAP    = "PYHELLO_small.png"
-    # data objects IDs
-    MODULE_ID        = 1000
-    OBJECT_ID        = 1010
-    FOREIGN_ID       = -1
     # menus/toolbars/actions IDs
     PYHELLO_MENU_ID  = 90
     HELLO_ID         = 941
@@ -133,45 +120,10 @@ sg = libSALOME_Swig.SALOMEGUI_Swig()
 
 ################################################
 
-# init ORB
-orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
-
-# create naming service instance
-naming_service = SALOME_NamingServicePy_i( orb )
-
-# create life cycle CORBA instance
-lcc = LifeCycleCORBA( orb )
-
-# get study manager
-obj = naming_service.Resolve( '/myStudyManager' )
-studyManager = obj._narrow( SALOMEDS.StudyManager )
-
 ################################################
 # Internal methods
 ################################################
 
-###
-# Check verbose mode
-### 
-__verbose__ = None
-def verbose():
-    global __verbose__
-    if __verbose__ is None:
-        try:
-            __verbose__ = int( os.getenv('SALOME_VERBOSE', 0) )
-        except:
-            __verbose__ = 0
-            pass
-        pass
-    return __verbose__
-
-###
-# get PYHELLO engine
-###
-def _getEngine():
-    engine = lcc.FindOrLoadComponent( "FactoryServerPy", GUIcontext.MODULE_NAME )
-    return engine
-
 ###
 # get active study ID
 ###
@@ -183,7 +135,7 @@ def _getStudyId():
 ###
 def _getStudy():
     studyId = _getStudyId()
-    study = studyManager.GetStudyByID( studyId )
+    study = getStudyManager().GetStudyByID( studyId )
     return study
 
 ###
@@ -202,29 +154,6 @@ def _hasChildren( sobj ):
         pass
     return False
 
-###
-# finds or creates component object
-###
-def _findOrCreateComponent():
-    study = _getStudy()
-    father = study.FindComponent( GUIcontext.MODULE_NAME )
-    if father is None:
-        builder = study.NewBuilder()
-        father = builder.NewComponent( GUIcontext.MODULE_NAME )
-        attr = builder.FindOrCreateAttribute( father, "AttributeName" )
-        attr.SetValue( GUIcontext.MODULE_NAME )
-        attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
-        attr.SetPixMap( GUIcontext.MODULE_PIXMAP )
-        attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
-        attr.SetValue( GUIcontext.MODULE_ID )
-        try:
-            builder.DefineComponentInstance( father, _getEngine() )
-            pass
-        except:
-            pass
-        pass
-    return father
-
 ###
 # get current GUI context
 ###
@@ -258,22 +187,8 @@ def _incObjToMap( m, id ):
 def _getSelection():
     selcount = sg.SelectedCount()
     seltypes = {}
-    study = _getStudy()
     for i in range( selcount ):
-        entry = sg.getSelected( i )
-        if entry:
-            sobj = study.FindObjectID( entry )
-            if sobj is not None:
-                test, anAttr = sobj.FindAttribute( "AttributeLocalID" )
-                if test:
-                    ID = anAttr._narrow( SALOMEDS.AttributeLocalID ).Value()
-                    if ID >= 0:
-                        _incObjToMap( seltypes, ID )
-                        continue
-                    pass
-                pass
-            pass
-        _incObjToMap( seltypes, GUIcontext.FOREIGN_ID )
+        _incObjToMap( seltypes, getObjectID( _getStudy(), sg.getSelected( i ) ) )
         pass
     return selcount, seltypes
 
@@ -356,10 +271,10 @@ def createPopupMenu( popup, context ):
     print selcount, selected
     if selcount == 1:
         # one object is selected
-        if GUIcontext.MODULE_ID in selected:
+        if moduleID() in selected:
             # menu for component
             popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
-        elif GUIcontext.OBJECT_ID in selected:
+        elif objectID() in selected:
             # menu for object
             popup.addAction( sgPyQt.action( GUIcontext.SHOW_ME_ID ) )
             popup.addAction( sgPyQt.action( GUIcontext.RENAME_ME_ID ) )
@@ -370,10 +285,10 @@ def createPopupMenu( popup, context ):
     elif selcount > 1:
         # several objects are selected
         if len( selected ) == 1:
-            if GUIcontext.MODULE_ID in selected:
+            if moduleID() in selected:
                 # menu for component
                 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ALL_ID ) )
-            elif GUIcontext.OBJECT_ID in selected:
+            elif objectID() in selected:
                 # menu for list of objects
                 popup.addAction( sgPyQt.action( GUIcontext.DELETE_ME_ID ) )
                 pass
@@ -461,7 +376,7 @@ class MyDialog( QDialog ):
     def accept( self ):
         name = str( self.entry.text() )
         if name != "":
-            banner = _getEngine().makeBanner( name )
+            banner = getEngine().makeBanner( name )
             QMessageBox.information( self, 'Info', banner )
             self.close()
         else:
@@ -509,14 +424,7 @@ def CreateObject():
         name = "%s %d" % ( default_name, __id__ )
         pass
     if not name: return
-    study   = _getStudy()
-    builder = study.NewBuilder()
-    father  = _findOrCreateComponent()
-    object  = builder.NewObject( father )
-    attr    = builder.FindOrCreateAttribute( object, "AttributeName" )
-    attr.SetValue( name )
-    attr    = builder.FindOrCreateAttribute( object, "AttributeLocalID" )
-    attr.SetValue( GUIcontext.OBJECT_ID )
+    getEngine().createObject( _getStudy(), name )
     sg.updateObjBrowser( True )
     pass
 
@@ -525,7 +433,7 @@ def CreateObject():
 ###
 def DeleteAll():
     study = _getStudy()
-    father = study.FindComponent( GUIcontext.MODULE_NAME )
+    father = study.FindComponent( moduleName() )
     if father:
         iter = study.NewChildIterator( father )
         builder = study.NewBuilder()