1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2014 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)
23 # XALOME means "eXtension for sALOME. This module contains specific
24 # helper functions that extends salome for the needs of the salome
25 # plugin examples, or hide some complex technical parts of SALOME to
26 # ease the global understanding of the examples.
28 # (gboulant - 09/02/2012)
31 from salome.kernel.studyedit import getStudyEditor
32 from salome.kernel.services import IDToSObject, IDToObject
33 from salome.geom import geomtools
35 # ======================================================================
36 # Helper functions to add/remove a geometrical shape in/from the study
37 # ======================================================================
39 def addToStudy(study,shape,shapeName,folderName=None):
41 Add a GEOM shape in the study. It returns the associated entry
42 that corresponds to the identifier of the entry in the study. This
43 entry can be used to retrieve an object in the study. A folderName
44 can be specified. In this case, a folder with this name is first
45 created in the Geometry part of the study, and the shape study
46 object is stored in this folder of the study.
48 studyId = study._get_StudyId()
49 geompy = geomtools.getGeompy(studyId)
51 if folderName is None:
52 # Insert the shape in the study by the standard way
53 entry = geompy.addToStudy( shape, shapeName )
55 # A folder name has been specified to embed this shape. Find
56 # or create a folder with this name in the Geometry study, and
57 # then store the shape in this folder.
58 studyEditor = getStudyEditor(studyId)
59 geomStudyFolder = studyEditor.findOrCreateComponent("GEOM")
60 shapeStudyFolder = studyEditor.findOrCreateItem(geomStudyFolder,folderName)
62 shapeIor = salome.orb.object_to_string(shape)
63 geomgui = salome.ImportComponentGUI("GEOM")
64 shapeIcon = geomgui.getShapeTypeIcon(shapeIor)
66 shapeStudyObject = studyEditor.createItem(shapeStudyFolder,
70 entry = shapeStudyObject.GetID()
74 def removeFromStudy(study,shapeStudyEntry):
76 This removes the specified entry from the study. Note that this
77 operation does not destroy the underlying GEOM object, neither
78 erase the drawing in the viewer.
79 The underlying GEOM object is returned (so that it can be destroyed)
81 studyId = study._get_StudyId()
82 shape = IDToObject(shapeStudyEntry)
83 studyObject = IDToSObject(shapeStudyEntry)
84 studyEditor = getStudyEditor(studyId)
85 studyEditor.removeItem(studyObject,True)
89 # ======================================================================
90 # Helper functions to display/erase a shape in/from the viewer. The
91 # shape must be previously put in the study and the study entry must
93 # ======================================================================
96 DisplayMode=ModeShading
97 PreviewColor=[236,163,255]
98 def displayShape(shapeStudyEntry, color=None):
99 """This displays the shape specified by its entry in the study"""
100 geomgui = salome.ImportComponentGUI("GEOM")
101 geomgui.createAndDisplayFitAllGO(shapeStudyEntry)
102 geomgui.setDisplayMode(shapeStudyEntry, DisplayMode)
103 if color is not None:
104 geomgui.setColor(shapeStudyEntry, color[0], color[1], color[2])
106 def eraseShape(shapeStudyEntry):
108 This erases from the viewers the shape specified by its study
111 geomgui = salome.ImportComponentGUI("GEOM")
112 eraseFromAllWindows=True
113 geomgui.eraseGO(shapeStudyEntry,eraseFromAllWindows)
115 # Available in SALOME 6.5 only
116 def displayShape_version65(shapeStudyEntry):
117 gst = geomtools.GeomStudyTools()
118 gst.displayShapeByEntry(shapeStudyEntry)
120 def eraseShape_version65(shapeStudyEntry):
121 gst = geomtools.GeomStudyTools()
122 gst.eraseShapeByEntry(shapeStudyEntry)
125 # ======================================================================
126 # Helper functions for a complete suppression of a shape from the
128 # ======================================================================
129 def deleteShape(study,shapeStudyEntry):
131 This completly deletes a geom shape.
133 WARNING: please be aware that to delete a geom object, you have
134 three operations to perform:
136 1. erase the shape from the viewers
137 2. remove the entry from the study
138 3. destroy the underlying geom object
140 eraseShape(shapeStudyEntry)
141 shape = removeFromStudy(study, shapeStudyEntry)
142 if shape is not None:
147 # ======================================================================
149 # ======================================================================
151 # To experiment this unit test, just execute this script in
152 # SALOME. The script is self-consistent.
154 def TEST_createAndDeleteShape():
156 This test is a simple use case that illustrates how to create a
157 GEOM shape in a SALOME session (create the GEOM object, put in in
158 the study, and display the shape in a viewer) and delete a shape
159 from a SALOME session (erase the shape from the viewer, delete the
160 entry from the study, and finally destroy the underlying GEOM
165 study = salome.myStudy
166 studyId = salome.myStudyId
168 from salome.geom import geomtools
169 geompy = geomtools.getGeompy(studyId)
171 # --------------------------------------------------
172 # Create a first shape (GEOM object)
175 cylinder = geompy.MakeCylinderRH(radius, length)
177 # Register the shape in the study, at the root of the GEOM
178 # folder. A name must be specified. The register operation
179 # (addToStudy) returns an identifier of the entry in the study.
180 cylinderName = "cyl.r%s.l%s"%(radius,length)
181 cylinderStudyEntry = addToStudy(study, cylinder, cylinderName)
183 # Display the registered shape in a viewer
184 displayShape(cylinderStudyEntry)
186 # --------------------------------------------------
189 sphere = geompy.MakeSphereR(radius)
190 sphereName = "sph.r%s"%radius
191 sphereStudyEntry = addToStudy(study, sphere, sphereName)
192 displayShape(sphereStudyEntry)
194 # --------------------------------------------------
195 # This new shape is stored in the study, but in a specific
196 # sub-folder, and is displayed in the viewer with a specific
199 box = geompy.MakeBoxDXDYDZ(length,length,length)
200 boxName = "box.l%s"%length
201 folderName = "boxset"
202 boxStudyEntry = addToStudy(study, box, boxName, folderName)
203 displayShape(boxStudyEntry,PreviewColor)
205 # --------------------------------------------------
206 # In this example, we illustrate how to erase a shape (the sphere)
207 # from the viewer. After this operation, the sphere is no longer
208 # displayed but still exists in the study. You can then redisplay
209 # it using the context menu of the SALOME object browser.
210 eraseShape(sphereStudyEntry)
212 # --------------------------------------------------
213 # In this last example, we completly delete an object from the
214 # SALOME session (erase from viewer, remove from study and finnaly
215 # destroy the object). This is done by a simple call to
217 deleteShape(study,cylinderStudyEntry)
219 # --------------------------------------------------
220 # At the end of the executioon of this test, you should have in
221 # the SALOME session:
222 # - the box, in a dedicated folder of the study, and displayed in the viewer
223 # - the sphere, in the standard place of the study, and not displayed
225 # If you comment the deleteShape line, you should see the cylinder
226 # in the study and displayed in the viewer.
228 if __name__=="__main__":
229 TEST_createAndDeleteShape()