Salome HOME
0e5d6ebe8c1ca88a628a88a5ff6b489e02285d49
[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     "getStudy",
36     "getEngine",
37     "getEngineIOR",
38     "findOrCreateComponent",
39     "getObjectID",
40     ]
41
42 from omniORB import CORBA
43 from SALOME_NamingServicePy import SALOME_NamingServicePy_i
44 from LifeCycleCORBA import LifeCycleCORBA
45 import SALOMEDS
46 import SALOMEDS_Attributes_idl
47 import PYHELLO_ORB
48 import os
49
50 ###
51 # Get PYHELLO module's ID
52 ###
53 def moduleID():
54     MODULE_ID = 1000
55     return MODULE_ID
56
57 ###
58 # Get PYHELLO object's ID
59 ###
60 def objectID():
61     OBJECT_ID = 1010
62     return OBJECT_ID
63
64 ###
65 # Get unknown ID
66 ###
67 def unknownID():
68     FOREIGN_ID = -1
69     return FOREIGN_ID
70
71 ###
72 # Get PYHELLO module's name
73 ###
74 def moduleName():
75     return "PYHELLO"
76
77 ###
78 # Get module's pixmap name
79 ###
80 def modulePixmap():
81     return "PYHELLO_small.png"
82
83 ###
84 # Get verbose level
85 ### 
86 __verbose__ = None
87 def verbose():
88     global __verbose__
89     if __verbose__ is None:
90         try:
91             __verbose__ = int( os.getenv( 'SALOME_VERBOSE', 0 ) )
92         except:
93             __verbose__ = 0
94             pass
95         pass
96     return __verbose__
97
98 ###
99 # Get ORB reference
100 ###
101 __orb__ = None
102 def getORB():
103     global __orb__
104     if __orb__ is None:
105         __orb__ = CORBA.ORB_init( [''], CORBA.ORB_ID )
106         pass
107     return __orb__
108
109 ###
110 # Get naming service instance
111 ###
112 __naming_service__ = None
113 def getNS():
114     global __naming_service__
115     if __naming_service__ is None:
116         __naming_service__ = SALOME_NamingServicePy_i( getORB() )
117         pass
118     return __naming_service__
119
120 ##
121 # Get life cycle CORBA instance
122 ##
123 __lcc__ = None
124 def getLCC():
125     global __lcc__
126     if __lcc__ is None:
127         __lcc__ = LifeCycleCORBA( getORB() )
128         pass
129     return __lcc__
130
131 ##
132 # Get study
133 ###
134 __study__ = None
135 def getStudy():
136     global __study__
137     if __study__ is None:
138         obj = getNS().Resolve( '/Study' )
139         __study__ = obj._narrow( SALOMEDS.Study )
140         pass
141     return __study__
142
143 ###
144 # Get PYHELLO engine
145 ###
146 __engine__ = None
147 def getEngine():
148     global __engine__
149     if __engine__ is None:
150         __engine__ = getLCC().FindOrLoadComponent( "FactoryServerPy", moduleName() )
151         pass
152     return __engine__
153
154 ###
155 # Get PYHELLO engine IOR
156 ###
157 def getEngineIOR():
158     IOR = ""
159     if getORB() and getEngine():
160         IOR = getORB().object_to_string( getEngine() )
161         pass
162     return IOR
163
164 ###
165 # Find or create PYHELLO component object in a study
166 ###
167 def findOrCreateComponent( study ):
168     father = study.FindComponent( moduleName() )
169     if father is None:
170         builder = study.NewBuilder()
171         father = builder.NewComponent( moduleName() )
172         attr = builder.FindOrCreateAttribute( father, "AttributeName" )
173         attr.SetValue( moduleName() )
174         attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
175         attr.SetPixMap( modulePixmap() )
176         attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
177         attr.SetValue( moduleID() )
178         try:
179             builder.DefineComponentInstance( father, getEngine() )
180             pass
181         except:
182             pass
183         pass
184     return father
185
186 ###
187 # Get object's ID
188 ###
189 def getObjectID( study, entry ):
190     ID = unknownID()
191     if study and entry:
192         sobj = study.FindObjectID( entry )
193         if sobj is not None:
194             test, anAttr = sobj.FindAttribute( "AttributeLocalID" )
195             if test: ID = anAttr._narrow( SALOMEDS.AttributeLocalID ).Value()
196             pass
197         pass
198     return ID
199