Salome HOME
9693525f8f5e2d7dd013e85afe747341297083fa
[modules/geom.git] / src / GEOM_PY / geomtools.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 tools to facilitate the use of geom engine and geom
23 objects in Salome.
24 """
25
26 import salome
27 GEOM = None    # GEOM module is loaded only when needed
28
29 from salome.kernel.logger import Logger
30 from salome.kernel import termcolor
31 logger = Logger("salome.geom.geomtools", color = termcolor.RED)
32
33 from salome.kernel.studyedit import getActiveStudyId, getStudyEditor
34
35 _geompys = {}
36
37 def getGeompy(studyId = None):
38     """
39     Return an object behaving exactly like geompy module, except that it is
40     associated with the study `studyId`. If `studyId` is :const:`None`, return
41     a pseudo geompy object for the current study.
42     """
43     # We can't use geompy module because it initializes GEOM with
44     # salome.myStudy, which may not exist. So we use this trick to create
45     # a pseudo geompy module. 
46     salome.salome_init()
47     if studyId is None:
48         studyId = getActiveStudyId()
49     if not _geompys.has_key(studyId):
50         import geompyDC
51         _geompys[studyId] = salome.lcc.FindOrLoadComponent("FactoryServer",
52                                                            "GEOM")
53         _geompys[studyId].ShapeType = geompyDC.ShapeType
54         _geompys[studyId].GEOM = geompyDC.GEOM
55         _geompys[studyId].kind = geompyDC.kind
56         _geompys[studyId].info = geompyDC.info
57         _geompys[studyId].PackData = geompyDC.PackData
58         _geompys[studyId].ReadTexture = geompyDC.ReadTexture
59         study = salome.myStudyManager.GetStudyByID(studyId)
60         _geompys[studyId].init_geom(study)
61     return _geompys[studyId]
62
63
64 class GeomStudyTools:
65     """
66     This class provides several methods to manipulate geom objects in Salome
67     study. The parameter `studyEditor` defines a
68     :class:`~salome.kernel.studyedit.StudyEditor` object used to access the study. If
69     :const:`None`, the method returns a :class:`~salome.kernel.studyedit.StudyEditor`
70     object on the current study.
71
72     .. attribute:: editor
73     
74        This instance attribute contains the underlying
75        :class:`~salome.kernel.studyedit.StudyEditor` object. It can be used to access
76        the study but the attribute itself should not be modified.
77
78     """
79
80     def __init__(self, studyEditor = None):
81         global GEOM
82         if GEOM is None:
83             GEOM = __import__("GEOM")
84         if studyEditor is None:
85             studyEditor = getStudyEditor()
86         self.editor = studyEditor
87
88     def displayShapeByName(self, shapeName, color = None):
89         """
90         Display the geometrical shape whose name in the study is `shapeName`.
91         
92         :type   shapeName: string
93         :param  shapeName: name of the geometrical shape
94         
95         :type   color: tuple (triplet)
96         :param  color: RGB components of the color of the shape
97         
98         :return: True if the shape was found, False otherwise
99         """
100         logger.debug("displayShapeByName in PAL: %s with color %s" %
101                      (shapeName, color))
102         listSO = self.editor.study.FindObjectByName(shapeName, "GEOM")
103         for sObj in listSO:
104             entry = sObj.GetID()
105             geomObj = self.editor.getOrLoadObject(sObj)
106             if geomObj:
107                 shape = geomObj._narrow(GEOM.GEOM_Object)
108                 if shape:                
109                     geomgui = salome.ImportComponentGUI("GEOM")            
110                     geomgui.createAndDisplayGO(entry)
111                     geomgui.setDisplayMode(entry, 1)
112                     if color is not None:
113                         geomgui.setColor(entry, color[0], color[1], color[2])
114                     return True
115         return False