Back to index

salome-gui  6.5.0
salome_plugins.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2010-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 # Author : Guillaume Boulant (EDF)
00021 
00022 import salome_pluginsmanager
00023 
00024 DEMO_IS_ACTIVATED=True
00025 
00026 # -------------------------------------------------------------------------
00027 # Example 1: creation of basic objects.
00028 # The plugin function is implemented here and declared in the pluginsmanager.
00029 #
00030 import salome
00031 
00032 def trihedron(context):
00033     import geompy
00034 
00035     # Intialize the geompy factory with the active study
00036     activeStudy = context.study
00037     geompy.init_geom(activeStudy)
00038 
00039     # Create the objects
00040     Vx = geompy.MakeVectorDXDYDZ(10, 0, 0)
00041     Vy = geompy.MakeVectorDXDYDZ(0, 10, 0)
00042     Vz = geompy.MakeVectorDXDYDZ(0, 0, 10)
00043     origin = geompy.MakeVertex(0, 0, 0)
00044 
00045     # Register the objects in the active study
00046     geompy.addToStudy( Vx, "Vx" )
00047     geompy.addToStudy( Vy, "Vy" )
00048     geompy.addToStudy( Vz, "Vz" )
00049     geompy.addToStudy( origin, "origin" )
00050 
00051 if DEMO_IS_ACTIVATED:
00052     salome_pluginsmanager.AddFunction('O,Vx,Vy,Vz',
00053                                       'Creates the trihedron',
00054                                       trihedron)
00055 
00056 
00057 # -------------------------------------------------------------------------
00058 # Example 1 bis: creation of basic objects and automatic display
00059 def trihedron_withdisplay(context):
00060     import geompy
00061 
00062     # Intialize the geompy factory with the active study
00063     activeStudy = context.study
00064     geompy.init_geom(activeStudy)
00065 
00066     # Create the objects
00067     Vx = geompy.MakeVectorDXDYDZ(10, 0, 0)
00068     Vy = geompy.MakeVectorDXDYDZ(0, 10, 0)
00069     Vz = geompy.MakeVectorDXDYDZ(0, 0, 10)
00070     origin = geompy.MakeVertex(0, 0, 0)
00071 
00072     # Register the objects in the active study
00073     entries=[]
00074     entries.append(geompy.addToStudy( Vx, "Vx" ))
00075     entries.append(geompy.addToStudy( Vy, "Vy" ))
00076     entries.append(geompy.addToStudy( Vz, "Vz" ))
00077     entries.append(geompy.addToStudy( origin, "origin" ))
00078 
00079     # This part is to automatically display the objects in the active viewer.
00080     gcomp = salome.ImportComponentGUI("GEOM")
00081     for entry in entries:
00082         gcomp.createAndDisplayFitAllGO(entry)
00083 
00084 if DEMO_IS_ACTIVATED:
00085     salome_pluginsmanager.AddFunction('O,Vx,Vy,Vz (2)',
00086                                       'Creates Basic GEOM objects',
00087                                       trihedron_withdisplay)
00088 
00089 # -------------------------------------------------------------------------
00090 # Example 2: creation of a shape with parameters that can be read from a GUI.
00091 # The plugin function (tube_shapewithgui) delegates some action to
00092 # dedicated imported modules (tube.py and tubedialog.py).
00093 #
00094 import tubebuilder
00095 import xalome
00096 
00097 # A single dialog box is defined and recycled for every call. The
00098 # fields are initialized with default values given by the tube factory
00099 # tube.py.
00100 import tubedialog
00101 dialog = tubedialog.TubeDialog()
00102 dialog.setData(tubebuilder.DEFAULT_RADIUS,
00103                tubebuilder.DEFAULT_LENGTH,
00104                tubebuilder.DEFAULT_WIDTH)
00105 
00106 def tube_shapewithgui(context):
00107     global tubebuilder, xalome, dialog
00108     activeStudy = context.study
00109 
00110     # Get the parameter values from a gui dialog box. If the dialog is
00111     # closed using the Ok button, then the data are requested from the
00112     # gui and used to create the shape of the tube.
00113     dialog.exec_()
00114     if dialog.wasOk():
00115         radius, length, width = dialog.getData()
00116         shape = tubebuilder.createGeometry(activeStudy, radius, length, width)
00117         entry = xalome.addToStudy(activeStudy, shape, "Tube" )
00118         xalome.displayShape(entry)
00119 
00120 if DEMO_IS_ACTIVATED:
00121     salome_pluginsmanager.AddFunction('Tube shape from parameters',
00122                                       'Creates a tube object from specified parameters',
00123                                       tube_shapewithgui)
00124 
00125 
00126 # -------------------------------------------------------------------------
00127 # Example 2 bis: creation of a mesh with parameters that can be read from a GUI.
00128 # The plugin function (tube_meshwithgui) delegates some action to
00129 # dedicated imported modules (tubebuilder.py and tubedialog.py).
00130 #
00131 def tube_meshwithgui(context):
00132     global tube, dialog
00133     activeStudy = context.study
00134 
00135     # Get the parameter values from a gui dialog box. If the dialog is
00136     # closed using the Ok button, then the data are requested from the
00137     # gui and used to create the shape of the tube.
00138     dialog.exec_()
00139     if dialog.wasOk():
00140         radius, length, width = dialog.getData()
00141         mesh = tubebuilder.createModel(activeStudy, radius, length, width)
00142 
00143 if DEMO_IS_ACTIVATED:
00144     salome_pluginsmanager.AddFunction('Tube mesh from parameters',
00145                                       'Creates a tube object from specified parameters',
00146                                       tube_meshwithgui)
00147 
00148 
00149 # -------------------------------------------------------------------------
00150 # Example 2 ter: creation of a geom object with a preview function in
00151 # the dialog box. This use case is more complex from the gui point of
00152 # view because the dialog box is a not modal so that we can have
00153 # interaction with the complete SALOME application. This modal
00154 # situation requires to connect button click signal on global
00155 # functions as a "callback" mechanism.
00156 #
00157 # TODO:
00158 # - coloring the preview in pink
00159 # - store the tmp study objects in a "preview" folder
00160 #
00161 dialogWithApply = tubedialog.TubeDialogOnTopWithApply()
00162 dialogWithApply.setData(tubebuilder.DEFAULT_RADIUS,
00163                         tubebuilder.DEFAULT_LENGTH,
00164                         tubebuilder.DEFAULT_WIDTH)
00165 activeStudy = None
00166 previewShapeEntry = None
00167 
00168 DEFAULT_FOLDER_NAME="TubeList"
00169 DEFAULT_SHAPE_NAME="tube"
00170 PREVIEW_SHAPE_NAME="preview"
00171 
00172 def acceptCallback():
00173     """Action to be done when click on Ok"""
00174     global tubebuilder, xalome
00175     global dialogWithApply, activeStudy
00176     global previewShapeEntry, deletePreviewShape
00177     global DEFAULT_FOLDER_NAME,DEFAULT_SHAPE_NAME 
00178 
00179     dialogWithApply.accept()
00180 
00181     if previewShapeEntry is not None:
00182         deletePreviewShape()
00183 
00184     radius, length, width = dialogWithApply.getData()
00185     shape = tubebuilder.createGeometry(activeStudy, radius, length, width)
00186     entry = xalome.addToStudy(activeStudy, shape, DEFAULT_SHAPE_NAME, DEFAULT_FOLDER_NAME)
00187     xalome.displayShape(entry)
00188     
00189 def rejectCallback():
00190     """Action to be done when click on Cancel"""
00191     global dialogWithApply, previewShapeEntry, deletePreviewShape
00192 
00193     dialogWithApply.reject()
00194 
00195     if previewShapeEntry is not None:
00196         deletePreviewShape()
00197 
00198 import SALOMEDS
00199 PREVIEW_COLOR=SALOMEDS.Color(1,0.6,1) # pink
00200 
00201 def applyCallback():
00202     """Action to be done when click on Apply"""
00203     global tubebuilder, xalome
00204     global dialogWithApply, activeStudy
00205     global previewShapeEntry, deletePreviewShape
00206     global PREVIEW_COLOR, DEFAULT_SHAPE_NAME, DEFAULT_FOLDER_NAME, PREVIEW_SHAPE_NAME
00207 
00208     # We first have to destroy the currently displayed preview shape.
00209     if previewShapeEntry is not None:
00210         deletePreviewShape()
00211 
00212     # Then we can create the new shape with the new parameter values
00213     radius, length, width = dialogWithApply.getData()
00214     shape = tubebuilder.createGeometry(activeStudy, radius, length, width)
00215     # We apply a specific color on the shape for the preview state
00216     shape.SetColor(PREVIEW_COLOR)
00217     previewShapeEntry = xalome.addToStudy(activeStudy, shape, PREVIEW_SHAPE_NAME, DEFAULT_FOLDER_NAME )
00218     xalome.displayShape(previewShapeEntry)
00219 
00220 def deletePreviewShape():
00221     """This delete the shape currently being displayed as a preview"""
00222     global activeStudy, previewShapeEntry, xsalome
00223     xalome.deleteShape(activeStudy,previewShapeEntry)
00224     previewShapeEntry = None
00225 
00226 # Connection of callback functions to the dialog butoon click signals
00227 dialogWithApply.handleAcceptWith(acceptCallback)
00228 dialogWithApply.handleRejectWith(rejectCallback)
00229 dialogWithApply.handleApplyWith(applyCallback)
00230 
00231 def tube_shapewithguiAndPreview(context):
00232     """
00233     This plugin open a dialog in an asynchronous mode. Then it
00234     required callback functions to be associated to the button
00235     signals.
00236     """
00237     global dialogWithApply, activeStudy
00238     activeStudy = context.study
00239     dialogWithApply.open()
00240 
00241 if DEMO_IS_ACTIVATED:
00242     salome_pluginsmanager.AddFunction('Tube geometry from parameters with preview',
00243                                       'Creates a tube object from specified parameters',
00244                                       tube_shapewithguiAndPreview)
00245 
00246 
00247 
00248 # -------------------------------------------------------------------------
00249 # Example 3: run a shell session in a xterm to get a SALOME python console
00250 def runSalomeShellSession(context):
00251     import os
00252     command="(xterm -e "+os.environ['KERNEL_ROOT_DIR']+"/runSession)&"
00253     os.system(command)
00254 
00255 if DEMO_IS_ACTIVATED:
00256     salome_pluginsmanager.AddFunction('SALOME shell session',
00257                                       'Execute a SALOME shell session in an external xterm',
00258                                       runSalomeShellSession)