1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2012 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
31 from salome.gui import helper
35 class SMeshStudyTools:
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.
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.
51 def __init__(self, studyEditor = None):
54 SMESH = __import__("SMESH")
55 if studyEditor is None:
56 studyEditor = getStudyEditor()
57 self.editor = studyEditor
60 def updateStudy(self, studyId=None):
62 This function updates the tools so that it works on the
65 self.editor = getStudyEditor(studyId)
67 def getMeshFromGroup(self, meshGroupItem):
69 Get the mesh item owning the mesh group `meshGroupItem`.
71 :type meshGroupItem: SObject
72 :param meshGroupItem: Mesh group belonging to the searched mesh.
74 :return: The SObject corresponding to the mesh, or None if it was not
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)
86 def getMeshObjectSelected(self):
88 Returns the MESH object currently selected in the active study.
90 sobject, entry = helper.getSObjectSelected()
91 meshObject = self.getMeshObjectFromEntry(entry)
94 def getMeshObjectFromEntry(self, entry):
96 Returns the MESH object associated to the specified entry,
97 (the entry is the identifier of an item in the objects browser).
102 smesh.SetCurrentStudy(self.editor.study)
103 meshObject=smesh.IDToObject(entry)
106 def getMeshObjectFromSObject(self, sobject):
108 Returns the SMESH object associated to the specified SObject,
109 (the SObject is an item in the objects browser).
114 obj = self.editor.getOrLoadObject(sobject)
115 meshObject = obj._narrow(SMESH.SMESH_Mesh)
118 def displayMeshObjectFromEntry(self,entry):
120 Display the SMESH object associated to the specified entry
121 (the entry is the identifier of an item in the objects browser).
123 if self.smeshGui is None:
124 self.smeshGui = salome.ImportComponentGUI("SMESH")
126 if not helper.SalomeGUI.hasDesktop():
127 print "displayMeshObject: no desktop available"
129 self.smeshGui.CreateAndDisplayActor(entry)
132 # ==================================================================
133 # Use cases and demo functions
134 # ==================================================================
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
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()
154 def TEST_createBoxMesh():
155 theStudy = helper.getActiveStudy()
158 geompy.init_geom(theStudy)
159 box = geompy.MakeBoxDXDYDZ(200, 200, 200)
161 import smesh, SMESH, SALOMEDS
162 smesh.SetCurrentStudy(theStudy)
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()
173 smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
174 if salome.sg.hasDesktop():
175 salome.sg.updateObjBrowser(1)
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.
186 def TEST_selectAndExport_01():
187 tool = SMeshStudyTools()
188 myMesh = tool.getMeshObjectSelected()
189 myMesh.ExportUNV("/tmp/myMesh.unv")
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
196 tool = SMeshStudyTools()
198 mySObject, myEntry = helper.getSObjectSelected()
199 myName = mySObject.GetName()
201 myMesh = tool.getMeshObjectFromEntry(myEntry)
202 exportFileName = "/tmp/"+myName+".unv"
203 myMesh.ExportUNV(exportFileName)
206 mySObject, myEntry = helper.getSObjectSelected()
208 tool = SMeshStudyTools()
209 tool.displayMeshObjectFromEntry(myEntry)
211 if __name__ == "__main__":
212 TEST_selectAndExport_01()
213 TEST_selectAndExport_02()