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