Salome HOME
Update copyrights 2014.
[modules/gui.git] / src / SalomeApp / pluginsdemo / salome_plugins.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2014  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Guillaume Boulant (EDF)
21
22 import salome_pluginsmanager
23
24 DEMO_IS_ACTIVATED = False
25
26 if DEMO_IS_ACTIVATED:
27   # Check that GEOM and SMESH modules are present
28   try:
29     import GEOM
30     from salome.geom import geomBuilder
31     geompy = geomBuilder.New(salome.myStudy)
32     
33     import SMESH, SALOMEDS
34     from salome.smesh import smeshBuilder
35     smesh =  smeshBuilder.New(salome.myStudy)
36   except:
37     DEMO_IS_ACTIVATED = False
38
39 if DEMO_IS_ACTIVATED:
40   # -------------------------------------------------------------------------
41   # Example 1: creation of basic objects.
42   # The plugin function is implemented here and declared in the pluginsmanager.
43   #
44   import salome
45
46   def trihedron(context):
47       import GEOM
48       from salome.geom import geomBuilder
49
50       # Intialize the geompy factory with the active study
51       activeStudy = context.study
52       geompy = geomBuilder.New(activeStudy)
53
54       # Create the objects
55       Vx = geompy.MakeVectorDXDYDZ(10, 0, 0)
56       Vy = geompy.MakeVectorDXDYDZ(0, 10, 0)
57       Vz = geompy.MakeVectorDXDYDZ(0, 0, 10)
58       origin = geompy.MakeVertex(0, 0, 0)
59
60       # Register the objects in the active study
61       geompy.addToStudy( Vx, "Vx" )
62       geompy.addToStudy( Vy, "Vy" )
63       geompy.addToStudy( Vz, "Vz" )
64       geompy.addToStudy( origin, "origin" )
65
66
67   salome_pluginsmanager.AddFunction('DEMO/O,Vx,Vy,Vz',
68                                     'Creates the trihedron',
69                                     trihedron)
70
71
72   # -------------------------------------------------------------------------
73   # Example 1 bis: creation of basic objects and automatic display
74   def trihedron_withdisplay(context):
75       import GEOM
76       from salome.geom import geomBuilder
77
78       # Intialize the geompy factory with the active study
79       activeStudy = context.study
80       geompy = geomBuilder.New(activeStudy)
81
82       # Create the objects
83       Vx = geompy.MakeVectorDXDYDZ(10, 0, 0)
84       Vy = geompy.MakeVectorDXDYDZ(0, 10, 0)
85       Vz = geompy.MakeVectorDXDYDZ(0, 0, 10)
86       origin = geompy.MakeVertex(0, 0, 0)
87
88       # Register the objects in the active study
89       entries=[]
90       entries.append(geompy.addToStudy( Vx, "Vx" ))
91       entries.append(geompy.addToStudy( Vy, "Vy" ))
92       entries.append(geompy.addToStudy( Vz, "Vz" ))
93       entries.append(geompy.addToStudy( origin, "origin" ))
94
95       # This part is to automatically display the objects in the active viewer.
96       gcomp = salome.ImportComponentGUI("GEOM")
97       for entry in entries:
98           gcomp.createAndDisplayFitAllGO(entry)
99
100
101   salome_pluginsmanager.AddFunction('DEMO/O,Vx,Vy,Vz (2)',
102                                     'Creates Basic GEOM objects',
103                                     trihedron_withdisplay)
104
105   # -------------------------------------------------------------------------
106   # Example 2: creation of a shape with parameters that can be read from a GUI.
107   # The plugin function (tube_shapewithgui) delegates some action to
108   # dedicated imported modules (tube.py and tubedialog.py).
109   #
110
111   import tubebuilder
112   import xalome
113
114   # A single dialog box is defined and recycled for every call. The
115   # fields are initialized with default values given by the tube factory
116   # tube.py.
117   import tubedialog
118   dialog = tubedialog.TubeDialog()
119   dialog.setData(tubebuilder.DEFAULT_RADIUS,
120                 tubebuilder.DEFAULT_LENGTH,
121                 tubebuilder.DEFAULT_WIDTH)
122
123   def tube_shapewithgui(context):
124       global tubebuilder, xalome, dialog
125       activeStudy = context.study
126
127       # Get the parameter values from a gui dialog box. If the dialog is
128       # closed using the Ok button, then the data are requested from the
129       # gui and used to create the shape of the tube.
130       dialog.exec_()
131       if dialog.wasOk():
132           radius, length, width = dialog.getData()
133           shape = tubebuilder.createGeometry(activeStudy, radius, length, width)
134           entry = xalome.addToStudy(activeStudy, shape, "Tube" )
135           xalome.displayShape(entry)
136
137
138   salome_pluginsmanager.AddFunction('DEMO/Tube shape from parameters',
139                                     'Creates a tube object from specified parameters',
140                                     tube_shapewithgui)
141
142
143   # -------------------------------------------------------------------------
144   # Example 2 bis: creation of a mesh with parameters that can be read from a GUI.
145   # The plugin function (tube_meshwithgui) delegates some action to
146   # dedicated imported modules (tubebuilder.py and tubedialog.py).
147   #
148   def tube_meshwithgui(context):
149       global tube, dialog
150       activeStudy = context.study
151
152       # Get the parameter values from a gui dialog box. If the dialog is
153       # closed using the Ok button, then the data are requested from the
154       # gui and used to create the shape of the tube.
155       dialog.exec_()
156       if dialog.wasOk():
157           radius, length, width = dialog.getData()
158           mesh = tubebuilder.createModel(activeStudy, radius, length, width)
159
160
161   salome_pluginsmanager.AddFunction('DEMO/Tube mesh from parameters',
162                                     'Creates a tube object from specified parameters',
163                                     tube_meshwithgui)
164
165
166   # -------------------------------------------------------------------------
167   # Example 2 ter: creation of a geom object with a preview function in
168   # the dialog box. This use case is more complex from the gui point of
169   # view because the dialog box is a not modal so that we can have
170   # interaction with the complete SALOME application. This modal
171   # situation requires to connect button click signal on global
172   # functions as a "callback" mechanism.
173   #
174   # TODO:
175   # - coloring the preview in pink
176   # - store the tmp study objects in a "preview" folder
177   #
178   dialogWithApply = tubedialog.TubeDialogOnTopWithApply()
179   dialogWithApply.setData(tubebuilder.DEFAULT_RADIUS,
180                           tubebuilder.DEFAULT_LENGTH,
181                           tubebuilder.DEFAULT_WIDTH)
182   activeStudy = None
183   previewShapeEntry = None
184
185   DEFAULT_FOLDER_NAME="TubeList"
186   DEFAULT_SHAPE_NAME="tube"
187   PREVIEW_SHAPE_NAME="preview"
188
189   def acceptCallback():
190       """Action to be done when click on Ok"""
191       global tubebuilder, xalome
192       global dialogWithApply, activeStudy
193       global previewShapeEntry, deletePreviewShape
194       global DEFAULT_FOLDER_NAME,DEFAULT_SHAPE_NAME
195
196       dialogWithApply.accept()
197
198       if previewShapeEntry is not None:
199           deletePreviewShape()
200
201       radius, length, width = dialogWithApply.getData()
202       shape = tubebuilder.createGeometry(activeStudy, radius, length, width)
203       entry = xalome.addToStudy(activeStudy, shape, DEFAULT_SHAPE_NAME, DEFAULT_FOLDER_NAME)
204       xalome.displayShape(entry)
205
206   def rejectCallback():
207       """Action to be done when click on Cancel"""
208       global dialogWithApply, previewShapeEntry, deletePreviewShape
209
210       dialogWithApply.reject()
211
212       if previewShapeEntry is not None:
213           deletePreviewShape()
214
215   import SALOMEDS
216   PREVIEW_COLOR=SALOMEDS.Color(1,0.6,1) # pink
217
218   def applyCallback():
219       """Action to be done when click on Apply"""
220       global tubebuilder, xalome
221       global dialogWithApply, activeStudy
222       global previewShapeEntry, deletePreviewShape
223       global PREVIEW_COLOR, DEFAULT_SHAPE_NAME, DEFAULT_FOLDER_NAME, PREVIEW_SHAPE_NAME
224
225       # We first have to destroy the currently displayed preview shape.
226       if previewShapeEntry is not None:
227           deletePreviewShape()
228
229       # Then we can create the new shape with the new parameter values
230       radius, length, width = dialogWithApply.getData()
231       shape = tubebuilder.createGeometry(activeStudy, radius, length, width)
232       # We apply a specific color on the shape for the preview state
233       shape.SetColor(PREVIEW_COLOR)
234       previewShapeEntry = xalome.addToStudy(activeStudy, shape, PREVIEW_SHAPE_NAME, DEFAULT_FOLDER_NAME )
235       xalome.displayShape(previewShapeEntry)
236
237   def deletePreviewShape():
238       """This delete the shape currently being displayed as a preview"""
239       global activeStudy, previewShapeEntry, xsalome
240       xalome.deleteShape(activeStudy,previewShapeEntry)
241       previewShapeEntry = None
242
243   # Connection of callback functions to the dialog butoon click signals
244   dialogWithApply.handleAcceptWith(acceptCallback)
245   dialogWithApply.handleRejectWith(rejectCallback)
246   dialogWithApply.handleApplyWith(applyCallback)
247
248   def tube_shapewithguiAndPreview(context):
249       """
250       This plugin open a dialog in an asynchronous mode. Then it
251       required callback functions to be associated to the button
252       signals.
253       """
254       global dialogWithApply, activeStudy
255       activeStudy = context.study
256       dialogWithApply.open()
257
258
259   salome_pluginsmanager.AddFunction('DEMO/Tube geometry from parameters with preview',
260                                     'Creates a tube object from specified parameters',
261                                     tube_shapewithguiAndPreview)
262
263
264
265 # -------------------------------------------------------------------------
266 # Example 3: run a shell session in a xterm to get a SALOME python console
267 def runSalomeShellSession(context):
268     import os,subprocess
269     import salome_version
270     version = salome_version.getVersion(full=True)
271     kernel_appli_dir = os.environ['KERNEL_ROOT_DIR']
272     command = ""
273     if os.path.exists("/usr/bin/gnome-terminal"):
274       command = 'gnome-terminal -t "SALOME %s - Shell session" -e %s/runSession &'%(version,kernel_appli_dir)
275     elif os.path.exists("/usr/bin/konsole"):
276       command = 'PATH="/usr/bin:/sbin:/bin" LD_LIBRARY_PATH="" konsole -e %s/runSession &'%(kernel_appli_dir)
277     elif os.path.exists("/usr/bin/xterm"):
278       command = 'xterm -T "SALOME %s - Shell session" -e %s/runSession &'%(version,kernel_appli_dir)
279     else:
280       print "Neither xterm nor gnome-terminal nor konsole is installed."
281
282     if command is not "":
283       try:
284         subprocess.check_call(command, shell = True)
285       except Exception, e:
286         print "Error: ",e
287
288
289 salome_pluginsmanager.AddFunction('SALOME shell session',
290                                   'Execute a SALOME shell session in an external xterm',
291                                   runSalomeShellSession)