Salome HOME
27db7285e41d6026e445ea66b812cf3194360e8e
[modules/gui.git] / src / SalomeApp / pluginsdemo / xalome.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2022  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Guillaume Boulant (EDF)
21
22 #
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.
27 #
28 # (gboulant - 09/02/2012)
29 #
30 import salome
31 from salome.kernel.studyedit import getStudyEditor
32 from salome.kernel.services import IDToSObject, IDToObject
33 from salome.geom import geomtools
34
35 # ======================================================================
36 # Helper functions to add/remove a geometrical shape in/from the study
37 # ======================================================================
38
39 def addToStudy(shape,shapeName,folderName=None):
40     """
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. 
47     """
48     geompy = geomtools.getGeompy()
49
50     if folderName is None:
51         # Insert the shape in the study by the standard way
52         entry = geompy.addToStudy( shape, shapeName )
53     else:
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)
60
61         shapeIor = salome.orb.object_to_string(shape)
62         geomgui = salome.ImportComponentGUI("GEOM")
63         shapeIcon = geomgui.getShapeTypeIcon(shapeIor)
64
65         shapeStudyObject = studyEditor.createItem(shapeStudyFolder,
66                                                   name=shapeName,
67                                                   IOR=shapeIor,
68                                                   icon=shapeIcon)
69         entry = shapeStudyObject.GetID()
70
71     return entry
72
73 def removeFromStudy(shapeStudyEntry):
74     """
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)
79     """
80     shape = IDToObject(shapeStudyEntry)    
81     studyObject = IDToSObject(shapeStudyEntry)
82     studyEditor = getStudyEditor()
83     studyEditor.removeItem(studyObject,True)
84     return shape
85
86
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
90 # be known.
91 # ======================================================================
92
93 ModeShading = 1
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])
103
104 def eraseShape(shapeStudyEntry):
105     """
106     This erases from the viewers the shape specified by its study
107     entry.
108     """
109     geomgui = salome.ImportComponentGUI("GEOM")
110     eraseFromAllWindows=True
111     geomgui.eraseGO(shapeStudyEntry,eraseFromAllWindows)
112
113 # Available in SALOME 6.5 only
114 def displayShape_version65(shapeStudyEntry):
115     gst = geomtools.GeomStudyTools()
116     gst.displayShapeByEntry(shapeStudyEntry)
117
118 def eraseShape_version65(shapeStudyEntry):
119     gst = geomtools.GeomStudyTools()
120     gst.eraseShapeByEntry(shapeStudyEntry)
121
122
123 # ======================================================================
124 # Helper functions for a complete suppression of a shape from the
125 # SALOME session.
126 # ======================================================================
127 def deleteShape(shapeStudyEntry):
128     """
129     This completly deletes a geom shape.
130
131     WARNING: please be aware that to delete a geom object, you have
132     three operations to perform:
133
134     1. erase the shape from the viewers
135     2. remove the entry from the study
136     3. destroy the underlying geom object
137     """
138     eraseShape(shapeStudyEntry)
139     shape = removeFromStudy(shapeStudyEntry)
140     if shape is not None:
141       shape.Destroy()
142     
143
144 #
145 # ======================================================================
146 # Unit tests
147 # ======================================================================
148 #
149 # To experiment this unit test, just execute this script in
150 # SALOME. The script is self-consistent.
151
152 def TEST_createAndDeleteShape():
153     """
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
159     object).
160     """
161     import salome
162     salome.salome_init()
163     study = salome.myStudy
164
165     from salome.geom import geomtools
166     geompy = geomtools.getGeompy()
167
168     # --------------------------------------------------
169     # Create a first shape (GEOM object)
170     radius = 5
171     length = 100
172     cylinder = geompy.MakeCylinderRH(radius, length)
173
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)
179
180     # Display the registered shape in a viewer
181     displayShape(cylinderStudyEntry)
182
183     # --------------------------------------------------
184     # A second shape
185     radius = 10
186     sphere = geompy.MakeSphereR(radius)
187     sphereName = "sph.r%s"%radius
188     sphereStudyEntry = addToStudy(sphere, sphereName)
189     displayShape(sphereStudyEntry)
190
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
194     # color.
195     length = 20
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)
201
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)
208
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
213     # deleteShape().
214     deleteShape(cylinderStudyEntry)
215
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 
221
222     # If you comment the deleteShape line, you should see the cylinder
223     # in the study and displayed in the viewer. 
224
225 if __name__=="__main__":
226     TEST_createAndDeleteShape()