1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # Author : Guillaume Boulant (EDF)
22 import salome_pluginsmanager
24 DEMO_IS_ACTIVATED = False
27 # Check that GEOM and SMESH modules are present
30 from salome.geom import geomBuilder
31 geompy = geomBuilder.New(salome.myStudy)
33 import SMESH, SALOMEDS
34 from salome.smesh import smeshBuilder
35 smesh = smeshBuilder.New(salome.myStudy)
37 DEMO_IS_ACTIVATED = False
40 # -------------------------------------------------------------------------
41 # Example 1: creation of basic objects.
42 # The plugin function is implemented here and declared in the pluginsmanager.
46 def trihedron(context):
48 from salome.geom import geomBuilder
50 # Intialize the geompy factory with the active study
51 activeStudy = context.study
52 geompy = geomBuilder.New(activeStudy)
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)
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" )
67 salome_pluginsmanager.AddFunction('DEMO/O,Vx,Vy,Vz',
68 'Creates the trihedron',
72 # -------------------------------------------------------------------------
73 # Example 1 bis: creation of basic objects and automatic display
74 def trihedron_withdisplay(context):
76 from salome.geom import geomBuilder
78 # Intialize the geompy factory with the active study
79 activeStudy = context.study
80 geompy = geomBuilder.New(activeStudy)
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)
88 # Register the objects in the active study
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" ))
95 # This part is to automatically display the objects in the active viewer.
96 gcomp = salome.ImportComponentGUI("GEOM")
98 gcomp.createAndDisplayFitAllGO(entry)
101 salome_pluginsmanager.AddFunction('DEMO/O,Vx,Vy,Vz (2)',
102 'Creates Basic GEOM objects',
103 trihedron_withdisplay)
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).
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
118 dialog = tubedialog.TubeDialog()
119 dialog.setData(tubebuilder.DEFAULT_RADIUS,
120 tubebuilder.DEFAULT_LENGTH,
121 tubebuilder.DEFAULT_WIDTH)
123 def tube_shapewithgui(context):
124 global tubebuilder, xalome, dialog
125 activeStudy = context.study
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.
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)
138 salome_pluginsmanager.AddFunction('DEMO/Tube shape from parameters',
139 'Creates a tube object from specified parameters',
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).
148 def tube_meshwithgui(context):
150 activeStudy = context.study
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.
157 radius, length, width = dialog.getData()
158 mesh = tubebuilder.createModel(activeStudy, radius, length, width)
161 salome_pluginsmanager.AddFunction('DEMO/Tube mesh from parameters',
162 'Creates a tube object from specified parameters',
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.
175 # - coloring the preview in pink
176 # - store the tmp study objects in a "preview" folder
178 dialogWithApply = tubedialog.TubeDialogOnTopWithApply()
179 dialogWithApply.setData(tubebuilder.DEFAULT_RADIUS,
180 tubebuilder.DEFAULT_LENGTH,
181 tubebuilder.DEFAULT_WIDTH)
183 previewShapeEntry = None
185 DEFAULT_FOLDER_NAME="TubeList"
186 DEFAULT_SHAPE_NAME="tube"
187 PREVIEW_SHAPE_NAME="preview"
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
196 dialogWithApply.accept()
198 if previewShapeEntry is not None:
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)
206 def rejectCallback():
207 """Action to be done when click on Cancel"""
208 global dialogWithApply, previewShapeEntry, deletePreviewShape
210 dialogWithApply.reject()
212 if previewShapeEntry is not None:
216 PREVIEW_COLOR=SALOMEDS.Color(1,0.6,1) # pink
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
225 # We first have to destroy the currently displayed preview shape.
226 if previewShapeEntry is not None:
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)
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
243 # Connection of callback functions to the dialog butoon click signals
244 dialogWithApply.handleAcceptWith(acceptCallback)
245 dialogWithApply.handleRejectWith(rejectCallback)
246 dialogWithApply.handleApplyWith(applyCallback)
248 def tube_shapewithguiAndPreview(context):
250 This plugin open a dialog in an asynchronous mode. Then it
251 required callback functions to be associated to the button
254 global dialogWithApply, activeStudy
255 activeStudy = context.study
256 dialogWithApply.open()
259 salome_pluginsmanager.AddFunction('DEMO/Tube geometry from parameters with preview',
260 'Creates a tube object from specified parameters',
261 tube_shapewithguiAndPreview)
265 # -------------------------------------------------------------------------
266 # Example 3: run a shell session in a xterm to get a SALOME python console
267 def runSalomeShellSession(context):
269 import salome_version
270 version = salome_version.getVersion(full=True)
271 kernel_appli_dir = os.environ['KERNEL_ROOT_DIR']
273 if os.path.exists("/usr/bin/gnome-terminal"):
274 command = 'gnome-terminal -t "SALOME %s - Shell session" -e "%s/salome shell" &'%(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/salome shell" &'%(kernel_appli_dir)
277 elif os.path.exists("/usr/bin/xterm"):
278 command = 'xterm -T "SALOME %s - Shell session" -e "%s/salome shell" &'%(version,kernel_appli_dir)
280 print "Neither xterm nor gnome-terminal nor konsole is installed."
282 if command is not "":
284 subprocess.check_call(command, shell = True)
289 salome_pluginsmanager.AddFunction('SALOME shell session',
290 'Execute a SALOME shell session in an external xterm',
291 runSalomeShellSession)