Salome HOME
IMP: extends the class GeomStudyTools with function to retrieve geom objects selected...
[modules/geom.git] / src / GEOM_PY / geomtools.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
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.
9 #
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.
14 #
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
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 from salome.gui.helper import getSObjectSelected
35 from salome.kernel.services import IDToObject
36
37 _geompys = {}
38
39 def getGeompy(studyId = None):
40     """
41     Return an object behaving exactly like geompy module, except that it is
42     associated with the study `studyId`. If `studyId` is :const:`None`, return
43     a pseudo geompy object for the current study.
44     """
45     # We can't use geompy module because it initializes GEOM with
46     # salome.myStudy, which may not exist. So we use this trick to create
47     # a pseudo geompy module. 
48     salome.salome_init()
49     if studyId is None:
50         studyId = getActiveStudyId()
51     if not _geompys.has_key(studyId):
52         import geompyDC
53         _geompys[studyId] = salome.lcc.FindOrLoadComponent("FactoryServer",
54                                                            "GEOM")
55         _geompys[studyId].ShapeType = geompyDC.ShapeType
56         _geompys[studyId].GEOM = geompyDC.GEOM
57         _geompys[studyId].kind = geompyDC.kind
58         _geompys[studyId].info = geompyDC.info
59         _geompys[studyId].PackData = geompyDC.PackData
60         _geompys[studyId].ReadTexture = geompyDC.ReadTexture
61         study = salome.myStudyManager.GetStudyByID(studyId)
62         _geompys[studyId].init_geom(study)
63     return _geompys[studyId]
64
65
66 ModeWireFrame = 0
67 ModeShading = 1
68 DisplayMode=ModeShading
69
70 class GeomStudyTools:
71     """
72     This class provides several methods to manipulate geom objects in Salome
73     study. The parameter `studyEditor` defines a
74     :class:`~salome.kernel.studyedit.StudyEditor` object used to access the study. If
75     :const:`None`, the method returns a :class:`~salome.kernel.studyedit.StudyEditor`
76     object on the current study.
77
78     .. attribute:: editor
79     
80        This instance attribute contains the underlying
81        :class:`~salome.kernel.studyedit.StudyEditor` object. It can be used to access
82        the study but the attribute itself should not be modified.
83
84     """
85
86     def __init__(self, studyEditor = None):
87         global GEOM
88         if GEOM is None:
89             GEOM = __import__("GEOM")
90         if studyEditor is None:
91             studyEditor = getStudyEditor()
92         self.editor = studyEditor
93
94     def displayShapeByName(self, shapeName, color = None):
95         """
96         Display the geometrical shape whose name in the study is `shapeName`.
97         
98         :type   shapeName: string
99         :param  shapeName: name of the geometrical shape
100         
101         :type   color: tuple (triplet)
102         :param  color: RGB components of the color of the shape
103         
104         :return: True if the shape was found, False otherwise
105         """
106         logger.debug("displayShapeByName in PAL: %s with color %s" %
107                      (shapeName, color))
108         listSO = self.editor.study.FindObjectByName(shapeName, "GEOM")
109         for sObj in listSO:
110             entry = sObj.GetID()
111             geomObj = self.editor.getOrLoadObject(sObj)
112             if geomObj:
113                 shape = geomObj._narrow(GEOM.GEOM_Object)
114                 if shape:                
115                     geomgui = salome.ImportComponentGUI("GEOM")            
116                     geomgui.createAndDisplayGO(entry)
117                     geomgui.setDisplayMode(entry, DisplayMode)
118                     if color is not None:
119                         geomgui.setColor(entry, color[0], color[1], color[2])
120                     return True
121         return False
122
123     def getGeomObjectSelected(self):
124         '''
125         Returns the GEOM object currently selected in the objects browser.
126         '''
127         sobject, entry = getSObjectSelected()
128         geomObject = self.getGeomObjectFromEntry(entry)
129         return geomObject
130
131     def getGeomObjectFromEntry(self,entry):
132         '''
133         Returns the GEOM object associated to the specified entry,
134         (the entry is the identifier of an item in the active study)
135         '''
136         if entry is None:
137             return None
138         geomObject=IDToObject(entry, self.editor.study)
139         return geomObject._narrow(GEOM.GEOM_Object)
140
141 #
142 # ==================================================================
143 # Use cases and demo functions
144 # ==================================================================
145 #
146
147 # How to test?
148 # 1. Run a SALOME application including GEOM, and create a new study
149 # 2. In the console, enter:
150 #    >>> from salome.geom import geomtools
151 #    >>> geomtools.TEST_createBox()
152 # 3. Select the object named "box" in the browser
153 # 4. In the console, enter:
154 #    >>> geomtools.TEST_getGeomObjectSelected()
155
156 def TEST_createBox():
157     geompy = getGeompy()
158     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
159     geompy.addToStudy( box, 'box' )    
160     if salome.sg.hasDesktop():
161         salome.sg.updateObjBrowser(1)
162
163
164 def TEST_getGeomObjectSelected():
165     tool = GeomStudyTools()
166     myGeomObject = tool.getGeomObjectSelected()
167     print myGeomObject
168
169 if __name__ == "__main__":
170     TEST_getGeomObjectSelected()
171
172
173