Back to index

salome-kernel  6.5.0
__init__.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 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 #  File   : salome.py renamed as __init__.py for python packaging (gboulant)
00025 #  Author : Paul RASCLE, EDF
00026 #  Module : SALOME
00027 #
00028 """ 
00029 Module salome gives access to Salome ressources.
00030 
00031 variables:
00032 
00033   - salome.orb             : CORBA
00034   - salome.naming_service  : instance of naming Service class
00035       - methods:
00036           - Resolve(name)  : find a CORBA object (ior) by its pathname
00037           - Register(name) : register a CORBA object under a pathname
00038 
00039   - salome.lcc             : instance of lifeCycleCORBA class
00040       - methods:
00041           - FindOrLoadComponent(server,name) :
00042                            obtain an Engine (CORBA object)
00043                            or launch the Engine if not found,
00044                            with a Server name and an Engine name
00045 
00046   - salome.sg              : salome object to communicate with the graphical user interface (if any)
00047       - methods:
00048          - updateObjBrowser(bool):
00049          - getActiveStudyId():
00050          - getActiveStudyName():
00051 
00052          - SelectedCount():      returns number of selected objects
00053          - getSelected(i):       returns entry of selected object number i
00054          - getAllSelected():     returns list of entry of selected objects
00055          - AddIObject(Entry):    select an existing Interactive object
00056          - RemoveIObject(Entry): remove object from selection
00057          - ClearIObjects():      clear selection
00058 
00059          - Display(*Entry):
00060          - DisplayOnly(Entry):
00061          - Erase(Entry):
00062          - DisplayAll():
00063          - EraseAll():
00064 
00065          - IDToObject(Entry):    returns CORBA reference from entry
00066 
00067   - salome.myStudyName     : active Study Name
00068   - salome.myStudyId       : active Study Id
00069   - salome.myStudy         : the active Study itself (CORBA ior)
00070       - methods : defined in SALOMEDS.idl
00071 
00072 """
00073 ## @package salome
00074 # Module salome gives access to Salome ressources.
00075 #
00076 #  \param salome.orb             : CORBA orb object
00077 #  \param salome.naming_service  : instance of naming Service class (SALOME_NamingServicePy::SALOME_NamingServicePy_i)
00078 #  \param salome.lcc             : instance of lifeCycleCORBA class (SALOME_LifeCycleCORBA)
00079 #  \param salome.sg              : Salome object to communicate with the graphical user interface, if running (see interface in salome_iapp::SalomeOutsideGUI)
00080 #  \param salome.myStudyName     : active Study Name
00081 #  \param salome.myStudyId       : active Study Id
00082 #  \param salome.myStudy         : the active Study (interface SALOMEDS::Study)
00083 
00084 #
00085 # ==========================================================================
00086 #
00087 # The function extend_path is used here to aggregate in a single
00088 # virtual python package all the python sub-packages embedded in each
00089 # SALOME modules (python "namespace" pattern).
00090 #
00091 ROOT_PYTHONPACKAGE_NAME="salome"
00092 #
00093 # This root package name is expected to be found as a directory in
00094 # some paths of the sys.path variable, especially the paths
00095 # <MODULE_ROOT_DIR>/lib/pythonX.Y/site-packages/salome where are
00096 # installed the python files. These paths are theorically appended by
00097 # the SALOME main runner and should be in the sys.path at this point
00098 # of the application. The extend_path is looking then for directories
00099 # of the type:
00100 #
00101 # <MODULE_ROOT_DIR>/lib/pythonX.Y/site-packages/salome/<ROOT_PYTHONPACKAGE_NAME>
00102 #
00103 # And append them to the sys.path. These directories are supposed to
00104 # be the pieces to be aggregated as a single virtual python package.
00105 #
00106 import os, sys
00107 from salome_utils import verbose
00108 
00109 MATCH_ENDING_PATTERN="site-packages" + os.path.sep + "salome"
00110 
00111 def extend_path(pname):
00112     for dir in sys.path:
00113         if not isinstance(dir, basestring) or not os.path.isdir(dir) or not dir.endswith(MATCH_ENDING_PATTERN):
00114             continue
00115         subdir = os.path.join(dir, pname)
00116         # XXX This may still add duplicate entries to path on
00117         # case-insensitive filesystems
00118         if os.path.isdir(subdir) and subdir not in __path__:
00119             if verbose(): print "INFO - The directory %s is appended to sys.path" % subdir
00120             __path__.append(subdir)
00121 
00122 extend_path(ROOT_PYTHONPACKAGE_NAME)
00123 # ==========================================================================
00124 #
00125 
00126 from salome_kernel import *
00127 from salome_study import *
00128 from salome_iapp import *
00129 import salome_study
00130 
00131 #
00132 # The next block is workaround for the problem of shared symbols loading for the extension modules (e.g. SWIG-generated)
00133 # that causes RTTI unavailable in some cases. To solve this problem, sys.setdlopenflags() function is used.
00134 # Depending on the Python version and platform, the dlopen flags can be defined in the dl, DLFUN or ctypes module.
00135 # 
00136 import sys
00137 flags = None
00138 if not flags:
00139     try:
00140         # dl module can be unavailable
00141         import dl
00142         flags = dl.RTLD_NOW | dl.RTLD_GLOBAL
00143     except:
00144         pass
00145     pass
00146 if not flags:
00147     try:
00148         # DLFCN module can be unavailable
00149         import DLFCN
00150         flags = DLFCN.RTLD_NOW | DLFCN.RTLD_GLOBAL
00151     except:
00152         pass
00153     pass
00154 if not flags:
00155     try:
00156         # ctypes module can be unavailable
00157         import ctypes
00158         flags = ctypes.RTLD_GLOBAL
00159     except:
00160         pass
00161     pass
00162 
00163 # Disable -> bug with scipy, seems very dangerous to do that
00164 #if flags:
00165 #    sys.setdlopenflags(flags)
00166 #    pass
00167 
00168 orb, lcc, naming_service, cm,sg=None,None,None,None,None
00169 myStudyManager, myStudyId, myStudy, myStudyName=None,None,None,None
00170 
00171 def setCurrentStudy(theStudy):
00172     """
00173     Change current study : an existing one given by a study object.
00174 
00175     :param theStudy: the study CORBA object to set as current study
00176     """
00177     global myStudyId, myStudy, myStudyName
00178     myStudyId, myStudy, myStudyName =salome_study.setCurrentStudy(theStudy)
00179 
00180 def setCurrentStudyId(theStudyId=0):
00181     """
00182     Change current study : an existing or new one given by Id.
00183 
00184     :param theStudyId: the study Id (optional argument)
00185            0      : create a new study (default).
00186            n (>0) : try connection to study with Id = n, or create a new one
00187                       if study not found.
00188     """
00189     global myStudyId, myStudy, myStudyName
00190     myStudyId, myStudy, myStudyName =salome_study.setCurrentStudyId(theStudyId)
00191 
00192 salome_initial=1
00193 def salome_init(theStudyId=0,embedded=0):
00194     """
00195     Performs only once SALOME general purpose intialisation for scripts.
00196     optional argument : theStudyId
00197       When in embedded interpreter inside IAPP, theStudyId is not used
00198       When used without GUI (external interpreter)
00199         0      : create a new study (default).
00200         n (>0) : try connection to study with Id = n, or create a new one
00201                  if study not found.
00202                  If study creation, its Id may be different from theStudyId !
00203     Provides:
00204     orb             reference to CORBA
00205     lcc             a LifeCycleCorba instance
00206     naming_service  a naming service instance
00207     cm              reference to the container manager
00208     sg              access to SALOME GUI (when linked with IAPP GUI)
00209     myStudyManager  the study manager
00210     myStudyId       active study identifier
00211     myStudy         active study itself (CORBA reference)
00212     myStudyName     active study name
00213     """
00214     global salome_initial
00215     global orb, lcc, naming_service, cm
00216     global sg
00217     global myStudyManager, myStudyId, myStudy, myStudyName
00218 
00219     try:
00220         if salome_initial:
00221             salome_initial=0
00222             sg = salome_iapp_init(embedded)
00223             orb, lcc, naming_service, cm = salome_kernel_init()
00224             myStudyManager, myStudyId, myStudy, myStudyName =salome_study_init(theStudyId)
00225             pass
00226         pass
00227     except RuntimeError, inst:
00228         # wait a little to avoid trace mix
00229         import time
00230         time.sleep(0.2)
00231         x = inst
00232         print "salome.salome_init():", x
00233         print """
00234         ============================================
00235         May be there is no running SALOME session
00236         salome.salome_init() is intented to be used
00237         within an already running session
00238         ============================================
00239         """
00240         raise
00241 
00242 #to expose all objects to pydoc
00243 __all__=dir()