1 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESH_subMesh_i.cxx
25 // Author : Paul RASCLE, EDF
30 #include "SMESH_subMesh_i.hxx"
31 #include "SMESH_Gen_i.hxx"
32 #include "SMESH_Mesh_i.hxx"
34 #include "Utils_CorbaException.hxx"
35 #include "utilities.h"
37 #include "Utils_ExceptHandlers.hxx"
39 #include <BRepTools.hxx>
41 #include <TopoDS_Iterator.hxx>
43 //=============================================================================
47 //=============================================================================
49 SMESH_subMesh_i::SMESH_subMesh_i()
50 : SALOME::GenericObj_i( PortableServer::POA::_nil() )
52 MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i default, not for use");
56 //=============================================================================
60 //=============================================================================
62 SMESH_subMesh_i::SMESH_subMesh_i( PortableServer::POA_ptr thePOA,
66 : SALOME::GenericObj_i( thePOA )
68 MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i");
72 thePOA->activate_object( this );
75 //=============================================================================
79 //=============================================================================
81 SMESH_subMesh_i::~SMESH_subMesh_i()
83 MESSAGE("SMESH_subMesh_i::~SMESH_subMesh_i");
87 //=======================================================================
88 //function : getSubMeshes
89 //purpose : for a submesh on shape to which elements are not bound directly,
90 // return submeshes containing elements
91 //=======================================================================
93 typedef list<SMESHDS_SubMesh*> TListOfSubMeshes;
95 bool getSubMeshes(::SMESH_subMesh* theSubMesh,
96 TListOfSubMeshes& theSubMeshList)
98 int size = theSubMeshList.size();
100 SMESH_Mesh* aMesh = theSubMesh->GetFather();
101 SMESHDS_Mesh* aMeshDS = aMesh->GetMeshDS();
102 SMESHDS_SubMesh* aSubMeshDS = theSubMesh->GetSubMeshDS();
104 // nodes can be bound to either vertex, edge, face or solid_or_shell
105 TopoDS_Shape aShape = theSubMesh->GetSubShape();
106 switch ( aShape.ShapeType() )
109 // add submesh of solid itself
110 aSubMeshDS = aMeshDS->MeshElements( aShape );
112 theSubMeshList.push_back( aSubMeshDS );
113 // and of the first shell
114 TopExp_Explorer exp( aShape, TopAbs_SHELL );
116 aSubMeshDS = aMeshDS->MeshElements( exp.Current() );
118 theSubMeshList.push_back( aSubMeshDS );
123 case TopAbs_COMPOUND:
124 case TopAbs_COMPSOLID: {
125 // call getSubMeshes() for sub-shapes
126 list<TopoDS_Shape> shapeList;
127 shapeList.push_back( aShape );
128 list<TopoDS_Shape>::iterator sh = shapeList.begin();
129 for ( ; sh != shapeList.end(); ++sh ) {
130 for ( TopoDS_Iterator it( *sh ); it.More(); it.Next() ) {
131 ::SMESH_subMesh* aSubMesh = aMesh->GetSubMeshContaining( it.Value() );
133 getSubMeshes( aSubMesh, theSubMeshList );
135 // no submesh for a compound inside compound
136 shapeList.push_back( it.Value() );
139 // return only unique submeshes
140 set<SMESHDS_SubMesh*> smSet;
141 TListOfSubMeshes::iterator sm = theSubMeshList.begin();
142 while ( sm != theSubMeshList.end() ) {
143 if ( !smSet.insert( *sm ).second )
144 sm = theSubMeshList.erase( sm );
151 return size < theSubMeshList.size();
154 //=============================================================================
158 //=============================================================================
160 CORBA::Long SMESH_subMesh_i::GetNumberOfElements()
161 throw (SALOME::SALOME_Exception)
163 Unexpect aCatch(SALOME_SalomeException);
164 MESSAGE("SMESH_subMesh_i::GetNumberOfElements");
165 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
168 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
169 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
171 int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
173 // volumes are bound to shell
174 TListOfSubMeshes smList;
175 if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
177 TListOfSubMeshes::iterator sm = smList.begin();
178 for ( ; sm != smList.end(); ++sm )
179 nbElems += (*sm)->NbElements();
184 //=============================================================================
188 //=============================================================================
190 CORBA::Long SMESH_subMesh_i::GetNumberOfNodes(CORBA::Boolean all)
191 throw (SALOME::SALOME_Exception)
193 Unexpect aCatch(SALOME_SalomeException);
194 MESSAGE("SMESH_subMesh_i::GetNumberOfNodes");
195 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
198 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
199 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
203 // nodes are bound to shell instead of solid
204 TListOfSubMeshes smList;
205 if ( all && getSubMeshes( aSubMesh, smList ))
207 TListOfSubMeshes::iterator sm = smList.begin();
208 for ( ; sm != smList.end(); ++sm )
210 SMDS_ElemIteratorPtr eIt = (*sm)->GetElements();
211 while ( eIt->more() ) {
212 const SMDS_MeshElement* anElem = eIt->next();
213 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
214 while ( nIt->more() )
215 nodeIds.insert( nIt->next()->GetID() );
218 return nodeIds.size();
221 if ( aSubMeshDS == NULL )
224 if ( all ) { // all nodes of submesh elements
225 SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
226 while ( eIt->more() ) {
227 const SMDS_MeshElement* anElem = eIt->next();
228 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
229 while ( nIt->more() )
230 nodeIds.insert( nIt->next()->GetID() );
232 return nodeIds.size();
235 return aSubMeshDS->NbNodes();
238 //=============================================================================
242 //=============================================================================
244 SMESH::long_array* SMESH_subMesh_i::GetElementsId()
245 throw (SALOME::SALOME_Exception)
247 Unexpect aCatch(SALOME_SalomeException);
248 MESSAGE("SMESH_subMesh_i::GetElementsId");
249 SMESH::long_array_var aResult = new SMESH::long_array();
251 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
252 return aResult._retn();
254 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
255 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
257 int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
258 TListOfSubMeshes smList;
260 smList.push_back( aSubMeshDS );
262 // volumes are bound to shell
263 if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
265 TListOfSubMeshes::iterator sm = smList.begin();
266 for ( ; sm != smList.end(); ++sm )
267 nbElems += (*sm)->NbElements();
270 aResult->length( nbElems );
273 TListOfSubMeshes::iterator sm = smList.begin();
274 for ( int i = 0; sm != smList.end(); sm++ )
276 SMDS_ElemIteratorPtr anIt = (*sm)->GetElements();
277 for ( int n = aSubMeshDS->NbElements(); i < n && anIt->more(); i++ )
278 aResult[i] = anIt->next()->GetID();
281 return aResult._retn();
285 //=============================================================================
289 //=============================================================================
291 SMESH::long_array* SMESH_subMesh_i::GetElementsByType( SMESH::ElementType theElemType )
292 throw (SALOME::SALOME_Exception)
294 Unexpect aCatch(SALOME_SalomeException);
295 MESSAGE("SMESH_subMesh_i::GetElementsByType");
296 SMESH::long_array_var aResult = new SMESH::long_array();
298 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
299 return aResult._retn();
301 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
302 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
304 // PAL5440, return all nodes belonging to elements of submesh
306 int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
308 // volumes may be bound to shell instead of solid
309 TListOfSubMeshes smList;
310 if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
312 TListOfSubMeshes::iterator sm = smList.begin();
313 for ( ; sm != smList.end(); ++sm )
315 if ( theElemType == SMESH::NODE )
317 SMDS_ElemIteratorPtr eIt = (*sm)->GetElements();
318 while ( eIt->more() ) {
319 const SMDS_MeshElement* anElem = eIt->next();
320 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
321 while ( nIt->more() )
322 nodeIds.insert( nIt->next()->GetID() );
327 nbElems += (*sm)->NbElements();
335 smList.push_back( aSubMeshDS );
338 if ( theElemType == SMESH::NODE && aSubMeshDS )
340 SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
341 while ( eIt->more() ) {
342 const SMDS_MeshElement* anElem = eIt->next();
343 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
344 while ( nIt->more() )
345 nodeIds.insert( nIt->next()->GetID() );
349 if ( theElemType == SMESH::NODE )
350 aResult->length( nodeIds.size() );
352 aResult->length( nbElems );
354 int i = 0, n = aResult->length();
356 if ( theElemType == SMESH::NODE && !nodeIds.empty() ) {
357 set<int>::iterator idIt = nodeIds.begin();
358 for ( ; i < n && idIt != nodeIds.end() ; i++, idIt++ )
362 if ( theElemType != SMESH::NODE ) {
363 TListOfSubMeshes::iterator sm = smList.begin();
364 for ( i = 0; sm != smList.end(); sm++ )
367 SMDS_ElemIteratorPtr anIt = aSubMeshDS->GetElements();
368 while ( i < n && anIt->more() ) {
369 const SMDS_MeshElement* anElem = anIt->next();
370 if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
371 aResult[i++] = anElem->GetID();
376 aResult->length( i );
378 return aResult._retn();
381 //=============================================================================
385 //=============================================================================
387 SMESH::long_array* SMESH_subMesh_i::GetNodesId()
388 throw (SALOME::SALOME_Exception)
390 Unexpect aCatch(SALOME_SalomeException);
391 MESSAGE("SMESH_subMesh_i::GetNodesId");
392 SMESH::long_array_var aResult = GetElementsByType( SMESH::NODE );
393 return aResult._retn();
396 //=============================================================================
400 //=============================================================================
402 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetFather()
403 throw (SALOME::SALOME_Exception)
405 Unexpect aCatch(SALOME_SalomeException);
406 MESSAGE("SMESH_subMesh_i::GetFather");
407 return _mesh_i->_this();
410 //=============================================================================
414 //=============================================================================
416 CORBA::Long SMESH_subMesh_i::GetId()
418 MESSAGE("SMESH_subMesh_i::GetId");
422 //=======================================================================
423 //function : GetSubShape
425 //=======================================================================
427 GEOM::GEOM_Object_ptr SMESH_subMesh_i::GetSubShape()
428 throw (SALOME::SALOME_Exception)
430 Unexpect aCatch(SALOME_SalomeException);
431 GEOM::GEOM_Object_var aShapeObj;
433 if ( _mesh_i->_mapSubMesh.find( _localId ) != _mesh_i->_mapSubMesh.end()) {
434 TopoDS_Shape S = _mesh_i->_mapSubMesh[ _localId ]->GetSubShape();
436 aShapeObj = _gen_i->ShapeToGeomObject( S );
439 catch(SALOME_Exception & S_ex) {
440 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
442 return aShapeObj._retn();
445 //=============================================================================
449 //=============================================================================
450 SALOME_MED::FAMILY_ptr SMESH_subMesh_i::GetFamily()
451 throw (SALOME::SALOME_Exception)
453 Unexpect aCatch(SALOME_SalomeException);
454 SALOME_MED::MESH_var MEDMesh = GetFather()->GetMEDMesh();
456 SALOME_MED::Family_array_var families =
457 MEDMesh->getFamilies(SALOME_MED::MED_NODE);
459 for ( int i = 0; i < families->length(); i++ ) {
460 if ( families[i]->getIdentifier() == ( _localId ) )
464 return SALOME_MED::FAMILY::_nil();
467 //=============================================================================
471 //=============================================================================
472 SMESH::long_array* SMESH_subMesh_i::GetIDs()
474 SMESH::long_array_var aResult = GetElementsId();
475 return aResult._retn();
478 //=============================================================================
482 //=============================================================================
483 SMESH::ElementType SMESH_subMesh_i::GetElementType( const CORBA::Long id, const bool iselem )
484 throw (SALOME::SALOME_Exception)
486 return GetFather()->GetElementType( id, iselem );