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; this = "<<this);
82 _id = myIdGenerator++;
84 thePOA->activate_object( this );
87 //=============================================================================
91 //=============================================================================
93 SMESH_Mesh_i::~SMESH_Mesh_i()
95 INFOS("~SMESH_Mesh_i; this = "<<this);
96 map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
97 for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
98 SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
101 // this method is colled from destructor of group (PAL6331)
102 //_impl->RemoveGroup( aGroup->GetLocalID() );
110 //=============================================================================
114 * Associates <this> mesh with <theShape> and puts a reference
115 * to <theShape> into the current study;
116 * the previous shape is substituted by the new one.
118 //=============================================================================
120 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
121 throw (SALOME::SALOME_Exception)
123 Unexpect aCatch(SALOME_SalomeException);
125 _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
127 catch(SALOME_Exception & S_ex) {
128 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
132 //=======================================================================
133 //function : GetShapeToMesh
135 //=======================================================================
137 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
138 throw (SALOME::SALOME_Exception)
140 Unexpect aCatch(SALOME_SalomeException);
141 GEOM::GEOM_Object_var aShapeObj;
143 TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
145 aShapeObj = _gen_i->ShapeToGeomObject( S );
147 catch(SALOME_Exception & S_ex) {
148 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
150 return aShapeObj._retn();
153 //=============================================================================
157 //=============================================================================
159 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
161 SMESH::DriverMED_ReadStatus res;
164 case DriverMED_R_SMESHDS_Mesh::DRS_OK:
165 res = SMESH::DRS_OK; break;
166 case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
167 res = SMESH::DRS_EMPTY; break;
168 case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
169 res = SMESH::DRS_WARN_RENUMBER; break;
170 case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
171 res = SMESH::DRS_WARN_SKIP_ELEM; break;
172 case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
174 res = SMESH::DRS_FAIL; break;
179 //=============================================================================
183 * Imports mesh data from MED file
185 //=============================================================================
187 SMESH::DriverMED_ReadStatus
188 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
189 throw ( SALOME::SALOME_Exception )
191 Unexpect aCatch(SALOME_SalomeException);
194 status = importMEDFile( theFileName, theMeshName );
196 catch( SALOME_Exception& S_ex ) {
197 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
200 THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
203 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
204 if ( !aStudy->_is_nil() ) {
205 // publishing of the groups in the study (sub-meshes are out of scope of MED import)
206 map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
207 for (; it != _mapGroups.end(); it++ ) {
208 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
209 _gen_i->PublishGroup( aStudy, _this(), aGroup,
210 GEOM::GEOM_Object::_nil(), aGroup->GetName());
213 return ConvertDriverMEDReadStatus(status);
216 //=============================================================================
220 * Imports mesh data from MED file
222 //=============================================================================
224 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
225 throw ( SALOME::SALOME_Exception )
227 // Read mesh with name = <theMeshName> into SMESH_Mesh
228 _impl->UNVToMesh( theFileName );
233 //=============================================================================
237 * Imports mesh data from STL file
239 //=============================================================================
240 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
241 throw ( SALOME::SALOME_Exception )
243 // Read mesh with name = <theMeshName> into SMESH_Mesh
244 _impl->STLToMesh( theFileName );
249 //=============================================================================
253 * Imports mesh data from MED file
255 //=============================================================================
257 int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
259 // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
260 int status = _impl->MEDToMesh( theFileName, theMeshName );
262 // Create group servants, if any groups were imported
263 list<int> aGroupIds = _impl->GetGroupIds();
264 for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
265 SMESH_Group_i* aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
267 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
268 SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
269 aGroupImpl->Register();
270 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
272 SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
273 _mapGroups[*it] = SMESH::SMESH_Group::_duplicate( aGroup );
275 // register CORBA object for persistence
276 int nextId = _gen_i->RegisterObject( aGroup );
277 if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
283 //=============================================================================
287 //=============================================================================
289 static SMESH::Hypothesis_Status ConvertHypothesisStatus
290 (SMESH_Hypothesis::Hypothesis_Status theStatus)
292 SMESH::Hypothesis_Status res;
295 case SMESH_Hypothesis::HYP_OK:
296 res = SMESH::HYP_OK; break;
297 case SMESH_Hypothesis::HYP_MISSING:
298 res = SMESH::HYP_MISSING; break;
299 case SMESH_Hypothesis::HYP_CONCURENT:
300 res = SMESH::HYP_CONCURENT; break;
301 case SMESH_Hypothesis::HYP_BAD_PARAMETER:
302 res = SMESH::HYP_BAD_PARAMETER; break;
303 case SMESH_Hypothesis::HYP_INCOMPATIBLE:
304 res = SMESH::HYP_INCOMPATIBLE; break;
305 case SMESH_Hypothesis::HYP_NOTCONFORM:
306 res = SMESH::HYP_NOTCONFORM; break;
307 case SMESH_Hypothesis::HYP_ALREADY_EXIST:
308 res = SMESH::HYP_ALREADY_EXIST; break;
309 case SMESH_Hypothesis::HYP_BAD_DIM:
310 res = SMESH::HYP_BAD_DIM; break;
312 res = SMESH::HYP_UNKNOWN_FATAL;
317 //=============================================================================
321 * calls internal addHypothesis() and then adds a reference to <anHyp> under
322 * the SObject actually having a reference to <aSubShape>.
323 * NB: For this method to work, it is necessary to add a reference to sub-shape first.
325 //=============================================================================
327 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
328 SMESH::SMESH_Hypothesis_ptr anHyp)
329 throw(SALOME::SALOME_Exception)
331 Unexpect aCatch(SALOME_SalomeException);
332 SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
334 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
335 _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
336 aSubShapeObject, anHyp );
338 if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
340 return ConvertHypothesisStatus(status);
343 //=============================================================================
347 //=============================================================================
349 SMESH_Hypothesis::Hypothesis_Status
350 SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
351 SMESH::SMESH_Hypothesis_ptr anHyp)
353 if(MYDEBUG) MESSAGE("addHypothesis");
355 if (CORBA::is_nil(aSubShapeObject))
356 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
359 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
360 if (CORBA::is_nil(myHyp))
361 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
364 SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
367 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
368 int hypId = myHyp->GetId();
369 status = _impl->AddHypothesis(myLocSubShape, hypId);
370 if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
371 _mapHypo[hypId] = myHyp;
372 // assure there is a corresponding submesh
373 if ( !_impl->IsMainShape( myLocSubShape )) {
374 int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
375 if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
376 createSubMesh( aSubShapeObject );
380 catch(SALOME_Exception & S_ex)
382 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
387 //=============================================================================
391 //=============================================================================
393 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
394 SMESH::SMESH_Hypothesis_ptr anHyp)
395 throw(SALOME::SALOME_Exception)
397 Unexpect aCatch(SALOME_SalomeException);
398 SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
400 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
401 _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
402 aSubShapeObject, anHyp );
404 return ConvertHypothesisStatus(status);
407 //=============================================================================
411 //=============================================================================
413 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
414 SMESH::SMESH_Hypothesis_ptr anHyp)
416 if(MYDEBUG) MESSAGE("removeHypothesis()");
417 // **** proposer liste de subShape (selection multiple)
419 if (CORBA::is_nil(aSubShapeObject))
420 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
423 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
424 if (CORBA::is_nil(myHyp))
425 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
428 SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
431 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
432 int hypId = myHyp->GetId();
433 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
434 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
435 _mapHypo.erase( hypId );
437 catch(SALOME_Exception & S_ex)
439 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
444 //=============================================================================
448 //=============================================================================
450 SMESH::ListOfHypothesis *
451 SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
452 throw(SALOME::SALOME_Exception)
454 Unexpect aCatch(SALOME_SalomeException);
455 if (MYDEBUG) MESSAGE("GetHypothesisList");
456 if (CORBA::is_nil(aSubShapeObject))
457 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
460 SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
463 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
464 const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
465 int i = 0, n = aLocalList.size();
468 for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
469 SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
470 if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
471 aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
476 catch(SALOME_Exception & S_ex) {
477 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
480 return aList._retn();
483 //=============================================================================
487 //=============================================================================
488 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
489 const char* theName )
490 throw(SALOME::SALOME_Exception)
492 Unexpect aCatch(SALOME_SalomeException);
493 MESSAGE("SMESH_Mesh_i::GetSubMesh");
494 if (CORBA::is_nil(aSubShapeObject))
495 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
498 SMESH::SMESH_subMesh_var subMesh;
499 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(_this());
501 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
503 //Get or Create the SMESH_subMesh object implementation
505 int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
506 subMesh = getSubMesh( subMeshId );
508 // create a new subMesh object servant if there is none for the shape
509 if ( subMesh->_is_nil() )
510 subMesh = createSubMesh( aSubShapeObject );
512 if ( _gen_i->CanPublishInStudy( subMesh ))
513 _gen_i->PublishSubMesh (_gen_i->GetCurrentStudy(), aMesh,
514 subMesh, aSubShapeObject, theName );
516 catch(SALOME_Exception & S_ex) {
517 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
519 return subMesh._retn();
522 //=============================================================================
526 //=============================================================================
528 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
529 throw (SALOME::SALOME_Exception)
531 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
532 if ( theSubMesh->_is_nil() )
535 GEOM::GEOM_Object_var aSubShapeObject;
536 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
537 if ( !aStudy->_is_nil() ) {
538 // Remove submesh's SObject
539 SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
540 if ( !anSO->_is_nil() ) {
541 long aTag = SMESH_Gen_i::GetRefOnShapeTag();
542 SALOMEDS::SObject_var anObj, aRef;
543 if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
544 aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
546 aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
550 removeSubMesh( theSubMesh, aSubShapeObject.in() );
554 //=============================================================================
558 //=============================================================================
560 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
561 const char* theName )
562 throw(SALOME::SALOME_Exception)
564 Unexpect aCatch(SALOME_SalomeException);
565 SMESH::SMESH_Group_var aNewGroup =
566 SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
568 _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
569 aNewGroup, GEOM::GEOM_Object::_nil(), theName);
571 return aNewGroup._retn();
575 //=============================================================================
579 //=============================================================================
580 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM( SMESH::ElementType theElemType,
582 GEOM::GEOM_Object_ptr theGeomObj)
583 throw(SALOME::SALOME_Exception)
585 Unexpect aCatch(SALOME_SalomeException);
586 SMESH::SMESH_GroupOnGeom_var aNewGroup;
588 TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
589 if ( !aShape.IsNull() ) {
590 aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
591 ( createGroup( theElemType, theName, aShape ));
592 if ( _gen_i->CanPublishInStudy( aNewGroup ) )
593 _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
594 aNewGroup, theGeomObj, theName );
597 return aNewGroup._retn();
599 //=============================================================================
603 //=============================================================================
605 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
606 throw (SALOME::SALOME_Exception)
608 if ( theGroup->_is_nil() )
611 SMESH_GroupBase_i* aGroup =
612 dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
616 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
617 if ( !aStudy->_is_nil() ) {
618 // Remove group's SObject
619 SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
620 if ( !aGroupSO->_is_nil() )
621 aStudy->NewBuilder()->RemoveObject( aGroupSO );
624 // Remove the group from SMESH data structures
625 removeGroup( aGroup->GetLocalID() );
628 //=============================================================================
629 /*! RemoveGroupWithContents
630 * Remove group with its contents
632 //=============================================================================
633 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
634 throw (SALOME::SALOME_Exception)
636 if ( theGroup->_is_nil() )
639 SMESH_GroupBase_i* aGroup =
640 dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
644 SMESH::long_array_var anIds = aGroup->GetListOfID();
645 SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
647 if ( aGroup->GetType() == SMESH::NODE )
648 aMeshEditor->RemoveNodes( anIds );
650 aMeshEditor->RemoveElements( anIds );
652 RemoveGroup( theGroup );
655 //=============================================================================
657 * New group is created. All mesh elements that are
658 * present in initial groups are added to the new one
660 //=============================================================================
661 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
662 SMESH::SMESH_GroupBase_ptr theGroup2,
663 const char* theName )
664 throw (SALOME::SALOME_Exception)
668 SMESH::SMESH_Group_var aResGrp;
670 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
671 theGroup1->GetType() != theGroup2->GetType() )
672 return SMESH::SMESH_Group::_nil();
674 aResGrp = CreateGroup( theGroup1->GetType(), theName );
675 if ( aResGrp->_is_nil() )
676 return SMESH::SMESH_Group::_nil();
678 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
679 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
681 TColStd_MapOfInteger aResMap;
683 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
684 aResMap.Add( anIds1[ i1 ] );
686 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
687 aResMap.Add( anIds2[ i2 ] );
689 SMESH::long_array_var aResIds = new SMESH::long_array;
690 aResIds->length( aResMap.Extent() );
693 TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
694 for( ; anIter.More(); anIter.Next() )
695 aResIds[ resI++ ] = anIter.Key();
697 aResGrp->Add( aResIds );
699 return aResGrp._retn();
703 return SMESH::SMESH_Group::_nil();
707 //=============================================================================
709 * New group is created. All mesh elements that are
710 * present in both initial groups are added to the new one.
712 //=============================================================================
713 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
714 SMESH::SMESH_GroupBase_ptr theGroup2,
715 const char* theName )
716 throw (SALOME::SALOME_Exception)
718 SMESH::SMESH_Group_var aResGrp;
720 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
721 theGroup1->GetType() != theGroup2->GetType() )
724 aResGrp = CreateGroup( theGroup1->GetType(), theName );
725 if ( aResGrp->_is_nil() )
728 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
729 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
731 TColStd_MapOfInteger aMap1;
733 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
734 aMap1.Add( anIds1[ i1 ] );
736 TColStd_SequenceOfInteger aSeq;
738 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
739 if ( aMap1.Contains( anIds2[ i2 ] ) )
740 aSeq.Append( anIds2[ i2 ] );
742 SMESH::long_array_var aResIds = new SMESH::long_array;
743 aResIds->length( aSeq.Length() );
745 for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
746 aResIds[ resI ] = aSeq( resI + 1 );
748 aResGrp->Add( aResIds );
750 return aResGrp._retn();
753 //=============================================================================
755 * New group is created. All mesh elements that are present in
756 * main group but do not present in tool group are added to the new one
758 //=============================================================================
759 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
760 SMESH::SMESH_GroupBase_ptr theGroup2,
761 const char* theName )
762 throw (SALOME::SALOME_Exception)
764 SMESH::SMESH_Group_var aResGrp;
766 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
767 theGroup1->GetType() != theGroup2->GetType() )
770 aResGrp = CreateGroup( theGroup1->GetType(), theName );
771 if ( aResGrp->_is_nil() )
774 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
775 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
777 TColStd_MapOfInteger aMap2;
779 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
780 aMap2.Add( anIds2[ i2 ] );
783 TColStd_SequenceOfInteger aSeq;
784 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
785 if ( !aMap2.Contains( anIds1[ i1 ] ) )
786 aSeq.Append( anIds1[ i1 ] );
788 SMESH::long_array_var aResIds = new SMESH::long_array;
789 aResIds->length( aSeq.Length() );
791 for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
792 aResIds[ resI ] = aSeq( resI + 1 );
794 aResGrp->Add( aResIds );
796 return aResGrp._retn();
799 //=============================================================================
803 //=============================================================================
805 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
807 if(MYDEBUG) MESSAGE( "createSubMesh" );
808 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
810 ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
811 int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
812 SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
813 SMESH::SMESH_subMesh_var subMesh
814 = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
816 _mapSubMesh[subMeshId] = mySubMesh;
817 _mapSubMesh_i[subMeshId] = subMeshServant;
818 _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
820 // register CORBA object for persistence
821 int nextId = _gen_i->RegisterObject( subMesh );
822 if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
824 return subMesh._retn();
827 //=======================================================================
828 //function : getSubMesh
830 //=======================================================================
832 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
834 map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
835 if ( it == _mapSubMeshIor.end() )
836 return SMESH::SMESH_subMesh::_nil();
838 return SMESH::SMESH_subMesh::_duplicate( (*it).second );
842 //=============================================================================
846 //=============================================================================
848 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
849 GEOM::GEOM_Object_ptr theSubShapeObject )
851 MESSAGE("SMESH_Mesh_i::removeSubMesh()");
852 if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
856 SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
857 for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
858 removeHypothesis( theSubShapeObject, aHypList[i] );
861 catch( const SALOME::SALOME_Exception& ) {
862 INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
865 int subMeshId = theSubMesh->GetId();
867 _mapSubMesh.erase(subMeshId);
868 _mapSubMesh_i.erase(subMeshId);
869 _mapSubMeshIor.erase(subMeshId);
870 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
873 //=============================================================================
877 //=============================================================================
879 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
881 const TopoDS_Shape& theShape )
884 SMESH::SMESH_GroupBase_var aGroup;
885 if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
886 SMESH_GroupBase_i* aGroupImpl;
887 if ( !theShape.IsNull() )
888 aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
890 aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
892 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
893 SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
894 aGroupImpl->Register();
895 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
897 aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
898 _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
900 // register CORBA object for persistence
901 int nextId = _gen_i->RegisterObject( aGroup );
902 if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
904 return aGroup._retn();
907 //=============================================================================
909 * SMESH_Mesh_i::removeGroup
911 * Should be called by ~SMESH_Group_i()
913 //=============================================================================
915 void SMESH_Mesh_i::removeGroup( const int theId )
917 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );
918 if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
919 _mapGroups.erase( theId );
920 _impl->RemoveGroup( theId );
925 //=============================================================================
929 //=============================================================================
931 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
932 throw(SALOME::SALOME_Exception)
934 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
936 SMESH::log_array_var aLog;
938 list < SMESHDS_Command * >logDS = _impl->GetLog();
939 aLog = new SMESH::log_array;
941 int lg = logDS.size();
944 list < SMESHDS_Command * >::iterator its = logDS.begin();
945 while(its != logDS.end()){
946 SMESHDS_Command *com = *its;
947 int comType = com->GetType();
949 int lgcom = com->GetNumber();
951 const list < int >&intList = com->GetIndexes();
952 int inum = intList.size();
954 list < int >::const_iterator ii = intList.begin();
955 const list < double >&coordList = com->GetCoords();
956 int rnum = coordList.size();
958 list < double >::const_iterator ir = coordList.begin();
959 aLog[indexLog].commandType = comType;
960 aLog[indexLog].number = lgcom;
961 aLog[indexLog].coords.length(rnum);
962 aLog[indexLog].indexes.length(inum);
963 for(int i = 0; i < rnum; i++){
964 aLog[indexLog].coords[i] = *ir;
965 //MESSAGE(" "<<i<<" "<<ir.Value());
968 for(int i = 0; i < inum; i++){
969 aLog[indexLog].indexes[i] = *ii;
970 //MESSAGE(" "<<i<<" "<<ii.Value());
979 catch(SALOME_Exception & S_ex){
980 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
986 //=============================================================================
990 //=============================================================================
992 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
994 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
998 //=============================================================================
1002 //=============================================================================
1004 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1006 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1010 //=============================================================================
1014 //=============================================================================
1016 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1021 //=============================================================================
1025 //=============================================================================
1027 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1029 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1033 //=============================================================================
1037 //=============================================================================
1039 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1041 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1046 //=============================================================================
1050 //=============================================================================
1052 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1054 SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl );
1055 SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1056 return aMesh._retn();
1059 //=============================================================================
1063 //=============================================================================
1065 void SMESH_Mesh_i::ExportToMED( const char* file,
1066 CORBA::Boolean auto_groups,
1067 SMESH::MED_VERSION theVersion )
1068 throw(SALOME::SALOME_Exception)
1070 Unexpect aCatch(SALOME_SalomeException);
1072 char* aMeshName = "Mesh";
1073 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1074 if ( !aStudy->_is_nil() ) {
1075 SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1076 if ( !aMeshSO->_is_nil() ) {
1077 aMeshName = aMeshSO->GetName();
1079 //SCRUTE(aMeshName);
1080 //SCRUTE(aMeshSO->GetID());
1082 // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes
1083 if ( !aStudy->GetProperties()->IsLocked() )
1085 SALOMEDS::GenericAttribute_var anAttr;
1086 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1087 SALOMEDS::AttributeExternalFileDef_var aFileName;
1088 anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1089 aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1090 ASSERT(!aFileName->_is_nil());
1091 aFileName->SetValue(file);
1092 SALOMEDS::AttributeFileType_var aFileType;
1093 anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1094 aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1095 ASSERT(!aFileType->_is_nil());
1096 aFileType->SetValue("FICHIERMED");
1100 _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1103 void SMESH_Mesh_i::ExportMED( const char* file,
1104 CORBA::Boolean auto_groups)
1105 throw(SALOME::SALOME_Exception)
1107 ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1110 void SMESH_Mesh_i::ExportDAT(const char *file) throw(SALOME::SALOME_Exception)
1112 Unexpect aCatch(SALOME_SalomeException);
1113 _impl->ExportDAT(file);
1115 void SMESH_Mesh_i::ExportUNV(const char *file) throw(SALOME::SALOME_Exception)
1117 Unexpect aCatch(SALOME_SalomeException);
1118 _impl->ExportUNV(file);
1121 void SMESH_Mesh_i::ExportSTL(const char *file, const bool isascii) throw(SALOME::SALOME_Exception)
1123 Unexpect aCatch(SALOME_SalomeException);
1124 _impl->ExportSTL(file, isascii);
1127 //=============================================================================
1131 //=============================================================================
1133 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1135 Unexpect aCatch(SALOME_SalomeException);
1136 SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1137 SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1138 return aMesh._retn();
1141 //=============================================================================
1145 //=============================================================================
1146 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1148 Unexpect aCatch(SALOME_SalomeException);
1149 return _impl->NbNodes();
1152 //=============================================================================
1156 //=============================================================================
1157 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1159 Unexpect aCatch(SALOME_SalomeException);
1160 return NbEdges() + NbFaces() + NbVolumes();
1163 //=============================================================================
1167 //=============================================================================
1168 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1170 Unexpect aCatch(SALOME_SalomeException);
1171 return _impl->NbEdges();
1174 //=============================================================================
1178 //=============================================================================
1179 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1181 Unexpect aCatch(SALOME_SalomeException);
1182 return _impl->NbFaces();
1185 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1187 Unexpect aCatch(SALOME_SalomeException);
1188 return _impl->NbTriangles();
1191 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1193 Unexpect aCatch(SALOME_SalomeException);
1194 return _impl->NbQuadrangles();
1197 //=============================================================================
1201 //=============================================================================
1202 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1204 Unexpect aCatch(SALOME_SalomeException);
1205 return _impl->NbVolumes();
1208 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1210 Unexpect aCatch(SALOME_SalomeException);
1211 return _impl->NbTetras();
1214 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1216 Unexpect aCatch(SALOME_SalomeException);
1217 return _impl->NbHexas();
1220 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1222 Unexpect aCatch(SALOME_SalomeException);
1223 return _impl->NbPyramids();
1226 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1228 Unexpect aCatch(SALOME_SalomeException);
1229 return _impl->NbPrisms();
1232 //=============================================================================
1236 //=============================================================================
1237 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1239 Unexpect aCatch(SALOME_SalomeException);
1240 return _impl->NbSubMesh();
1243 //=============================================================================
1247 //=============================================================================
1248 char* SMESH_Mesh_i::Dump()
1250 std::ostringstream os;
1252 return CORBA::string_dup( os.str().c_str() );
1255 //=============================================================================
1259 //=============================================================================
1260 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1262 SMESH::long_array_var aResult = new SMESH::long_array();
1263 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1264 int aMinId = aSMESHDS_Mesh->MinElementID();
1265 int aMaxId = aSMESHDS_Mesh->MaxElementID();
1267 aResult->length(aMaxId - aMinId + 1);
1269 for (int i = 0, id = aMinId; id <= aMaxId; id++ )
1272 return aResult._retn();
1275 //=============================================================================
1279 //=============================================================================
1281 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1282 throw (SALOME::SALOME_Exception)
1284 Unexpect aCatch(SALOME_SalomeException);
1285 MESSAGE("SMESH_Mesh_i::GetElementsId");
1286 SMESH::long_array_var aResult = new SMESH::long_array();
1287 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1289 if ( aSMESHDS_Mesh == NULL )
1290 return aResult._retn();
1292 long nbElements = NbElements();
1293 aResult->length( nbElements );
1294 SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1295 for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1296 aResult[i] = anIt->next()->GetID();
1298 return aResult._retn();
1302 //=============================================================================
1306 //=============================================================================
1308 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1309 throw (SALOME::SALOME_Exception)
1311 Unexpect aCatch(SALOME_SalomeException);
1312 MESSAGE("SMESH_subMesh_i::GetElementsByType");
1313 SMESH::long_array_var aResult = new SMESH::long_array();
1314 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1316 if ( aSMESHDS_Mesh == NULL )
1317 return aResult._retn();
1319 long nbElements = NbElements();
1321 // No sense in returning ids of elements along with ids of nodes:
1322 // when theElemType == SMESH::ALL, return node ids only if
1323 // there are no elements
1324 if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1325 return GetNodesId();
1327 aResult->length( nbElements );
1331 SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1332 while ( i < nbElements && anIt->more() ) {
1333 const SMDS_MeshElement* anElem = anIt->next();
1334 if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1335 aResult[i++] = anElem->GetID();
1338 aResult->length( i );
1340 return aResult._retn();
1343 //=============================================================================
1347 //=============================================================================
1349 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1350 throw (SALOME::SALOME_Exception)
1352 Unexpect aCatch(SALOME_SalomeException);
1353 MESSAGE("SMESH_subMesh_i::GetNodesId");
1354 SMESH::long_array_var aResult = new SMESH::long_array();
1355 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1357 if ( aSMESHDS_Mesh == NULL )
1358 return aResult._retn();
1360 long nbNodes = NbNodes();
1361 aResult->length( nbNodes );
1362 SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1363 for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1364 aResult[i] = anIt->next()->GetID();
1366 return aResult._retn();