Salome HOME
Patch to fix build issues do to sphinx importing python modules and causing SIGSEGV
[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         smesh.SetCurrentStudy(self.editor.study)
102         meshObject=smesh.IDToObject(entry)
103         return meshObject
104
105     def getMeshObjectFromSObject(self, sobject):
106         '''
107         Returns the SMESH object associated to the specified SObject,
108         (the SObject is an item in the objects browser).
109         '''
110         if sobject is None:
111             return None
112         
113         obj = self.editor.getOrLoadObject(sobject)
114         meshObject = obj._narrow(SMESH.SMESH_Mesh)
115         return meshObject
116
117     def displayMeshObjectFromEntry(self,entry):
118         '''
119         Display the SMESH object associated to the specified entry
120         (the entry is the identifier of an item in the objects browser).    
121         '''
122         if self.smeshGui is None:
123             self.smeshGui = salome.ImportComponentGUI("SMESH")
124
125         if not helper.SalomeGUI.hasDesktop():
126             print "displayMeshObject: no desktop available"
127             return
128         self.smeshGui.CreateAndDisplayActor(entry)
129
130 #
131 # ==================================================================
132 # Use cases and demo functions
133 # ==================================================================
134 #
135
136 # CAUTION: Before running this test functions, you first have to
137 # create (or import) an smesh object and select this object in the
138 # objects browser. You can run the box mesh creation procedure below
139 # instead.
140
141 # How to test?
142 # 1. Run a SALOME application including GEOM and SMESH, and create a new study
143 # 2. In the console, enter:
144 #    >>> from salome.smesh import smeshstudytools
145 #    >>> smeshstudytools.TEST_createBoxMesh()
146 # 3. Select the object named "boxmesh" in the browser
147 # 4. In the console, enter:
148 #    >>> smeshstudytools.TEST_selectAndExport_01()
149 #    >>> smeshstudytools.TEST_selectAndExport_02()
150 #    >>> smeshstudytools.TEST_display()
151
152
153 def TEST_createBoxMesh():
154     theStudy = helper.getActiveStudy()
155     
156     import geompy
157     geompy.init_geom(theStudy)
158     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
159
160     import smesh, SMESH, SALOMEDS    
161     smesh.SetCurrentStudy(theStudy)
162     import StdMeshers
163     boxmesh = smesh.Mesh(box)
164     Regular_1D = boxmesh.Segment()
165     Nb_Segments_1 = Regular_1D.NumberOfSegments(15)
166     Nb_Segments_1.SetDistrType( 0 )
167     Quadrangle_2D = boxmesh.Quadrangle()
168     Hexa_3D = smesh.CreateHypothesis('Hexa_3D')
169     status = boxmesh.AddHypothesis(Hexa_3D)
170     isDone = boxmesh.Compute()
171
172     smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
173     if salome.sg.hasDesktop():
174         salome.sg.updateObjBrowser(1)
175
176 #
177 # Definitions:
178 # - the SObject is an item in the study (Study Object).
179 # - the entry is the identifier of an item.
180 # - the object (geom object or smesh object) is a CORBA servant
181 #   embedded in the SALOME component container and with a reference in
182 #   the SALOME study, so that it can be retrieved.
183 #
184
185 def TEST_selectAndExport_01():
186     tool = SMeshStudyTools()
187     myMesh = tool.getMeshObjectSelected()
188     myMesh.ExportUNV("/tmp/myMesh.unv")
189
190 def TEST_selectAndExport_02():
191     # In this case, we want to retrieve the name of the mesh in the
192     # object browser. Note that in SALOME, a mesh object has no
193     # name. Only the SObject in the object browser has a name
194     # attribute.
195     tool = SMeshStudyTools()
196
197     mySObject, myEntry = helper.getSObjectSelected()
198     myName = mySObject.GetName()
199
200     myMesh = tool.getMeshObjectFromEntry(myEntry)
201     exportFileName = "/tmp/"+myName+".unv"
202     myMesh.ExportUNV(exportFileName)
203
204 def TEST_display():
205     mySObject, myEntry = helper.getSObjectSelected()
206
207     tool = SMeshStudyTools()
208     tool.displayMeshObjectFromEntry(myEntry)
209
210 if __name__ == "__main__":
211     TEST_selectAndExport_01()
212     TEST_selectAndExport_02()
213     TEST_display()