Back to index

salome-gui  6.5.0
xalome.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 #
00023 # XALOME means "eXtension for sALOME. This module contains specific
00024 # helper functions that extends salome for the needs of the salome
00025 # plugin examples, or hide some complex technical parts of SALOME to
00026 # ease the global understanding of the examples.
00027 #
00028 # (gboulant - 09/02/2012)
00029 #
00030 import salome
00031 from salome.kernel.studyedit import getStudyEditor
00032 from salome.kernel.services import IDToSObject, IDToObject
00033 from salome.geom import geomtools
00034 
00035 # ======================================================================
00036 # Helper functions to add/remove a geometrical shape in/from the study
00037 # ======================================================================
00038 
00039 def addToStudy(study,shape,shapeName,folderName=None):
00040     """
00041     Add a GEOM shape in the study. It returns the associated entry
00042     that corresponds to the identifier of the entry in the study. This
00043     entry can be used to retrieve an object in the study. A folderName
00044     can be specified. In this case, a folder with this name is first
00045     created in the Geometry part of the study, and the shape study
00046     object is stored in this folder of the study. 
00047     """
00048     studyId = study._get_StudyId()
00049     geompy = geomtools.getGeompy(studyId)
00050 
00051     if folderName is None:
00052         # Insert the shape in the study by the standard way
00053         entry = geompy.addToStudy( shape, shapeName )
00054     else:
00055         # A folder name has been specified to embed this shape. Find
00056         # or create a folder with this name in the Geometry study, and
00057         # then store the shape in this folder.
00058         studyEditor = getStudyEditor(studyId)
00059         geomStudyFolder = studyEditor.findOrCreateComponent("GEOM")
00060         shapeStudyFolder = studyEditor.findOrCreateItem(geomStudyFolder,folderName)
00061 
00062         shapeIor = salome.orb.object_to_string(shape)
00063         geomgui = salome.ImportComponentGUI("GEOM")
00064         shapeIcon = geomgui.getShapeTypeIcon(shapeIor)
00065 
00066         shapeStudyObject = studyEditor.createItem(shapeStudyFolder,
00067                                                   name=shapeName,
00068                                                   IOR=shapeIor,
00069                                                   icon=shapeIcon)
00070         entry = shapeStudyObject.GetID()
00071 
00072     return entry
00073 
00074 def removeFromStudy(study,shapeStudyEntry):
00075     """
00076     This removes the specified entry from the study. Note that this
00077     operation does not destroy the underlying GEOM object, neither
00078     erase the drawing in the viewer.
00079     The underlying GEOM object is returned (so that it can be destroyed)
00080     """
00081     studyId = study._get_StudyId()
00082     shape = IDToObject(shapeStudyEntry)    
00083     studyObject = IDToSObject(shapeStudyEntry)
00084     studyEditor = getStudyEditor(studyId)
00085     studyEditor.removeItem(studyObject,True)
00086     return shape
00087 
00088 
00089 # ======================================================================
00090 # Helper functions to display/erase a shape in/from the viewer. The
00091 # shape must be previously put in the study and the study entry must
00092 # be known.
00093 # ======================================================================
00094 
00095 ModeShading = 1
00096 DisplayMode=ModeShading
00097 PreviewColor=[236,163,255]
00098 def displayShape(shapeStudyEntry, color=None):
00099     """This displays the shape specified by its entry in the study"""
00100     geomgui = salome.ImportComponentGUI("GEOM")            
00101     geomgui.createAndDisplayFitAllGO(shapeStudyEntry)
00102     geomgui.setDisplayMode(shapeStudyEntry, DisplayMode)
00103     if color is not None:
00104         geomgui.setColor(shapeStudyEntry, color[0], color[1], color[2])
00105 
00106 def eraseShape(shapeStudyEntry):
00107     """
00108     This erases from the viewers the shape specified by its study
00109     entry.
00110     """
00111     geomgui = salome.ImportComponentGUI("GEOM")
00112     eraseFromAllWindows=True
00113     geomgui.eraseGO(shapeStudyEntry,eraseFromAllWindows)
00114 
00115 # Available in SALOME 6.5 only
00116 def displayShape_version65(shapeStudyEntry):
00117     gst = geomtools.GeomStudyTools()
00118     gst.displayShapeByEntry(shapeStudyEntry)
00119 
00120 def eraseShape_version65(shapeStudyEntry):
00121     gst = geomtools.GeomStudyTools()
00122     gst.eraseShapeByEntry(shapeStudyEntry)
00123 
00124 
00125 # ======================================================================
00126 # Helper functions for a complete suppression of a shape from the
00127 # SALOME session.
00128 # ======================================================================
00129 def deleteShape(study,shapeStudyEntry):
00130     """
00131     This completly deletes a geom shape.
00132 
00133     WARNING: please be aware that to delete a geom object, you have
00134     three operations to perform:
00135 
00136     1. erase the shape from the viewers
00137     2. remove the entry from the study
00138     3. destroy the underlying geom object
00139     """
00140     eraseShape(shapeStudyEntry)
00141     shape = removeFromStudy(study, shapeStudyEntry)
00142     shape.Destroy()
00143     
00144 
00145 #
00146 # ======================================================================
00147 # Unit tests
00148 # ======================================================================
00149 #
00150 # To experiment this unit test, just execute this script in
00151 # SALOME. The script is self-consistent.
00152 
00153 def TEST_createAndDeleteShape():
00154     """
00155     This test is a simple use case that illustrates how to create a
00156     GEOM shape in a SALOME session (create the GEOM object, put in in
00157     the study, and display the shape in a viewer) and delete a shape
00158     from a SALOME session (erase the shape from the viewer, delete the
00159     entry from the study, and finally destroy the underlying GEOM
00160     object).
00161     """
00162     import salome
00163     salome.salome_init()
00164     study   = salome.myStudy
00165     studyId = salome.myStudyId
00166 
00167     from salome.geom import geomtools
00168     geompy = geomtools.getGeompy(studyId)
00169 
00170     # --------------------------------------------------
00171     # Create a first shape (GEOM object)
00172     radius = 5
00173     length = 100
00174     cylinder = geompy.MakeCylinderRH(radius, length)
00175 
00176     # Register the shape in the study, at the root of the GEOM
00177     # folder. A name must be specified. The register operation
00178     # (addToStudy) returns an identifier of the entry in the study.
00179     cylinderName = "cyl.r%s.l%s"%(radius,length)
00180     cylinderStudyEntry = addToStudy(study, cylinder, cylinderName)
00181 
00182     # Display the registered shape in a viewer
00183     displayShape(cylinderStudyEntry)
00184 
00185     # --------------------------------------------------
00186     # A second shape
00187     radius = 10
00188     sphere = geompy.MakeSphereR(radius)
00189     sphereName = "sph.r%s"%radius
00190     sphereStudyEntry = addToStudy(study, sphere, sphereName)
00191     displayShape(sphereStudyEntry)
00192 
00193     # --------------------------------------------------
00194     # This new shape is stored in the study, but in a specific
00195     # sub-folder, and is displayed in the viewer with a specific
00196     # color.
00197     length = 20
00198     box = geompy.MakeBoxDXDYDZ(length,length,length)
00199     boxName = "box.l%s"%length
00200     folderName = "boxset" 
00201     boxStudyEntry = addToStudy(study, box, boxName, folderName)
00202     displayShape(boxStudyEntry,PreviewColor)
00203 
00204     # --------------------------------------------------
00205     # In this example, we illustrate how to erase a shape (the sphere)
00206     # from the viewer. After this operation, the sphere is no longer
00207     # displayed but still exists in the study. You can then redisplay
00208     # it using the context menu of the SALOME object browser.
00209     eraseShape(sphereStudyEntry)
00210 
00211     # --------------------------------------------------
00212     # In this last example, we completly delete an object from the
00213     # SALOME session (erase from viewer, remove from study and finnaly
00214     # destroy the object). This is done by a simple call to
00215     # deleteShape().
00216     deleteShape(study,cylinderStudyEntry)
00217 
00218     # --------------------------------------------------
00219     # At the end of the executioon of this test, you should have in
00220     # the SALOME session:
00221     # - the box, in a dedicated folder of the study, and displayed in the viewer
00222     # - the sphere, in the standard place of the study, and not displayed 
00223 
00224     # If you comment the deleteShape line, you should see the cylinder
00225     # in the study and displayed in the viewer. 
00226 
00227 if __name__=="__main__":
00228     TEST_createAndDeleteShape()