1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2011 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.gui import helper
32 class SMeshStudyTools:
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.
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.
48 def __init__(self, studyEditor = None):
51 SMESH = __import__("SMESH")
52 if studyEditor is None:
53 studyEditor = getStudyEditor()
54 self.editor = studyEditor
57 def updateStudy(self, studyId=None):
59 This function updates the tools so that it works on the
62 self.editor = getStudyEditor(studyId)
64 def getMeshFromGroup(self, meshGroupItem):
66 Get the mesh item owning the mesh group `meshGroupItem`.
68 :type meshGroupItem: SObject
69 :param meshGroupItem: Mesh group belonging to the searched mesh.
71 :return: The SObject corresponding to the mesh, or None if it was not
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)
83 def getMeshObjectSelected(self):
85 Returns the MESH object currently selected in the active study.
87 sobject, entry = helper.getSObjectSelected()
88 meshObject = self.getMeshObjectFromEntry(entry)
91 def getMeshObjectFromEntry(self, entry):
93 Returns the MESH object associated to the specified entry,
94 (the entry is the identifier of an item in the objects browser).
99 smesh.SetCurrentStudy(self.editor.study)
100 meshObject=smesh.IDToObject(entry)
103 def getMeshObjectFromSObject(self, sobject):
105 Returns the SMESH object associated to the specified SObject,
106 (the SObject is an item in the objects browser).
111 obj = self.editor.getOrLoadObject(sobject)
112 meshObject = obj._narrow(SMESH.SMESH_Mesh)
115 def displayMeshObjectFromEntry(self,entry):
117 Display the SMESH object associated to the specified entry
118 (the entry is the identifier of an item in the objects browser).
120 if self.smeshGui is None:
121 self.smeshGui = salome.ImportComponentGUI("SMESH")
123 if not helper.SalomeGUI.hasDesktop():
124 print "displayMeshObject: no desktop available"
126 self.smeshGui.CreateAndDisplayActor(entry)
129 # ==================================================================
130 # Use cases and demo functions
131 # ==================================================================
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
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()
151 def TEST_createBoxMesh():
152 theStudy = helper.getActiveStudy()
155 geompy.init_geom(theStudy)
156 box = geompy.MakeBoxDXDYDZ(200, 200, 200)
158 import smesh, SMESH, SALOMEDS
159 smesh.SetCurrentStudy(theStudy)
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()
170 smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
171 if salome.sg.hasDesktop():
172 salome.sg.updateObjBrowser(1)
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.
183 def TEST_selectAndExport_01():
184 tool = SMeshStudyTools()
185 myMesh = tool.getMeshObjectSelected()
186 myMesh.ExportUNV("/tmp/myMesh.unv")
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
193 tool = SMeshStudyTools()
195 mySObject, myEntry = helper.getSObjectSelected()
196 myName = mySObject.GetName()
198 myMesh = tool.getMeshObjectFromEntry(myEntry)
199 exportFileName = "/tmp/"+myName+".unv"
200 myMesh.ExportUNV(exportFileName)
203 mySObject, myEntry = helper.getSObjectSelected()
205 tool = SMeshStudyTools()
206 tool.displayMeshObjectFromEntry(myEntry)
208 if __name__ == "__main__":
209 TEST_selectAndExport_01()
210 TEST_selectAndExport_02()