1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Guillaume Boulant (EDF/R&D)
24 # WARNING: development notes
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).
36 from deprecation import is_called_by_sphinx
37 if not is_called_by_sphinx() and salome.lcc is None:
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
48 # Alternatively, you may obtain these objects directly with the
49 # following instructions:
51 # from omniORB import CORBA
52 # from LifeCycleCORBA import LifeCycleCORBA
53 # orb = CORBA.ORB_init( [''], CORBA.ORB_ID )
54 # lcc = LifeCycleCORBA( orb )
56 # from SALOME_NamingServicePy import SALOME_NamingServicePy_i
57 # naming_service = SALOME_NamingServicePy_i( orb )
59 # (See salome.py page in the KERNEL documentation)
62 # ==============================================================================
63 # Helper functions for SALOME components
64 # ==============================================================================
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:
73 # PortableServer::ObjectId * <componentName>Engine_factory(
75 # PortableServer::POA_ptr poa,
76 # PortableServer::ObjectId * contId,
77 # const char *instanceName,
78 # const char *interfaceName);
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.
84 # containerType specified the container in which the servants are
87 def getComponent(componentName = "SalomeTestComponent",
88 corbaModule = "Engines",
89 containerType = "FactoryServer"):
91 Get a SALOME CORBA component from its name
93 print "INF: getting component %s from CORBA module %s ..."%(componentName,corbaModule)
94 __import__(corbaModule)
95 component=salome.lcc.FindOrLoadComponent(containerType,componentName)
97 print "ERR: the SALOME component "+componentName+" can't be reached"
98 print "INF: component %s obtained from CORBA module %s"%(componentName,corbaModule)
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():
107 Get the list of names of all SALOME componenents register in
110 obj = salome.naming_service.Resolve('Kernel/ModulCatalog')
111 catalog = obj._narrow(SALOME_ModuleCatalog.ModuleCatalog)
113 raise RuntimeError, "Can't accesss module catalog"
114 return catalog.GetComponentList()
116 def getStudyManager():
117 """Get a study manager to create and manage SALOME studies"""
118 return salome.myStudyManager
121 def __getStudyManager_demo():
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
127 naming_service = SALOME_NamingServicePy_i( orb )
128 obj = naming_service.Resolve( '/myStudyManager' )
129 studyManager = obj._narrow( SALOMEDS.StudyManager)
134 # ==============================================================================
135 # Helper functions for manipulating objects, sobjects and entry
136 # ==============================================================================
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.
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.
153 def IDToObject(id, study=None):
156 myStudy = salome.myStudy
159 mySO = myStudy.FindObjectID(id);
161 ok, anAttr = mySO.FindAttribute("AttributeIOR")
163 AtIOR = anAttr._narrow(SALOMEDS.AttributeIOR)
164 if AtIOR.Value() != "":
165 myObj = salome.orb.string_to_object(AtIOR.Value())
168 def ObjectToSObject(obj, study=None):
172 myStudy = salome.myStudy
177 ior = salome.orb.object_to_string(obj)
179 mySO = myStudy.FindObjectIOR(ior)
182 def ObjectToID(obj, study=None):
183 mySO = ObjectToSObject(obj,study)
188 def IDToSObject(id, study=None):
190 myStudy = salome.myStudy
194 mySO = myStudy.FindObjectID(id);
197 def SObjectToID(sobject):
198 if sobject is None: return None
199 return sobject.GetID()
203 # ==============================================================================
204 # Basic use cases and unit tests
205 # ==============================================================================
208 def TEST_getComponent():
209 component=getComponent(componentName = "SalomeTestComponent")
211 ref_string = 'TestComponent_i : L = 3'
212 res_string = component.Coucou(3)
213 if ref_string != ref_string:
217 def TEST_getComponentList():
218 componentList=getComponentList()
219 if 'SalomeTestComponent' not in componentList:
223 def TEST_createObject():
225 WARNING: for this test, we need GEOM (used to create an object
228 geompy.init_geom(salome.myStudy)
229 box = geompy.MakeBoxDXDYDZ(200, 200, 200)
230 id = geompy.addToStudy( box, 'box' )
233 def TEST_objectsManipulation():
234 myEntry = TEST_createObject()
236 mySObject = IDToSObject(myEntry)
237 entry = SObjectToID(mySObject)
239 if str(entry) != str(myEntry):
243 myObject = IDToObject(myEntry)
250 if __name__ == "__main__":
252 unittester.run("services","TEST_getComponent")
253 unittester.run("services","TEST_getComponentList")
254 unittester.run("services","TEST_objectsManipulation")