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_Mesh_i.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_Mesh_i.hxx"
30 #include "SMESH_subMesh_i.hxx"
31 #include "SMESH_MEDMesh_i.hxx"
32 #include "SMESH_Group_i.hxx"
33 #include "SMESH_Filter_i.hxx"
35 #include "Utils_CorbaException.hxx"
36 #include "Utils_ExceptHandlers.hxx"
37 #include "utilities.h"
39 #include "SALOME_NamingService.hxx"
40 #include "Utils_SINGLETON.hxx"
43 #include "TCollection_AsciiString.hxx"
44 #include "SMESHDS_Command.hxx"
45 #include "SMESHDS_CommandType.hxx"
46 #include "SMESH_MeshEditor_i.hxx"
47 #include "SMESH_Gen_i.hxx"
48 #include "DriverMED_R_SMESHDS_Mesh.h"
50 #include <TColStd_MapOfInteger.hxx>
51 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
52 #include <TColStd_SequenceOfInteger.hxx>
59 static int MYDEBUG = 0;
61 static int MYDEBUG = 0;
66 int SMESH_Mesh_i::myIdGenerator = 0;
68 //=============================================================================
72 //=============================================================================
74 SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
77 : SALOME::GenericObj_i( thePOA )
79 INFOS("SMESH_Mesh_i");
81 _id = myIdGenerator++;
83 thePOA->activate_object( this );
86 //=============================================================================
90 //=============================================================================
92 SMESH_Mesh_i::~SMESH_Mesh_i()
94 INFOS("~SMESH_Mesh_i");
95 map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
96 for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
97 SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
100 // this method is colled from destructor of group (PAL6331)
101 //_impl->RemoveGroup( aGroup->GetLocalID() );
109 //=============================================================================
113 * Associates <this> mesh with <theShape> and puts a reference
114 * to <theShape> into the current study;
115 * the previous shape is substituted by the new one.
117 //=============================================================================
119 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
120 throw (SALOME::SALOME_Exception)
122 Unexpect aCatch(SALOME_SalomeException);
124 _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
126 catch(SALOME_Exception & S_ex) {
127 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
131 //=======================================================================
132 //function : GetShapeToMesh
134 //=======================================================================
136 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
137 throw (SALOME::SALOME_Exception)
139 Unexpect aCatch(SALOME_SalomeException);
140 GEOM::GEOM_Object_var aShapeObj;
142 TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
144 aShapeObj = _gen_i->ShapeToGeomObject( S );
146 catch(SALOME_Exception & S_ex) {
147 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
149 return aShapeObj._retn();
152 //=============================================================================
156 //=============================================================================
158 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
160 SMESH::DriverMED_ReadStatus res;
163 case DriverMED_R_SMESHDS_Mesh::DRS_OK:
164 res = SMESH::DRS_OK; break;
165 case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
166 res = SMESH::DRS_EMPTY; break;
167 case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
168 res = SMESH::DRS_WARN_RENUMBER; break;
169 case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
170 res = SMESH::DRS_WARN_SKIP_ELEM; break;
171 case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
173 res = SMESH::DRS_FAIL; break;
178 //=============================================================================
182 * Imports mesh data from MED file
184 //=============================================================================
186 SMESH::DriverMED_ReadStatus
187 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
188 throw ( SALOME::SALOME_Exception )
190 Unexpect aCatch(SALOME_SalomeException);
193 status = importMEDFile( theFileName, theMeshName );
195 catch( SALOME_Exception& S_ex ) {
196 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
199 THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
202 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
203 if ( !aStudy->_is_nil() ) {
204 // publishing of the groups in the study (sub-meshes are out of scope of MED import)
205 map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
206 for (; it != _mapGroups.end(); it++ ) {
207 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
208 _gen_i->PublishGroup( aStudy, _this(), aGroup,
209 GEOM::GEOM_Object::_nil(), aGroup->GetName());
212 return ConvertDriverMEDReadStatus(status);
215 //=============================================================================
219 * Imports mesh data from MED file
221 //=============================================================================
223 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
224 throw ( SALOME::SALOME_Exception )
226 // Read mesh with name = <theMeshName> into SMESH_Mesh
227 _impl->UNVToMesh( theFileName );
232 //=============================================================================
236 * Imports mesh data from STL file
238 //=============================================================================
239 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
240 throw ( SALOME::SALOME_Exception )
242 // Read mesh with name = <theMeshName> into SMESH_Mesh
243 _impl->STLToMesh( theFileName );
248 //=============================================================================
252 * Imports mesh data from MED file
254 //=============================================================================
256 int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
258 // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
259 int status = _impl->MEDToMesh( theFileName, theMeshName );
261 // Create group servants, if any groups were imported
262 list<int> aGroupIds = _impl->GetGroupIds();
263 for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
264 SMESH_Group_i* aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
266 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
267 SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
268 aGroupImpl->Register();
269 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
271 SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
272 _mapGroups[*it] = SMESH::SMESH_Group::_duplicate( aGroup );
274 // register CORBA object for persistence
275 int nextId = _gen_i->RegisterObject( aGroup );
276 if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
282 //=============================================================================
286 //=============================================================================
288 static SMESH::Hypothesis_Status ConvertHypothesisStatus
289 (SMESH_Hypothesis::Hypothesis_Status theStatus)
291 SMESH::Hypothesis_Status res;
294 case SMESH_Hypothesis::HYP_OK:
295 res = SMESH::HYP_OK; break;
296 case SMESH_Hypothesis::HYP_MISSING:
297 res = SMESH::HYP_MISSING; break;
298 case SMESH_Hypothesis::HYP_CONCURENT:
299 res = SMESH::HYP_CONCURENT; break;
300 case SMESH_Hypothesis::HYP_BAD_PARAMETER:
301 res = SMESH::HYP_BAD_PARAMETER; break;
302 case SMESH_Hypothesis::HYP_INCOMPATIBLE:
303 res = SMESH::HYP_INCOMPATIBLE; break;
304 case SMESH_Hypothesis::HYP_NOTCONFORM:
305 res = SMESH::HYP_NOTCONFORM; break;
306 case SMESH_Hypothesis::HYP_ALREADY_EXIST:
307 res = SMESH::HYP_ALREADY_EXIST; break;
308 case SMESH_Hypothesis::HYP_BAD_DIM:
309 res = SMESH::HYP_BAD_DIM; break;
311 res = SMESH::HYP_UNKNOWN_FATAL;
316 //=============================================================================
320 * calls internal addHypothesis() and then adds a reference to <anHyp> under
321 * the SObject actually having a reference to <aSubShape>.
322 * NB: For this method to work, it is necessary to add a reference to sub-shape first.
324 //=============================================================================
326 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
327 SMESH::SMESH_Hypothesis_ptr anHyp)
328 throw(SALOME::SALOME_Exception)
330 Unexpect aCatch(SALOME_SalomeException);
331 SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
333 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
334 _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
335 aSubShapeObject, anHyp );
337 if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
339 return ConvertHypothesisStatus(status);
342 //=============================================================================
346 //=============================================================================
348 SMESH_Hypothesis::Hypothesis_Status
349 SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
350 SMESH::SMESH_Hypothesis_ptr anHyp)
352 if(MYDEBUG) MESSAGE("addHypothesis");
354 if (CORBA::is_nil(aSubShapeObject))
355 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
358 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
359 if (CORBA::is_nil(myHyp))
360 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
363 SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
366 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
367 int hypId = myHyp->GetId();
368 status = _impl->AddHypothesis(myLocSubShape, hypId);
369 if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
370 _mapHypo[hypId] = myHyp;
371 // assure there is a corresponding submesh
372 if ( !_impl->IsMainShape( myLocSubShape )) {
373 int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
374 if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
375 createSubMesh( aSubShapeObject );
379 catch(SALOME_Exception & S_ex)
381 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
386 //=============================================================================
390 //=============================================================================
392 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
393 SMESH::SMESH_Hypothesis_ptr anHyp)
394 throw(SALOME::SALOME_Exception)
396 Unexpect aCatch(SALOME_SalomeException);
397 SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
399 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
400 _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
401 aSubShapeObject, anHyp );
403 return ConvertHypothesisStatus(status);
406 //=============================================================================
410 //=============================================================================
412 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
413 SMESH::SMESH_Hypothesis_ptr anHyp)
415 if(MYDEBUG) MESSAGE("removeHypothesis()");
416 // **** proposer liste de subShape (selection multiple)
418 if (CORBA::is_nil(aSubShapeObject))
419 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
422 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
423 if (CORBA::is_nil(myHyp))
424 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
427 SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
430 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
431 int hypId = myHyp->GetId();
432 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
433 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
434 _mapHypo.erase( hypId );
436 catch(SALOME_Exception & S_ex)
438 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
443 //=============================================================================
447 //=============================================================================
449 SMESH::ListOfHypothesis *
450 SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
451 throw(SALOME::SALOME_Exception)
453 Unexpect aCatch(SALOME_SalomeException);
454 if (MYDEBUG) MESSAGE("GetHypothesisList");
455 if (CORBA::is_nil(aSubShapeObject))
456 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
459 SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
462 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
463 const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
464 int i = 0, n = aLocalList.size();
467 for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
468 SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
469 if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
470 aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
475 catch(SALOME_Exception & S_ex) {
476 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
479 return aList._retn();
482 //=============================================================================
486 //=============================================================================
487 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
488 const char* theName )
489 throw(SALOME::SALOME_Exception)
491 Unexpect aCatch(SALOME_SalomeException);
492 MESSAGE("SMESH_Mesh_i::GetSubMesh");
493 if (CORBA::is_nil(aSubShapeObject))
494 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
497 SMESH::SMESH_subMesh_var subMesh;
498 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(_this());
500 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
502 //Get or Create the SMESH_subMesh object implementation
504 int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
505 subMesh = getSubMesh( subMeshId );
507 // create a new subMesh object servant if there is none for the shape
508 if ( subMesh->_is_nil() )
509 subMesh = createSubMesh( aSubShapeObject );
511 if ( _gen_i->CanPublishInStudy( subMesh ))
512 _gen_i->PublishSubMesh (_gen_i->GetCurrentStudy(), aMesh,
513 subMesh, aSubShapeObject, theName );
515 catch(SALOME_Exception & S_ex) {
516 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
518 return subMesh._retn();
521 //=============================================================================
525 //=============================================================================
527 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
528 throw (SALOME::SALOME_Exception)
530 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
531 if ( theSubMesh->_is_nil() )
534 GEOM::GEOM_Object_var aSubShapeObject;
535 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
536 if ( !aStudy->_is_nil() ) {
537 // Remove submesh's SObject
538 SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
539 if ( !anSO->_is_nil() ) {
540 long aTag = SMESH_Gen_i::GetRefOnShapeTag();
541 SALOMEDS::SObject_var anObj, aRef;
542 if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
543 aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
545 aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
549 removeSubMesh( theSubMesh, aSubShapeObject.in() );
553 //=============================================================================
557 //=============================================================================
559 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
560 const char* theName )
561 throw(SALOME::SALOME_Exception)
563 Unexpect aCatch(SALOME_SalomeException);
564 SMESH::SMESH_Group_var aNewGroup =
565 SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
567 _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
568 aNewGroup, GEOM::GEOM_Object::_nil(), theName);
570 return aNewGroup._retn();
574 //=============================================================================
578 //=============================================================================
579 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM( SMESH::ElementType theElemType,
581 GEOM::GEOM_Object_ptr theGeomObj)
582 throw(SALOME::SALOME_Exception)
584 Unexpect aCatch(SALOME_SalomeException);
585 SMESH::SMESH_GroupOnGeom_var aNewGroup;
587 TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
588 if ( !aShape.IsNull() ) {
589 aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
590 ( createGroup( theElemType, theName, aShape ));
591 if ( _gen_i->CanPublishInStudy( aNewGroup ) )
592 _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
593 aNewGroup, theGeomObj, theName );
596 return aNewGroup._retn();
598 //=============================================================================
602 //=============================================================================
604 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
605 throw (SALOME::SALOME_Exception)
607 if ( theGroup->_is_nil() )
610 SMESH_GroupBase_i* aGroup =
611 dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
615 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
616 if ( !aStudy->_is_nil() ) {
617 // Remove group's SObject
618 SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
619 if ( !aGroupSO->_is_nil() )
620 aStudy->NewBuilder()->RemoveObject( aGroupSO );
623 // Remove the group from SMESH data structures
624 removeGroup( aGroup->GetLocalID() );
627 //=============================================================================
628 /*! RemoveGroupWithContents
629 * Remove group with its contents
631 //=============================================================================
632 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
633 throw (SALOME::SALOME_Exception)
635 if ( theGroup->_is_nil() )
638 SMESH_GroupBase_i* aGroup =
639 dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
643 SMESH::long_array_var anIds = aGroup->GetListOfID();
644 SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
646 if ( aGroup->GetType() == SMESH::NODE )
647 aMeshEditor->RemoveNodes( anIds );
649 aMeshEditor->RemoveElements( anIds );
651 RemoveGroup( theGroup );
654 //=============================================================================
656 * New group is created. All mesh elements that are
657 * present in initial groups are added to the new one
659 //=============================================================================
660 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
661 SMESH::SMESH_GroupBase_ptr theGroup2,
662 const char* theName )
663 throw (SALOME::SALOME_Exception)
667 SMESH::SMESH_Group_var aResGrp;
669 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
670 theGroup1->GetType() != theGroup2->GetType() )
671 return SMESH::SMESH_Group::_nil();
673 aResGrp = CreateGroup( theGroup1->GetType(), theName );
674 if ( aResGrp->_is_nil() )
675 return SMESH::SMESH_Group::_nil();
677 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
678 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
680 TColStd_MapOfInteger aResMap;
682 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
683 aResMap.Add( anIds1[ i1 ] );
685 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
686 aResMap.Add( anIds2[ i2 ] );
688 SMESH::long_array_var aResIds = new SMESH::long_array;
689 aResIds->length( aResMap.Extent() );
692 TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
693 for( ; anIter.More(); anIter.Next() )
694 aResIds[ resI++ ] = anIter.Key();
696 aResGrp->Add( aResIds );
698 return aResGrp._retn();
702 return SMESH::SMESH_Group::_nil();
706 //=============================================================================
708 * New group is created. All mesh elements that are
709 * present in both initial groups are added to the new one.
711 //=============================================================================
712 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
713 SMESH::SMESH_GroupBase_ptr theGroup2,
714 const char* theName )
715 throw (SALOME::SALOME_Exception)
717 SMESH::SMESH_Group_var aResGrp;
719 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
720 theGroup1->GetType() != theGroup2->GetType() )
723 aResGrp = CreateGroup( theGroup1->GetType(), theName );
724 if ( aResGrp->_is_nil() )
727 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
728 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
730 TColStd_MapOfInteger aMap1;
732 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
733 aMap1.Add( anIds1[ i1 ] );
735 TColStd_SequenceOfInteger aSeq;
737 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
738 if ( aMap1.Contains( anIds2[ i2 ] ) )
739 aSeq.Append( anIds2[ i2 ] );
741 SMESH::long_array_var aResIds = new SMESH::long_array;
742 aResIds->length( aSeq.Length() );
744 for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
745 aResIds[ resI ] = aSeq( resI + 1 );
747 aResGrp->Add( aResIds );
749 return aResGrp._retn();
752 //=============================================================================
754 * New group is created. All mesh elements that are present in
755 * main group but do not present in tool group are added to the new one
757 //=============================================================================
758 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
759 SMESH::SMESH_GroupBase_ptr theGroup2,
760 const char* theName )
761 throw (SALOME::SALOME_Exception)
763 SMESH::SMESH_Group_var aResGrp;
765 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
766 theGroup1->GetType() != theGroup2->GetType() )
769 aResGrp = CreateGroup( theGroup1->GetType(), theName );
770 if ( aResGrp->_is_nil() )
773 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
774 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
776 TColStd_MapOfInteger aMap2;
778 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
779 aMap2.Add( anIds2[ i2 ] );
782 TColStd_SequenceOfInteger aSeq;
783 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
784 if ( !aMap2.Contains( anIds1[ i1 ] ) )
785 aSeq.Append( anIds1[ i1 ] );
787 SMESH::long_array_var aResIds = new SMESH::long_array;
788 aResIds->length( aSeq.Length() );
790 for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
791 aResIds[ resI ] = aSeq( resI + 1 );
793 aResGrp->Add( aResIds );
795 return aResGrp._retn();
798 //=============================================================================
802 //=============================================================================
804 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
806 if(MYDEBUG) MESSAGE( "createSubMesh" );
807 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
809 ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
810 int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
811 SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
812 SMESH::SMESH_subMesh_var subMesh
813 = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
815 _mapSubMesh[subMeshId] = mySubMesh;
816 _mapSubMesh_i[subMeshId] = subMeshServant;
817 _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
819 // register CORBA object for persistence
820 int nextId = _gen_i->RegisterObject( subMesh );
821 if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
823 return subMesh._retn();
826 //=======================================================================
827 //function : getSubMesh
829 //=======================================================================
831 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
833 map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
834 if ( it == _mapSubMeshIor.end() )
835 return SMESH::SMESH_subMesh::_nil();
837 return SMESH::SMESH_subMesh::_duplicate( (*it).second );
841 //=============================================================================
845 //=============================================================================
847 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
848 GEOM::GEOM_Object_ptr theSubShapeObject )
850 MESSAGE("SMESH_Mesh_i::removeSubMesh()");
851 if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
855 SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
856 for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
857 removeHypothesis( theSubShapeObject, aHypList[i] );
860 catch( const SALOME::SALOME_Exception& ) {
861 INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
864 int subMeshId = theSubMesh->GetId();
866 _mapSubMesh.erase(subMeshId);
867 _mapSubMesh_i.erase(subMeshId);
868 _mapSubMeshIor.erase(subMeshId);
869 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
872 //=============================================================================
876 //=============================================================================
878 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
880 const TopoDS_Shape& theShape )
883 SMESH::SMESH_GroupBase_var aGroup;
884 if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
885 SMESH_GroupBase_i* aGroupImpl;
886 if ( !theShape.IsNull() )
887 aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
889 aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
891 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
892 SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
893 aGroupImpl->Register();
894 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
896 aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
897 _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
899 // register CORBA object for persistence
900 int nextId = _gen_i->RegisterObject( aGroup );
901 if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
903 return aGroup._retn();
906 //=============================================================================
908 * SMESH_Mesh_i::removeGroup
910 * Should be called by ~SMESH_Group_i()
912 //=============================================================================
914 void SMESH_Mesh_i::removeGroup( const int theId )
916 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );
917 if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
918 _mapGroups.erase( theId );
919 _impl->RemoveGroup( theId );
924 //=============================================================================
928 //=============================================================================
930 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
931 throw(SALOME::SALOME_Exception)
933 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
935 SMESH::log_array_var aLog;
937 list < SMESHDS_Command * >logDS = _impl->GetLog();
938 aLog = new SMESH::log_array;
940 int lg = logDS.size();
943 list < SMESHDS_Command * >::iterator its = logDS.begin();
944 while(its != logDS.end()){
945 SMESHDS_Command *com = *its;
946 int comType = com->GetType();
948 int lgcom = com->GetNumber();
950 const list < int >&intList = com->GetIndexes();
951 int inum = intList.size();
953 list < int >::const_iterator ii = intList.begin();
954 const list < double >&coordList = com->GetCoords();
955 int rnum = coordList.size();
957 list < double >::const_iterator ir = coordList.begin();
958 aLog[indexLog].commandType = comType;
959 aLog[indexLog].number = lgcom;
960 aLog[indexLog].coords.length(rnum);
961 aLog[indexLog].indexes.length(inum);
962 for(int i = 0; i < rnum; i++){
963 aLog[indexLog].coords[i] = *ir;
964 //MESSAGE(" "<<i<<" "<<ir.Value());
967 for(int i = 0; i < inum; i++){
968 aLog[indexLog].indexes[i] = *ii;
969 //MESSAGE(" "<<i<<" "<<ii.Value());
978 catch(SALOME_Exception & S_ex){
979 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
985 //=============================================================================
989 //=============================================================================
991 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
993 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
997 //=============================================================================
1001 //=============================================================================
1003 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1005 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1009 //=============================================================================
1013 //=============================================================================
1015 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1020 //=============================================================================
1024 //=============================================================================
1026 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1028 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1032 //=============================================================================
1036 //=============================================================================
1038 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1040 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1045 //=============================================================================
1049 //=============================================================================
1051 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1053 SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl );
1054 SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1055 return aMesh._retn();
1058 //=============================================================================
1062 //=============================================================================
1064 void SMESH_Mesh_i::ExportToMED( const char* file,
1065 CORBA::Boolean auto_groups,
1066 SMESH::MED_VERSION theVersion )
1067 throw(SALOME::SALOME_Exception)
1069 Unexpect aCatch(SALOME_SalomeException);
1071 char* aMeshName = "Mesh";
1072 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1073 if ( !aStudy->_is_nil() ) {
1074 SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1075 if ( !aMeshSO->_is_nil() ) {
1076 aMeshName = aMeshSO->GetName();
1078 //SCRUTE(aMeshName);
1079 //SCRUTE(aMeshSO->GetID());
1081 // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes
1082 if ( !aStudy->GetProperties()->IsLocked() )
1084 SALOMEDS::GenericAttribute_var anAttr;
1085 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1086 SALOMEDS::AttributeExternalFileDef_var aFileName;
1087 anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1088 aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1089 ASSERT(!aFileName->_is_nil());
1090 aFileName->SetValue(file);
1091 SALOMEDS::AttributeFileType_var aFileType;
1092 anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1093 aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1094 ASSERT(!aFileType->_is_nil());
1095 aFileType->SetValue("FICHIERMED");
1099 _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1102 void SMESH_Mesh_i::ExportMED( const char* file,
1103 CORBA::Boolean auto_groups)
1104 throw(SALOME::SALOME_Exception)
1106 ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1109 void SMESH_Mesh_i::ExportDAT(const char *file) throw(SALOME::SALOME_Exception)
1111 Unexpect aCatch(SALOME_SalomeException);
1112 _impl->ExportDAT(file);
1114 void SMESH_Mesh_i::ExportUNV(const char *file) throw(SALOME::SALOME_Exception)
1116 Unexpect aCatch(SALOME_SalomeException);
1117 _impl->ExportUNV(file);
1120 void SMESH_Mesh_i::ExportSTL(const char *file, const bool isascii) throw(SALOME::SALOME_Exception)
1122 Unexpect aCatch(SALOME_SalomeException);
1123 _impl->ExportSTL(file, isascii);
1126 //=============================================================================
1130 //=============================================================================
1132 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1134 Unexpect aCatch(SALOME_SalomeException);
1135 SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1136 SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1137 return aMesh._retn();
1140 //=============================================================================
1144 //=============================================================================
1145 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1147 Unexpect aCatch(SALOME_SalomeException);
1148 return _impl->NbNodes();
1151 //=============================================================================
1155 //=============================================================================
1156 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1158 Unexpect aCatch(SALOME_SalomeException);
1159 return NbEdges() + NbFaces() + NbVolumes();
1162 //=============================================================================
1166 //=============================================================================
1167 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1169 Unexpect aCatch(SALOME_SalomeException);
1170 return _impl->NbEdges();
1173 //=============================================================================
1177 //=============================================================================
1178 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1180 Unexpect aCatch(SALOME_SalomeException);
1181 return _impl->NbFaces();
1184 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1186 Unexpect aCatch(SALOME_SalomeException);
1187 return _impl->NbTriangles();
1190 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1192 Unexpect aCatch(SALOME_SalomeException);
1193 return _impl->NbQuadrangles();
1196 //=============================================================================
1200 //=============================================================================
1201 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1203 Unexpect aCatch(SALOME_SalomeException);
1204 return _impl->NbVolumes();
1207 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1209 Unexpect aCatch(SALOME_SalomeException);
1210 return _impl->NbTetras();
1213 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1215 Unexpect aCatch(SALOME_SalomeException);
1216 return _impl->NbHexas();
1219 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1221 Unexpect aCatch(SALOME_SalomeException);
1222 return _impl->NbPyramids();
1225 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1227 Unexpect aCatch(SALOME_SalomeException);
1228 return _impl->NbPrisms();
1231 //=============================================================================
1235 //=============================================================================
1236 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1238 Unexpect aCatch(SALOME_SalomeException);
1239 return _impl->NbSubMesh();
1242 //=============================================================================
1246 //=============================================================================
1247 char* SMESH_Mesh_i::Dump()
1249 std::ostringstream os;
1251 return CORBA::string_dup( os.str().c_str() );
1254 //=============================================================================
1258 //=============================================================================
1259 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1261 SMESH::long_array_var aResult = new SMESH::long_array();
1262 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1263 int aMinId = aSMESHDS_Mesh->MinElementID();
1264 int aMaxId = aSMESHDS_Mesh->MaxElementID();
1266 aResult->length(aMaxId - aMinId + 1);
1268 for (int i = 0, id = aMinId; id <= aMaxId; id++ )
1271 return aResult._retn();
1274 //=============================================================================
1278 //=============================================================================
1280 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1281 throw (SALOME::SALOME_Exception)
1283 Unexpect aCatch(SALOME_SalomeException);
1284 MESSAGE("SMESH_Mesh_i::GetElementsId");
1285 SMESH::long_array_var aResult = new SMESH::long_array();
1286 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1288 if ( aSMESHDS_Mesh == NULL )
1289 return aResult._retn();
1291 long nbElements = NbElements();
1292 aResult->length( nbElements );
1293 SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1294 for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1295 aResult[i] = anIt->next()->GetID();
1297 return aResult._retn();
1301 //=============================================================================
1305 //=============================================================================
1307 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1308 throw (SALOME::SALOME_Exception)
1310 Unexpect aCatch(SALOME_SalomeException);
1311 MESSAGE("SMESH_subMesh_i::GetElementsByType");
1312 SMESH::long_array_var aResult = new SMESH::long_array();
1313 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1315 if ( aSMESHDS_Mesh == NULL )
1316 return aResult._retn();
1318 long nbElements = NbElements();
1320 // No sense in returning ids of elements along with ids of nodes:
1321 // when theElemType == SMESH::ALL, return node ids only if
1322 // there are no elements
1323 if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1324 return GetNodesId();
1326 aResult->length( nbElements );
1330 SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1331 while ( i < nbElements && anIt->more() ) {
1332 const SMDS_MeshElement* anElem = anIt->next();
1333 if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1334 aResult[i++] = anElem->GetID();
1337 aResult->length( i );
1339 return aResult._retn();
1342 //=============================================================================
1346 //=============================================================================
1348 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1349 throw (SALOME::SALOME_Exception)
1351 Unexpect aCatch(SALOME_SalomeException);
1352 MESSAGE("SMESH_subMesh_i::GetNodesId");
1353 SMESH::long_array_var aResult = new SMESH::long_array();
1354 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1356 if ( aSMESHDS_Mesh == NULL )
1357 return aResult._retn();
1359 long nbNodes = NbNodes();
1360 aResult->length( nbNodes );
1361 SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1362 for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1363 aResult[i] = anIt->next()->GetID();
1365 return aResult._retn();