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