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 from salome.smesh import smeshBuilder
102 smesh = smeshBuilder.New(self.editor.study)
104 meshObject=smesh.IDToObject(entry)
107 def getMeshObjectFromSObject(self, sobject):
109 Returns the SMESH object associated to the specified SObject,
110 (the SObject is an item in the objects browser).
115 obj = self.editor.getOrLoadObject(sobject)
116 meshObject = obj._narrow(SMESH.SMESH_Mesh)
119 def displayMeshObjectFromEntry(self,entry):
121 Display the SMESH object associated to the specified entry
122 (the entry is the identifier of an item in the objects browser).
124 if self.smeshGui is None:
125 self.smeshGui = salome.ImportComponentGUI("SMESH")
127 if not helper.SalomeGUI.hasDesktop():
128 print "displayMeshObject: no desktop available"
130 self.smeshGui.CreateAndDisplayActor(entry)
133 # ==================================================================
134 # Use cases and demo functions
135 # ==================================================================
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
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()
155 def TEST_createBoxMesh():
156 theStudy = helper.getActiveStudy()
159 from salome.geom import geomBuilder
160 geompy = geomBuilder.New(theStudy)
162 box = geompy.MakeBoxDXDYDZ(200, 200, 200)
164 import SMESH, SALOMEDS
165 from salome.smesh import smeshBuilder
166 smesh = smeshBuilder.New(theStudy)
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()
178 smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
179 if salome.sg.hasDesktop():
180 salome.sg.updateObjBrowser(1)
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.
191 def TEST_selectAndExport_01():
192 tool = SMeshStudyTools()
193 myMesh = tool.getMeshObjectSelected()
194 myMesh.ExportUNV("/tmp/myMesh.unv")
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
201 tool = SMeshStudyTools()
203 mySObject, myEntry = helper.getSObjectSelected()
204 myName = mySObject.GetName()
206 myMesh = tool.getMeshObjectFromEntry(myEntry)
207 exportFileName = "/tmp/"+myName+".unv"
208 myMesh.ExportUNV(exportFileName)
211 mySObject, myEntry = helper.getSObjectSelected()
213 tool = SMeshStudyTools()
214 tool.displayMeshObjectFromEntry(myEntry)
216 if __name__ == "__main__":
217 TEST_selectAndExport_01()
218 TEST_selectAndExport_02()