Salome HOME
Update copyrights 2014.
[modules/gui.git] / src / SalomeApp / pluginsdemo / xalome.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2014  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(study,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     studyId = study._get_StudyId()
49     geompy = geomtools.getGeompy(studyId)
50
51     if folderName is None:
52         # Insert the shape in the study by the standard way
53         entry = geompy.addToStudy( shape, shapeName )
54     else:
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)
61
62         shapeIor = salome.orb.object_to_string(shape)
63         geomgui = salome.ImportComponentGUI("GEOM")
64         shapeIcon = geomgui.getShapeTypeIcon(shapeIor)
65
66         shapeStudyObject = studyEditor.createItem(shapeStudyFolder,
67                                                   name=shapeName,
68                                                   IOR=shapeIor,
69                                                   icon=shapeIcon)
70         entry = shapeStudyObject.GetID()
71
72     return entry
73
74 def removeFromStudy(study,shapeStudyEntry):
75     """
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)
80     """
81     studyId = study._get_StudyId()
82     shape = IDToObject(shapeStudyEntry)    
83     studyObject = IDToSObject(shapeStudyEntry)
84     studyEditor = getStudyEditor(studyId)
85     studyEditor.removeItem(studyObject,True)
86     return shape
87
88
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
92 # be known.
93 # ======================================================================
94
95 ModeShading = 1
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])
105
106 def eraseShape(shapeStudyEntry):
107     """
108     This erases from the viewers the shape specified by its study
109     entry.
110     """
111     geomgui = salome.ImportComponentGUI("GEOM")
112     eraseFromAllWindows=True
113     geomgui.eraseGO(shapeStudyEntry,eraseFromAllWindows)
114
115 # Available in SALOME 6.5 only
116 def displayShape_version65(shapeStudyEntry):
117     gst = geomtools.GeomStudyTools()
118     gst.displayShapeByEntry(shapeStudyEntry)
119
120 def eraseShape_version65(shapeStudyEntry):
121     gst = geomtools.GeomStudyTools()
122     gst.eraseShapeByEntry(shapeStudyEntry)
123
124
125 # ======================================================================
126 # Helper functions for a complete suppression of a shape from the
127 # SALOME session.
128 # ======================================================================
129 def deleteShape(study,shapeStudyEntry):
130     """
131     This completly deletes a geom shape.
132
133     WARNING: please be aware that to delete a geom object, you have
134     three operations to perform:
135
136     1. erase the shape from the viewers
137     2. remove the entry from the study
138     3. destroy the underlying geom object
139     """
140     eraseShape(shapeStudyEntry)
141     shape = removeFromStudy(study, shapeStudyEntry)
142     if shape is not None:
143       shape.Destroy()
144     
145
146 #
147 # ======================================================================
148 # Unit tests
149 # ======================================================================
150 #
151 # To experiment this unit test, just execute this script in
152 # SALOME. The script is self-consistent.
153
154 def TEST_createAndDeleteShape():
155     """
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
161     object).
162     """
163     import salome
164     salome.salome_init()
165     study   = salome.myStudy
166     studyId = salome.myStudyId
167
168     from salome.geom import geomtools
169     geompy = geomtools.getGeompy(studyId)
170
171     # --------------------------------------------------
172     # Create a first shape (GEOM object)
173     radius = 5
174     length = 100
175     cylinder = geompy.MakeCylinderRH(radius, length)
176
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)
182
183     # Display the registered shape in a viewer
184     displayShape(cylinderStudyEntry)
185
186     # --------------------------------------------------
187     # A second shape
188     radius = 10
189     sphere = geompy.MakeSphereR(radius)
190     sphereName = "sph.r%s"%radius
191     sphereStudyEntry = addToStudy(study, sphere, sphereName)
192     displayShape(sphereStudyEntry)
193
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
197     # color.
198     length = 20
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)
204
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)
211
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
216     # deleteShape().
217     deleteShape(study,cylinderStudyEntry)
218
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 
224
225     # If you comment the deleteShape line, you should see the cylinder
226     # in the study and displayed in the viewer. 
227
228 if __name__=="__main__":
229     TEST_createAndDeleteShape()