Salome HOME
Add methods to select mesh elements from python.
[modules/smesh.git] / src / SMESH_SWIG / smesh_selection.py
1 # Copyright (C) 2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 #  File   : smesh_selection.py
20 #  Author : Roman NIKOLAEV, OPEN CASCADE ( roman.nikolaev@opencascade.com )
21 #  Module : SMESH
22
23 import salome
24 salome.salome_init()
25
26 import libSMESH_Swig
27 sm_gui = libSMESH_Swig.SMESH_Swig()
28
29 import SMESH, SALOMEDS
30 from salome.smesh import smeshBuilder
31 smesh =  smeshBuilder.New(salome.myStudy)
32
33 import GEOM
34
35 # swig -> idl
36 _converter = { 
37     libSMESH_Swig.EdgeOfCell    :  None, # TODO: check how to process it
38     libSMESH_Swig.Node          :  SMESH.NODE,
39     libSMESH_Swig.Edge          :  SMESH.EDGE,
40     libSMESH_Swig.Face          :  SMESH.FACE,
41     libSMESH_Swig.Volume        :  SMESH.VOLUME,
42     libSMESH_Swig.Elem0D        :  SMESH.ELEM0D,
43     libSMESH_Swig.Ball          :  SMESH.BALL,
44     libSMESH_Swig.Cell          :  SMESH.ALL
45 }
46
47 # Converts swig to idl enumeration
48 def _swig2idl( type ):
49     if _converter.has_key( type ) :
50         return _converter[type]
51     return None
52
53 def _getEntry(mesh):
54     if isinstance( mesh, smeshBuilder.Mesh ) :
55         return salome.ObjectToID( mesh.GetMesh() )
56     else :
57         if isinstance( mesh, str ) :
58             return mesh
59     return None
60
61 def _getMesh(mesh):
62     if isinstance( mesh, smeshBuilder.Mesh ) :
63         return mesh.GetMesh()
64     else :
65         if isinstance( mesh, str ) :
66             return salome.IDToObject( mesh )
67     return None
68
69 def _getGeom(geom):
70     if isinstance( geom, GEOM._objref_GEOM_Object ) :
71         return geom
72     else :
73         if isinstance( geom, str ) :
74             return salome.IDToObject( geom )
75     return None
76
77
78 # Selects an elements lst on the mesh
79 def select( mesh, lst, append = False ) :
80     # Check mesh parameter
81     entry = _getEntry(mesh)   
82     if entry is None:
83         print "Wrong 'mesh' parameter"
84         return
85     
86     # Check lst parameter
87     tmp = []
88     if isinstance( lst, int ) :
89         tmp.append( lst )
90     else :
91         if isinstance( lst,list ) :
92             tmp = lst
93         else :
94             print "Wrong 'lst' parameter"
95             return
96     sm_gui.select( entry, tmp, append )
97
98
99 def _preProcess(mesh) :
100     m = _getMesh(mesh);
101     if m is None:
102         print "Wrong 'mesh' parameter"
103         return [None, None]
104     
105     elemType = _swig2idl(sm_gui.getSelectionMode())
106     if elemType is None:
107         return [None, None]
108     return [m, elemType]
109
110
111
112 # Selects an elements on the mesh inside the sphere with radius r and center (x, y, z)
113 def selectInsideSphere( mesh, x, y, z, r, append = False ) :
114
115     [m, elemType] = _preProcess(mesh)
116     if m is None or elemType is None :
117         return
118     
119     l = smesh.GetInsideSphere( m, elemType, x, y, z, r )
120     if len(l) > 0:
121         select(mesh, l, append)
122
123 # Selects an elements on the mesh inside the box
124 def selectInsideBox( mesh, x1, y1, z1, x2, y2, z2 , append = False ) :    
125
126     [m, elemType] = _preProcess(mesh)
127     if m is None or elemType is None :
128         return
129
130     l = smesh.GetInsideBox( m, elemType, x1, y1, z1, x2, y2, z2 )
131     if len(l) > 0:
132         select(mesh, l, append)
133
134 # Selects an elements on the mesh inside the cylinder
135 def selectInsideCylinder( mesh, x, y, z, dx, dy, dz, h, r, append = False ) :
136
137     [m, elemType] = _preProcess(mesh)
138     if m is None or elemType is None :
139         return
140
141     l = smesh.GetInsideCylinder( m, elemType, x, y, z, dx, dy, dz, h, r )
142     if len(l) > 0:
143         select(mesh, l, append)
144
145 # Selects an elements on the mesh inside the geometrical object
146 def selectInside( mesh, geom, tolerance , append = False ):
147
148     [m, elemType] = _preProcess(mesh)
149     if m is None or elemType is None :
150         return    
151
152     g = _getGeom(geom)
153
154     l = smesh.GetInside( m, elemType, g ,tolerance )
155     if len(l) > 0:
156         select(mesh, l, append)