Salome HOME
bos #24078 Reinitialize study services properly when requested
[modules/shaper_study.git] / src / PY / SHAPERSTUDY_utils.py
1 # Copyright (C) 2019-2021  CEA/DEN, EDF R&D
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   : SHAPERSTUDY_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 SHAPERSTUDY_ORB
49 import os
50    
51 ###
52 # Get SHAPERSTUDY module's name
53 ###
54 def moduleName():
55     return "SHAPERSTUDY"
56
57 ###
58 # Get module's pixmap name
59 ###
60 def modulePixmap():
61     return "shaper.png"
62
63 ###
64 # Get verbose level
65 ### 
66 __verbose__ = None
67 def verbose():
68     global __verbose__
69     if __verbose__ is None:
70         try:
71             __verbose__ = int( os.getenv( 'SALOME_VERBOSE', 0 ) )
72         except:
73             __verbose__ = 0
74             pass
75         pass
76     return __verbose__
77
78 ###
79 # Get ORB reference
80 ###
81 __orb__ = None
82 def getORB():
83     import salome
84     salome.salome_init()
85     global __orb__
86     if __orb__ is None:
87         __orb__ = CORBA.ORB_init( [''], CORBA.ORB_ID )
88         pass
89     return __orb__
90
91 ###
92 # Get POA
93 ###
94 __poa__ = None
95 def getPOA():
96     global __poa__
97     if __poa__ is None:
98         import salome
99         salome.salome_init()
100         __poa__ = salome.orb.resolve_initial_references("RootPOA")
101         pass
102     return __poa__
103
104 ###
105 # Get naming service instance
106 ###
107 def getNS():
108     import salome
109     salome.salome_init()
110     return salome.naming_service
111
112 ##
113 # Get life cycle CORBA instance
114 ##
115 def getLCC():
116     import salome
117     salome.salome_init()
118     return salome.lcc
119
120 ##
121 # Get study
122 ###
123
124 def getStudy():
125     import salome
126     salome.salome_init()
127     return salome.myStudy
128
129 ###
130 # Get SHAPERSTUDY engine
131 ###
132 __engine__ = None
133 def getEngine():
134     global __engine__
135     if __engine__ is None:
136         import salome
137         salome.salome_init()
138         __engine__ = salome.lcc.FindOrLoadComponent( "FactoryServer", moduleName() )
139         pass
140     return __engine__
141
142 ###
143 # Get SHAPERSTUDY engine IOR
144 ###
145 def getEngineIOR():
146     IOR = ""
147     if getORB() and getEngine():
148         IOR = getORB().object_to_string( getEngine() )
149         pass
150     return IOR
151
152 ###
153 # Find or create SHAPERSTUDY component object in a study
154 ###
155 def findOrCreateComponent():
156     study = getStudy()
157     builder = study.NewBuilder()
158     father = study.FindComponent( moduleName() )
159     if father is None:
160         father = builder.NewComponent( moduleName() )
161         attr = builder.FindOrCreateAttribute( father, "AttributeName" )
162         attr.SetValue( "ShaperResults" )
163         attr = builder.FindOrCreateAttribute( father, "AttributePixMap" )
164         attr.SetPixMap( modulePixmap() )
165         #attr = builder.FindOrCreateAttribute( father, "AttributeLocalID" )
166         #attr.SetValue( moduleID() )
167         try:
168             builder.DefineComponentInstance( father, getEngine() )
169             pass
170         except:
171             pass
172         pass
173     # load the SHAPER-STUDY file if it is not done yet
174     builder.LoadWith(father, getEngine())
175     return father
176