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