Salome HOME
c5246ec1678c3f09b1ce9c1c992668c41c89a997
[samples/pyhello.git] / src / PYHELLO / PYHELLO_utils.py
1 # Copyright (C) 2007-2021  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     "getLCC",
34     "getEngine",
35     "getStudy",
36     "getEngineIOR",
37     "findOrCreateComponent",
38     "getObjectID",
39     ]
40
41
42 from omniORB import CORBA
43 from SALOME_NamingServicePy import SALOME_NamingServicePy_i
44 from LifeCycleCORBA import LifeCycleCORBA
45 import salome
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 def getORB():
103     salome.salome_init()
104     return salome.orb
105
106 ##
107 # Get life cycle CORBA instance
108 ##
109 def getLCC():
110     salome.salome_init()
111     return salome.lcc
112
113 ##
114 # Get study
115 ###
116 def getStudy():
117     salome.salome_init()
118     return salome.myStudy
119
120 ###
121 # Get PYHELLO engine
122 ###
123 __engine__ = None
124 def getEngine():
125     global __engine__
126     if __engine__ is None:
127         __engine__ = getLCC().FindOrLoadComponent( "FactoryServer", moduleName() )
128         pass
129     return __engine__
130
131 ###
132 # Get PYHELLO engine IOR
133 ###
134 def getEngineIOR():
135     IOR = ""
136     if getORB() and getEngine():
137         IOR = getORB().object_to_string( getEngine() )
138         pass
139     return IOR
140
141 ###
142 # Find or create PYHELLO component object in a study
143 ###
144 def findOrCreateComponent():
145     study = getStudy()
146     father =study.FindComponent( moduleName() )
147     if father is None:
148         builder = study.NewBuilder()
149         father = builder.NewComponent( moduleName() )
150         attr = builder.FindOrCreateAttribute( father, "AttributeName" )
151         attr.SetValue( moduleName() )
152         attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
153         attr.SetPixMap( modulePixmap() )
154         attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
155         attr.SetValue( moduleID() )
156         try:
157             builder.DefineComponentInstance( father, getEngine() )
158             pass
159         except:
160             pass
161         pass
162     return father
163
164 ###
165 # Get object's ID
166 ###
167 def getObjectID( entry ):
168     ID = unknownID()
169     study = getStudy()
170     if entry:
171         sobj = study.FindObjectID( entry )
172         if sobj is not None:
173             test, anAttr = sobj.FindAttribute( "AttributeLocalID" )
174             if test: ID = anAttr._narrow( SALOMEDS.AttributeLocalID ).Value()
175             pass
176         pass
177     return ID
178