Back to index

salome-kernel  6.5.0
services.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 
00021 # Author: Guillaume Boulant (EDF/R&D)
00022 
00023 #
00024 # WARNING: development notes
00025 #
00026 # This python module is aimed to be a (yet another?) end user
00027 # interface to manipulate the KERNEL SALOME services. Note that it
00028 # does not replace the salome module (i.e. what you get when typing
00029 # "import salome", actually implemented in the file __init__.py of the
00030 # salome python package). It provides instead functions that help to
00031 # use the salome module in a end user context (development of domain
00032 # specific tools by programmers that are not ten years experienced in
00033 # SALOME development).
00034 
00035 import salome
00036 from deprecation import is_called_by_sphinx
00037 if not is_called_by_sphinx() and salome.lcc is None:
00038     try:
00039         salome.salome_init()
00040     except Exception, e:
00041         print e
00042 
00043 # Note that the salome module provides you with standard SALOME
00044 # objects: CORBA broker (orb): salome.orb lyfe cycle (lcc) :
00045 # salome.lcc naming service : salome.naming_service study manager :
00046 # salome.myStudyManager The default study : salome.myStudy
00047 #
00048 # Alternatively, you may obtain these objects directly with the
00049 # following instructions:
00050 #
00051 #   from omniORB import CORBA
00052 #   from LifeCycleCORBA import LifeCycleCORBA
00053 #   orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
00054 #   lcc = LifeCycleCORBA( orb )
00055 # 
00056 #   from SALOME_NamingServicePy import SALOME_NamingServicePy_i
00057 #   naming_service = SALOME_NamingServicePy_i( orb )
00058 #
00059 # (See salome.py page in the KERNEL documentation)
00060 
00061 #
00062 # ==============================================================================
00063 # Helper functions for SALOME components
00064 # ==============================================================================
00065 #
00066 
00067 #
00068 # componentName is the name of the component as declared in the XML
00069 # SALOME catalog. A loadable library with name lib<componentName>Engine.so
00070 # is supposed to be reachable. This library is supposed to provide a
00071 # factory function with the prototype:
00072 #
00073 #   PortableServer::ObjectId * <componentName>Engine_factory(
00074 #                                             CORBA::ORB_ptr orb,
00075 #                                            PortableServer::POA_ptr poa,
00076 #                                        PortableServer::ObjectId * contId,
00077 #                                        const char *instanceName,
00078 #                                        const char *interfaceName);
00079 #
00080 # corbaModule is the name of the IDL module that contains the
00081 # definition of the interface of the component. This name corresponds
00082 # to the namespace of the servant classes.
00083 #
00084 # containerType specified the container in which the servants are
00085 # executed.
00086 #
00087 def getComponent(componentName = "SalomeTestComponent",
00088                  corbaModule   = "Engines",
00089                  containerType = "FactoryServer"):
00090     """
00091     Get a SALOME CORBA component from its name
00092     """
00093     print "INF: getting component %s from CORBA module %s ..."%(componentName,corbaModule)
00094     __import__(corbaModule)
00095     component=salome.lcc.FindOrLoadComponent(containerType,componentName)
00096     if component is None:
00097         print "ERR: the SALOME component "+componentName+" can't be reached"
00098     print "INF: component %s obtained from CORBA module %s"%(componentName,corbaModule)
00099     return component
00100 
00101 # Note that an alternative (and maybe better) method to get a component
00102 # is to use the module catalog. Here, we just use the catalog to get
00103 # the list of components defined in the current session.
00104 import SALOME_ModuleCatalog
00105 def getComponentList():
00106     """
00107     Get the list of names of all SALOME componenents register in
00108     the catalog.
00109     """
00110     obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
00111     catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
00112     if not catalog:
00113         raise RuntimeError, "Can't accesss module catalog"
00114     return catalog.GetComponentList()
00115 
00116 def getStudyManager():
00117     """Get a study manager to create and manage SALOME studies"""
00118     return salome.myStudyManager
00119 
00120 import SALOMEDS
00121 def __getStudyManager_demo():
00122     """
00123     Get a study manager to create and manage SALOME studies. WARN: you
00124     should use instead the variable salome.myStudyManager. This
00125     function is given for illustration of usage of the naming service
00126     """
00127     naming_service = SALOME_NamingServicePy_i( orb )
00128     obj = naming_service.Resolve( '/myStudyManager' )
00129     studyManager = obj._narrow( SALOMEDS.StudyManager)
00130     return studyManager
00131 
00132 
00133 #
00134 # ==============================================================================
00135 # Helper functions for manipulating objects, sobjects and entry
00136 # ==============================================================================
00137 #
00138 
00139 # - the SObject is an item in a study (Study Object).
00140 # - the entry is the identifier of an item.
00141 # - the ID is the entry
00142 # - the object (geom object or smesh object) is a CORBA servant
00143 #   embedded in the SALOME component container and with a reference in
00144 #   the SALOME study, so that it can be retrieved.
00145 
00146 # __GBO__ WARN: theses functions are already defined in
00147 # salome_study.py, but without the possibility to specify the
00148 # underlying study (in salome_study.py, the study is the default study
00149 # binded to the salome.myStudy attribute). TODO: see if it can be
00150 # extends to the prototype (with the study as an argument) below and
00151 # resorb the functions below.
00152 
00153 def IDToObject(id, study=None):
00154     myObj = None
00155     if study is None:
00156         myStudy = salome.myStudy
00157     else:
00158         myStudy = study
00159     mySO = myStudy.FindObjectID(id);
00160     if mySO is not None:
00161         ok, anAttr = mySO.FindAttribute("AttributeIOR")
00162         if ok:
00163             AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
00164             if AtIOR.Value() != "":
00165                 myObj = salome.orb.string_to_object(AtIOR.Value())
00166     return myObj
00167 
00168 def ObjectToSObject(obj, study=None):
00169     mySO = None
00170 
00171     if study is None:
00172         myStudy = salome.myStudy
00173     else:
00174         myStudy = study
00175 
00176     if obj is not None:
00177         ior =  salome.orb.object_to_string(obj)
00178         if ior != "":
00179             mySO = myStudy.FindObjectIOR(ior)
00180     return mySO
00181 
00182 def ObjectToID(obj, study=None):
00183     mySO = ObjectToSObject(obj,study)
00184     if mySO:
00185         return mySO.GetID()
00186     return ""
00187 
00188 def IDToSObject(id, study=None):
00189     if study is None:
00190         myStudy = salome.myStudy
00191     else:
00192         myStudy = study
00193 
00194     mySO = myStudy.FindObjectID(id);
00195     return mySO
00196 
00197 def SObjectToID(sobject):
00198     if sobject is None: return None
00199     return sobject.GetID()
00200 
00201 
00202 #
00203 # ==============================================================================
00204 # Basic use cases and unit tests
00205 # ==============================================================================
00206 #
00207 
00208 def TEST_getComponent():
00209     component=getComponent(componentName = "SalomeTestComponent")
00210     
00211     ref_string = 'TestComponent_i : L = 3'
00212     res_string = component.Coucou(3)
00213     if ref_string != ref_string:
00214         return False
00215     return True
00216 
00217 def TEST_getComponentList():
00218     componentList=getComponentList()
00219     if 'SalomeTestComponent' not in componentList:
00220         return False
00221     return True
00222 
00223 def TEST_createObject():
00224     """
00225     WARNING: for this test, we need GEOM (used to create an object 
00226     """
00227     import geompy
00228     geompy.init_geom(salome.myStudy)
00229     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
00230     id = geompy.addToStudy( box, 'box' )
00231     return id
00232 
00233 def TEST_objectsManipulation():
00234     myEntry = TEST_createObject()
00235 
00236     mySObject = IDToSObject(myEntry)
00237     entry     = SObjectToID(mySObject)
00238 
00239     if str(entry) != str(myEntry):
00240         return False
00241 
00242 
00243     myObject = IDToObject(myEntry)
00244     print myObject
00245     if myObject is None:
00246         return False
00247 
00248     return True
00249 
00250 if __name__ == "__main__":
00251     import unittester
00252     unittester.run("services","TEST_getComponent")
00253     unittester.run("services","TEST_getComponentList")
00254     unittester.run("services","TEST_objectsManipulation")