Salome HOME
0022523: [CEA 1096] Add a colomn "biquadratic" in "Mesh Information"
[modules/smesh.git] / src / SMESH_PY / smeshstudytools.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 ## \package smeshstudytools Python API to access SMESH objects in the study.
23
24 ## \defgroup smeshstudytools Accessing SMESH object in the study
25 #  \{ 
26 #  \details
27 #  Module \b smeshstudytools provides a new class SMeshStudyTools to facilitate the
28 #  use of mesh objects in Salome study.
29 #  \}
30
31 """
32 This module provides a new class :class:`SMeshStudyTools` to facilitate the
33 use of mesh objects in Salome study.
34 """
35
36 import salome
37 SMESH = None    # SMESH module is loaded only when needed
38
39 from salome.kernel.studyedit import getStudyEditor
40 from salome.kernel.deprecation import is_called_by_sphinx
41 if not is_called_by_sphinx():
42   from salome.gui import helper
43
44 ## This class provides several methods to manipulate mesh objects in Salome
45 #  study. The parameter \em studyEditor defines a \b StudyEditor
46 #  object used to access the study. If \b None, the method returns a 
47 #  \b StudyEditor object on the current study.
48 #
49 #  \b editor
50 #  This instance attribute contains the underlying \b StudyEditor object. 
51 #  It can be used to access the study but the attribute itself should not be modified.
52 #  \ingroup smeshstudytools
53 class SMeshStudyTools:
54     """
55     This class provides several methods to manipulate mesh objects in Salome
56     study. The parameter `studyEditor` defines a
57     :class:`~salome.kernel.studyedit.StudyEditor` object used to access the study. If
58     :const:`None`, the method returns a :class:`~salome.kernel.studyedit.StudyEditor`
59     object on the current study.
60
61     .. attribute:: editor
62     
63        This instance attribute contains the underlying
64        :class:`~salome.kernel.studyedit.StudyEditor` object. It can be used to access
65        the study but the attribute itself should not be modified.
66
67     """
68
69     def __init__(self, studyEditor = None):
70         global SMESH
71         if SMESH is None:
72             SMESH = __import__("SMESH")
73         if studyEditor is None:
74             studyEditor = getStudyEditor()
75         self.editor = studyEditor
76         self.smeshGui = None
77
78     ## This function updates the tools so that it works on the
79     #  specified study.
80     def updateStudy(self, studyId=None):
81         """
82         This function updates the tools so that it works on the
83         specified study.
84         """
85         self.editor = getStudyEditor(studyId)
86
87     ## Get the mesh item owning the mesh group \em meshGroupItem.
88     #  \param  meshGroupItem (SObject) mesh group belonging to the searched mesh.
89     #  \return The SObject corresponding to the mesh, or None if it was not found.        
90     def getMeshFromGroup(self, meshGroupItem):
91         """
92         Get the mesh item owning the mesh group `meshGroupItem`.
93
94         :type   meshGroupItem: SObject
95         :param  meshGroupItem: Mesh group belonging to the searched mesh.
96         
97         :return: The SObject corresponding to the mesh, or None if it was not
98                  found.
99         """
100         meshItem = None
101         obj = self.editor.getOrLoadObject(meshGroupItem)
102         group = obj._narrow(SMESH.SMESH_GroupBase)
103         if group is not None: # The type of the object is ok
104             meshObj = group.GetMesh()
105             meshItem = salome.ObjectToSObject(meshObj)
106         return meshItem
107
108     ## Returns the MESH object currently selected in the active study.
109     def getMeshObjectSelected(self):
110         """
111         Returns the MESH object currently selected in the active study.
112         """
113         sobject, entry = helper.getSObjectSelected()
114         meshObject = self.getMeshObjectFromEntry(entry)
115         return meshObject
116
117     ## Returns the MESH object associated to the specified entry,
118     #  (the entry is the identifier of an item in the objects browser).
119     def getMeshObjectFromEntry(self, entry):
120         """
121         Returns the MESH object associated to the specified entry,
122         (the entry is the identifier of an item in the objects browser).
123         """
124         if entry is None:
125             return None
126         import SMESH
127         from salome.smesh import smeshBuilder
128         smesh = smeshBuilder.New(self.editor.study)
129
130         meshObject=smesh.IDToObject(entry)
131         return meshObject
132     
133     ## Returns the SMESH object associated to the specified \em SObject,
134     #  (the SObject is an item in the objects browser).
135     def getMeshObjectFromSObject(self, sobject):
136         """
137         Returns the SMESH object associated to the specified SObject,
138         (the SObject is an item in the objects browser).
139         """
140         if sobject is None:
141             return None
142         
143         obj = self.editor.getOrLoadObject(sobject)
144         meshObject = obj._narrow(SMESH.SMESH_Mesh)
145         return meshObject
146
147     ## Display the SMESH object associated to the specified \em entry
148     #  (the entry is the identifier of an item in the objects browser).
149     def displayMeshObjectFromEntry(self,entry):
150         """
151         Display the SMESH object associated to the specified entry
152         (the entry is the identifier of an item in the objects browser).    
153         """
154         if self.smeshGui is None:
155             self.smeshGui = salome.ImportComponentGUI("SMESH")
156
157         if not helper.SalomeGUI.hasDesktop():
158             print "displayMeshObject: no desktop available"
159             return
160         self.smeshGui.CreateAndDisplayActor(entry)
161
162 #
163 # ==================================================================
164 # Use cases and demo functions
165 # ==================================================================
166 #
167
168 # CAUTION: Before running this test functions, you first have to
169 # create (or import) an smesh object and select this object in the
170 # objects browser. You can run the box mesh creation procedure below
171 # instead.
172
173 # How to test?
174 # 1. Run a SALOME application including GEOM and SMESH, and create a new study
175 # 2. In the console, enter:
176 #    >>> from salome.smesh import smeshstudytools
177 #    >>> smeshstudytools.TEST_createBoxMesh()
178 # 3. Select the object named "boxmesh" in the browser
179 # 4. In the console, enter:
180 #    >>> smeshstudytools.TEST_selectAndExport_01()
181 #    >>> smeshstudytools.TEST_selectAndExport_02()
182 #    >>> smeshstudytools.TEST_display()
183
184
185 def TEST_createBoxMesh():
186     theStudy = helper.getActiveStudy()
187     
188     import GEOM
189     from salome.geom import geomBuilder
190     geompy = geomBuilder.New(theStudy)
191     
192     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
193
194     import SMESH, SALOMEDS
195     from salome.smesh import smeshBuilder
196     smesh = smeshBuilder.New(theStudy) 
197
198     from salome.StdMeshers import StdMeshersBuilder
199     boxmesh = smesh.Mesh(box)
200     Regular_1D = boxmesh.Segment()
201     Nb_Segments_1 = Regular_1D.NumberOfSegments(15)
202     Nb_Segments_1.SetDistrType( 0 )
203     Quadrangle_2D = boxmesh.Quadrangle()
204     Hexa_3D = smesh.CreateHypothesis('Hexa_3D')
205     status = boxmesh.AddHypothesis(Hexa_3D)
206     isDone = boxmesh.Compute()
207
208     smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
209     if salome.sg.hasDesktop():
210         salome.sg.updateObjBrowser(1)
211
212 #
213 # Definitions:
214 # - the SObject is an item in the study (Study Object).
215 # - the entry is the identifier of an item.
216 # - the object (geom object or smesh object) is a CORBA servant
217 #   embedded in the SALOME component container and with a reference in
218 #   the SALOME study, so that it can be retrieved.
219 #
220
221 def TEST_selectAndExport_01():
222     tool = SMeshStudyTools()
223     myMesh = tool.getMeshObjectSelected()
224     myMesh.ExportUNV("/tmp/myMesh.unv")
225
226 def TEST_selectAndExport_02():
227     # In this case, we want to retrieve the name of the mesh in the
228     # object browser. Note that in SALOME, a mesh object has no
229     # name. Only the SObject in the object browser has a name
230     # attribute.
231     tool = SMeshStudyTools()
232
233     mySObject, myEntry = helper.getSObjectSelected()
234     myName = mySObject.GetName()
235
236     myMesh = tool.getMeshObjectFromEntry(myEntry)
237     exportFileName = "/tmp/"+myName+".unv"
238     myMesh.ExportUNV(exportFileName)
239
240 def TEST_display():
241     mySObject, myEntry = helper.getSObjectSelected()
242
243     tool = SMeshStudyTools()
244     tool.displayMeshObjectFromEntry(myEntry)
245
246 if __name__ == "__main__":
247     TEST_selectAndExport_01()
248     TEST_selectAndExport_02()
249     TEST_display()