1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2012 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.
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
31 DEMO_IS_ACTIVATED = False
34 # -------------------------------------------------------------------------
35 # Example 1: creation of basic objects.
36 # The plugin function is implemented here and declared in the pluginsmanager.
40 def trihedron(context):
43 # Intialize the geompy factory with the active study
44 activeStudy = context.study
45 geompy.init_geom(activeStudy)
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)
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" )
60 salome_pluginsmanager.AddFunction('DEMO/O,Vx,Vy,Vz',
61 'Creates the trihedron',
65 # -------------------------------------------------------------------------
66 # Example 1 bis: creation of basic objects and automatic display
67 def trihedron_withdisplay(context):
70 # Intialize the geompy factory with the active study
71 activeStudy = context.study
72 geompy.init_geom(activeStudy)
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)
80 # Register the objects in the active study
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" ))
87 # This part is to automatically display the objects in the active viewer.
88 gcomp = salome.ImportComponentGUI("GEOM")
90 gcomp.createAndDisplayFitAllGO(entry)
93 salome_pluginsmanager.AddFunction('DEMO/O,Vx,Vy,Vz (2)',
94 'Creates Basic GEOM objects',
95 trihedron_withdisplay)
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).
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
110 dialog = tubedialog.TubeDialog()
111 dialog.setData(tubebuilder.DEFAULT_RADIUS,
112 tubebuilder.DEFAULT_LENGTH,
113 tubebuilder.DEFAULT_WIDTH)
115 def tube_shapewithgui(context):
116 global tubebuilder, xalome, dialog
117 activeStudy = context.study
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.
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)
130 salome_pluginsmanager.AddFunction('DEMO/Tube shape from parameters',
131 'Creates a tube object from specified parameters',
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).
140 def tube_meshwithgui(context):
142 activeStudy = context.study
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.
149 radius, length, width = dialog.getData()
150 mesh = tubebuilder.createModel(activeStudy, radius, length, width)
153 salome_pluginsmanager.AddFunction('DEMO/Tube mesh from parameters',
154 'Creates a tube object from specified parameters',
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.
167 # - coloring the preview in pink
168 # - store the tmp study objects in a "preview" folder
170 dialogWithApply = tubedialog.TubeDialogOnTopWithApply()
171 dialogWithApply.setData(tubebuilder.DEFAULT_RADIUS,
172 tubebuilder.DEFAULT_LENGTH,
173 tubebuilder.DEFAULT_WIDTH)
175 previewShapeEntry = None
177 DEFAULT_FOLDER_NAME="TubeList"
178 DEFAULT_SHAPE_NAME="tube"
179 PREVIEW_SHAPE_NAME="preview"
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
188 dialogWithApply.accept()
190 if previewShapeEntry is not None:
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)
198 def rejectCallback():
199 """Action to be done when click on Cancel"""
200 global dialogWithApply, previewShapeEntry, deletePreviewShape
202 dialogWithApply.reject()
204 if previewShapeEntry is not None:
208 PREVIEW_COLOR=SALOMEDS.Color(1,0.6,1) # pink
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
217 # We first have to destroy the currently displayed preview shape.
218 if previewShapeEntry is not None:
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)
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
235 # Connection of callback functions to the dialog butoon click signals
236 dialogWithApply.handleAcceptWith(acceptCallback)
237 dialogWithApply.handleRejectWith(rejectCallback)
238 dialogWithApply.handleApplyWith(applyCallback)
240 def tube_shapewithguiAndPreview(context):
242 This plugin open a dialog in an asynchronous mode. Then it
243 required callback functions to be associated to the button
246 global dialogWithApply, activeStudy
247 activeStudy = context.study
248 dialogWithApply.open()
251 salome_pluginsmanager.AddFunction('DEMO/Tube geometry from parameters with preview',
252 'Creates a tube object from specified parameters',
253 tube_shapewithguiAndPreview)
257 # -------------------------------------------------------------------------
258 # Example 3: run a shell session in a xterm to get a SALOME python console
259 def runSalomeShellSession(context):
261 import salome_version
262 version = salome_version.getVersion(full=True)
263 kernel_appli_dir = os.environ['KERNEL_ROOT_DIR']
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)
272 print "Neither xterm nor gnome-terminal nor konsole is installed."
274 if command is not "":
276 subprocess.check_call(command, shell = True)
281 salome_pluginsmanager.AddFunction('SALOME shell session',
282 'Execute a SALOME shell session in an external xterm',
283 runSalomeShellSession)