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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 if ( ::SMESH_subMesh* aSubMesh = aMesh->GetSubMeshContaining( it.Value() ))
132 getSubMeshes( aSubMesh, theSubMeshList ); // add found submesh or explore deeper
134 // no submesh for a compound inside compound
135 shapeList.push_back( it.Value() );
138 // return only unique submeshes
139 set<SMESHDS_SubMesh*> smSet;
140 TListOfSubMeshes::iterator sm = theSubMeshList.begin();
141 while ( sm != theSubMeshList.end() ) {
142 if ( !smSet.insert( *sm ).second )
143 sm = theSubMeshList.erase( sm );
151 theSubMeshList.push_back( aSubMeshDS );
153 return size < theSubMeshList.size();
156 //=============================================================================
160 //=============================================================================
162 CORBA::Long SMESH_subMesh_i::GetNumberOfElements()
163 throw (SALOME::SALOME_Exception)
165 Unexpect aCatch(SALOME_SalomeException);
166 MESSAGE("SMESH_subMesh_i::GetNumberOfElements");
167 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
170 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
171 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
173 int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
175 // volumes are bound to shell
176 TListOfSubMeshes smList;
177 if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
179 TListOfSubMeshes::iterator sm = smList.begin();
180 for ( ; sm != smList.end(); ++sm )
181 nbElems += (*sm)->NbElements();
186 //=============================================================================
190 //=============================================================================
192 CORBA::Long SMESH_subMesh_i::GetNumberOfNodes(CORBA::Boolean all)
193 throw (SALOME::SALOME_Exception)
195 Unexpect aCatch(SALOME_SalomeException);
196 MESSAGE("SMESH_subMesh_i::GetNumberOfNodes");
197 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
200 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
201 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
205 // nodes are bound to shell instead of solid
206 TListOfSubMeshes smList;
207 if ( all && getSubMeshes( aSubMesh, smList ))
209 TListOfSubMeshes::iterator sm = smList.begin();
210 for ( ; sm != smList.end(); ++sm )
212 SMDS_ElemIteratorPtr eIt = (*sm)->GetElements();
213 while ( eIt->more() ) {
214 const SMDS_MeshElement* anElem = eIt->next();
215 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
216 while ( nIt->more() )
217 nodeIds.insert( nIt->next()->GetID() );
220 return nodeIds.size();
223 if ( aSubMeshDS == NULL )
226 if ( all ) { // all nodes of submesh elements
227 SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
228 while ( eIt->more() ) {
229 const SMDS_MeshElement* anElem = eIt->next();
230 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
231 while ( nIt->more() )
232 nodeIds.insert( nIt->next()->GetID() );
234 return nodeIds.size();
237 return aSubMeshDS->NbNodes();
240 //=============================================================================
244 //=============================================================================
246 SMESH::long_array* SMESH_subMesh_i::GetElementsId()
247 throw (SALOME::SALOME_Exception)
249 Unexpect aCatch(SALOME_SalomeException);
250 MESSAGE("SMESH_subMesh_i::GetElementsId");
251 SMESH::long_array_var aResult = new SMESH::long_array();
253 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
254 return aResult._retn();
256 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
257 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
259 int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
260 TListOfSubMeshes smList;
262 smList.push_back( aSubMeshDS );
264 // volumes are bound to shell
265 if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
267 TListOfSubMeshes::iterator sm = smList.begin();
268 for ( ; sm != smList.end(); ++sm )
269 nbElems += (*sm)->NbElements();
272 aResult->length( nbElems );
275 TListOfSubMeshes::iterator sm = smList.begin();
276 for ( int i = 0; sm != smList.end(); sm++ )
278 SMDS_ElemIteratorPtr anIt = (*sm)->GetElements();
279 for ( ; i < nbElems && anIt->more(); i++ )
280 aResult[i] = anIt->next()->GetID();
283 return aResult._retn();
287 //=============================================================================
291 //=============================================================================
293 SMESH::long_array* SMESH_subMesh_i::GetElementsByType( SMESH::ElementType theElemType )
294 throw (SALOME::SALOME_Exception)
296 Unexpect aCatch(SALOME_SalomeException);
297 MESSAGE("SMESH_subMesh_i::GetElementsByType");
298 SMESH::long_array_var aResult = new SMESH::long_array();
300 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
301 return aResult._retn();
303 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
304 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
306 // PAL5440, return all nodes belonging to elements of submesh
308 int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
310 // volumes may be bound to shell instead of solid
311 TListOfSubMeshes smList;
312 if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
314 TListOfSubMeshes::iterator sm = smList.begin();
315 for ( ; sm != smList.end(); ++sm )
317 if ( theElemType == SMESH::NODE )
319 SMDS_ElemIteratorPtr eIt = (*sm)->GetElements();
320 while ( eIt->more() ) {
321 const SMDS_MeshElement* anElem = eIt->next();
322 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
323 while ( nIt->more() )
324 nodeIds.insert( nIt->next()->GetID() );
329 nbElems += (*sm)->NbElements();
337 smList.push_back( aSubMeshDS );
340 if ( theElemType == SMESH::NODE && aSubMeshDS )
342 SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
343 while ( eIt->more() ) {
344 const SMDS_MeshElement* anElem = eIt->next();
345 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
346 while ( nIt->more() )
347 nodeIds.insert( nIt->next()->GetID() );
351 if ( theElemType == SMESH::NODE )
352 aResult->length( nodeIds.size() );
354 aResult->length( nbElems );
356 int i = 0, n = aResult->length();
358 if ( theElemType == SMESH::NODE && !nodeIds.empty() ) {
359 set<int>::iterator idIt = nodeIds.begin();
360 for ( ; i < n && idIt != nodeIds.end() ; i++, idIt++ )
364 if ( theElemType != SMESH::NODE ) {
365 TListOfSubMeshes::iterator sm = smList.begin();
366 for ( i = 0; sm != smList.end(); sm++ )
369 SMDS_ElemIteratorPtr anIt = aSubMeshDS->GetElements();
370 while ( i < n && anIt->more() ) {
371 const SMDS_MeshElement* anElem = anIt->next();
372 if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
373 aResult[i++] = anElem->GetID();
378 aResult->length( i );
380 return aResult._retn();
383 //=============================================================================
387 //=============================================================================
389 SMESH::long_array* SMESH_subMesh_i::GetNodesId()
390 throw (SALOME::SALOME_Exception)
392 Unexpect aCatch(SALOME_SalomeException);
393 MESSAGE("SMESH_subMesh_i::GetNodesId");
394 SMESH::long_array_var aResult = GetElementsByType( SMESH::NODE );
395 return aResult._retn();
398 //=============================================================================
402 //=============================================================================
404 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetFather()
405 throw (SALOME::SALOME_Exception)
407 Unexpect aCatch(SALOME_SalomeException);
408 MESSAGE("SMESH_subMesh_i::GetFather");
409 return _mesh_i->_this();
412 //=============================================================================
416 //=============================================================================
418 CORBA::Long SMESH_subMesh_i::GetId()
420 MESSAGE("SMESH_subMesh_i::GetId");
424 //=======================================================================
425 //function : GetSubShape
427 //=======================================================================
429 GEOM::GEOM_Object_ptr SMESH_subMesh_i::GetSubShape()
430 throw (SALOME::SALOME_Exception)
432 Unexpect aCatch(SALOME_SalomeException);
433 GEOM::GEOM_Object_var aShapeObj;
435 if ( _mesh_i->_mapSubMesh.find( _localId ) != _mesh_i->_mapSubMesh.end()) {
436 TopoDS_Shape S = _mesh_i->_mapSubMesh[ _localId ]->GetSubShape();
438 aShapeObj = _gen_i->ShapeToGeomObject( S );
441 catch(SALOME_Exception & S_ex) {
442 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
444 return aShapeObj._retn();
447 //=============================================================================
451 //=============================================================================
452 SALOME_MED::FAMILY_ptr SMESH_subMesh_i::GetFamily()
453 throw (SALOME::SALOME_Exception)
455 Unexpect aCatch(SALOME_SalomeException);
456 SALOME_MED::MESH_var MEDMesh = GetFather()->GetMEDMesh();
458 SALOME_MED::Family_array_var families =
459 MEDMesh->getFamilies(SALOME_MED::MED_NODE);
461 for ( int i = 0; i < families->length(); i++ ) {
462 if ( families[i]->getIdentifier() == ( _localId ) )
466 return SALOME_MED::FAMILY::_nil();
469 //=============================================================================
473 //=============================================================================
474 SMESH::long_array* SMESH_subMesh_i::GetIDs()
476 SMESH::long_array_var aResult = GetElementsId();
477 return aResult._retn();
480 //=============================================================================
484 //=============================================================================
485 SMESH::ElementType SMESH_subMesh_i::GetElementType( const CORBA::Long id, const bool iselem )
486 throw (SALOME::SALOME_Exception)
488 return GetFather()->GetElementType( id, iselem );