1 // Copyright (C) 2007-2020 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 classes
24 // File : SMESH_subMesh_i.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh_i.hxx"
30 #include "SMESHDS_Mesh.hxx"
31 #include "SMESHDS_SubMesh.hxx"
32 #include "SMESH_Gen_i.hxx"
33 #include "SMESH_Mesh_i.hxx"
34 #include "SMESH_MesherHelper.hxx"
35 #include "SMESH_PreMeshInfo.hxx"
37 #include "Utils_CorbaException.hxx"
38 #include "utilities.h"
40 #include "Utils_ExceptHandlers.hxx"
42 #include <TopoDS_Iterator.hxx>
46 //=============================================================================
50 //=============================================================================
52 SMESH_subMesh_i::SMESH_subMesh_i()
53 : SALOME::GenericObj_i( PortableServer::POA::_nil() )
55 MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i default, not for use");
59 //=============================================================================
63 //=============================================================================
65 SMESH_subMesh_i::SMESH_subMesh_i( PortableServer::POA_ptr thePOA,
69 : SALOME::GenericObj_i( thePOA )
76 //=============================================================================
80 //=============================================================================
82 SMESH_subMesh_i::~SMESH_subMesh_i()
84 if ( _preMeshInfo ) delete _preMeshInfo;
88 //=======================================================================
89 //function : getSubMeshes
90 //purpose : for a submesh on shape to which elements are not bound directly,
91 // return submeshes containing elements
92 //=======================================================================
94 typedef list<SMESHDS_SubMesh*> TListOfSubMeshes;
96 bool getSubMeshes(::SMESH_subMesh* theSubMesh,
97 TListOfSubMeshes& theSubMeshList)
100 return false; // "invalid sub-mesh" created by SMESH_Gen_i::CopyMeshWithGeom()
101 size_t size = theSubMeshList.size();
103 // check all child sub-meshes of one complexity,
104 // if no elements found and no algo assigned, go to children of lower complexity
106 TopoDS_Shape shape = theSubMesh->GetSubShape();
107 TopAbs_ShapeEnum mainType = SMESH_MesherHelper::GetGroupType( shape, /*noCompound=*/true );
108 TopAbs_ShapeEnum shapeType = shape.ShapeType();
109 bool elementsFound = false;
110 bool algoFound = false;
111 SMESH_subMeshIteratorPtr smIt = theSubMesh->getDependsOnIterator(/*includeSelf=*/true,
112 /*complexFirst=*/true);
113 while ( smIt->more() )
115 ::SMESH_subMesh* sm = smIt->next();
116 if ( sm->GetSubShape().ShapeType() != shapeType )
118 if ( elementsFound || algoFound )
120 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX &&
121 mainType != TopAbs_VERTEX )
124 shapeType = sm->GetSubShape().ShapeType();
125 if ( !sm->IsEmpty() )
127 elementsFound = true;
128 theSubMeshList.push_back( sm->GetSubMeshDS() );
134 return size < theSubMeshList.size();
137 //=============================================================================
141 //=============================================================================
143 CORBA::Long SMESH_subMesh_i::GetNumberOfElements()
145 Unexpect aCatch(SALOME_SalomeException);
148 return _preMeshInfo->NbElements();
150 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
153 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
157 TListOfSubMeshes smList;
158 if ( getSubMeshes( aSubMesh, smList ))
160 TListOfSubMeshes::iterator sm = smList.begin();
161 for ( ; sm != smList.end(); ++sm )
162 nbElems += (*sm)->NbElements();
167 //=============================================================================
171 //=============================================================================
173 CORBA::Long SMESH_subMesh_i::GetNumberOfNodes(CORBA::Boolean all)
175 Unexpect aCatch(SALOME_SalomeException);
177 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
182 if ( all ) return _preMeshInfo->NbNodes();
183 else _preMeshInfo->FullLoadFromFile();
185 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
186 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
188 if ( aSubMeshDS && aSubMeshDS->IsComplexSubmesh() )
190 // sub-mesh on a geom group, always return all nodes
191 return aSubMeshDS->NbNodes();
193 if ( aSubMeshDS && !all )
195 // return anything we have
196 return aSubMeshDS->NbNodes();
198 if ( all ) // get nodes from aSubMesh and all child sub-meshes
201 SMESH_subMeshIteratorPtr smIt = aSubMesh->getDependsOnIterator( /*includeSelf=*/true );
202 while ( smIt->more() )
204 aSubMesh = smIt->next();
205 if (( aSubMeshDS = aSubMesh->GetSubMeshDS() ))
206 nbNodes += aSubMeshDS->NbNodes();
211 return aSubMeshDS ? aSubMeshDS->NbNodes() : 0;
214 //=============================================================================
218 //=============================================================================
220 SMESH::long_array* SMESH_subMesh_i::GetElementsId()
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 )
266 Unexpect aCatch(SALOME_SalomeException);
268 SMESH::long_array_var aResult = new SMESH::long_array();
270 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
271 return aResult._retn();
274 _preMeshInfo->FullLoadFromFile();
276 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
278 // PAL5440, return all nodes belonging to elements of the sub-mesh
282 // volumes may be bound to shell instead of solid
283 TListOfSubMeshes smList;
284 if ( getSubMeshes( aSubMesh, smList ))
286 TListOfSubMeshes::iterator sm = smList.begin();
287 for ( ; sm != smList.end(); ++sm )
289 if ( theElemType == SMESH::NODE )
291 SMDS_ElemIteratorPtr eIt = (*sm)->GetElements();
293 while ( eIt->more() ) {
294 const SMDS_MeshElement* anElem = eIt->next();
295 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
296 while ( nIt->more() )
297 nodeIds.insert( nIt->next()->GetID() );
300 SMDS_NodeIteratorPtr nIt = (*sm)->GetNodes();
301 while ( nIt->more() )
302 nodeIds.insert( nIt->next()->GetID() );
307 nbElems += (*sm)->NbElements();
312 if ( theElemType == SMESH::NODE )
313 aResult->length( nodeIds.size() );
315 aResult->length( nbElems );
317 int i = 0, n = aResult->length();
319 if ( theElemType == SMESH::NODE && !nodeIds.empty() ) {
320 set<int>::iterator idIt = nodeIds.begin();
321 for ( ; i < n && idIt != nodeIds.end() ; i++, idIt++ )
325 if ( theElemType != SMESH::NODE ) {
326 TListOfSubMeshes::iterator sm = smList.begin();
327 for ( i = 0; sm != smList.end(); sm++ )
329 SMDS_ElemIteratorPtr anIt = (*sm)->GetElements();
330 while ( i < n && anIt->more() ) {
331 const SMDS_MeshElement* anElem = anIt->next();
332 if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
333 aResult[i++] = anElem->GetID();
338 aResult->length( i );
340 return aResult._retn();
343 //=============================================================================
347 //=============================================================================
349 SMESH::long_array* SMESH_subMesh_i::GetNodesId()
351 Unexpect aCatch(SALOME_SalomeException);
353 SMESH::long_array_var aResult = GetElementsByType( SMESH::NODE );
354 return aResult._retn();
357 //=============================================================================
361 //=============================================================================
363 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetFather()
365 Unexpect aCatch(SALOME_SalomeException);
366 return _mesh_i->_this();
369 //=============================================================================
373 //=============================================================================
375 CORBA::Long SMESH_subMesh_i::GetId()
380 //=======================================================================
381 //function : GetSubShape
383 //=======================================================================
385 GEOM::GEOM_Object_ptr SMESH_subMesh_i::GetSubShape()
387 Unexpect aCatch(SALOME_SalomeException);
388 GEOM::GEOM_Object_var aShapeObj;
390 if ( _mesh_i->_mapSubMesh.find( _localId ) != _mesh_i->_mapSubMesh.end()) {
391 TopoDS_Shape S = _mesh_i->_mapSubMesh[ _localId ]->GetSubShape();
393 aShapeObj = _gen_i->ShapeToGeomObject( S );
394 //mzn: N7PAL16232, N7PAL16233
395 //In some cases it's possible that GEOM_Client contains the shape same to S, but
396 //with another orientation.
397 if (aShapeObj->_is_nil())
398 aShapeObj = _gen_i->ShapeToGeomObject( S.Reversed() );
402 catch(SALOME_Exception & S_ex) {
403 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
405 return aShapeObj._retn();
408 //=============================================================================
412 //=============================================================================
413 SMESH::long_array* SMESH_subMesh_i::GetIDs()
415 return GetElementsId();
418 //=============================================================================
422 //=============================================================================
423 SMESH::ElementType SMESH_subMesh_i::GetElementType( const CORBA::Long id, const bool iselem )
426 _preMeshInfo->FullLoadFromFile();
427 return GetFather()->GetElementType( id, iselem );
430 //=============================================================================
432 * Returns number of mesh elements of each \a EntityType
433 * @return array of number of elements per \a EntityType
435 //=============================================================================
437 SMESH::long_array* SMESH_subMesh_i::GetMeshInfo()
440 return _preMeshInfo->GetMeshInfo();
442 SMESH::long_array_var aRes = new SMESH::long_array();
443 aRes->length(SMESH::Entity_Last);
444 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
447 // get number of nodes
448 aRes[ SMESH::Entity_Node ] = GetNumberOfNodes(true);
450 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
452 // get statistic from child sub-meshes
453 TListOfSubMeshes smList;
454 if ( getSubMeshes( aSubMesh, smList ) )
455 for ( TListOfSubMeshes::iterator sm = smList.begin(); sm != smList.end(); ++sm )
456 SMESH_Mesh_i::CollectMeshInfo( (*sm)->GetElements(), aRes );
461 //=======================================================================
463 * Returns number of mesh elements of each \a ElementType
465 //=======================================================================
467 SMESH::long_array* SMESH_subMesh_i::GetNbElementsByType()
469 SMESH::long_array_var aRes = new SMESH::long_array();
470 aRes->length(SMESH::NB_ELEMENT_TYPES);
471 for (int i = 0; i < SMESH::NB_ELEMENT_TYPES; i++)
473 aRes[ i ] = _preMeshInfo->NbElements( SMDSAbs_ElementType( i ));
479 aRes[ SMESH::NODE ] = GetNumberOfNodes(true);
481 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
482 TListOfSubMeshes smList;
483 if ( getSubMeshes( aSubMesh, smList ))
485 TListOfSubMeshes::iterator smDS = smList.begin();
486 for ( ; smDS != smList.end(); ++smDS )
488 SMDS_ElemIteratorPtr eIt = (*smDS)->GetElements();
490 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];
512 TListOfSubMeshes smList;
513 if ( getSubMeshes( aSubMesh, smList ))
515 TListOfSubMeshes::iterator smDS = smList.begin();
516 for ( ; smDS != smList.end(); ++smDS )
518 SMDS_ElemIteratorPtr eIt = (*smDS)->GetElements();
522 types[0] = SMESH::ElementType( eIt->next()->GetType());
527 if ( types->length() == 0 )
529 for ( smDS = smList.begin(); smDS != smList.end(); ++smDS )
531 if ( (*smDS)->GetNodes()->more() )
533 int smID = (*smDS)->GetID();
534 TopoDS_Shape shape = (*smDS)->GetParent()->IndexToShape( smID );
535 if ( !shape.IsNull() && shape.ShapeType() == TopAbs_VERTEX )
538 types[0] = SMESH::NODE;
545 return types._retn();
548 //=======================================================================
550 //purpose : interface SMESH_IDSource
551 //=======================================================================
553 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetMesh()
558 //=======================================================================
559 //function : IsMeshInfoCorrect
560 //purpose : * Returns false if GetMeshInfo() returns incorrect information that may
561 // * happen if mesh data is not yet fully loaded from the file of study.
562 //=======================================================================
564 bool SMESH_subMesh_i::IsMeshInfoCorrect()
566 return _preMeshInfo ? _preMeshInfo->IsMeshInfoCorrect() : true;
569 //=======================================================================
570 //function : GetVtkUgStream
571 //purpose : Return data vtk unstructured grid (not implemented)
572 //=======================================================================
574 SALOMEDS::TMPFile* SMESH_subMesh_i::GetVtkUgStream()
576 SALOMEDS::TMPFile_var SeqFile;
577 return SeqFile._retn();
580 //=======================================================================
581 //function : SMESH_Invalid_subMesh_i
582 //purpose : constructor of "invalid sub-mesh" created by SMESH_Gen_i::CopyMeshWithGeom()
583 //=======================================================================
585 SMESH_Invalid_subMesh_i::SMESH_Invalid_subMesh_i( PortableServer::POA_ptr thePOA,
587 SMESH_Mesh_i* mesh_i,
589 GEOM::GEOM_Object_ptr shape )
590 : SALOME::GenericObj_i( thePOA ),
591 SMESH_subMesh_i( thePOA, gen_i, mesh_i, localId )
593 _geom = GEOM::GEOM_Object::_duplicate( shape );
596 //=======================================================================
597 //function : GetSubShape
598 //purpose : return geometry which is not a sub-shape of the main shape
599 //=======================================================================
601 GEOM::GEOM_Object_ptr SMESH_Invalid_subMesh_i::GetSubShape()
603 return GEOM::GEOM_Object::_duplicate( _geom );