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 // Update Python script
340 SALOMEDS::SObject_var aMeshSO = SMESH_Gen_i::ObjectToSObject(_gen_i->GetCurrentStudy(), _this());
341 SALOMEDS::SObject_var aHypoSO = SMESH_Gen_i::ObjectToSObject(_gen_i->GetCurrentStudy(), anHyp);
343 TCollection_AsciiString aStr ("status = ");
344 aStr += aMeshSO->GetID();
345 aStr += ".AddHypothesis(salome.IDToObject(\"";
346 aStr += aSubShapeObject->GetStudyEntry();
348 aStr += aHypoSO->GetID();
351 _gen_i->AddToPythonScript(_gen_i->GetCurrentStudy()->StudyId(), aStr);
353 aStr = "print \"AddHypothesis: \", status";
354 _gen_i->AddToPythonScript(_gen_i->GetCurrentStudy()->StudyId(), aStr);
356 return ConvertHypothesisStatus(status);
359 //=============================================================================
363 //=============================================================================
365 SMESH_Hypothesis::Hypothesis_Status
366 SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
367 SMESH::SMESH_Hypothesis_ptr anHyp)
369 if(MYDEBUG) MESSAGE("addHypothesis");
371 if (CORBA::is_nil(aSubShapeObject))
372 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
375 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
376 if (CORBA::is_nil(myHyp))
377 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
380 SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
383 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
384 int hypId = myHyp->GetId();
385 status = _impl->AddHypothesis(myLocSubShape, hypId);
386 if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
387 _mapHypo[hypId] = myHyp;
388 // assure there is a corresponding submesh
389 if ( !_impl->IsMainShape( myLocSubShape )) {
390 int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
391 if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
392 createSubMesh( aSubShapeObject );
396 catch(SALOME_Exception & S_ex)
398 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
403 //=============================================================================
407 //=============================================================================
409 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
410 SMESH::SMESH_Hypothesis_ptr anHyp)
411 throw(SALOME::SALOME_Exception)
413 Unexpect aCatch(SALOME_SalomeException);
414 SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
416 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
417 _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
418 aSubShapeObject, anHyp );
420 return ConvertHypothesisStatus(status);
423 //=============================================================================
427 //=============================================================================
429 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
430 SMESH::SMESH_Hypothesis_ptr anHyp)
432 if(MYDEBUG) MESSAGE("removeHypothesis()");
433 // **** proposer liste de subShape (selection multiple)
435 if (CORBA::is_nil(aSubShapeObject))
436 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
439 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
440 if (CORBA::is_nil(myHyp))
441 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
444 SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
447 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
448 int hypId = myHyp->GetId();
449 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
450 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
451 _mapHypo.erase( hypId );
453 catch(SALOME_Exception & S_ex)
455 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
460 //=============================================================================
464 //=============================================================================
466 SMESH::ListOfHypothesis *
467 SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
468 throw(SALOME::SALOME_Exception)
470 Unexpect aCatch(SALOME_SalomeException);
471 if (MYDEBUG) MESSAGE("GetHypothesisList");
472 if (CORBA::is_nil(aSubShapeObject))
473 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
476 SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
479 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
480 const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
481 int i = 0, n = aLocalList.size();
484 for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
485 SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
486 if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
487 aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
492 catch(SALOME_Exception & S_ex) {
493 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
496 return aList._retn();
499 //=============================================================================
503 //=============================================================================
504 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
505 const char* theName )
506 throw(SALOME::SALOME_Exception)
508 Unexpect aCatch(SALOME_SalomeException);
509 MESSAGE("SMESH_Mesh_i::GetSubMesh");
510 if (CORBA::is_nil(aSubShapeObject))
511 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
514 SMESH::SMESH_subMesh_var subMesh;
515 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(_this());
517 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
519 //Get or Create the SMESH_subMesh object implementation
521 int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
522 subMesh = getSubMesh( subMeshId );
524 // create a new subMesh object servant if there is none for the shape
525 if ( subMesh->_is_nil() )
526 subMesh = createSubMesh( aSubShapeObject );
528 if ( _gen_i->CanPublishInStudy( subMesh ))
529 _gen_i->PublishSubMesh (_gen_i->GetCurrentStudy(), aMesh,
530 subMesh, aSubShapeObject, theName );
532 catch(SALOME_Exception & S_ex) {
533 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
535 return subMesh._retn();
538 //=============================================================================
542 //=============================================================================
544 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
545 throw (SALOME::SALOME_Exception)
547 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
548 if ( theSubMesh->_is_nil() )
551 GEOM::GEOM_Object_var aSubShapeObject;
552 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
553 if ( !aStudy->_is_nil() ) {
554 // Remove submesh's SObject
555 SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
556 if ( !anSO->_is_nil() ) {
557 long aTag = SMESH_Gen_i::GetRefOnShapeTag();
558 SALOMEDS::SObject_var anObj, aRef;
559 if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
560 aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
562 aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
566 removeSubMesh( theSubMesh, aSubShapeObject.in() );
570 //=============================================================================
574 //=============================================================================
576 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
577 const char* theName )
578 throw(SALOME::SALOME_Exception)
580 Unexpect aCatch(SALOME_SalomeException);
581 SMESH::SMESH_Group_var aNewGroup =
582 SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
584 _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
585 aNewGroup, GEOM::GEOM_Object::_nil(), theName);
587 return aNewGroup._retn();
591 //=============================================================================
595 //=============================================================================
596 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM( SMESH::ElementType theElemType,
598 GEOM::GEOM_Object_ptr theGeomObj)
599 throw(SALOME::SALOME_Exception)
601 Unexpect aCatch(SALOME_SalomeException);
602 SMESH::SMESH_GroupOnGeom_var aNewGroup;
604 TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
605 if ( !aShape.IsNull() ) {
606 aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
607 ( createGroup( theElemType, theName, aShape ));
608 if ( _gen_i->CanPublishInStudy( aNewGroup ) )
609 _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
610 aNewGroup, theGeomObj, theName );
613 return aNewGroup._retn();
615 //=============================================================================
619 //=============================================================================
621 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
622 throw (SALOME::SALOME_Exception)
624 if ( theGroup->_is_nil() )
627 SMESH_GroupBase_i* aGroup =
628 dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
632 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
633 if ( !aStudy->_is_nil() ) {
634 // Remove group's SObject
635 SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
636 if ( !aGroupSO->_is_nil() )
637 aStudy->NewBuilder()->RemoveObject( aGroupSO );
640 // Remove the group from SMESH data structures
641 removeGroup( aGroup->GetLocalID() );
644 //=============================================================================
645 /*! RemoveGroupWithContents
646 * Remove group with its contents
648 //=============================================================================
649 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
650 throw (SALOME::SALOME_Exception)
652 if ( theGroup->_is_nil() )
655 SMESH_GroupBase_i* aGroup =
656 dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
660 SMESH::long_array_var anIds = aGroup->GetListOfID();
661 SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
663 if ( aGroup->GetType() == SMESH::NODE )
664 aMeshEditor->RemoveNodes( anIds );
666 aMeshEditor->RemoveElements( anIds );
668 RemoveGroup( theGroup );
671 //=============================================================================
673 * New group is created. All mesh elements that are
674 * present in initial groups are added to the new one
676 //=============================================================================
677 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
678 SMESH::SMESH_GroupBase_ptr theGroup2,
679 const char* theName )
680 throw (SALOME::SALOME_Exception)
684 SMESH::SMESH_Group_var aResGrp;
686 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
687 theGroup1->GetType() != theGroup2->GetType() )
688 return SMESH::SMESH_Group::_nil();
690 aResGrp = CreateGroup( theGroup1->GetType(), theName );
691 if ( aResGrp->_is_nil() )
692 return SMESH::SMESH_Group::_nil();
694 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
695 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
697 TColStd_MapOfInteger aResMap;
699 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
700 aResMap.Add( anIds1[ i1 ] );
702 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
703 aResMap.Add( anIds2[ i2 ] );
705 SMESH::long_array_var aResIds = new SMESH::long_array;
706 aResIds->length( aResMap.Extent() );
709 TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
710 for( ; anIter.More(); anIter.Next() )
711 aResIds[ resI++ ] = anIter.Key();
713 aResGrp->Add( aResIds );
715 return aResGrp._retn();
719 return SMESH::SMESH_Group::_nil();
723 //=============================================================================
725 * New group is created. All mesh elements that are
726 * present in both initial groups are added to the new one.
728 //=============================================================================
729 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
730 SMESH::SMESH_GroupBase_ptr theGroup2,
731 const char* theName )
732 throw (SALOME::SALOME_Exception)
734 SMESH::SMESH_Group_var aResGrp;
736 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
737 theGroup1->GetType() != theGroup2->GetType() )
740 aResGrp = CreateGroup( theGroup1->GetType(), theName );
741 if ( aResGrp->_is_nil() )
744 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
745 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
747 TColStd_MapOfInteger aMap1;
749 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
750 aMap1.Add( anIds1[ i1 ] );
752 TColStd_SequenceOfInteger aSeq;
754 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
755 if ( aMap1.Contains( anIds2[ i2 ] ) )
756 aSeq.Append( anIds2[ i2 ] );
758 SMESH::long_array_var aResIds = new SMESH::long_array;
759 aResIds->length( aSeq.Length() );
761 for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
762 aResIds[ resI ] = aSeq( resI + 1 );
764 aResGrp->Add( aResIds );
766 return aResGrp._retn();
769 //=============================================================================
771 * New group is created. All mesh elements that are present in
772 * main group but do not present in tool group are added to the new one
774 //=============================================================================
775 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
776 SMESH::SMESH_GroupBase_ptr theGroup2,
777 const char* theName )
778 throw (SALOME::SALOME_Exception)
780 SMESH::SMESH_Group_var aResGrp;
782 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
783 theGroup1->GetType() != theGroup2->GetType() )
786 aResGrp = CreateGroup( theGroup1->GetType(), theName );
787 if ( aResGrp->_is_nil() )
790 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
791 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
793 TColStd_MapOfInteger aMap2;
795 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
796 aMap2.Add( anIds2[ i2 ] );
799 TColStd_SequenceOfInteger aSeq;
800 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
801 if ( !aMap2.Contains( anIds1[ i1 ] ) )
802 aSeq.Append( anIds1[ i1 ] );
804 SMESH::long_array_var aResIds = new SMESH::long_array;
805 aResIds->length( aSeq.Length() );
807 for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
808 aResIds[ resI ] = aSeq( resI + 1 );
810 aResGrp->Add( aResIds );
812 return aResGrp._retn();
815 //=============================================================================
819 //=============================================================================
821 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
823 if(MYDEBUG) MESSAGE( "createSubMesh" );
824 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
826 ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
827 int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
828 SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
829 SMESH::SMESH_subMesh_var subMesh
830 = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
832 _mapSubMesh[subMeshId] = mySubMesh;
833 _mapSubMesh_i[subMeshId] = subMeshServant;
834 _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
836 // register CORBA object for persistence
837 int nextId = _gen_i->RegisterObject( subMesh );
838 if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
840 return subMesh._retn();
843 //=======================================================================
844 //function : getSubMesh
846 //=======================================================================
848 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
850 map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
851 if ( it == _mapSubMeshIor.end() )
852 return SMESH::SMESH_subMesh::_nil();
854 return SMESH::SMESH_subMesh::_duplicate( (*it).second );
858 //=============================================================================
862 //=============================================================================
864 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
865 GEOM::GEOM_Object_ptr theSubShapeObject )
867 MESSAGE("SMESH_Mesh_i::removeSubMesh()");
868 if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
872 SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
873 for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
874 removeHypothesis( theSubShapeObject, aHypList[i] );
877 catch( const SALOME::SALOME_Exception& ) {
878 INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
881 int subMeshId = theSubMesh->GetId();
883 _mapSubMesh.erase(subMeshId);
884 _mapSubMesh_i.erase(subMeshId);
885 _mapSubMeshIor.erase(subMeshId);
886 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
889 //=============================================================================
893 //=============================================================================
895 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
897 const TopoDS_Shape& theShape )
900 SMESH::SMESH_GroupBase_var aGroup;
901 if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
902 SMESH_GroupBase_i* aGroupImpl;
903 if ( !theShape.IsNull() )
904 aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
906 aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
908 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
909 SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
910 aGroupImpl->Register();
911 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
913 aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
914 _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
916 // register CORBA object for persistence
917 int nextId = _gen_i->RegisterObject( aGroup );
918 if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
920 return aGroup._retn();
923 //=============================================================================
925 * SMESH_Mesh_i::removeGroup
927 * Should be called by ~SMESH_Group_i()
929 //=============================================================================
931 void SMESH_Mesh_i::removeGroup( const int theId )
933 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );
934 if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
935 _mapGroups.erase( theId );
936 _impl->RemoveGroup( theId );
941 //=============================================================================
945 //=============================================================================
947 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
948 throw(SALOME::SALOME_Exception)
950 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
952 SMESH::log_array_var aLog;
954 list < SMESHDS_Command * >logDS = _impl->GetLog();
955 aLog = new SMESH::log_array;
957 int lg = logDS.size();
960 list < SMESHDS_Command * >::iterator its = logDS.begin();
961 while(its != logDS.end()){
962 SMESHDS_Command *com = *its;
963 int comType = com->GetType();
965 int lgcom = com->GetNumber();
967 const list < int >&intList = com->GetIndexes();
968 int inum = intList.size();
970 list < int >::const_iterator ii = intList.begin();
971 const list < double >&coordList = com->GetCoords();
972 int rnum = coordList.size();
974 list < double >::const_iterator ir = coordList.begin();
975 aLog[indexLog].commandType = comType;
976 aLog[indexLog].number = lgcom;
977 aLog[indexLog].coords.length(rnum);
978 aLog[indexLog].indexes.length(inum);
979 for(int i = 0; i < rnum; i++){
980 aLog[indexLog].coords[i] = *ir;
981 //MESSAGE(" "<<i<<" "<<ir.Value());
984 for(int i = 0; i < inum; i++){
985 aLog[indexLog].indexes[i] = *ii;
986 //MESSAGE(" "<<i<<" "<<ii.Value());
995 catch(SALOME_Exception & S_ex){
996 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1002 //=============================================================================
1006 //=============================================================================
1008 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
1010 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
1014 //=============================================================================
1018 //=============================================================================
1020 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1022 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1026 //=============================================================================
1030 //=============================================================================
1032 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1037 //=============================================================================
1041 //=============================================================================
1043 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1045 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1049 //=============================================================================
1053 //=============================================================================
1055 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1057 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1062 //=============================================================================
1066 //=============================================================================
1068 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1070 // Update Python script
1071 SALOMEDS::SObject_var aSO =
1072 SMESH_Gen_i::ObjectToSObject(_gen_i->GetCurrentStudy(), _this());
1073 TCollection_AsciiString aStr ("mesh_editor = ");
1074 aStr += aSO->GetID();
1075 aStr += ".GetMeshEditor()";
1077 _gen_i->AddToPythonScript(_gen_i->GetCurrentStudy()->StudyId(), aStr);
1079 // Create MeshEditor
1080 SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl );
1081 SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1082 return aMesh._retn();
1085 //=============================================================================
1089 //=============================================================================
1091 void SMESH_Mesh_i::ExportToMED( const char* file,
1092 CORBA::Boolean auto_groups,
1093 SMESH::MED_VERSION theVersion )
1094 throw(SALOME::SALOME_Exception)
1096 Unexpect aCatch(SALOME_SalomeException);
1098 char* aMeshName = "Mesh";
1099 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1100 if ( !aStudy->_is_nil() ) {
1101 SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1102 if ( !aMeshSO->_is_nil() ) {
1103 aMeshName = aMeshSO->GetName();
1105 //SCRUTE(aMeshName);
1106 //SCRUTE(aMeshSO->GetID());
1108 // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes
1109 if ( !aStudy->GetProperties()->IsLocked() )
1111 SALOMEDS::GenericAttribute_var anAttr;
1112 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1113 SALOMEDS::AttributeExternalFileDef_var aFileName;
1114 anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1115 aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1116 ASSERT(!aFileName->_is_nil());
1117 aFileName->SetValue(file);
1118 SALOMEDS::AttributeFileType_var aFileType;
1119 anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1120 aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1121 ASSERT(!aFileType->_is_nil());
1122 aFileType->SetValue("FICHIERMED");
1126 _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1129 void SMESH_Mesh_i::ExportMED( const char* file,
1130 CORBA::Boolean auto_groups)
1131 throw(SALOME::SALOME_Exception)
1133 ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1136 void SMESH_Mesh_i::ExportDAT(const char *file) throw(SALOME::SALOME_Exception)
1138 Unexpect aCatch(SALOME_SalomeException);
1139 _impl->ExportDAT(file);
1141 void SMESH_Mesh_i::ExportUNV(const char *file) throw(SALOME::SALOME_Exception)
1143 Unexpect aCatch(SALOME_SalomeException);
1144 _impl->ExportUNV(file);
1147 void SMESH_Mesh_i::ExportSTL(const char *file, const bool isascii) throw(SALOME::SALOME_Exception)
1149 Unexpect aCatch(SALOME_SalomeException);
1150 _impl->ExportSTL(file, isascii);
1153 //=============================================================================
1157 //=============================================================================
1159 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1161 Unexpect aCatch(SALOME_SalomeException);
1162 SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1163 SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1164 return aMesh._retn();
1167 //=============================================================================
1171 //=============================================================================
1172 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1174 Unexpect aCatch(SALOME_SalomeException);
1175 return _impl->NbNodes();
1178 //=============================================================================
1182 //=============================================================================
1183 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1185 Unexpect aCatch(SALOME_SalomeException);
1186 return NbEdges() + NbFaces() + NbVolumes();
1189 //=============================================================================
1193 //=============================================================================
1194 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1196 Unexpect aCatch(SALOME_SalomeException);
1197 return _impl->NbEdges();
1200 //=============================================================================
1204 //=============================================================================
1205 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1207 Unexpect aCatch(SALOME_SalomeException);
1208 return _impl->NbFaces();
1211 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1213 Unexpect aCatch(SALOME_SalomeException);
1214 return _impl->NbTriangles();
1217 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1219 Unexpect aCatch(SALOME_SalomeException);
1220 return _impl->NbQuadrangles();
1223 CORBA::Long SMESH_Mesh_i::NbPolygons()throw(SALOME::SALOME_Exception)
1225 Unexpect aCatch(SALOME_SalomeException);
1226 return _impl->NbPolygons();
1229 //=============================================================================
1233 //=============================================================================
1234 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1236 Unexpect aCatch(SALOME_SalomeException);
1237 return _impl->NbVolumes();
1240 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1242 Unexpect aCatch(SALOME_SalomeException);
1243 return _impl->NbTetras();
1246 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1248 Unexpect aCatch(SALOME_SalomeException);
1249 return _impl->NbHexas();
1252 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1254 Unexpect aCatch(SALOME_SalomeException);
1255 return _impl->NbPyramids();
1258 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1260 Unexpect aCatch(SALOME_SalomeException);
1261 return _impl->NbPrisms();
1264 CORBA::Long SMESH_Mesh_i::NbPolyhedrons()throw(SALOME::SALOME_Exception)
1266 Unexpect aCatch(SALOME_SalomeException);
1267 return _impl->NbPolyhedrons();
1270 //=============================================================================
1274 //=============================================================================
1275 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1277 Unexpect aCatch(SALOME_SalomeException);
1278 return _impl->NbSubMesh();
1281 //=============================================================================
1285 //=============================================================================
1286 char* SMESH_Mesh_i::Dump()
1288 std::ostringstream os;
1290 return CORBA::string_dup( os.str().c_str() );
1293 //=============================================================================
1297 //=============================================================================
1298 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1300 SMESH::long_array_var aResult = new SMESH::long_array();
1301 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1302 int aMinId = aSMESHDS_Mesh->MinElementID();
1303 int aMaxId = aSMESHDS_Mesh->MaxElementID();
1305 aResult->length(aMaxId - aMinId + 1);
1307 for (int i = 0, id = aMinId; id <= aMaxId; id++ )
1310 return aResult._retn();
1313 //=============================================================================
1317 //=============================================================================
1319 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1320 throw (SALOME::SALOME_Exception)
1322 Unexpect aCatch(SALOME_SalomeException);
1323 MESSAGE("SMESH_Mesh_i::GetElementsId");
1324 SMESH::long_array_var aResult = new SMESH::long_array();
1325 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1327 if ( aSMESHDS_Mesh == NULL )
1328 return aResult._retn();
1330 long nbElements = NbElements();
1331 aResult->length( nbElements );
1332 SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1333 for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1334 aResult[i] = anIt->next()->GetID();
1336 return aResult._retn();
1340 //=============================================================================
1344 //=============================================================================
1346 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1347 throw (SALOME::SALOME_Exception)
1349 Unexpect aCatch(SALOME_SalomeException);
1350 MESSAGE("SMESH_subMesh_i::GetElementsByType");
1351 SMESH::long_array_var aResult = new SMESH::long_array();
1352 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1354 if ( aSMESHDS_Mesh == NULL )
1355 return aResult._retn();
1357 long nbElements = NbElements();
1359 // No sense in returning ids of elements along with ids of nodes:
1360 // when theElemType == SMESH::ALL, return node ids only if
1361 // there are no elements
1362 if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1363 return GetNodesId();
1365 aResult->length( nbElements );
1369 SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1370 while ( i < nbElements && anIt->more() ) {
1371 const SMDS_MeshElement* anElem = anIt->next();
1372 if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1373 aResult[i++] = anElem->GetID();
1376 aResult->length( i );
1378 return aResult._retn();
1381 //=============================================================================
1385 //=============================================================================
1387 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1388 throw (SALOME::SALOME_Exception)
1390 Unexpect aCatch(SALOME_SalomeException);
1391 MESSAGE("SMESH_subMesh_i::GetNodesId");
1392 SMESH::long_array_var aResult = new SMESH::long_array();
1393 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1395 if ( aSMESHDS_Mesh == NULL )
1396 return aResult._retn();
1398 long nbNodes = NbNodes();
1399 aResult->length( nbNodes );
1400 SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1401 for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1402 aResult[i] = anIt->next()->GetID();
1404 return aResult._retn();