Salome HOME
Fix sphinx doc generation in GEOM and SMESH
[modules/kernel.git] / src / KERNEL_PY / kernel / services.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # Author: Guillaume Boulant (EDF/R&D)
22
23 #
24 # WARNING: development notes
25 #
26 # This python module is aimed to be a (yet another?) end user
27 # interface to manipulate the KERNEL SALOME services. Note that it
28 # does not replace the salome module (i.e. what you get when typing
29 # "import salome", actually implemented in the file __init__.py of the
30 # salome python package). It provides instead functions that help to
31 # use the salome module in a end user context (development of domain
32 # specific tools by programmers that are not ten years experienced in
33 # SALOME development).
34
35 import salome
36 from deprecation import is_called_by_sphinx
37 if not is_called_by_sphinx() and salome.lcc is None:
38     try:
39         salome.salome_init()
40     except Exception, e:
41         print e
42
43 # Note that the salome module provides you with standard SALOME
44 # objects: CORBA broker (orb): salome.orb lyfe cycle (lcc) :
45 # salome.lcc naming service : salome.naming_service study manager :
46 # salome.myStudyManager The default study : salome.myStudy
47 #
48 # Alternatively, you may obtain these objects directly with the
49 # following instructions:
50 #
51 #   from omniORB import CORBA
52 #   from LifeCycleCORBA import LifeCycleCORBA
53 #   orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
54 #   lcc = LifeCycleCORBA( orb )
55
56 #   from SALOME_NamingServicePy import SALOME_NamingServicePy_i
57 #   naming_service = SALOME_NamingServicePy_i( orb )
58 #
59 # (See salome.py page in the KERNEL documentation)
60
61 #
62 # ==============================================================================
63 # Helper functions for SALOME components
64 # ==============================================================================
65 #
66
67 #
68 # componentName is the name of the component as declared in the XML
69 # SALOME catalog. A loadable library with name lib<componentName>Engine.so
70 # is supposed to be reachable. This library is supposed to provide a
71 # factory function with the prototype:
72 #
73 #   PortableServer::ObjectId * <componentName>Engine_factory(
74 #                                             CORBA::ORB_ptr orb,
75 #                                             PortableServer::POA_ptr poa,
76 #                                             PortableServer::ObjectId * contId,
77 #                                             const char *instanceName,
78 #                                             const char *interfaceName);
79 #
80 # corbaModule is the name of the IDL module that contains the
81 # definition of the interface of the component. This name corresponds
82 # to the namespace of the servant classes.
83 #
84 # containerType specified the container in which the servants are
85 # executed.
86 #
87 def getComponent(componentName = "SalomeTestComponent",
88                  corbaModule   = "Engines",
89                  containerType = "FactoryServer"):
90     """
91     Get a SALOME CORBA component from its name
92     """
93     print "INF: getting component %s from CORBA module %s ..."%(componentName,corbaModule)
94     __import__(corbaModule)
95     component=salome.lcc.FindOrLoadComponent(containerType,componentName)
96     if component is None:
97         print "ERR: the SALOME component "+componentName+" can't be reached"
98     print "INF: component %s obtained from CORBA module %s"%(componentName,corbaModule)
99     return component
100
101 # Note that an alternative (and maybe better) method to get a component
102 # is to use the module catalog. Here, we just use the catalog to get
103 # the list of components defined in the current session.
104 import SALOME_ModuleCatalog
105 def getComponentList():
106     """
107     Get the list of names of all SALOME componenents register in
108     the catalog.
109     """
110     obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
111     catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
112     if not catalog:
113         raise RuntimeError, "Can't accesss module catalog"
114     return catalog.GetComponentList()
115
116 def getStudyManager():
117     """Get a study manager to create and manage SALOME studies"""
118     return salome.myStudyManager
119
120 import SALOMEDS
121 def __getStudyManager_demo():
122     """
123     Get a study manager to create and manage SALOME studies. WARN: you
124     should use instead the variable salome.myStudyManager. This
125     function is given for illustration of usage of the naming service
126     """
127     naming_service = SALOME_NamingServicePy_i( orb )
128     obj = naming_service.Resolve( '/myStudyManager' )
129     studyManager = obj._narrow( SALOMEDS.StudyManager)
130     return studyManager
131
132
133 #
134 # ==============================================================================
135 # Helper functions for manipulating objects, sobjects and entry
136 # ==============================================================================
137 #
138
139 # - the SObject is an item in a study (Study Object).
140 # - the entry is the identifier of an item.
141 # - the ID is the entry
142 # - the object (geom object or smesh object) is a CORBA servant
143 #   embedded in the SALOME component container and with a reference in
144 #   the SALOME study, so that it can be retrieved.
145
146 # __GBO__ WARN: theses functions are already defined in
147 # salome_study.py, but without the possibility to specify the
148 # underlying study (in salome_study.py, the study is the default study
149 # binded to the salome.myStudy attribute). TODO: see if it can be
150 # extends to the prototype (with the study as an argument) below and
151 # resorb the functions below.
152
153 def IDToObject(id, study=None):
154     myObj = None
155     if study is None:
156         myStudy = salome.myStudy
157     else:
158         myStudy = study
159     mySO = myStudy.FindObjectID(id);
160     if mySO is not None:
161         ok, anAttr = mySO.FindAttribute("AttributeIOR")
162         if ok:
163             AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
164             if AtIOR.Value() != "":
165                 myObj = salome.orb.string_to_object(AtIOR.Value())
166     return myObj
167
168 def ObjectToSObject(obj, study=None):
169     mySO = None
170
171     if study is None:
172         myStudy = salome.myStudy
173     else:
174         myStudy = study
175
176     if obj is not None:
177         ior =  salome.orb.object_to_string(obj)
178         if ior != "":
179             mySO = myStudy.FindObjectIOR(ior)
180     return mySO
181
182 def ObjectToID(obj, study=None):
183     mySO = ObjectToSObject(obj,study)
184     if mySO:
185         return mySO.GetID()
186     return ""
187
188 def IDToSObject(id, study=None):
189     if study is None:
190         myStudy = salome.myStudy
191     else:
192         myStudy = study
193
194     mySO = myStudy.FindObjectID(id);
195     return mySO
196
197 def SObjectToID(sobject):
198     if sobject is None: return None
199     return sobject.GetID()
200
201
202 #
203 # ==============================================================================
204 # Basic use cases and unit tests
205 # ==============================================================================
206 #
207
208 def TEST_getComponent():
209     component=getComponent(componentName = "SalomeTestComponent")
210     
211     ref_string = 'TestComponent_i : L = 3'
212     res_string = component.Coucou(3)
213     if ref_string != ref_string:
214         return False
215     return True
216
217 def TEST_getComponentList():
218     componentList=getComponentList()
219     if 'SalomeTestComponent' not in componentList:
220         return False
221     return True
222
223 def TEST_createObject():
224     """
225     WARNING: for this test, we need GEOM (used to create an object 
226     """
227     import geompy
228     geompy.init_geom(salome.myStudy)
229     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
230     id = geompy.addToStudy( box, 'box' )
231     return id
232
233 def TEST_objectsManipulation():
234     myEntry = TEST_createObject()
235
236     mySObject = IDToSObject(myEntry)
237     entry     = SObjectToID(mySObject)
238
239     if str(entry) != str(myEntry):
240         return False
241
242
243     myObject = IDToObject(myEntry)
244     print myObject
245     if myObject is None:
246         return False
247
248     return True
249
250 if __name__ == "__main__":
251     import unittester
252     unittester.run("services","TEST_getComponent")
253     unittester.run("services","TEST_getComponentList")
254     unittester.run("services","TEST_objectsManipulation")