1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Guillaume Boulant (EDF/R&D)
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 # ==================================================================
29 from salome.gui import helper
32 # ==================================================================
33 # Special functions to deal with SMESH objects
34 # ==================================================================
40 def getSmeshObjectFromSObject(sobject):
42 Returns the SMESH object associated to the specified SObject,
43 (the SObject is an item in the objects browser).
48 #theObject = sobject.GetObject()
49 #if theObject is not None:
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 )
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
64 smeshObject = theObject._narrow(SMESH.SMESH_Mesh)
68 def getSmeshObjectFromEntry(entry):
70 Returns the SMESH object associated to the specified entry,
71 (the entry is the identifier of an item in the objects browser).
75 theStudy = helper.getActiveStudy()
76 smesh.SetCurrentStudy(theStudy)
77 smeshObject=smesh.IDToObject(entry)
80 def getSmeshObjectSelected():
82 Returns the SMESH object currently selected in the objects browser.
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)
91 SmeshGUI = salome.ImportComponentGUI("SMESH")
92 def displaySmeshObject(entry):
94 Display the SMESH object associated to the specified entry
95 (the entry is the identifier of an item in the objects browser).
97 if not SalomeGUI.hasDesktop():
98 print "displayGeomObjects: no desktop available"
100 SmeshGUI.CreateAndDisplayActor(entry)
103 # ==================================================================
104 # Use cases and demo functions
105 # ==================================================================
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
113 def TEST_createBoxMesh():
114 theStudy = helper.getActiveStudy()
117 geompy.init_geom(theStudy)
118 box = geompy.MakeBoxDXDYDZ(200, 200, 200)
120 import smesh, SMESH, SALOMEDS
121 smesh.SetCurrentStudy(theStudy)
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()
132 smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
133 if salome.sg.hasDesktop():
134 salome.sg.updateObjBrowser(1)
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.
145 def TEST_selectAndExport_01():
146 myMesh = getSmeshObjectSelected()
147 myMesh.ExportUNV("/tmp/myMesh.unv")
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
154 mySObject, myEntry = helper.getSObjectSelected()
155 myName = mySObject.GetName()
157 myMesh = getSmeshObjectFromEntry(myEntry)
158 exportFileName = "/tmp/"+myName+".unv"
159 myMesh.ExportUNV(exportFileName)
161 if __name__ == "__main__":
162 TEST_selectAndExport_01()
163 TEST_selectAndExport_02()