Salome HOME
615fde7ce7bdff5bc8712430297b2170ad74cd57
[samples/pyhello.git] / src / PYHELLO / PYHELLO_utils.py
1 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 # ---
21 # File   : PYHELLO_utils.py
22 # Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
23 # ---
24 #
25 __all__ = [
26     "moduleID",
27     "objectID",
28     "unknownID",
29     "moduleName",
30     "modulePixmap",
31     "verbose",
32     "getORB",
33     "getNS",
34     "getLCC",
35     "getEngine",
36     "getStudy",
37     "getEngineIOR",
38     "findOrCreateComponent",
39     "getObjectID",
40     ]
41
42
43 from omniORB import CORBA
44 from SALOME_NamingServicePy import SALOME_NamingServicePy_i
45 from LifeCycleCORBA import LifeCycleCORBA
46 import SALOMEDS
47 import SALOMEDS_Attributes_idl
48 import PYHELLO_ORB
49 import os
50
51 ###
52 # Get PYHELLO module's ID
53 ###
54 def moduleID():
55     MODULE_ID = 1000
56     return MODULE_ID
57
58 ###
59 # Get PYHELLO object's ID
60 ###
61 def objectID():
62     OBJECT_ID = 1010
63     return OBJECT_ID
64
65 ###
66 # Get unknown ID
67 ###
68 def unknownID():
69     FOREIGN_ID = -1
70     return FOREIGN_ID
71
72 ###
73 # Get PYHELLO module's name
74 ###
75 def moduleName():
76     return "PYHELLO"
77
78 ###
79 # Get module's pixmap name
80 ###
81 def modulePixmap():
82     return "PYHELLO_small.png"
83
84 ###
85 # Get verbose level
86 ### 
87 __verbose__ = None
88 def verbose():
89     global __verbose__
90     if __verbose__ is None:
91         try:
92             __verbose__ = int( os.getenv( 'SALOME_VERBOSE', 0 ) )
93         except:
94             __verbose__ = 0
95             pass
96         pass
97     return __verbose__
98
99 ###
100 # Get ORB reference
101 ###
102 __orb__ = None
103 def getORB():
104     global __orb__
105     if __orb__ is None:
106         __orb__ = CORBA.ORB_init( [''], CORBA.ORB_ID )
107         pass
108     return __orb__
109
110 ###
111 # Get naming service instance
112 ###
113 __naming_service__ = None
114 def getNS():
115     global __naming_service__
116     if __naming_service__ is None:
117         __naming_service__ = SALOME_NamingServicePy_i( getORB() )
118         pass
119     return __naming_service__
120
121 ##
122 # Get life cycle CORBA instance
123 ##
124 __lcc__ = None
125 def getLCC():
126     global __lcc__
127     if __lcc__ is None:
128         __lcc__ = LifeCycleCORBA( getORB() )
129         pass
130     return __lcc__
131
132 ##
133 # Get study
134 ###
135 __study__ = None
136 def getStudy():
137     global __study__
138     if __study__ is None:
139         obj = getNS().Resolve( '/Study' )
140         __study__ = obj._narrow( SALOMEDS.Study )
141         pass
142     return __study__
143
144 ###
145 # Get PYHELLO engine
146 ###
147 __engine__ = None
148 def getEngine():
149     global __engine__
150     if __engine__ is None:
151         __engine__ = getLCC().FindOrLoadComponent( "FactoryServerPy", moduleName() )
152         pass
153     return __engine__
154
155 ###
156 # Get PYHELLO engine IOR
157 ###
158 def getEngineIOR():
159     IOR = ""
160     if getORB() and getEngine():
161         IOR = getORB().object_to_string( getEngine() )
162         pass
163     return IOR
164
165 ###
166 # Find or create PYHELLO component object in a study
167 ###
168 def findOrCreateComponent():
169     study = getStudy()
170     father =study.FindComponent( moduleName() )
171     if father is None:
172         builder = study.NewBuilder()
173         father = builder.NewComponent( moduleName() )
174         attr = builder.FindOrCreateAttribute( father, "AttributeName" )
175         attr.SetValue( moduleName() )
176         attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
177         attr.SetPixMap( modulePixmap() )
178         attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
179         attr.SetValue( moduleID() )
180         try:
181             builder.DefineComponentInstance( father, getEngine() )
182             pass
183         except:
184             pass
185         pass
186     return father
187
188 ###
189 # Get object's ID
190 ###
191 def getObjectID( entry ):
192     ID = unknownID()
193     study = getStudy()
194     if entry:
195         sobj = study.FindObjectID( entry )
196         if sobj is not None:
197             test, anAttr = sobj.FindAttribute( "AttributeLocalID" )
198             if test: ID = anAttr._narrow( SALOMEDS.AttributeLocalID ).Value()
199             pass
200         pass
201     return ID
202