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