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 "SMESHDS_Command.hxx"
44 #include "SMESHDS_CommandType.hxx"
45 #include "SMESH_MeshEditor_i.hxx"
46 #include "SMESH_Gen_i.hxx"
47 #include "DriverMED_R_SMESHDS_Mesh.h"
50 #include <OSD_Path.hxx>
51 #include <OSD_File.hxx>
52 #include <OSD_Directory.hxx>
53 #include <OSD_Protection.hxx>
54 #include <TColStd_MapOfInteger.hxx>
55 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
56 #include <TColStd_SequenceOfInteger.hxx>
57 #include <TCollection_AsciiString.hxx>
65 static int MYDEBUG = 0;
67 static int MYDEBUG = 0;
72 int SMESH_Mesh_i::myIdGenerator = 0;
74 //=============================================================================
78 //=============================================================================
80 SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
83 : SALOME::GenericObj_i( thePOA )
85 INFOS("SMESH_Mesh_i; this = "<<this);
88 _id = myIdGenerator++;
90 thePOA->activate_object( this );
93 //=============================================================================
97 //=============================================================================
99 SMESH_Mesh_i::~SMESH_Mesh_i()
101 INFOS("~SMESH_Mesh_i; this = "<<this);
102 map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
103 for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
104 SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
107 // this method is colled from destructor of group (PAL6331)
108 //_impl->RemoveGroup( aGroup->GetLocalID() );
116 //=============================================================================
120 * Associates <this> mesh with <theShape> and puts a reference
121 * to <theShape> into the current study;
122 * the previous shape is substituted by the new one.
124 //=============================================================================
126 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
127 throw (SALOME::SALOME_Exception)
129 Unexpect aCatch(SALOME_SalomeException);
131 _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
133 catch(SALOME_Exception & S_ex) {
134 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
138 //=======================================================================
139 //function : GetShapeToMesh
141 //=======================================================================
143 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
144 throw (SALOME::SALOME_Exception)
146 Unexpect aCatch(SALOME_SalomeException);
147 GEOM::GEOM_Object_var aShapeObj;
149 TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
151 aShapeObj = _gen_i->ShapeToGeomObject( S );
153 catch(SALOME_Exception & S_ex) {
154 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
156 return aShapeObj._retn();
159 //=============================================================================
163 //=============================================================================
165 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
167 SMESH::DriverMED_ReadStatus res;
170 case DriverMED_R_SMESHDS_Mesh::DRS_OK:
171 res = SMESH::DRS_OK; break;
172 case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
173 res = SMESH::DRS_EMPTY; break;
174 case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
175 res = SMESH::DRS_WARN_RENUMBER; break;
176 case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
177 res = SMESH::DRS_WARN_SKIP_ELEM; break;
178 case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
180 res = SMESH::DRS_FAIL; break;
185 //=============================================================================
189 * Imports mesh data from MED file
191 //=============================================================================
193 SMESH::DriverMED_ReadStatus
194 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
195 throw ( SALOME::SALOME_Exception )
197 Unexpect aCatch(SALOME_SalomeException);
200 status = importMEDFile( theFileName, theMeshName );
202 catch( SALOME_Exception& S_ex ) {
203 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
206 THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
209 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
210 if ( !aStudy->_is_nil() ) {
211 // publishing of the groups in the study (sub-meshes are out of scope of MED import)
212 map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
213 for (; it != _mapGroups.end(); it++ ) {
214 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
215 _gen_i->PublishGroup( aStudy, _this(), aGroup,
216 GEOM::GEOM_Object::_nil(), aGroup->GetName());
219 return ConvertDriverMEDReadStatus(status);
222 //=============================================================================
226 * Imports mesh data from MED file
228 //=============================================================================
230 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
231 throw ( SALOME::SALOME_Exception )
233 // Read mesh with name = <theMeshName> into SMESH_Mesh
234 _impl->UNVToMesh( theFileName );
239 //=============================================================================
243 * Imports mesh data from STL file
245 //=============================================================================
246 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
247 throw ( SALOME::SALOME_Exception )
249 // Read mesh with name = <theMeshName> into SMESH_Mesh
250 _impl->STLToMesh( theFileName );
255 //=============================================================================
259 * Imports mesh data from MED file
261 //=============================================================================
263 int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
265 // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
266 int status = _impl->MEDToMesh( theFileName, theMeshName );
268 // Create group servants, if any groups were imported
269 list<int> aGroupIds = _impl->GetGroupIds();
270 for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
271 SMESH_Group_i* aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
273 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
274 SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
275 aGroupImpl->Register();
276 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
278 SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
279 _mapGroups[*it] = SMESH::SMESH_Group::_duplicate( aGroup );
281 // register CORBA object for persistence
282 int nextId = _gen_i->RegisterObject( aGroup );
283 if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
289 //=============================================================================
293 //=============================================================================
295 static SMESH::Hypothesis_Status ConvertHypothesisStatus
296 (SMESH_Hypothesis::Hypothesis_Status theStatus)
298 SMESH::Hypothesis_Status res;
301 case SMESH_Hypothesis::HYP_OK:
302 res = SMESH::HYP_OK; break;
303 case SMESH_Hypothesis::HYP_MISSING:
304 res = SMESH::HYP_MISSING; break;
305 case SMESH_Hypothesis::HYP_CONCURENT:
306 res = SMESH::HYP_CONCURENT; break;
307 case SMESH_Hypothesis::HYP_BAD_PARAMETER:
308 res = SMESH::HYP_BAD_PARAMETER; break;
309 case SMESH_Hypothesis::HYP_INCOMPATIBLE:
310 res = SMESH::HYP_INCOMPATIBLE; break;
311 case SMESH_Hypothesis::HYP_NOTCONFORM:
312 res = SMESH::HYP_NOTCONFORM; break;
313 case SMESH_Hypothesis::HYP_ALREADY_EXIST:
314 res = SMESH::HYP_ALREADY_EXIST; break;
315 case SMESH_Hypothesis::HYP_BAD_DIM:
316 res = SMESH::HYP_BAD_DIM; break;
318 res = SMESH::HYP_UNKNOWN_FATAL;
323 //=============================================================================
327 * calls internal addHypothesis() and then adds a reference to <anHyp> under
328 * the SObject actually having a reference to <aSubShape>.
329 * NB: For this method to work, it is necessary to add a reference to sub-shape first.
331 //=============================================================================
333 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
334 SMESH::SMESH_Hypothesis_ptr anHyp)
335 throw(SALOME::SALOME_Exception)
337 Unexpect aCatch(SALOME_SalomeException);
338 SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
340 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
341 _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
342 aSubShapeObject, anHyp );
344 if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
346 return ConvertHypothesisStatus(status);
349 //=============================================================================
353 //=============================================================================
355 SMESH_Hypothesis::Hypothesis_Status
356 SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
357 SMESH::SMESH_Hypothesis_ptr anHyp)
359 if(MYDEBUG) MESSAGE("addHypothesis");
361 if (CORBA::is_nil(aSubShapeObject))
362 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
365 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
366 if (CORBA::is_nil(myHyp))
367 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
370 SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
373 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
374 int hypId = myHyp->GetId();
375 status = _impl->AddHypothesis(myLocSubShape, hypId);
376 if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
377 _mapHypo[hypId] = myHyp;
378 // assure there is a corresponding submesh
379 if ( !_impl->IsMainShape( myLocSubShape )) {
380 int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
381 if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
382 createSubMesh( aSubShapeObject );
386 catch(SALOME_Exception & S_ex)
388 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
393 //=============================================================================
397 //=============================================================================
399 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
400 SMESH::SMESH_Hypothesis_ptr anHyp)
401 throw(SALOME::SALOME_Exception)
403 Unexpect aCatch(SALOME_SalomeException);
404 SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
406 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
407 _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
408 aSubShapeObject, anHyp );
410 return ConvertHypothesisStatus(status);
413 //=============================================================================
417 //=============================================================================
419 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
420 SMESH::SMESH_Hypothesis_ptr anHyp)
422 if(MYDEBUG) MESSAGE("removeHypothesis()");
423 // **** proposer liste de subShape (selection multiple)
425 if (CORBA::is_nil(aSubShapeObject))
426 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
429 SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
430 if (CORBA::is_nil(myHyp))
431 THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
434 SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
437 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
438 int hypId = myHyp->GetId();
439 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
440 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
441 _mapHypo.erase( hypId );
443 catch(SALOME_Exception & S_ex)
445 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
450 //=============================================================================
454 //=============================================================================
456 SMESH::ListOfHypothesis *
457 SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
458 throw(SALOME::SALOME_Exception)
460 Unexpect aCatch(SALOME_SalomeException);
461 if (MYDEBUG) MESSAGE("GetHypothesisList");
462 if (CORBA::is_nil(aSubShapeObject))
463 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
466 SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
469 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
470 const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
471 int i = 0, n = aLocalList.size();
474 for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
475 SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
476 if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
477 aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
482 catch(SALOME_Exception & S_ex) {
483 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
486 return aList._retn();
489 //=============================================================================
493 //=============================================================================
494 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
495 const char* theName )
496 throw(SALOME::SALOME_Exception)
498 Unexpect aCatch(SALOME_SalomeException);
499 MESSAGE("SMESH_Mesh_i::GetSubMesh");
500 if (CORBA::is_nil(aSubShapeObject))
501 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
504 SMESH::SMESH_subMesh_var subMesh;
505 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(_this());
507 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
509 //Get or Create the SMESH_subMesh object implementation
511 int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
512 subMesh = getSubMesh( subMeshId );
514 // create a new subMesh object servant if there is none for the shape
515 if ( subMesh->_is_nil() )
516 subMesh = createSubMesh( aSubShapeObject );
518 if ( _gen_i->CanPublishInStudy( subMesh ))
519 _gen_i->PublishSubMesh (_gen_i->GetCurrentStudy(), aMesh,
520 subMesh, aSubShapeObject, theName );
522 catch(SALOME_Exception & S_ex) {
523 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
525 return subMesh._retn();
528 //=============================================================================
532 //=============================================================================
534 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
535 throw (SALOME::SALOME_Exception)
537 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
538 if ( theSubMesh->_is_nil() )
541 GEOM::GEOM_Object_var aSubShapeObject;
542 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
543 if ( !aStudy->_is_nil() ) {
544 // Remove submesh's SObject
545 SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
546 if ( !anSO->_is_nil() ) {
547 long aTag = SMESH_Gen_i::GetRefOnShapeTag();
548 SALOMEDS::SObject_var anObj, aRef;
549 if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
550 aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
552 aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
556 removeSubMesh( theSubMesh, aSubShapeObject.in() );
560 //=============================================================================
564 //=============================================================================
566 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
567 const char* theName )
568 throw(SALOME::SALOME_Exception)
570 Unexpect aCatch(SALOME_SalomeException);
571 SMESH::SMESH_Group_var aNewGroup =
572 SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
574 _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
575 aNewGroup, GEOM::GEOM_Object::_nil(), theName);
577 return aNewGroup._retn();
581 //=============================================================================
585 //=============================================================================
586 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM( SMESH::ElementType theElemType,
588 GEOM::GEOM_Object_ptr theGeomObj)
589 throw(SALOME::SALOME_Exception)
591 Unexpect aCatch(SALOME_SalomeException);
592 SMESH::SMESH_GroupOnGeom_var aNewGroup;
594 TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
595 if ( !aShape.IsNull() ) {
596 aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
597 ( createGroup( theElemType, theName, aShape ));
598 if ( _gen_i->CanPublishInStudy( aNewGroup ) )
599 _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
600 aNewGroup, theGeomObj, theName );
603 return aNewGroup._retn();
605 //=============================================================================
609 //=============================================================================
611 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
612 throw (SALOME::SALOME_Exception)
614 if ( theGroup->_is_nil() )
617 SMESH_GroupBase_i* aGroup =
618 dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
622 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
623 if ( !aStudy->_is_nil() ) {
624 // Remove group's SObject
625 SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
626 if ( !aGroupSO->_is_nil() )
627 aStudy->NewBuilder()->RemoveObject( aGroupSO );
630 // Remove the group from SMESH data structures
631 removeGroup( aGroup->GetLocalID() );
634 //=============================================================================
635 /*! RemoveGroupWithContents
636 * Remove group with its contents
638 //=============================================================================
639 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
640 throw (SALOME::SALOME_Exception)
642 if ( theGroup->_is_nil() )
645 SMESH_GroupBase_i* aGroup =
646 dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
650 SMESH::long_array_var anIds = aGroup->GetListOfID();
651 SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
653 if ( aGroup->GetType() == SMESH::NODE )
654 aMeshEditor->RemoveNodes( anIds );
656 aMeshEditor->RemoveElements( anIds );
658 RemoveGroup( theGroup );
661 //=============================================================================
663 * New group is created. All mesh elements that are
664 * present in initial groups are added to the new one
666 //=============================================================================
667 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
668 SMESH::SMESH_GroupBase_ptr theGroup2,
669 const char* theName )
670 throw (SALOME::SALOME_Exception)
674 SMESH::SMESH_Group_var aResGrp;
676 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
677 theGroup1->GetType() != theGroup2->GetType() )
678 return SMESH::SMESH_Group::_nil();
680 aResGrp = CreateGroup( theGroup1->GetType(), theName );
681 if ( aResGrp->_is_nil() )
682 return SMESH::SMESH_Group::_nil();
684 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
685 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
687 TColStd_MapOfInteger aResMap;
689 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
690 aResMap.Add( anIds1[ i1 ] );
692 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
693 aResMap.Add( anIds2[ i2 ] );
695 SMESH::long_array_var aResIds = new SMESH::long_array;
696 aResIds->length( aResMap.Extent() );
699 TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
700 for( ; anIter.More(); anIter.Next() )
701 aResIds[ resI++ ] = anIter.Key();
703 aResGrp->Add( aResIds );
705 return aResGrp._retn();
709 return SMESH::SMESH_Group::_nil();
713 //=============================================================================
715 * New group is created. All mesh elements that are
716 * present in both initial groups are added to the new one.
718 //=============================================================================
719 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
720 SMESH::SMESH_GroupBase_ptr theGroup2,
721 const char* theName )
722 throw (SALOME::SALOME_Exception)
724 SMESH::SMESH_Group_var aResGrp;
726 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
727 theGroup1->GetType() != theGroup2->GetType() )
730 aResGrp = CreateGroup( theGroup1->GetType(), theName );
731 if ( aResGrp->_is_nil() )
734 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
735 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
737 TColStd_MapOfInteger aMap1;
739 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
740 aMap1.Add( anIds1[ i1 ] );
742 TColStd_SequenceOfInteger aSeq;
744 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
745 if ( aMap1.Contains( anIds2[ i2 ] ) )
746 aSeq.Append( anIds2[ i2 ] );
748 SMESH::long_array_var aResIds = new SMESH::long_array;
749 aResIds->length( aSeq.Length() );
751 for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
752 aResIds[ resI ] = aSeq( resI + 1 );
754 aResGrp->Add( aResIds );
756 return aResGrp._retn();
759 //=============================================================================
761 * New group is created. All mesh elements that are present in
762 * main group but do not present in tool group are added to the new one
764 //=============================================================================
765 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
766 SMESH::SMESH_GroupBase_ptr theGroup2,
767 const char* theName )
768 throw (SALOME::SALOME_Exception)
770 SMESH::SMESH_Group_var aResGrp;
772 if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
773 theGroup1->GetType() != theGroup2->GetType() )
776 aResGrp = CreateGroup( theGroup1->GetType(), theName );
777 if ( aResGrp->_is_nil() )
780 SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
781 SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
783 TColStd_MapOfInteger aMap2;
785 for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
786 aMap2.Add( anIds2[ i2 ] );
789 TColStd_SequenceOfInteger aSeq;
790 for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
791 if ( !aMap2.Contains( anIds1[ i1 ] ) )
792 aSeq.Append( anIds1[ i1 ] );
794 SMESH::long_array_var aResIds = new SMESH::long_array;
795 aResIds->length( aSeq.Length() );
797 for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
798 aResIds[ resI ] = aSeq( resI + 1 );
800 aResGrp->Add( aResIds );
802 return aResGrp._retn();
805 //=============================================================================
809 //=============================================================================
811 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
813 if(MYDEBUG) MESSAGE( "createSubMesh" );
814 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
816 ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
817 int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
818 SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
819 SMESH::SMESH_subMesh_var subMesh
820 = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
822 _mapSubMesh[subMeshId] = mySubMesh;
823 _mapSubMesh_i[subMeshId] = subMeshServant;
824 _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
826 // register CORBA object for persistence
827 int nextId = _gen_i->RegisterObject( subMesh );
828 if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
830 return subMesh._retn();
833 //=======================================================================
834 //function : getSubMesh
836 //=======================================================================
838 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
840 map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
841 if ( it == _mapSubMeshIor.end() )
842 return SMESH::SMESH_subMesh::_nil();
844 return SMESH::SMESH_subMesh::_duplicate( (*it).second );
848 //=============================================================================
852 //=============================================================================
854 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
855 GEOM::GEOM_Object_ptr theSubShapeObject )
857 MESSAGE("SMESH_Mesh_i::removeSubMesh()");
858 if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
862 SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
863 for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
864 removeHypothesis( theSubShapeObject, aHypList[i] );
867 catch( const SALOME::SALOME_Exception& ) {
868 INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
871 int subMeshId = theSubMesh->GetId();
873 _mapSubMesh.erase(subMeshId);
874 _mapSubMesh_i.erase(subMeshId);
875 _mapSubMeshIor.erase(subMeshId);
876 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
879 //=============================================================================
883 //=============================================================================
885 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
887 const TopoDS_Shape& theShape )
890 SMESH::SMESH_GroupBase_var aGroup;
891 if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
892 SMESH_GroupBase_i* aGroupImpl;
893 if ( !theShape.IsNull() )
894 aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
896 aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
898 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
899 SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
900 aGroupImpl->Register();
901 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
903 aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
904 _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
906 // register CORBA object for persistence
907 int nextId = _gen_i->RegisterObject( aGroup );
908 if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
910 return aGroup._retn();
913 //=============================================================================
915 * SMESH_Mesh_i::removeGroup
917 * Should be called by ~SMESH_Group_i()
919 //=============================================================================
921 void SMESH_Mesh_i::removeGroup( const int theId )
923 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );
924 if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
925 _mapGroups.erase( theId );
926 _impl->RemoveGroup( theId );
931 //=============================================================================
935 //=============================================================================
937 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
938 throw(SALOME::SALOME_Exception)
940 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
942 SMESH::log_array_var aLog;
944 list < SMESHDS_Command * >logDS = _impl->GetLog();
945 aLog = new SMESH::log_array;
947 int lg = logDS.size();
950 list < SMESHDS_Command * >::iterator its = logDS.begin();
951 while(its != logDS.end()){
952 SMESHDS_Command *com = *its;
953 int comType = com->GetType();
955 int lgcom = com->GetNumber();
957 const list < int >&intList = com->GetIndexes();
958 int inum = intList.size();
960 list < int >::const_iterator ii = intList.begin();
961 const list < double >&coordList = com->GetCoords();
962 int rnum = coordList.size();
964 list < double >::const_iterator ir = coordList.begin();
965 aLog[indexLog].commandType = comType;
966 aLog[indexLog].number = lgcom;
967 aLog[indexLog].coords.length(rnum);
968 aLog[indexLog].indexes.length(inum);
969 for(int i = 0; i < rnum; i++){
970 aLog[indexLog].coords[i] = *ir;
971 //MESSAGE(" "<<i<<" "<<ir.Value());
974 for(int i = 0; i < inum; i++){
975 aLog[indexLog].indexes[i] = *ii;
976 //MESSAGE(" "<<i<<" "<<ii.Value());
985 catch(SALOME_Exception & S_ex){
986 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
992 //=============================================================================
996 //=============================================================================
998 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
1000 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
1004 //=============================================================================
1008 //=============================================================================
1010 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1012 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1016 //=============================================================================
1020 //=============================================================================
1022 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1027 //=============================================================================
1031 //=============================================================================
1033 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1035 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1039 //=============================================================================
1043 //=============================================================================
1045 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1047 if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1052 //=============================================================================
1056 //=============================================================================
1058 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1060 SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl );
1061 SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1062 return aMesh._retn();
1065 //=============================================================================
1069 //=============================================================================
1071 static void PrepareForWriting (const char* file)
1073 TCollection_AsciiString aFullName ((char*)file);
1074 OSD_Path aPath (aFullName);
1075 OSD_File aFile (aPath);
1076 if (aFile.Exists()) {
1077 // existing filesystem node
1078 if (aFile.KindOfFile() == OSD_FILE) {
1079 if (aFile.IsWriteable()) {
1082 if (aFile.Failed()) {
1083 TCollection_AsciiString msg ("File ");
1084 msg += aFullName + " cannot be replaced.";
1085 THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1088 TCollection_AsciiString msg ("File ");
1089 msg += aFullName + " cannot be overwritten.";
1090 THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1093 TCollection_AsciiString msg ("Location ");
1094 msg += aFullName + " is not a file.";
1095 THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1099 TCollection_AsciiString aDirName = aPath.TrekValue(aPath.TrekLength());
1101 aPath.SetName(aDirName);
1102 aPath.SetExtension("");
1103 OSD_Directory aDir (aPath);
1104 TCollection_AsciiString aFullDirName;
1105 aPath.SystemName(aFullDirName);
1106 if (aDir.Exists()) {
1108 aFile.Build(OSD_WriteOnly, OSD_Protection());
1109 if (aFile.Failed()) {
1110 TCollection_AsciiString msg ("You cannot write to directory ");
1111 msg += aFullDirName + ".";
1112 THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1118 TCollection_AsciiString msg ("Directory ");
1119 msg += aFullDirName + " does not exist.";
1120 THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1125 void SMESH_Mesh_i::ExportToMED( const char* file,
1126 CORBA::Boolean auto_groups,
1127 SMESH::MED_VERSION theVersion )
1128 throw(SALOME::SALOME_Exception)
1130 Unexpect aCatch(SALOME_SalomeException);
1132 PrepareForWriting(file);
1133 char* aMeshName = "Mesh";
1134 SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1135 if ( !aStudy->_is_nil() ) {
1136 SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1137 if ( !aMeshSO->_is_nil() ) {
1138 aMeshName = aMeshSO->GetName();
1140 //SCRUTE(aMeshName);
1141 //SCRUTE(aMeshSO->GetID());
1143 // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes
1144 if ( !aStudy->GetProperties()->IsLocked() )
1146 SALOMEDS::GenericAttribute_var anAttr;
1147 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1148 SALOMEDS::AttributeExternalFileDef_var aFileName;
1149 anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1150 aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1151 ASSERT(!aFileName->_is_nil());
1152 aFileName->SetValue(file);
1153 SALOMEDS::AttributeFileType_var aFileType;
1154 anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1155 aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1156 ASSERT(!aFileType->_is_nil());
1157 aFileType->SetValue("FICHIERMED");
1161 _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1164 void SMESH_Mesh_i::ExportMED( const char* file,
1165 CORBA::Boolean auto_groups)
1166 throw(SALOME::SALOME_Exception)
1168 ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1171 void SMESH_Mesh_i::ExportDAT(const char *file) throw(SALOME::SALOME_Exception)
1173 Unexpect aCatch(SALOME_SalomeException);
1174 PrepareForWriting(file);
1175 _impl->ExportDAT(file);
1177 void SMESH_Mesh_i::ExportUNV(const char *file) throw(SALOME::SALOME_Exception)
1179 Unexpect aCatch(SALOME_SalomeException);
1180 PrepareForWriting(file);
1181 _impl->ExportUNV(file);
1184 void SMESH_Mesh_i::ExportSTL(const char *file, const bool isascii) throw(SALOME::SALOME_Exception)
1186 Unexpect aCatch(SALOME_SalomeException);
1187 PrepareForWriting(file);
1188 _impl->ExportSTL(file, isascii);
1191 //=============================================================================
1195 //=============================================================================
1197 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1199 Unexpect aCatch(SALOME_SalomeException);
1200 SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1201 SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1202 return aMesh._retn();
1205 //=============================================================================
1209 //=============================================================================
1210 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1212 Unexpect aCatch(SALOME_SalomeException);
1213 return _impl->NbNodes();
1216 //=============================================================================
1220 //=============================================================================
1221 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1223 Unexpect aCatch(SALOME_SalomeException);
1224 return NbEdges() + NbFaces() + NbVolumes();
1227 //=============================================================================
1231 //=============================================================================
1232 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1234 Unexpect aCatch(SALOME_SalomeException);
1235 return _impl->NbEdges();
1238 //=============================================================================
1242 //=============================================================================
1243 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1245 Unexpect aCatch(SALOME_SalomeException);
1246 return _impl->NbFaces();
1249 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1251 Unexpect aCatch(SALOME_SalomeException);
1252 return _impl->NbTriangles();
1255 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1257 Unexpect aCatch(SALOME_SalomeException);
1258 return _impl->NbQuadrangles();
1261 //=============================================================================
1265 //=============================================================================
1266 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1268 Unexpect aCatch(SALOME_SalomeException);
1269 return _impl->NbVolumes();
1272 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1274 Unexpect aCatch(SALOME_SalomeException);
1275 return _impl->NbTetras();
1278 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1280 Unexpect aCatch(SALOME_SalomeException);
1281 return _impl->NbHexas();
1284 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1286 Unexpect aCatch(SALOME_SalomeException);
1287 return _impl->NbPyramids();
1290 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1292 Unexpect aCatch(SALOME_SalomeException);
1293 return _impl->NbPrisms();
1296 //=============================================================================
1300 //=============================================================================
1301 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1303 Unexpect aCatch(SALOME_SalomeException);
1304 return _impl->NbSubMesh();
1307 //=============================================================================
1311 //=============================================================================
1312 char* SMESH_Mesh_i::Dump()
1314 std::ostringstream os;
1316 return CORBA::string_dup( os.str().c_str() );
1319 //=============================================================================
1323 //=============================================================================
1324 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1326 SMESH::long_array_var aResult = new SMESH::long_array();
1327 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1328 int aMinId = aSMESHDS_Mesh->MinElementID();
1329 int aMaxId = aSMESHDS_Mesh->MaxElementID();
1331 aResult->length(aMaxId - aMinId + 1);
1333 for (int i = 0, id = aMinId; id <= aMaxId; id++ )
1336 return aResult._retn();
1339 //=============================================================================
1343 //=============================================================================
1345 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1346 throw (SALOME::SALOME_Exception)
1348 Unexpect aCatch(SALOME_SalomeException);
1349 MESSAGE("SMESH_Mesh_i::GetElementsId");
1350 SMESH::long_array_var aResult = new SMESH::long_array();
1351 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1353 if ( aSMESHDS_Mesh == NULL )
1354 return aResult._retn();
1356 long nbElements = NbElements();
1357 aResult->length( nbElements );
1358 SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1359 for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1360 aResult[i] = anIt->next()->GetID();
1362 return aResult._retn();
1366 //=============================================================================
1370 //=============================================================================
1372 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1373 throw (SALOME::SALOME_Exception)
1375 Unexpect aCatch(SALOME_SalomeException);
1376 MESSAGE("SMESH_subMesh_i::GetElementsByType");
1377 SMESH::long_array_var aResult = new SMESH::long_array();
1378 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1380 if ( aSMESHDS_Mesh == NULL )
1381 return aResult._retn();
1383 long nbElements = NbElements();
1385 // No sense in returning ids of elements along with ids of nodes:
1386 // when theElemType == SMESH::ALL, return node ids only if
1387 // there are no elements
1388 if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1389 return GetNodesId();
1391 aResult->length( nbElements );
1395 SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1396 while ( i < nbElements && anIt->more() ) {
1397 const SMDS_MeshElement* anElem = anIt->next();
1398 if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1399 aResult[i++] = anElem->GetID();
1402 aResult->length( i );
1404 return aResult._retn();
1407 //=============================================================================
1411 //=============================================================================
1413 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1414 throw (SALOME::SALOME_Exception)
1416 Unexpect aCatch(SALOME_SalomeException);
1417 MESSAGE("SMESH_subMesh_i::GetNodesId");
1418 SMESH::long_array_var aResult = new SMESH::long_array();
1419 SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1421 if ( aSMESHDS_Mesh == NULL )
1422 return aResult._retn();
1424 long nbNodes = NbNodes();
1425 aResult->length( nbNodes );
1426 SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1427 for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1428 aResult[i] = anIt->next()->GetID();
1430 return aResult._retn();