1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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, or (at your option) any later version.
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
23 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 // File : SMESH_subMesh_i.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh_i.hxx"
30 #include "SMESHDS_SubMesh.hxx"
31 #include "SMESH_Gen_i.hxx"
32 #include "SMESH_Mesh_i.hxx"
33 #include "SMESH_MesherHelper.hxx"
34 #include "SMESH_PreMeshInfo.hxx"
36 #include "Utils_CorbaException.hxx"
37 #include "utilities.h"
39 #include "Utils_ExceptHandlers.hxx"
41 #include <TopoDS_Iterator.hxx>
45 //=============================================================================
49 //=============================================================================
51 SMESH_subMesh_i::SMESH_subMesh_i()
52 : SALOME::GenericObj_i( PortableServer::POA::_nil() )
54 MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i default, not for use");
58 //=============================================================================
62 //=============================================================================
64 SMESH_subMesh_i::SMESH_subMesh_i( PortableServer::POA_ptr thePOA,
68 : SALOME::GenericObj_i( thePOA )
75 //=============================================================================
79 //=============================================================================
81 SMESH_subMesh_i::~SMESH_subMesh_i()
83 if ( _preMeshInfo ) delete _preMeshInfo;
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 size_t size = theSubMeshList.size();
100 // check all child sub-meshes of one complexity,
101 // if no elements found and no algo assigned, go to children of lower complexity
103 TopoDS_Shape shape = theSubMesh->GetSubShape();
104 TopAbs_ShapeEnum mainType = SMESH_MesherHelper::GetGroupType( shape, /*noCompound=*/true );
105 TopAbs_ShapeEnum shapeType = shape.ShapeType();
106 bool elementsFound = false;
107 bool algoFound = false;
108 SMESH_subMeshIteratorPtr smIt = theSubMesh->getDependsOnIterator(/*includeSelf=*/true,
109 /*complexFirst=*/true);
110 while ( smIt->more() )
112 ::SMESH_subMesh* sm = smIt->next();
113 if ( sm->GetSubShape().ShapeType() != shapeType )
115 if ( elementsFound || algoFound )
117 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX &&
118 mainType != TopAbs_VERTEX )
121 shapeType = sm->GetSubShape().ShapeType();
122 if ( !sm->IsEmpty() )
124 elementsFound = true;
125 theSubMeshList.push_back( sm->GetSubMeshDS() );
131 return size < theSubMeshList.size();
134 //=============================================================================
138 //=============================================================================
140 CORBA::Long SMESH_subMesh_i::GetNumberOfElements()
141 throw (SALOME::SALOME_Exception)
143 Unexpect aCatch(SALOME_SalomeException);
146 return _preMeshInfo->NbElements();
148 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
151 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
155 TListOfSubMeshes smList;
156 if ( getSubMeshes( aSubMesh, smList ))
158 TListOfSubMeshes::iterator sm = smList.begin();
159 for ( ; sm != smList.end(); ++sm )
160 nbElems += (*sm)->NbElements();
165 //=============================================================================
169 //=============================================================================
171 CORBA::Long SMESH_subMesh_i::GetNumberOfNodes(CORBA::Boolean all)
172 throw (SALOME::SALOME_Exception)
174 Unexpect aCatch(SALOME_SalomeException);
176 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
181 if ( all ) return _preMeshInfo->NbNodes();
182 else _preMeshInfo->FullLoadFromFile();
184 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
185 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
187 if ( aSubMeshDS && aSubMeshDS->IsComplexSubmesh() )
189 // sub-mesh on a geom group, always return all nodes
190 return aSubMeshDS->NbNodes();
192 if ( aSubMeshDS && !all )
194 // return anything we have
195 return aSubMeshDS->NbNodes();
197 if ( all ) // get nodes from aSubMesh and all child sub-meshes
200 SMESH_subMeshIteratorPtr smIt = aSubMesh->getDependsOnIterator( /*includeSelf=*/true );
201 while ( smIt->more() )
203 aSubMesh = smIt->next();
204 if (( aSubMeshDS = aSubMesh->GetSubMeshDS() ))
205 nbNodes += aSubMeshDS->NbNodes();
210 return aSubMeshDS ? aSubMeshDS->NbNodes() : 0;
213 //=============================================================================
217 //=============================================================================
219 SMESH::long_array* SMESH_subMesh_i::GetElementsId()
220 throw (SALOME::SALOME_Exception)
222 Unexpect aCatch(SALOME_SalomeException);
224 SMESH::long_array_var aResult = new SMESH::long_array();
226 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
227 return aResult._retn();
230 _preMeshInfo->FullLoadFromFile();
232 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
235 TListOfSubMeshes smList;
236 if ( getSubMeshes( aSubMesh, smList ))
238 TListOfSubMeshes::iterator sm = smList.begin();
239 for ( ; sm != smList.end(); ++sm )
240 nbElems += (*sm)->NbElements();
243 aResult->length( nbElems );
246 TListOfSubMeshes::iterator sm = smList.begin();
247 for ( int i = 0; sm != smList.end(); sm++ )
249 SMDS_ElemIteratorPtr anIt = (*sm)->GetElements();
250 for ( ; i < nbElems && anIt->more(); i++ )
251 aResult[i] = anIt->next()->GetID();
254 return aResult._retn();
258 //=============================================================================
262 //=============================================================================
264 SMESH::long_array* SMESH_subMesh_i::GetElementsByType( SMESH::ElementType theElemType )
265 throw (SALOME::SALOME_Exception)
267 Unexpect aCatch(SALOME_SalomeException);
269 SMESH::long_array_var aResult = new SMESH::long_array();
271 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
272 return aResult._retn();
275 _preMeshInfo->FullLoadFromFile();
277 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
279 // PAL5440, return all nodes belonging to elements of the sub-mesh
283 // volumes may be bound to shell instead of solid
284 TListOfSubMeshes smList;
285 if ( getSubMeshes( aSubMesh, smList ))
287 TListOfSubMeshes::iterator sm = smList.begin();
288 for ( ; sm != smList.end(); ++sm )
290 if ( theElemType == SMESH::NODE )
292 SMDS_ElemIteratorPtr eIt = (*sm)->GetElements();
294 while ( eIt->more() ) {
295 const SMDS_MeshElement* anElem = eIt->next();
296 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
297 while ( nIt->more() )
298 nodeIds.insert( nIt->next()->GetID() );
301 SMDS_NodeIteratorPtr nIt = (*sm)->GetNodes();
302 while ( nIt->more() )
303 nodeIds.insert( nIt->next()->GetID() );
308 nbElems += (*sm)->NbElements();
313 if ( theElemType == SMESH::NODE )
314 aResult->length( nodeIds.size() );
316 aResult->length( nbElems );
318 int i = 0, n = aResult->length();
320 if ( theElemType == SMESH::NODE && !nodeIds.empty() ) {
321 set<int>::iterator idIt = nodeIds.begin();
322 for ( ; i < n && idIt != nodeIds.end() ; i++, idIt++ )
326 if ( theElemType != SMESH::NODE ) {
327 TListOfSubMeshes::iterator sm = smList.begin();
328 for ( i = 0; sm != smList.end(); sm++ )
330 SMDS_ElemIteratorPtr anIt = (*sm)->GetElements();
331 while ( i < n && anIt->more() ) {
332 const SMDS_MeshElement* anElem = anIt->next();
333 if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
334 aResult[i++] = anElem->GetID();
339 aResult->length( i );
341 return aResult._retn();
344 //=============================================================================
348 //=============================================================================
350 SMESH::long_array* SMESH_subMesh_i::GetNodesId()
351 throw (SALOME::SALOME_Exception)
353 Unexpect aCatch(SALOME_SalomeException);
355 SMESH::long_array_var aResult = GetElementsByType( SMESH::NODE );
356 return aResult._retn();
359 //=============================================================================
363 //=============================================================================
365 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetFather()
366 throw (SALOME::SALOME_Exception)
368 Unexpect aCatch(SALOME_SalomeException);
369 return _mesh_i->_this();
372 //=============================================================================
376 //=============================================================================
378 CORBA::Long SMESH_subMesh_i::GetId()
383 //=======================================================================
384 //function : GetSubShape
386 //=======================================================================
388 GEOM::GEOM_Object_ptr SMESH_subMesh_i::GetSubShape()
389 throw (SALOME::SALOME_Exception)
391 Unexpect aCatch(SALOME_SalomeException);
392 GEOM::GEOM_Object_var aShapeObj;
394 if ( _mesh_i->_mapSubMesh.find( _localId ) != _mesh_i->_mapSubMesh.end()) {
395 TopoDS_Shape S = _mesh_i->_mapSubMesh[ _localId ]->GetSubShape();
397 aShapeObj = _gen_i->ShapeToGeomObject( S );
398 //mzn: N7PAL16232, N7PAL16233
399 //In some cases it's possible that GEOM_Client contains the shape same to S, but
400 //with another orientation.
401 if (aShapeObj->_is_nil())
402 aShapeObj = _gen_i->ShapeToGeomObject( S.Reversed() );
406 catch(SALOME_Exception & S_ex) {
407 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
409 return aShapeObj._retn();
412 //=============================================================================
416 //=============================================================================
417 SMESH::long_array* SMESH_subMesh_i::GetIDs()
419 return GetElementsId();
422 //=============================================================================
426 //=============================================================================
427 SMESH::ElementType SMESH_subMesh_i::GetElementType( const CORBA::Long id, const bool iselem )
428 throw (SALOME::SALOME_Exception)
431 _preMeshInfo->FullLoadFromFile();
432 return GetFather()->GetElementType( id, iselem );
435 //=============================================================================
437 * Returns number of mesh elements of each \a EntityType
438 * @return array of number of elements per \a EntityType
440 //=============================================================================
442 SMESH::long_array* SMESH_subMesh_i::GetMeshInfo()
445 return _preMeshInfo->GetMeshInfo();
447 SMESH::long_array_var aRes = new SMESH::long_array();
448 aRes->length(SMESH::Entity_Last);
449 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
452 // get number of nodes
453 aRes[ SMESH::Entity_Node ] = GetNumberOfNodes(true);
455 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
457 // get statistic from child sub-meshes
458 TListOfSubMeshes smList;
459 if ( getSubMeshes( aSubMesh, smList ) )
460 for ( TListOfSubMeshes::iterator sm = smList.begin(); sm != smList.end(); ++sm )
461 SMESH_Mesh_i::CollectMeshInfo( (*sm)->GetElements(), aRes );
466 //=======================================================================
468 * Returns number of mesh elements of each \a ElementType
470 //=======================================================================
472 SMESH::long_array* SMESH_subMesh_i::GetNbElementsByType()
474 SMESH::long_array_var aRes = new SMESH::long_array();
475 aRes->length(SMESH::NB_ELEMENT_TYPES);
476 for (int i = 0; i < SMESH::NB_ELEMENT_TYPES; i++)
478 aRes[ i ] = _preMeshInfo->NbElements( SMDSAbs_ElementType( i ));
484 aRes[ SMESH::NODE ] = GetNumberOfNodes(true);
486 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
487 if ( SMESHDS_SubMesh* smDS = aSubMesh->GetSubMeshDS() )
489 SMDS_ElemIteratorPtr eIt = smDS->GetElements();
491 aRes[ eIt->next()->GetType() ] = smDS->NbElements();
498 //=======================================================================
499 //function : GetTypes
500 //purpose : Returns types of elements it contains
501 //=======================================================================
503 SMESH::array_of_ElementType* SMESH_subMesh_i::GetTypes()
506 return _preMeshInfo->GetTypes();
508 SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
510 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
511 if ( SMESHDS_SubMesh* smDS = aSubMesh->GetSubMeshDS() )
513 SMDS_ElemIteratorPtr eIt = smDS->GetElements();
517 types[0] = SMESH::ElementType( eIt->next()->GetType());
519 else if ( smDS->GetNodes()->more() )
521 TopoDS_Shape shape = aSubMesh->GetSubShape();
522 while ( !shape.IsNull() && shape.ShapeType() == TopAbs_COMPOUND )
524 TopoDS_Iterator it( shape );
525 shape = it.More() ? it.Value() : TopoDS_Shape();
527 if ( !shape.IsNull() && shape.ShapeType() == TopAbs_VERTEX )
530 types[0] = SMESH::NODE;
534 return types._retn();
537 //=======================================================================
539 //purpose : interface SMESH_IDSource
540 //=======================================================================
542 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetMesh()
547 //=======================================================================
548 //function : IsMeshInfoCorrect
549 //purpose : * Returns false if GetMeshInfo() returns incorrect information that may
550 // * happen if mesh data is not yet fully loaded from the file of study.
551 //=======================================================================
553 bool SMESH_subMesh_i::IsMeshInfoCorrect()
555 return _preMeshInfo ? _preMeshInfo->IsMeshInfoCorrect() : true;
558 //=======================================================================
559 //function : GetVtkUgStream
560 //purpose : Return data vtk unstructured grid (not implemented)
561 //=======================================================================
563 SALOMEDS::TMPFile* SMESH_subMesh_i::GetVtkUgStream()
565 SALOMEDS::TMPFile_var SeqFile;
566 return SeqFile._retn();