1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 This module provides a new class :class:`SMeshStudyTools` to facilitate the
23 use of mesh objects in Salome study.
27 SMESH = None # SMESH module is loaded only when needed
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
34 class SMeshStudyTools:
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.
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.
50 def __init__(self, studyEditor = None):
53 SMESH = __import__("SMESH")
54 if studyEditor is None:
55 studyEditor = getStudyEditor()
56 self.editor = studyEditor
59 def updateStudy(self, studyId=None):
61 This function updates the tools so that it works on the
64 self.editor = getStudyEditor(studyId)
66 def getMeshFromGroup(self, meshGroupItem):
68 Get the mesh item owning the mesh group `meshGroupItem`.
70 :type meshGroupItem: SObject
71 :param meshGroupItem: Mesh group belonging to the searched mesh.
73 :return: The SObject corresponding to the mesh, or None if it was not
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)
85 def getMeshObjectSelected(self):
87 Returns the MESH object currently selected in the active study.
89 sobject, entry = helper.getSObjectSelected()
90 meshObject = self.getMeshObjectFromEntry(entry)
93 def getMeshObjectFromEntry(self, entry):
95 Returns the MESH object associated to the specified entry,
96 (the entry is the identifier of an item in the objects browser).
101 smesh.SetCurrentStudy(self.editor.study)
102 meshObject=smesh.IDToObject(entry)
105 def getMeshObjectFromSObject(self, sobject):
107 Returns the SMESH object associated to the specified SObject,
108 (the SObject is an item in the objects browser).
113 obj = self.editor.getOrLoadObject(sobject)
114 meshObject = obj._narrow(SMESH.SMESH_Mesh)
117 def displayMeshObjectFromEntry(self,entry):
119 Display the SMESH object associated to the specified entry
120 (the entry is the identifier of an item in the objects browser).
122 if self.smeshGui is None:
123 self.smeshGui = salome.ImportComponentGUI("SMESH")
125 if not helper.SalomeGUI.hasDesktop():
126 print "displayMeshObject: no desktop available"
128 self.smeshGui.CreateAndDisplayActor(entry)
131 # ==================================================================
132 # Use cases and demo functions
133 # ==================================================================
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
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()
153 def TEST_createBoxMesh():
154 theStudy = helper.getActiveStudy()
157 geompy.init_geom(theStudy)
158 box = geompy.MakeBoxDXDYDZ(200, 200, 200)
160 import smesh, SMESH, SALOMEDS
161 smesh.SetCurrentStudy(theStudy)
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()
172 smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
173 if salome.sg.hasDesktop():
174 salome.sg.updateObjBrowser(1)
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.
185 def TEST_selectAndExport_01():
186 tool = SMeshStudyTools()
187 myMesh = tool.getMeshObjectSelected()
188 myMesh.ExportUNV("/tmp/myMesh.unv")
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
195 tool = SMeshStudyTools()
197 mySObject, myEntry = helper.getSObjectSelected()
198 myName = mySObject.GetName()
200 myMesh = tool.getMeshObjectFromEntry(myEntry)
201 exportFileName = "/tmp/"+myName+".unv"
202 myMesh.ExportUNV(exportFileName)
205 mySObject, myEntry = helper.getSObjectSelected()
207 tool = SMeshStudyTools()
208 tool.displayMeshObjectFromEntry(myEntry)
210 if __name__ == "__main__":
211 TEST_selectAndExport_01()
212 TEST_selectAndExport_02()