Salome HOME
updated copyright message
[modules/kernel.git] / src / KERNEL_PY / salome_iapp.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  CEA, EDF, 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:
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     def UpdateView(self):
158         """Update current view"""
159         print("SalomeOutsideGUI.UpdateView: not available outside GUI")
160         pass
161
162     #--------------------------------------------------------------------------
163
164     
165 def salome_iapp_init(embedded):
166     global salome_iapp_initial
167     global sg,IN_SALOME_GUI
168
169     if salome_iapp_initial:
170         salome_iapp_initial=0
171         if embedded:
172             import libSALOME_Swig
173         
174             class SalomeGUI(libSALOME_Swig.SALOMEGUI_Swig):
175                 getAllSelected = SalomeGUIgetAllSelected
176
177             # create a SALOMEGUI_Swig instance
178             sg = SalomeGUI()
179             inSalomeGUI = sg.hasDesktop()
180         else:
181             # Not embedded in GUI
182             sg=SalomeOutsideGUI()
183             inSalomeGUI=0
184         if IN_SALOME_GUI is None:
185           IN_SALOME_GUI = inSalomeGUI
186     return sg
187
188 def salome_iapp_close():
189     global salome_iapp_initial
190     salome_iapp_initial=1
191     pass
192
193
194 def register_module_in_study(name, syncCall=True):
195     try:
196         import salome
197         salome.salome_init()
198         session_server = salome.naming_service.Resolve("/Kernel/Session")
199         if session_server:
200             message = "register_module_in_study/"+name
201             if syncCall:
202                 session_server.emitMessage(message)
203             else:
204                 session_server.emitMessageOneWay(message)
205     except Exception:
206          pass