Salome HOME
IMP: add a guihelper.py module in SMESH_PY (package salome.smesh) for managing the...
[modules/smesh.git] / src / SMESH_PY / guihelper.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # Author: Guillaume Boulant (EDF/R&D)
22
23 # ==================================================================
24 # This file provides helper functions to drive some SMESH gui features
25 # of a SALOME Application (the selection of mesh objects in the object
26 # browser for now, further development coming soon).
27 # ==================================================================
28
29 from salome.gui import helper
30
31 #
32 # ==================================================================
33 # Special functions to deal with SMESH objects
34 # ==================================================================
35 #
36 import salome
37 salome.salome_init()
38 import SMESH
39
40 def getSmeshObjectFromSObject(sobject):
41     '''
42     Returns the SMESH object associated to the specified SObject,
43     (the SObject is an item in the objects browser).
44     '''
45     if sobject is None:
46         return None
47
48     #theObject = sobject.GetObject()
49     #if theObject is not None:
50     #    return theObject
51
52     # The engine must be loaded
53     theStudy   = helper.getActiveStudy()
54     theBuilder = theStudy.NewBuilder()
55     strContainer, strComponentName = "FactoryServer", "SMESH"
56     engine     = salome.lcc.FindOrLoadComponent( strContainer, strComponentName )
57     scomponent = theStudy.FindComponent( strComponentName )
58     theBuilder.LoadWith( scomponent , engine  )
59
60     # The servant can be retrieved from the sobject
61     theObject = sobject.GetObject()
62     # Then cast to the smesh desired class (supposed to be a mesh in
63     # this example).
64     smeshObject = theObject._narrow(SMESH.SMESH_Mesh)
65     return smeshObject
66
67 import smesh
68 def getSmeshObjectFromEntry(entry):
69     '''
70     Returns the SMESH object associated to the specified entry,
71     (the entry is the identifier of an item in the objects browser).
72     '''
73     if entry is None:
74         return None
75     theStudy = helper.getActiveStudy()
76     smesh.SetCurrentStudy(theStudy)
77     smeshObject=smesh.IDToObject(entry)
78     return smeshObject
79
80 def getSmeshObjectSelected():
81     '''
82     Returns the SMESH object currently selected in the objects browser.
83     '''
84     sobject, entry = helper.getSObjectSelected()
85     # You can retrieve the smesh object either from the sobject or
86     # from the entry. From the entry is quicker.
87     #smeshObject = getSmeshObjectFromSObject(sobject)
88     smeshObject = getSmeshObjectFromEntry(entry)
89     return smeshObject
90
91 SmeshGUI = salome.ImportComponentGUI("SMESH")
92 def displaySmeshObject(entry):
93     '''
94     Display the SMESH object associated to the specified entry
95     (the entry is the identifier of an item in the objects browser).    
96     '''
97     if not SalomeGUI.hasDesktop():
98         print "displayGeomObjects: no desktop available"
99         return
100     SmeshGUI.CreateAndDisplayActor(entry)
101
102 #
103 # ==================================================================
104 # Use cases and demo functions
105 # ==================================================================
106 #
107
108 # CAUTION: Before running this test functions, you first have to
109 # create (or import) an smesh object and select this object in the
110 # objects browser. You can run the box mesh creation procedure below
111 # instead.
112
113 def TEST_createBoxMesh():
114     theStudy = helper.getActiveStudy()
115     
116     import geompy
117     geompy.init_geom(theStudy)
118     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
119
120     import smesh, SMESH, SALOMEDS    
121     smesh.SetCurrentStudy(theStudy)
122     import StdMeshers
123     boxmesh = smesh.Mesh(box)
124     Regular_1D = boxmesh.Segment()
125     Nb_Segments_1 = Regular_1D.NumberOfSegments(15)
126     Nb_Segments_1.SetDistrType( 0 )
127     Quadrangle_2D = boxmesh.Quadrangle()
128     Hexa_3D = smesh.CreateHypothesis('Hexa_3D')
129     status = boxmesh.AddHypothesis(Hexa_3D)
130     isDone = boxmesh.Compute()
131
132     smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
133     if salome.sg.hasDesktop():
134         salome.sg.updateObjBrowser(1)
135
136 #
137 # Definitions:
138 # - the SObject is an item in the study (Study Object).
139 # - the entry is the identifier of an item.
140 # - the object (geom object or smesh object) is a CORBA servant
141 #   embedded in the SALOME component container and with a reference in
142 #   the SALOME study, so that it can be retrieved.
143 #
144
145 def TEST_selectAndExport_01():
146     myMesh = getSmeshObjectSelected()
147     myMesh.ExportUNV("/tmp/myMesh.unv")
148
149 def TEST_selectAndExport_02():
150     # In this case, we want to retrieve the name of the mesh in the
151     # object browser. Note that in SALOME, a mesh object has no
152     # name. Only the SObject in the object browser has a name
153     # attribute.
154     mySObject, myEntry = helper.getSObjectSelected()
155     myName = mySObject.GetName()
156
157     myMesh = getSmeshObjectFromEntry(myEntry)
158     exportFileName = "/tmp/"+myName+".unv"
159     myMesh.ExportUNV(exportFileName)
160
161 if __name__ == "__main__":
162     TEST_selectAndExport_01()
163     TEST_selectAndExport_02()