Back to index

salome-smesh  6.5.0
smeshstudytools.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 #
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # This library is free software; you can redistribute it and/or
00006 # modify it under the terms of the GNU Lesser General Public
00007 # License as published by the Free Software Foundation; either
00008 # version 2.1 of the License.
00009 #
00010 # This library is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 # Lesser General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU Lesser General Public
00016 # License along with this library; if not, write to the Free Software
00017 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 #
00019 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00020 #
00021 """
00022 This module provides a new class :class:`SMeshStudyTools` to facilitate the
00023 use of mesh objects in Salome study.
00024 """
00025 
00026 import salome
00027 SMESH = None    # SMESH module is loaded only when needed
00028 
00029 from salome.kernel.studyedit import getStudyEditor
00030 try:
00031     from salome.gui import helper
00032 except ImportError:
00033     pass
00034 
00035 class SMeshStudyTools:
00036     """
00037     This class provides several methods to manipulate mesh objects in Salome
00038     study. The parameter `studyEditor` defines a
00039     :class:`~salome.kernel.studyedit.StudyEditor` object used to access the study. If
00040     :const:`None`, the method returns a :class:`~salome.kernel.studyedit.StudyEditor`
00041     object on the current study.
00042 
00043     .. attribute:: editor
00044     
00045        This instance attribute contains the underlying
00046        :class:`~salome.kernel.studyedit.StudyEditor` object. It can be used to access
00047        the study but the attribute itself should not be modified.
00048 
00049     """
00050 
00051     def __init__(self, studyEditor = None):
00052         global SMESH
00053         if SMESH is None:
00054             SMESH = __import__("SMESH")
00055         if studyEditor is None:
00056             studyEditor = getStudyEditor()
00057         self.editor = studyEditor
00058         self.smeshGui = None
00059 
00060     def updateStudy(self, studyId=None):
00061         """
00062         This function updates the tools so that it works on the
00063         specified study.
00064         """
00065         self.editor = getStudyEditor(studyId)
00066         
00067     def getMeshFromGroup(self, meshGroupItem):
00068         """
00069         Get the mesh item owning the mesh group `meshGroupItem`.
00070 
00071         :type   meshGroupItem: SObject
00072         :param  meshGroupItem: Mesh group belonging to the searched mesh.
00073         
00074         :return: The SObject corresponding to the mesh, or None if it was not
00075                  found.
00076         """
00077         meshItem = None
00078         obj = self.editor.getOrLoadObject(meshGroupItem)
00079         group = obj._narrow(SMESH.SMESH_GroupBase)
00080         if group is not None: # The type of the object is ok
00081             meshObj = group.GetMesh()
00082             meshItem = salome.ObjectToSObject(meshObj)
00083         return meshItem
00084 
00085 
00086     def getMeshObjectSelected(self):
00087         '''
00088         Returns the MESH object currently selected in the active study.
00089         '''
00090         sobject, entry = helper.getSObjectSelected()
00091         meshObject = self.getMeshObjectFromEntry(entry)
00092         return meshObject
00093 
00094     def getMeshObjectFromEntry(self, entry):
00095         '''
00096         Returns the MESH object associated to the specified entry,
00097         (the entry is the identifier of an item in the objects browser).
00098         '''
00099         if entry is None:
00100             return None
00101         import smesh
00102         smesh.SetCurrentStudy(self.editor.study)
00103         meshObject=smesh.IDToObject(entry)
00104         return meshObject
00105 
00106     def getMeshObjectFromSObject(self, sobject):
00107         '''
00108         Returns the SMESH object associated to the specified SObject,
00109         (the SObject is an item in the objects browser).
00110         '''
00111         if sobject is None:
00112             return None
00113         
00114         obj = self.editor.getOrLoadObject(sobject)
00115         meshObject = obj._narrow(SMESH.SMESH_Mesh)
00116         return meshObject
00117 
00118     def displayMeshObjectFromEntry(self,entry):
00119         '''
00120         Display the SMESH object associated to the specified entry
00121         (the entry is the identifier of an item in the objects browser).    
00122         '''
00123         if self.smeshGui is None:
00124             self.smeshGui = salome.ImportComponentGUI("SMESH")
00125 
00126         if not helper.SalomeGUI.hasDesktop():
00127             print "displayMeshObject: no desktop available"
00128             return
00129         self.smeshGui.CreateAndDisplayActor(entry)
00130 
00131 #
00132 # ==================================================================
00133 # Use cases and demo functions
00134 # ==================================================================
00135 #
00136 
00137 # CAUTION: Before running this test functions, you first have to
00138 # create (or import) an smesh object and select this object in the
00139 # objects browser. You can run the box mesh creation procedure below
00140 # instead.
00141 
00142 # How to test?
00143 # 1. Run a SALOME application including GEOM and SMESH, and create a new study
00144 # 2. In the console, enter:
00145 #    >>> from salome.smesh import smeshstudytools
00146 #    >>> smeshstudytools.TEST_createBoxMesh()
00147 # 3. Select the object named "boxmesh" in the browser
00148 # 4. In the console, enter:
00149 #    >>> smeshstudytools.TEST_selectAndExport_01()
00150 #    >>> smeshstudytools.TEST_selectAndExport_02()
00151 #    >>> smeshstudytools.TEST_display()
00152 
00153 
00154 def TEST_createBoxMesh():
00155     theStudy = helper.getActiveStudy()
00156     
00157     import geompy
00158     geompy.init_geom(theStudy)
00159     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
00160 
00161     import smesh, SMESH, SALOMEDS    
00162     smesh.SetCurrentStudy(theStudy)
00163     import StdMeshers
00164     boxmesh = smesh.Mesh(box)
00165     Regular_1D = boxmesh.Segment()
00166     Nb_Segments_1 = Regular_1D.NumberOfSegments(15)
00167     Nb_Segments_1.SetDistrType( 0 )
00168     Quadrangle_2D = boxmesh.Quadrangle()
00169     Hexa_3D = smesh.CreateHypothesis('Hexa_3D')
00170     status = boxmesh.AddHypothesis(Hexa_3D)
00171     isDone = boxmesh.Compute()
00172 
00173     smesh.SetName(boxmesh.GetMesh(), 'boxmesh')
00174     if salome.sg.hasDesktop():
00175         salome.sg.updateObjBrowser(1)
00176 
00177 #
00178 # Definitions:
00179 # - the SObject is an item in the study (Study Object).
00180 # - the entry is the identifier of an item.
00181 # - the object (geom object or smesh object) is a CORBA servant
00182 #   embedded in the SALOME component container and with a reference in
00183 #   the SALOME study, so that it can be retrieved.
00184 #
00185 
00186 def TEST_selectAndExport_01():
00187     tool = SMeshStudyTools()
00188     myMesh = tool.getMeshObjectSelected()
00189     myMesh.ExportUNV("/tmp/myMesh.unv")
00190 
00191 def TEST_selectAndExport_02():
00192     # In this case, we want to retrieve the name of the mesh in the
00193     # object browser. Note that in SALOME, a mesh object has no
00194     # name. Only the SObject in the object browser has a name
00195     # attribute.
00196     tool = SMeshStudyTools()
00197 
00198     mySObject, myEntry = helper.getSObjectSelected()
00199     myName = mySObject.GetName()
00200 
00201     myMesh = tool.getMeshObjectFromEntry(myEntry)
00202     exportFileName = "/tmp/"+myName+".unv"
00203     myMesh.ExportUNV(exportFileName)
00204 
00205 def TEST_display():
00206     mySObject, myEntry = helper.getSObjectSelected()
00207 
00208     tool = SMeshStudyTools()
00209     tool.displayMeshObjectFromEntry(myEntry)
00210 
00211 if __name__ == "__main__":
00212     TEST_selectAndExport_01()
00213     TEST_selectAndExport_02()
00214     TEST_display()