Salome HOME
Merge from BR_pypkg (python packaging)
[modules/smesh.git] / src / SMESH_PY / smeshstudytools.py
1 # -*- coding: utf-8 -*-
2 #
3 #  Copyright (C) 2007-2009     EDF R&D
4
5 #    This file is part of PAL_SRC.
6 #
7 #    PAL_SRC is free software; you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation; either version 2 of the License, or
10 #    (at your option) any later version.
11 #
12 #    PAL_SRC is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with PAL_SRC; if not, write to the Free Software
19 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
20 #
21 """
22 This module provides a new class :class:`SMeshStudyTools` to facilitate the
23 use of mesh objects in Salome study.
24 """
25
26 import salome
27 SMESH = None    # SMESH module is loaded only when needed
28
29 from salome.kernel.studyedit import getStudyEditor
30
31 class SMeshStudyTools:
32     """
33     This class provides several methods to manipulate mesh objects in Salome
34     study. The parameter `studyEditor` defines a
35     :class:`~salome.kernel.studyedit.StudyEditor` object used to access the study. If
36     :const:`None`, the method returns a :class:`~salome.kernel.studyedit.StudyEditor`
37     object on the current study.
38
39     .. attribute:: editor
40     
41        This instance attribute contains the underlying
42        :class:`~salome.kernel.studyedit.StudyEditor` object. It can be used to access
43        the study but the attribute itself should not be modified.
44
45     """
46
47     def __init__(self, studyEditor = None):
48         global SMESH
49         if SMESH is None:
50             SMESH = __import__("SMESH")
51         if studyEditor is None:
52             studyEditor = getStudyEditor()
53         self.editor = studyEditor
54
55     def getMeshFromGroup(self, meshGroupItem):
56         """
57         Get the mesh item owning the mesh group `meshGroupItem`.
58
59         :type   meshGroupItem: SObject
60         :param  meshGroupItem: Mesh group belonging to the searched mesh.
61         
62         :return: The SObject corresponding to the mesh, or None if it was not
63                  found.
64         """
65         meshItem = None
66         obj = self.editor.getOrLoadObject(meshGroupItem)
67         group = obj._narrow(SMESH.SMESH_GroupBase)
68         if group is not None: # The type of the object is ok
69             meshObj = group.GetMesh()
70             meshItem = salome.ObjectToSObject(meshObj)
71         return meshItem