1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2023 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(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 geompy = geomtools.getGeompy()
50 if folderName is None:
51 # Insert the shape in the study by the standard way
52 entry = geompy.addToStudy( shape, shapeName )
54 # A folder name has been specified to embed this shape. Find
55 # or create a folder with this name in the Geometry study, and
56 # then store the shape in this folder.
57 studyEditor = getStudyEditor()
58 geomStudyFolder = studyEditor.findOrCreateComponent("GEOM")
59 shapeStudyFolder = studyEditor.findOrCreateItem(geomStudyFolder,folderName)
61 shapeIor = salome.orb.object_to_string(shape)
62 geomgui = salome.ImportComponentGUI("GEOM")
63 shapeIcon = geomgui.getShapeTypeIcon(shapeIor)
65 shapeStudyObject = studyEditor.createItem(shapeStudyFolder,
69 entry = shapeStudyObject.GetID()
73 def removeFromStudy(shapeStudyEntry):
75 This removes the specified entry from the study. Note that this
76 operation does not destroy the underlying GEOM object, neither
77 erase the drawing in the viewer.
78 The underlying GEOM object is returned (so that it can be destroyed)
80 shape = IDToObject(shapeStudyEntry)
81 studyObject = IDToSObject(shapeStudyEntry)
82 studyEditor = getStudyEditor()
83 studyEditor.removeItem(studyObject,True)
87 # ======================================================================
88 # Helper functions to display/erase a shape in/from the viewer. The
89 # shape must be previously put in the study and the study entry must
91 # ======================================================================
94 DisplayMode=ModeShading
95 PreviewColor=[236,163,255]
96 def displayShape(shapeStudyEntry, color=None):
97 """This displays the shape specified by its entry in the study"""
98 geomgui = salome.ImportComponentGUI("GEOM")
99 geomgui.createAndDisplayFitAllGO(shapeStudyEntry)
100 geomgui.setDisplayMode(shapeStudyEntry, DisplayMode)
101 if color is not None:
102 geomgui.setColor(shapeStudyEntry, color[0], color[1], color[2])
104 def eraseShape(shapeStudyEntry):
106 This erases from the viewers the shape specified by its study
109 geomgui = salome.ImportComponentGUI("GEOM")
110 eraseFromAllWindows=True
111 geomgui.eraseGO(shapeStudyEntry,eraseFromAllWindows)
113 # Available in SALOME 6.5 only
114 def displayShape_version65(shapeStudyEntry):
115 gst = geomtools.GeomStudyTools()
116 gst.displayShapeByEntry(shapeStudyEntry)
118 def eraseShape_version65(shapeStudyEntry):
119 gst = geomtools.GeomStudyTools()
120 gst.eraseShapeByEntry(shapeStudyEntry)
123 # ======================================================================
124 # Helper functions for a complete suppression of a shape from the
126 # ======================================================================
127 def deleteShape(shapeStudyEntry):
129 This completly deletes a geom shape.
131 WARNING: please be aware that to delete a geom object, you have
132 three operations to perform:
134 1. erase the shape from the viewers
135 2. remove the entry from the study
136 3. destroy the underlying geom object
138 eraseShape(shapeStudyEntry)
139 shape = removeFromStudy(shapeStudyEntry)
140 if shape is not None:
145 # ======================================================================
147 # ======================================================================
149 # To experiment this unit test, just execute this script in
150 # SALOME. The script is self-consistent.
152 def TEST_createAndDeleteShape():
154 This test is a simple use case that illustrates how to create a
155 GEOM shape in a SALOME session (create the GEOM object, put in in
156 the study, and display the shape in a viewer) and delete a shape
157 from a SALOME session (erase the shape from the viewer, delete the
158 entry from the study, and finally destroy the underlying GEOM
163 study = salome.myStudy
165 from salome.geom import geomtools
166 geompy = geomtools.getGeompy()
168 # --------------------------------------------------
169 # Create a first shape (GEOM object)
172 cylinder = geompy.MakeCylinderRH(radius, length)
174 # Register the shape in the study, at the root of the GEOM
175 # folder. A name must be specified. The register operation
176 # (addToStudy) returns an identifier of the entry in the study.
177 cylinderName = "cyl.r%s.l%s"%(radius,length)
178 cylinderStudyEntry = addToStudy(cylinder, cylinderName)
180 # Display the registered shape in a viewer
181 displayShape(cylinderStudyEntry)
183 # --------------------------------------------------
186 sphere = geompy.MakeSphereR(radius)
187 sphereName = "sph.r%s"%radius
188 sphereStudyEntry = addToStudy(sphere, sphereName)
189 displayShape(sphereStudyEntry)
191 # --------------------------------------------------
192 # This new shape is stored in the study, but in a specific
193 # sub-folder, and is displayed in the viewer with a specific
196 box = geompy.MakeBoxDXDYDZ(length,length,length)
197 boxName = "box.l%s"%length
198 folderName = "boxset"
199 boxStudyEntry = addToStudy(box, boxName, folderName)
200 displayShape(boxStudyEntry,PreviewColor)
202 # --------------------------------------------------
203 # In this example, we illustrate how to erase a shape (the sphere)
204 # from the viewer. After this operation, the sphere is no longer
205 # displayed but still exists in the study. You can then redisplay
206 # it using the context menu of the SALOME object browser.
207 eraseShape(sphereStudyEntry)
209 # --------------------------------------------------
210 # In this last example, we completly delete an object from the
211 # SALOME session (erase from viewer, remove from study and finnaly
212 # destroy the object). This is done by a simple call to
214 deleteShape(cylinderStudyEntry)
216 # --------------------------------------------------
217 # At the end of the executioon of this test, you should have in
218 # the SALOME session:
219 # - the box, in a dedicated folder of the study, and displayed in the viewer
220 # - the sphere, in the standard place of the study, and not displayed
222 # If you comment the deleteShape line, you should see the cylinder
223 # in the study and displayed in the viewer.
225 if __name__=="__main__":
226 TEST_createAndDeleteShape()