Salome HOME
From the community forum: none -> None
[modules/kernel.git] / src / KERNEL_PY / salome_iapp.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 #  File   : salome_iapp.py
25 #  Author : Paul RASCLE, EDF
26 #  Module : SALOME
27 #  $Header$
28 ## @package salome_iapp
29 # Module salome gives access to Salome GUI resources (if GUI has been launched).
30 #
31 import salome_ComponentGUI
32
33     #--------------------------------------------------------------------------
34
35 IN_SALOME_GUI=None
36
37 def ImportComponentGUI(ComponentName, updateOB=True):
38     if IN_SALOME_GUI:
39         libName = "lib" + ComponentName + "_Swig"
40         command = "from " + libName + " import *"
41         exec (command, globals())
42         constructor = ComponentName + "_Swig()"
43         if not updateOB :
44           constructor = ComponentName + "_Swig( False )"
45         command = "gui = " + constructor
46         exec (command, globals())
47         return gui  # @UndefinedVariable
48     else:
49         print("Warning: ImportComponentGUI(",ComponentName,") outside GUI !")
50         print("calls to GUI methods may crash...")
51         return salome_ComponentGUI
52
53     #--------------------------------------------------------------------------
54
55 def SalomeGUIgetAllSelected(self):
56     selNumber = self.SelectedCount()
57     listSelected = []
58     for i in range(selNumber):
59         listSelected.append(self.getSelected(i))
60     return listSelected
61
62     #--------------------------------------------------------------------------
63
64 def hasDesktop():
65     return IN_SALOME_GUI
66
67     #--------------------------------------------------------------------------
68
69 salome_iapp_initial = 1
70
71 class SalomeOutsideGUI(object):
72     """
73     Provides a replacement for class SalomeGUI outside GUI process.
74     Do almost nothing
75     """
76     global myStudyName
77     
78     def hasDesktop(self):
79         """Indicate if GUI is running"""
80         return False
81     
82     def updateObjBrowser(self):
83         """update the GUI object browser"""
84         print("SalomeOutsideGUI: no objectBrowser update outside GUI")
85         pass
86     
87     def getStudyName(self):
88         """Get the study name"""
89         print("SalomeOutsideGUI.getStudyName: avoid use outside GUI")
90         return myStudyName
91     
92     def SelectedCount(self):
93         """Get the number of active selections"""
94         print("SalomeOutsideGUI: no selection mechanism available outside GUI")
95         return 0
96     
97     def getSelected(self, i):
98         """Get the selection number i """
99         print("SalomeOutsideGUI: no selection mechanism available outside GUI")
100         return None
101     
102     def AddIObject(self, Entry):
103         """Add an entry"""
104         print("SalomeOutsideGUI.AddIOObject: not available outside GUI")
105         pass
106     
107     def RemoveIObject(self, Entry):
108         """Remove an entry"""
109         print("SalomeOutsideGUI.REmoveIOObject: not available outside GUI")
110         pass
111     
112     def ClearIObjects(self):
113         """Clear entries"""
114         print("SalomeOutsideGUI.ClearIOObject: not available outside GUI")
115         pass
116     
117     def Display(self, Entry):
118         """Display an entry"""
119         print("SalomeOutsideGUI.Display: not available outside GUI")
120         pass
121     
122     def DisplayOnly(self, Entry):
123         """Display only an entry"""
124         print("SalomeOutsideGUI.DisplayOnly: not available outside GUI")
125         pass
126     
127     def Erase(self, Entry):
128         """Erase en entry"""
129         print("SalomeOutsideGUI.Erase: not available outside GUI")
130         pass
131     
132     def DisplayAll(self):
133         """Display all"""
134         print("SalomeOutsideGUI.Erase: not available outside GUI")
135         pass
136     
137     def EraseAll(self):
138         """Erase all"""
139         print("SalomeOutsideGUI.EraseAll: not available outside GUI")
140         pass
141
142     def IsInCurrentView(self, Entry):
143         """Indicate if an entry is in current view"""
144         print("SalomeOutsideGUI.IsIncurrentView: not available outside GUI")
145         return False
146         
147     def getComponentName(self, ComponentUserName ):
148         """Get component name from component user name"""
149         print("SalomeOutsideGUI.getComponentName: not available outside GUI")
150         return ""
151    
152     def getComponentUserName( self, ComponentName ):
153         """Get component user name from component name"""
154         print("SalomeOutsideGUI.getComponentUserName: not available outside GUI")
155         return ""
156         
157     #--------------------------------------------------------------------------
158
159     
160 def salome_iapp_init(embedded):
161     global salome_iapp_initial
162     global sg,IN_SALOME_GUI
163
164     if salome_iapp_initial:
165         salome_iapp_initial=0
166         if embedded:
167             import libSALOME_Swig
168         
169             class SalomeGUI(libSALOME_Swig.SALOMEGUI_Swig):
170                 getAllSelected = SalomeGUIgetAllSelected
171
172             # create a SALOMEGUI_Swig instance
173             sg = SalomeGUI()
174             inSalomeGUI = sg.hasDesktop()
175         else:
176             # Not embedded in GUI
177             sg=SalomeOutsideGUI()
178             inSalomeGUI=0
179         if IN_SALOME_GUI is None:
180           IN_SALOME_GUI = inSalomeGUI
181     return sg
182
183 def salome_iapp_close():
184     global salome_iapp_initial
185     salome_iapp_initial=1
186     pass
187
188
189 def register_module_in_study(name, syncCall=True):
190     try:
191         import salome
192         salome.salome_init()
193         session_server = salome.naming_service.Resolve("/Kernel/Session")
194         if session_server:
195             message = "register_module_in_study/"+name
196             if syncCall:
197                 session_server.emitMessage(message)
198             else:
199                 session_server.emitMessageOneWay(message)
200     except:
201          pass