1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2016 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, or (at your option) any later version.
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 ## \package smeshstudytools Python API to access SMESH objects in the study.
24 ## \defgroup smeshstudytools Accessing SMESH object in the study
27 # Module \b smeshstudytools provides a new class SMeshStudyTools to facilitate the
28 # use of mesh objects in Salome study.
32 This module provides a new class :class:`SMeshStudyTools` to facilitate the
33 use of mesh objects in Salome study.
37 SMESH = None # SMESH module is loaded only when needed
39 from salome.kernel.studyedit import getStudyEditor
40 from salome.kernel.deprecation import is_called_by_sphinx
41 if not is_called_by_sphinx():
42 from salome.gui import helper
44 ## This class provides several methods to manipulate mesh objects in Salome
45 # study. The parameter \em studyEditor defines a \b StudyEditor
46 # object used to access the study. If \b None, the method returns a
47 # \b StudyEditor object on the current study.
50 # This instance attribute contains the underlying \b StudyEditor object.
51 # It can be used to access the study but the attribute itself should not be modified.
52 # \ingroup smeshstudytools
53 class SMeshStudyTools:
55 This class provides several methods to manipulate mesh objects in Salome
56 study. The parameter `studyEditor` defines a
57 :class:`~salome.kernel.studyedit.StudyEditor` object used to access the study. If
58 :const:`None`, the method returns a :class:`~salome.kernel.studyedit.StudyEditor`
59 object on the current study.
63 This instance attribute contains the underlying
64 :class:`~salome.kernel.studyedit.StudyEditor` object. It can be used to access
65 the study but the attribute itself should not be modified.
69 def __init__(self, studyEditor = None):
72 SMESH = __import__("SMESH")
73 if studyEditor is None:
74 studyEditor = getStudyEditor()
75 self.editor = studyEditor
78 ## This function updates the tools so that it works on the
80 def updateStudy(self, studyId=None):
82 This function updates the tools so that it works on the
85 self.editor = getStudyEditor(studyId)
87 ## Get the mesh item owning the mesh group \em meshGroupItem.
88 # \param meshGroupItem (SObject) mesh group belonging to the searched mesh.
89 # \return The SObject corresponding to the mesh, or None if it was not found.
90 def getMeshFromGroup(self, meshGroupItem):
92 Get the mesh item owning the mesh group `meshGroupItem`.
94 :type meshGroupItem: SObject
95 :param meshGroupItem: Mesh group belonging to the searched mesh.
97 :return: The SObject corresponding to the mesh, or None if it was not
101 obj = self.editor.getOrLoadObject(meshGroupItem)
102 group = obj._narrow(SMESH.SMESH_GroupBase)
103 if group is not None: # The type of the object is ok
104 meshObj = group.GetMesh()
105 meshItem = salome.ObjectToSObject(meshObj)
108 ## Returns the MESH object currently selected in the active study.
109 def getMeshObjectSelected(self):
111 Returns the MESH object currently selected in the active study.
113 sobject, entry = helper.getSObjectSelected()
114 meshObject = self.getMeshObjectFromEntry(entry)
117 ## Returns the MESH object associated to the specified entry,
118 # (the entry is the identifier of an item in the objects browser).
119 def getMeshObjectFromEntry(self, entry):
121 Returns the MESH object associated to the specified entry,
122 (the entry is the identifier of an item in the objects browser).
127 from salome.smesh import smeshBuilder
128 smesh = smeshBuilder.New(self.editor.study)
130 meshObject=salome.IDToObject(entry)
131 return smesh.Mesh( meshObject )
133 ## Returns the SMESH object associated to the specified \em SObject,
134 # (the SObject is an item in the objects browser).
135 def getMeshObjectFromSObject(self, sobject):
137 Returns the SMESH object associated to the specified SObject,
138 (the SObject is an item in the objects browser).
143 obj = self.editor.getOrLoadObject(sobject)
144 meshObject = obj._narrow(SMESH.SMESH_Mesh)
147 ## Display the SMESH object associated to the specified \em entry
148 # (the entry is the identifier of an item in the objects browser).
149 def displayMeshObjectFromEntry(self,entry):
151 Display the SMESH object associated to the specified entry
152 (the entry is the identifier of an item in the objects browser).
154 if self.smeshGui is None:
155 self.smeshGui = salome.ImportComponentGUI("SMESH")
157 if not helper.SalomeGUI.hasDesktop():
158 print "displayMeshObject: no desktop available"
160 self.smeshGui.CreateAndDisplayActor(entry)
163 # ==================================================================
164 # Use cases and demo functions
165 # ==================================================================
168 # CAUTION: Before running this test functions, you first have to
169 # create (or import) an smesh object and select this object in the
170 # objects browser. You can run the box mesh creation procedure below
174 # 1. Run a SALOME application including GEOM and SMESH, and create a new study
175 # 2. In the console, enter:
176 # >>> from salome.smesh import smeshstudytools
177 # >>> smeshstudytools.TEST_createBoxMesh()
178 # 3. Select the object named "boxmesh" in the browser
179 # 4. In the console, enter:
180 # >>> smeshstudytools.TEST_selectAndExport_01()
181 # >>> smeshstudytools.TEST_selectAndExport_02()
182 # >>> smeshstudytools.TEST_display()
185 def TEST_createBoxMesh():
186 theStudy = helper.getActiveStudy()
189 from salome.geom import geomBuilder
190 geompy = geomBuilder.New(theStudy)
192 box = geompy.MakeBoxDXDYDZ(200, 200, 200)
194 import SMESH, SALOMEDS
195 from salome.smesh import smeshBuilder
196 smesh = smeshBuilder.New(theStudy)
198 from salome.StdMeshers import StdMeshersBuilder
199 boxmesh = smesh.Mesh(box)
200 Regular_1D = boxmesh.Segment()
201 Nb_Segments_1 = Regular_1D.NumberOfSegments(15)
202 Nb_Segments_1.SetDistrType( 0 )
203 Quadrangle_2D = boxmesh.Quadrangle()
204 Hexa_3D = smesh.CreateHypothesis('Hexa_3D')
205 status = boxmesh.AddHypothesis(Hexa_3D)
206 isDone = boxmesh.Compute()
208 smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
209 if salome.sg.hasDesktop():
210 salome.sg.updateObjBrowser(True)
214 # - the SObject is an item in the study (Study Object).
215 # - the entry is the identifier of an item.
216 # - the object (geom object or smesh object) is a CORBA servant
217 # embedded in the SALOME component container and with a reference in
218 # the SALOME study, so that it can be retrieved.
221 def TEST_selectAndExport_01():
222 tool = SMeshStudyTools()
223 myMesh = tool.getMeshObjectSelected()
224 myMesh.ExportUNV("/tmp/myMesh.unv")
226 def TEST_selectAndExport_02():
227 # In this case, we want to retrieve the name of the mesh in the
228 # object browser. Note that in SALOME, a mesh object has no
229 # name. Only the SObject in the object browser has a name
231 tool = SMeshStudyTools()
233 mySObject, myEntry = helper.getSObjectSelected()
234 myName = mySObject.GetName()
236 myMesh = tool.getMeshObjectFromEntry(myEntry)
237 exportFileName = "/tmp/"+myName+".unv"
238 myMesh.ExportUNV(exportFileName)
241 mySObject, myEntry = helper.getSObjectSelected()
243 tool = SMeshStudyTools()
244 tool.displayMeshObjectFromEntry(myEntry)
246 if __name__ == "__main__":
247 TEST_selectAndExport_01()
248 TEST_selectAndExport_02()