Salome HOME
ed21fd31757ddbe83217ce84b1e34107f433032d
[modules/smesh.git] / src / SMESH_PY / smeshstudytools.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2007-2011  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.
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 This module provides a new class :class:`SMeshStudyTools` to facilitate the
23 use of mesh objects in Salome study.
24 """
25
26 import salome
27 SMESH = None    # SMESH module is loaded only when needed
28
29 from salome.kernel.studyedit import getStudyEditor
30 from salome.gui import helper
31
32 class SMeshStudyTools:
33     """
34     This class provides several methods to manipulate mesh objects in Salome
35     study. The parameter `studyEditor` defines a
36     :class:`~salome.kernel.studyedit.StudyEditor` object used to access the study. If
37     :const:`None`, the method returns a :class:`~salome.kernel.studyedit.StudyEditor`
38     object on the current study.
39
40     .. attribute:: editor
41     
42        This instance attribute contains the underlying
43        :class:`~salome.kernel.studyedit.StudyEditor` object. It can be used to access
44        the study but the attribute itself should not be modified.
45
46     """
47
48     def __init__(self, studyEditor = None):
49         global SMESH
50         if SMESH is None:
51             SMESH = __import__("SMESH")
52         if studyEditor is None:
53             studyEditor = getStudyEditor()
54         self.editor = studyEditor
55         self.smeshGui = None
56
57     def updateStudy(self, studyId=None):
58         """
59         This function updates the tools so that it works on the
60         specified study.
61         """
62         self.editor = getStudyEditor(studyId)
63         
64     def getMeshFromGroup(self, meshGroupItem):
65         """
66         Get the mesh item owning the mesh group `meshGroupItem`.
67
68         :type   meshGroupItem: SObject
69         :param  meshGroupItem: Mesh group belonging to the searched mesh.
70         
71         :return: The SObject corresponding to the mesh, or None if it was not
72                  found.
73         """
74         meshItem = None
75         obj = self.editor.getOrLoadObject(meshGroupItem)
76         group = obj._narrow(SMESH.SMESH_GroupBase)
77         if group is not None: # The type of the object is ok
78             meshObj = group.GetMesh()
79             meshItem = salome.ObjectToSObject(meshObj)
80         return meshItem
81
82
83     def getMeshObjectSelected(self):
84         '''
85         Returns the MESH object currently selected in the active study.
86         '''
87         sobject, entry = helper.getSObjectSelected()
88         meshObject = self.getMeshObjectFromEntry(entry)
89         return meshObject
90
91     def getMeshObjectFromEntry(self, entry):
92         '''
93         Returns the MESH object associated to the specified entry,
94         (the entry is the identifier of an item in the objects browser).
95         '''
96         if entry is None:
97             return None
98         import smesh
99         smesh.SetCurrentStudy(self.editor.study)
100         meshObject=smesh.IDToObject(entry)
101         return meshObject
102
103     def getMeshObjectFromSObject(self, sobject):
104         '''
105         Returns the SMESH object associated to the specified SObject,
106         (the SObject is an item in the objects browser).
107         '''
108         if sobject is None:
109             return None
110         
111         obj = self.editor.getOrLoadObject(sobject)
112         meshObject = obj._narrow(SMESH.SMESH_Mesh)
113         return meshObject
114
115     def displayMeshObjectFromEntry(self,entry):
116         '''
117         Display the SMESH object associated to the specified entry
118         (the entry is the identifier of an item in the objects browser).    
119         '''
120         if self.smeshGui is None:
121             self.smeshGui = salome.ImportComponentGUI("SMESH")
122
123         if not helper.SalomeGUI.hasDesktop():
124             print "displayMeshObject: no desktop available"
125             return
126         self.smeshGui.CreateAndDisplayActor(entry)
127
128 #
129 # ==================================================================
130 # Use cases and demo functions
131 # ==================================================================
132 #
133
134 # CAUTION: Before running this test functions, you first have to
135 # create (or import) an smesh object and select this object in the
136 # objects browser. You can run the box mesh creation procedure below
137 # instead.
138
139 # How to test?
140 # 1. Run a SALOME application including GEOM and SMESH, and create a new study
141 # 2. In the console, enter:
142 #    >>> from salome.smesh import smeshstudytools
143 #    >>> smeshstudytools.TEST_createBoxMesh()
144 # 3. Select the object named "boxmesh" in the browser
145 # 4. In the console, enter:
146 #    >>> smeshstudytools.TEST_selectAndExport_01()
147 #    >>> smeshstudytools.TEST_selectAndExport_02()
148 #    >>> smeshstudytools.TEST_display()
149
150
151 def TEST_createBoxMesh():
152     theStudy = helper.getActiveStudy()
153     
154     import geompy
155     geompy.init_geom(theStudy)
156     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
157
158     import smesh, SMESH, SALOMEDS    
159     smesh.SetCurrentStudy(theStudy)
160     import StdMeshers
161     boxmesh = smesh.Mesh(box)
162     Regular_1D = boxmesh.Segment()
163     Nb_Segments_1 = Regular_1D.NumberOfSegments(15)
164     Nb_Segments_1.SetDistrType( 0 )
165     Quadrangle_2D = boxmesh.Quadrangle()
166     Hexa_3D = smesh.CreateHypothesis('Hexa_3D')
167     status = boxmesh.AddHypothesis(Hexa_3D)
168     isDone = boxmesh.Compute()
169
170     smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
171     if salome.sg.hasDesktop():
172         salome.sg.updateObjBrowser(1)
173
174 #
175 # Definitions:
176 # - the SObject is an item in the study (Study Object).
177 # - the entry is the identifier of an item.
178 # - the object (geom object or smesh object) is a CORBA servant
179 #   embedded in the SALOME component container and with a reference in
180 #   the SALOME study, so that it can be retrieved.
181 #
182
183 def TEST_selectAndExport_01():
184     tool = SMeshStudyTools()
185     myMesh = tool.getMeshObjectSelected()
186     myMesh.ExportUNV("/tmp/myMesh.unv")
187
188 def TEST_selectAndExport_02():
189     # In this case, we want to retrieve the name of the mesh in the
190     # object browser. Note that in SALOME, a mesh object has no
191     # name. Only the SObject in the object browser has a name
192     # attribute.
193     tool = SMeshStudyTools()
194
195     mySObject, myEntry = helper.getSObjectSelected()
196     myName = mySObject.GetName()
197
198     myMesh = tool.getMeshObjectFromEntry(myEntry)
199     exportFileName = "/tmp/"+myName+".unv"
200     myMesh.ExportUNV(exportFileName)
201
202 def TEST_display():
203     mySObject, myEntry = helper.getSObjectSelected()
204
205     tool = SMeshStudyTools()
206     tool.displayMeshObjectFromEntry(myEntry)
207
208 if __name__ == "__main__":
209     TEST_selectAndExport_01()
210     TEST_selectAndExport_02()
211     TEST_display()