Version: 6.3.1

src/SMESH_I/SMESH_Mesh_i.cxx

Go to the documentation of this file.
00001 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : SMESH_Mesh_i.cxx
00023 //  Author : Paul RASCLE, EDF
00024 //  Module : SMESH
00025 
00026 #include "SMESH_Mesh_i.hxx"
00027 
00028 #include "SMESH_Filter_i.hxx"
00029 #include "SMESH_Gen_i.hxx"
00030 #include "SMESH_Group_i.hxx"
00031 #include "SMESH_MEDMesh_i.hxx"
00032 #include "SMESH_MeshEditor_i.hxx"
00033 #include "SMESH_PythonDump.hxx"
00034 #include "SMESH_subMesh_i.hxx"
00035 
00036 #include "DriverMED_R_SMESHDS_Mesh.h"
00037 #include "DriverMED_W_SMESHDS_Mesh.h"
00038 #include "SMDS_VolumeTool.hxx"
00039 #include "SMDS_ElemIterator.hxx"
00040 #include "SMESHDS_Command.hxx"
00041 #include "SMESHDS_CommandType.hxx"
00042 #include "SMESHDS_GroupOnGeom.hxx"
00043 #include "SMESH_Group.hxx"
00044 #include "SMESH_MeshEditor.hxx"
00045 #include "SMESH_MesherHelper.hxx"
00046 #include "SMDS_EdgePosition.hxx"
00047 #include "SMDS_FacePosition.hxx"
00048 
00049 #include "OpUtil.hxx"
00050 #include "SALOME_NamingService.hxx"
00051 #include "Utils_CorbaException.hxx"
00052 #include "Utils_ExceptHandlers.hxx"
00053 #include "Utils_SINGLETON.hxx"
00054 #include "utilities.h"
00055 #include "GEOMImpl_Types.hxx"
00056 
00057 // OCCT Includes
00058 #include <BRep_Builder.hxx>
00059 #include <OSD_Directory.hxx>
00060 #include <OSD_File.hxx>
00061 #include <OSD_Path.hxx>
00062 #include <OSD_Protection.hxx>
00063 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
00064 #include <TColStd_MapOfInteger.hxx>
00065 #include <TColStd_SequenceOfInteger.hxx>
00066 #include <TCollection_AsciiString.hxx>
00067 #include <TopExp.hxx>
00068 #include <TopExp_Explorer.hxx>
00069 #include <TopoDS_Compound.hxx>
00070 #include <TopTools_MapOfShape.hxx>
00071 #include <TopTools_MapIteratorOfMapOfShape.hxx>
00072 
00073 // STL Includes
00074 #include <algorithm>
00075 #include <string>
00076 #include <iostream>
00077 #include <sstream>
00078 #include <sys/stat.h>
00079 
00080 #ifdef _DEBUG_
00081 static int MYDEBUG = 0;
00082 #else
00083 static int MYDEBUG = 0;
00084 #endif
00085 
00086 using namespace std;
00087 using SMESH::TPythonDump;
00088 
00089 int SMESH_Mesh_i::myIdGenerator = 0;
00090 
00091 //To disable automatic genericobj management, the following line should be commented.
00092 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
00093 #define WITHGENERICOBJ
00094 
00095 //=============================================================================
00099 //=============================================================================
00100 
00101 SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
00102                             SMESH_Gen_i*            gen_i,
00103                             CORBA::Long studyId )
00104 : SALOME::GenericObj_i( thePOA )
00105 {
00106   MESSAGE("SMESH_Mesh_i");
00107   _impl = NULL;
00108   _gen_i = gen_i;
00109   _id = myIdGenerator++;
00110   _studyId = studyId;
00111 }
00112 
00113 //=============================================================================
00117 //=============================================================================
00118 
00119 SMESH_Mesh_i::~SMESH_Mesh_i()
00120 {
00121   MESSAGE("~SMESH_Mesh_i");
00122 
00123 #ifdef WITHGENERICOBJ
00124   // destroy groups
00125   map<int, SMESH::SMESH_GroupBase_ptr>::iterator itGr;
00126   for (itGr = _mapGroups.begin(); itGr != _mapGroups.end(); itGr++) {
00127     if ( CORBA::is_nil( itGr->second ))
00128       continue;
00129     SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>(SMESH_Gen_i::GetServant(itGr->second).in());
00130     if (aGroup) {
00131       // this method is called from destructor of group (PAL6331)
00132       //_impl->RemoveGroup( aGroup->GetLocalID() );
00133       aGroup->myMeshServant = 0;
00134       aGroup->UnRegister();
00135     }
00136   }
00137   _mapGroups.clear();
00138 
00139   // destroy submeshes
00140   map<int, SMESH::SMESH_subMesh_ptr>::iterator itSM;
00141   for ( itSM = _mapSubMeshIor.begin(); itSM != _mapSubMeshIor.end(); itSM++ ) {
00142     if ( CORBA::is_nil( itSM->second ))
00143       continue;
00144     SMESH_subMesh_i* aSubMesh = dynamic_cast<SMESH_subMesh_i*>(SMESH_Gen_i::GetServant(itSM->second).in());
00145     if (aSubMesh) {
00146       aSubMesh->UnRegister();
00147     }
00148   }
00149   _mapSubMeshIor.clear();
00150 
00151   // destroy hypotheses
00152   map<int, SMESH::SMESH_Hypothesis_ptr>::iterator itH;
00153   for ( itH = _mapHypo.begin(); itH != _mapHypo.end(); itH++ ) {
00154     if ( CORBA::is_nil( itH->second ))
00155       continue;
00156     SMESH_Hypothesis_i* aHypo = dynamic_cast<SMESH_Hypothesis_i*>(SMESH_Gen_i::GetServant(itH->second).in());
00157     if (aHypo) {
00158       aHypo->UnRegister();
00159     }
00160   }
00161   _mapHypo.clear();
00162 #endif
00163 
00164   delete _impl;
00165 }
00166 
00167 //=============================================================================
00175 //=============================================================================
00176 
00177 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
00178     throw (SALOME::SALOME_Exception)
00179 {
00180   Unexpect aCatch(SALOME_SalomeException);
00181   try {
00182     _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
00183   }
00184   catch(SALOME_Exception & S_ex) {
00185     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00186   }
00187   // to track changes of GEOM groups
00188   addGeomGroupData( theShapeObject, _this() );
00189 }
00190 
00191 //================================================================================
00195 //================================================================================
00196 
00197 CORBA::Boolean SMESH_Mesh_i::HasShapeToMesh()
00198   throw (SALOME::SALOME_Exception)
00199 {
00200   Unexpect aCatch(SALOME_SalomeException);
00201   bool res = false;
00202   try {
00203     res = _impl->HasShapeToMesh();
00204   }
00205   catch(SALOME_Exception & S_ex) {
00206     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00207   }
00208   return res;
00209 }
00210 
00211 //=======================================================================
00212 //function : GetShapeToMesh
00213 //purpose  :
00214 //=======================================================================
00215 
00216 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
00217   throw (SALOME::SALOME_Exception)
00218 {
00219   Unexpect aCatch(SALOME_SalomeException);
00220   GEOM::GEOM_Object_var aShapeObj;
00221   try {
00222     TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
00223     if ( !S.IsNull() )
00224       aShapeObj = _gen_i->ShapeToGeomObject( S );
00225   }
00226   catch(SALOME_Exception & S_ex) {
00227     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00228   }
00229   return aShapeObj._retn();
00230 }
00231 
00232 //================================================================================
00236 //================================================================================
00237 
00238 void SMESH_Mesh_i::Clear() throw (SALOME::SALOME_Exception)
00239 {
00240   Unexpect aCatch(SALOME_SalomeException);
00241   try {
00242     _impl->Clear();
00243     CheckGeomGroupModif(); // issue 20145
00244   }
00245   catch(SALOME_Exception & S_ex) {
00246     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00247   }
00248   TPythonDump() <<  _this() << ".Clear()";
00249 }
00250 
00251 //================================================================================
00255 //================================================================================
00256 
00257 void SMESH_Mesh_i::ClearSubMesh(CORBA::Long ShapeID)
00258   throw (SALOME::SALOME_Exception)
00259 {
00260   Unexpect aCatch(SALOME_SalomeException);
00261   try {
00262     _impl->ClearSubMesh( ShapeID );
00263   }
00264   catch(SALOME_Exception & S_ex) {
00265     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00266   }
00267 }
00268 
00269 //=============================================================================
00273 //=============================================================================
00274 
00275 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
00276 {
00277   SMESH::DriverMED_ReadStatus res;
00278   switch (theStatus)
00279   {
00280   case DriverMED_R_SMESHDS_Mesh::DRS_OK:
00281     res = SMESH::DRS_OK; break;
00282   case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
00283     res = SMESH::DRS_EMPTY; break;
00284   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
00285     res = SMESH::DRS_WARN_RENUMBER; break;
00286   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
00287     res = SMESH::DRS_WARN_SKIP_ELEM; break;
00288   case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
00289   default:
00290     res = SMESH::DRS_FAIL; break;
00291   }
00292   return res;
00293 }
00294 
00295 //=============================================================================
00301 //=============================================================================
00302 
00303 SMESH::DriverMED_ReadStatus
00304 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
00305   throw ( SALOME::SALOME_Exception )
00306 {
00307   Unexpect aCatch(SALOME_SalomeException);
00308   int status;
00309   try {
00310     status = _impl->MEDToMesh( theFileName, theMeshName );
00311   }
00312   catch( SALOME_Exception& S_ex ) {
00313     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00314   }
00315   catch ( ... ) {
00316     THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
00317   }
00318 
00319   CreateGroupServants();
00320 
00321   int major, minor, release;
00322   if( !MED::getMEDVersion( theFileName, major, minor, release ) )
00323     major = minor = release = -1;
00324   myFileInfo           = new SALOME_MED::MedFileInfo();
00325   myFileInfo->fileName = theFileName;
00326   myFileInfo->fileSize = 0;
00327 #ifdef WIN32
00328   struct _stati64 d;
00329   if ( ::_stati64( theFileName, &d ) != -1 )
00330 #else
00331   struct stat64 d;
00332   if ( ::stat64( theFileName, &d ) != -1 )
00333 #endif
00334     myFileInfo->fileSize = d.st_size;
00335   myFileInfo->major    = major;
00336   myFileInfo->minor    = minor;
00337   myFileInfo->release  = release;
00338 
00339   return ConvertDriverMEDReadStatus(status);
00340 }
00341 
00342 //================================================================================
00346 //================================================================================
00347 
00348 char* SMESH_Mesh_i::GetVersionString(SMESH::MED_VERSION version, CORBA::Short nbDigits)
00349 {
00350   string ver = DriverMED_W_SMESHDS_Mesh::GetVersionString(MED::EVersion(version),
00351                                                                nbDigits);
00352   return CORBA::string_dup( ver.c_str() );
00353 }
00354 
00355 //=============================================================================
00361 //=============================================================================
00362 
00363 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
00364   throw ( SALOME::SALOME_Exception )
00365 {
00366   // Read mesh with name = <theMeshName> into SMESH_Mesh
00367   _impl->UNVToMesh( theFileName );
00368 
00369   CreateGroupServants();
00370 
00371   return 1;
00372 }
00373 
00374 //=============================================================================
00380 //=============================================================================
00381 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
00382   throw ( SALOME::SALOME_Exception )
00383 {
00384   // Read mesh with name = <theMeshName> into SMESH_Mesh
00385   _impl->STLToMesh( theFileName );
00386 
00387   return 1;
00388 }
00389 
00390 //=============================================================================
00396 //=============================================================================
00397 
00398 // int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
00399 // {
00400 //   // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
00401 //   int status = _impl->MEDToMesh( theFileName, theMeshName );
00402 //   CreateGroupServants();
00403 
00404 //   return status;
00405 // }
00406 
00407 //=============================================================================
00411 //=============================================================================
00412 
00413 #define RETURNCASE(hyp_stat) case SMESH_Hypothesis::hyp_stat: return SMESH::hyp_stat;
00414 
00415 SMESH::Hypothesis_Status SMESH_Mesh_i::ConvertHypothesisStatus
00416                          (SMESH_Hypothesis::Hypothesis_Status theStatus)
00417 {
00418   switch (theStatus) {
00419   RETURNCASE( HYP_OK            );
00420   RETURNCASE( HYP_MISSING       );
00421   RETURNCASE( HYP_CONCURENT     );
00422   RETURNCASE( HYP_BAD_PARAMETER );
00423   RETURNCASE( HYP_HIDDEN_ALGO   );
00424   RETURNCASE( HYP_HIDING_ALGO   );
00425   RETURNCASE( HYP_UNKNOWN_FATAL );
00426   RETURNCASE( HYP_INCOMPATIBLE  );
00427   RETURNCASE( HYP_NOTCONFORM    );
00428   RETURNCASE( HYP_ALREADY_EXIST );
00429   RETURNCASE( HYP_BAD_DIM       );
00430   RETURNCASE( HYP_BAD_SUBSHAPE  );
00431   RETURNCASE( HYP_BAD_GEOMETRY  );
00432   RETURNCASE( HYP_NEED_SHAPE    );
00433   default:;
00434   }
00435   return SMESH::HYP_UNKNOWN_FATAL;
00436 }
00437 
00438 //=============================================================================
00446 //=============================================================================
00447 
00448 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
00449                                                      SMESH::SMESH_Hypothesis_ptr anHyp)
00450   throw(SALOME::SALOME_Exception)
00451 {
00452   Unexpect aCatch(SALOME_SalomeException);
00453   SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
00454 
00455   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
00456     _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
00457                                  aSubShapeObject, anHyp );
00458 
00459   if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
00460 
00461   // Update Python script
00462   if(_impl->HasShapeToMesh()) {
00463     TPythonDump() << "status = " << _this() << ".AddHypothesis( "
00464                   << aSubShapeObject << ", " << anHyp << " )";
00465   }
00466   else {
00467     TPythonDump() << "status = " << _this() << ".AddHypothesis( "<< anHyp << " )";
00468   }
00469   
00470   return ConvertHypothesisStatus(status);
00471 }
00472 
00473 //=============================================================================
00477 //=============================================================================
00478 
00479 SMESH_Hypothesis::Hypothesis_Status
00480   SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr       aSubShapeObject,
00481                               SMESH::SMESH_Hypothesis_ptr anHyp)
00482 {
00483   if(MYDEBUG) MESSAGE("addHypothesis");
00484 
00485   if (CORBA::is_nil(aSubShapeObject) && HasShapeToMesh())
00486     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
00487                                  SALOME::BAD_PARAM);
00488 
00489   SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
00490   if (CORBA::is_nil(myHyp))
00491     THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
00492                                  SALOME::BAD_PARAM);
00493 
00494   SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
00495   try
00496   {
00497     TopoDS_Shape myLocSubShape;
00498     //use PseudoShape in case if mesh has no shape
00499     if(HasShapeToMesh())
00500       myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
00501     else              
00502       myLocSubShape = _impl->GetShapeToMesh();
00503     
00504     int hypId = myHyp->GetId();
00505     status = _impl->AddHypothesis(myLocSubShape, hypId);
00506     if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
00507       _mapHypo[hypId] = SMESH::SMESH_Hypothesis::_duplicate( myHyp );
00508 #ifdef WITHGENERICOBJ
00509       _mapHypo[hypId]->Register();
00510 #endif
00511       // assure there is a corresponding submesh
00512       if ( !_impl->IsMainShape( myLocSubShape )) {
00513         int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
00514         if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
00515           createSubMesh( aSubShapeObject );
00516       }
00517     }
00518   }
00519   catch(SALOME_Exception & S_ex)
00520   {
00521     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00522   }
00523   return status;
00524 }
00525 
00526 //=============================================================================
00530 //=============================================================================
00531 
00532 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
00533                                                         SMESH::SMESH_Hypothesis_ptr anHyp)
00534      throw(SALOME::SALOME_Exception)
00535 {
00536   Unexpect aCatch(SALOME_SalomeException);
00537   SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
00538 
00539   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
00540     _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
00541                                       aSubShapeObject, anHyp );
00542 
00543   // Update Python script
00544     // Update Python script
00545   if(_impl->HasShapeToMesh()) {
00546   TPythonDump() << "status = " << _this() << ".RemoveHypothesis( "
00547                 << aSubShapeObject << ", " << anHyp << " )";
00548   }
00549   else {
00550     TPythonDump() << "status = " << _this() << ".RemoveHypothesis( "
00551                   << anHyp << " )";
00552   }
00553 
00554   return ConvertHypothesisStatus(status);
00555 }
00556 
00557 //=============================================================================
00561 //=============================================================================
00562 
00563 SMESH_Hypothesis::Hypothesis_Status
00564 SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr       aSubShapeObject,
00565                                SMESH::SMESH_Hypothesis_ptr anHyp)
00566 {
00567   if(MYDEBUG) MESSAGE("removeHypothesis()");
00568   // **** proposer liste de subShape (selection multiple)
00569 
00570   if (CORBA::is_nil(aSubShapeObject) && HasShapeToMesh())
00571     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference", SALOME::BAD_PARAM);
00572 
00573   SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
00574   if (CORBA::is_nil(myHyp))
00575     THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference", SALOME::BAD_PARAM);
00576 
00577   SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
00578   try
00579   {
00580     TopoDS_Shape myLocSubShape;
00581     //use PseudoShape in case if mesh has no shape
00582     if(HasShapeToMesh())
00583       myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
00584     else
00585       myLocSubShape = _impl->GetShapeToMesh();
00586 
00587     int hypId = myHyp->GetId();
00588     status = _impl->RemoveHypothesis(myLocSubShape, hypId);
00589 //     if ( !SMESH_Hypothesis::IsStatusFatal(status) ) EAP: hyp can be used on many subshapes
00590 //       _mapHypo.erase( hypId );
00591   }
00592   catch(SALOME_Exception & S_ex)
00593   {
00594     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00595   }
00596   return status;
00597 }
00598 
00599 //=============================================================================
00603 //=============================================================================
00604 
00605 SMESH::ListOfHypothesis *
00606         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
00607 throw(SALOME::SALOME_Exception)
00608 {
00609   Unexpect aCatch(SALOME_SalomeException);
00610   if (MYDEBUG) MESSAGE("GetHypothesisList");
00611   if (_impl->HasShapeToMesh() && CORBA::is_nil(aSubShapeObject))
00612     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference", SALOME::BAD_PARAM);
00613 
00614   SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
00615 
00616   try {
00617     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
00618     if ( myLocSubShape.IsNull() && !_impl->HasShapeToMesh() )
00619       myLocSubShape = _impl->GetShapeToMesh();
00620     const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
00621     int i = 0, n = aLocalList.size();
00622     aList->length( n );
00623 
00624     for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
00625       SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
00626       if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
00627         aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
00628     }
00629 
00630     aList->length( i );
00631   }
00632   catch(SALOME_Exception & S_ex) {
00633     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00634   }
00635 
00636   return aList._retn();
00637 }
00638 
00639 //=============================================================================
00643 //=============================================================================
00644 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
00645                                                   const char*           theName )
00646      throw(SALOME::SALOME_Exception)
00647 {
00648   Unexpect aCatch(SALOME_SalomeException);
00649   MESSAGE("SMESH_Mesh_i::GetSubMesh");
00650   if (CORBA::is_nil(aSubShapeObject))
00651     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
00652                                  SALOME::BAD_PARAM);
00653 
00654   SMESH::SMESH_subMesh_var subMesh;
00655   SMESH::SMESH_Mesh_var    aMesh = SMESH::SMESH_Mesh::_narrow(_this());
00656   try {
00657     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
00658 
00659     //Get or Create the SMESH_subMesh object implementation
00660 
00661     int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
00662 
00663     if ( !subMeshId && ! _impl->GetMeshDS()->IsGroupOfSubShapes( myLocSubShape ))
00664     {
00665       TopoDS_Iterator it( myLocSubShape );
00666       if ( it.More() )
00667         THROW_SALOME_CORBA_EXCEPTION("not sub-shape of the main shape", SALOME::BAD_PARAM);
00668     }
00669     subMesh = getSubMesh( subMeshId );
00670 
00671     // create a new subMesh object servant if there is none for the shape
00672     if ( subMesh->_is_nil() )
00673       subMesh = createSubMesh( aSubShapeObject );
00674     if ( _gen_i->CanPublishInStudy( subMesh )) {
00675       SALOMEDS::SObject_var aSO =
00676         _gen_i->PublishSubMesh(_gen_i->GetCurrentStudy(), aMesh,
00677                                subMesh, aSubShapeObject, theName );
00678       if ( !aSO->_is_nil()) {
00679         // Update Python script
00680         TPythonDump() << aSO << " = " << _this() << ".GetSubMesh( "
00681                       << aSubShapeObject << ", '" << theName << "' )";
00682       }
00683     }
00684   }
00685   catch(SALOME_Exception & S_ex) {
00686     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00687   }
00688   return subMesh._retn();
00689 }
00690 
00691 //=============================================================================
00695 //=============================================================================
00696 
00697 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
00698      throw (SALOME::SALOME_Exception)
00699 {
00700   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
00701   if ( theSubMesh->_is_nil() )
00702     return;
00703 
00704   GEOM::GEOM_Object_var aSubShapeObject;
00705   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
00706   if ( !aStudy->_is_nil() )  {
00707     // Remove submesh's SObject
00708     SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
00709     if ( !anSO->_is_nil() ) {
00710       long aTag = SMESH_Gen_i::GetRefOnShapeTag();
00711       SALOMEDS::SObject_var anObj, aRef;
00712       if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
00713         aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
00714 
00715 //       if ( aSubShapeObject->_is_nil() ) // not published shape (IPAL13617)
00716 //         aSubShapeObject = theSubMesh->GetSubShape();
00717 
00718       aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
00719 
00720       // Update Python script
00721       TPythonDump() << _this() << ".RemoveSubMesh( " << anSO << " )";
00722     }
00723   }
00724 
00725   removeSubMesh( theSubMesh, aSubShapeObject.in() );
00726 }
00727 
00728 //=============================================================================
00732 //=============================================================================
00733 #define CASE2STRING(enum) case SMESH::enum: return "SMESH."#enum;
00734 inline TCollection_AsciiString ElementTypeString (SMESH::ElementType theElemType)
00735 {
00736   switch (theElemType) {
00737     CASE2STRING( ALL );
00738     CASE2STRING( NODE );
00739     CASE2STRING( EDGE );
00740     CASE2STRING( FACE );
00741     CASE2STRING( VOLUME );
00742     CASE2STRING( ELEM0D );
00743   default:;
00744   }
00745   return "";
00746 }
00747 
00748 //=============================================================================
00752 //=============================================================================
00753 
00754 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
00755                                                  const char*         theName )
00756      throw(SALOME::SALOME_Exception)
00757 {
00758   Unexpect aCatch(SALOME_SalomeException);
00759   SMESH::SMESH_Group_var aNewGroup =
00760     SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
00761 
00762   if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
00763     SALOMEDS::SObject_var aSO =
00764       _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
00765                            aNewGroup, GEOM::GEOM_Object::_nil(), theName);
00766     if ( !aSO->_is_nil()) {
00767       // Update Python script
00768       TPythonDump() << aSO << " = " << _this() << ".CreateGroup( "
00769                     << ElementTypeString(theElemType) << ", '" << theName << "' )";
00770     }
00771   }
00772   return aNewGroup._retn();
00773 }
00774 
00775 
00776 //=============================================================================
00780 //=============================================================================
00781 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM (SMESH::ElementType    theElemType,
00782                                                                 const char*           theName,
00783                                                                 GEOM::GEOM_Object_ptr theGeomObj)
00784      throw(SALOME::SALOME_Exception)
00785 {
00786   Unexpect aCatch(SALOME_SalomeException);
00787   SMESH::SMESH_GroupOnGeom_var aNewGroup;
00788 
00789   TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
00790   if ( !aShape.IsNull() )
00791   {
00792     aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
00793       ( createGroup( theElemType, theName, aShape ));
00794 
00795     if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
00796       SALOMEDS::SObject_var aSO =
00797         _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
00798                              aNewGroup, theGeomObj, theName);
00799       if ( !aSO->_is_nil()) {
00800         // Update Python script
00801         TPythonDump() << aSO << " = " << _this() << ".CreateGroupFromGEOM("
00802                       << ElementTypeString(theElemType) << ", '" << theName << "', "
00803                       << theGeomObj << " )";
00804       }
00805     }
00806   }
00807 
00808   return aNewGroup._retn();
00809 }
00810 
00811 //=============================================================================
00815 //=============================================================================
00816 
00817 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
00818      throw (SALOME::SALOME_Exception)
00819 {
00820   if ( theGroup->_is_nil() )
00821     return;
00822 
00823   SMESH_GroupBase_i* aGroup =
00824     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
00825   if ( !aGroup )
00826     return;
00827 
00828   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
00829   if ( !aStudy->_is_nil() )  {
00830     SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
00831 
00832     if ( !aGroupSO->_is_nil() ) {
00833       // Update Python script
00834       TPythonDump() << _this() << ".RemoveGroup( " << aGroupSO << " )";
00835 
00836       // Remove group's SObject
00837       aStudy->NewBuilder()->RemoveObjectWithChildren( aGroupSO );
00838     }
00839   }
00840 
00841   // Remove the group from SMESH data structures
00842   removeGroup( aGroup->GetLocalID() );
00843 }
00844 
00845 //=============================================================================
00849 //=============================================================================
00850 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
00851   throw (SALOME::SALOME_Exception)
00852 {
00853   if ( theGroup->_is_nil() )
00854     return;
00855 
00856   SMESH_GroupBase_i* aGroup =
00857     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
00858   if ( !aGroup )
00859     return;
00860 
00861   SMESH::long_array_var anIds = aGroup->GetListOfID();
00862   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
00863 
00864   TPythonDump pyDump; // Supress dump from RemoveNodes/Elements() and RemoveGroup()
00865 
00866   // Remove contents
00867   if ( aGroup->GetType() == SMESH::NODE )
00868     aMeshEditor->RemoveNodes( anIds );
00869   else
00870     aMeshEditor->RemoveElements( anIds );
00871 
00872   // Remove group
00873   RemoveGroup( theGroup );
00874 
00875   // Update Python script
00876   pyDump << _this() << ".RemoveGroupWithContents( " << theGroup << " )";
00877 }
00878 
00879 //================================================================================
00884 //================================================================================
00885 
00886 SMESH::ListOfGroups * SMESH_Mesh_i::GetGroups() throw(SALOME::SALOME_Exception)
00887 {
00888   Unexpect aCatch(SALOME_SalomeException);
00889   if (MYDEBUG) MESSAGE("GetGroups");
00890 
00891   SMESH::ListOfGroups_var aList = new SMESH::ListOfGroups();
00892 
00893   // Python Dump
00894   TPythonDump aPythonDump;
00895   if ( !_mapGroups.empty() ) // (IMP13463) avoid "SyntaxError: can't assign to []"
00896     aPythonDump << "[ ";
00897 
00898   try {
00899     aList->length( _mapGroups.size() );
00900     int i = 0;
00901     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
00902     for ( ; it != _mapGroups.end(); it++ ) {
00903       if ( CORBA::is_nil( it->second )) continue;
00904       aList[i++] = SMESH::SMESH_GroupBase::_duplicate( it->second );
00905       // Python Dump
00906       if (i > 1) aPythonDump << ", ";
00907       aPythonDump << it->second;
00908     }
00909     aList->length( i );
00910   }
00911   catch(SALOME_Exception & S_ex) {
00912     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
00913   }
00914 
00915   // Update Python script
00916   if ( !_mapGroups.empty() ) // (IMP13463) avoid "SyntaxError: can't assign to []"
00917     aPythonDump << " ] = " << _this() << ".GetGroups()";
00918 
00919   return aList._retn();
00920 }
00921 
00922 //=============================================================================
00926 //=============================================================================
00927 
00928 CORBA::Long SMESH_Mesh_i::NbGroups() throw (SALOME::SALOME_Exception)
00929 {
00930   Unexpect aCatch(SALOME_SalomeException);
00931   return _mapGroups.size();
00932 }
00933 
00934 //=============================================================================
00939 //=============================================================================
00940 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
00941                                                   SMESH::SMESH_GroupBase_ptr theGroup2,
00942                                                   const char* theName )
00943   throw (SALOME::SALOME_Exception)
00944 {
00945   try
00946   {
00947     if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
00948          theGroup1->GetType() != theGroup2->GetType() )
00949       return SMESH::SMESH_Group::_nil();
00950 
00951     // Create Union
00952     SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
00953     if ( aResGrp->_is_nil() )
00954       return SMESH::SMESH_Group::_nil();
00955 
00956     SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
00957     SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
00958 
00959     TColStd_MapOfInteger aResMap;
00960 
00961     for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
00962       aResMap.Add( anIds1[ i1 ] );
00963 
00964     for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
00965       aResMap.Add( anIds2[ i2 ] );
00966 
00967     SMESH::long_array_var aResIds = new SMESH::long_array;
00968     aResIds->length( aResMap.Extent() );
00969 
00970     int resI = 0;
00971     TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
00972     for( ; anIter.More(); anIter.Next() )
00973       aResIds[ resI++ ] = anIter.Key();
00974 
00975     aResGrp->Add( aResIds );
00976 
00977     // Clear python lines, created by CreateGroup() and Add()
00978     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
00979     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
00980     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
00981 
00982     // Update Python script
00983     TPythonDump() << aResGrp << " = " << _this() << ".UnionGroups( "
00984                   << theGroup1 << ", " << theGroup2 << ", '"
00985                   << theName << "' )";
00986 
00987     return aResGrp._retn();
00988   }
00989   catch( ... )
00990   {
00991     return SMESH::SMESH_Group::_nil();
00992   }
00993 }
00994 
00995 //=============================================================================
01003 //=============================================================================
01004 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionListOfGroups(const SMESH::ListOfGroups& theGroups,
01005                                                        const char*                theName )
01006 throw (SALOME::SALOME_Exception)
01007 {
01008   if ( !theName )
01009     return SMESH::SMESH_Group::_nil();
01010 
01011   try
01012   {
01013     vector< int > anIds;
01014     SMESH::ElementType aType = SMESH::ALL;
01015     for ( int g = 0, n = theGroups.length(); g < n; g++ )
01016     {
01017       SMESH::SMESH_GroupBase_var aGrp = theGroups[ g ];
01018       if ( CORBA::is_nil( aGrp ) )
01019         continue;
01020 
01021       // check type
01022       SMESH::ElementType aCurrType = aGrp->GetType();
01023       if ( aType == SMESH::ALL )
01024         aType = aCurrType;
01025       else 
01026       {
01027         if ( aType != aCurrType )
01028           return SMESH::SMESH_Group::_nil();
01029       }
01030 
01031       // unite ids
01032       SMESH::long_array_var aCurrIds = aGrp->GetListOfID();
01033       for ( int i = 0, n = aCurrIds->length(); i < n; i++ )
01034       {
01035         int aCurrId = aCurrIds[ i ];
01036         anIds.push_back( aCurrId );
01037       }
01038     }
01039 
01040     // Create group
01041     SMESH::SMESH_Group_var aResGrp = CreateGroup( aType, theName );
01042     if ( aResGrp->_is_nil() )
01043       return SMESH::SMESH_Group::_nil();
01044     
01045     // Create array of identifiers
01046     SMESH::long_array_var aResIds = new SMESH::long_array;
01047     aResIds->length( anIds.size() );
01048     
01049     //NCollection_Map< int >::Iterator anIter( anIds );
01050     for ( int i = 0; i<anIds.size(); i++ )
01051     {
01052       aResIds[ i ] = anIds[i];
01053     }
01054     aResGrp->Add( aResIds );
01055 
01056     // Clear python lines, created by CreateGroup() and Add()
01057     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
01058     _gen_i->RemoveLastFromPythonScript( aStudy->StudyId() );
01059     _gen_i->RemoveLastFromPythonScript( aStudy->StudyId() );
01060 
01061     // Update Python script
01062     
01063     TPythonDump() << aResGrp << " = " << _this() << ".UnionListOfGroups( "
01064                   << &theGroups << ", '" << theName << "' )";
01065 
01066     return aResGrp._retn();
01067   }
01068   catch( ... )
01069   {
01070     return SMESH::SMESH_Group::_nil();
01071   }
01072 }
01073 
01074 //=============================================================================
01079 //=============================================================================
01080 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
01081                                                       SMESH::SMESH_GroupBase_ptr theGroup2,
01082                                                       const char* theName )
01083   throw (SALOME::SALOME_Exception)
01084 {
01085   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
01086        theGroup1->GetType() != theGroup2->GetType() )
01087     return SMESH::SMESH_Group::_nil();
01088 
01089   // Create Intersection
01090   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
01091   if ( aResGrp->_is_nil() )
01092     return aResGrp;
01093 
01094   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
01095   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
01096 
01097   TColStd_MapOfInteger aMap1;
01098 
01099   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
01100     aMap1.Add( anIds1[ i1 ] );
01101 
01102   TColStd_SequenceOfInteger aSeq;
01103 
01104   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
01105     if ( aMap1.Contains( anIds2[ i2 ] ) )
01106       aSeq.Append( anIds2[ i2 ] );
01107 
01108   SMESH::long_array_var aResIds = new SMESH::long_array;
01109   aResIds->length( aSeq.Length() );
01110 
01111   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
01112     aResIds[ resI ] = aSeq( resI + 1 );
01113 
01114   aResGrp->Add( aResIds );
01115 
01116   // Clear python lines, created by CreateGroup() and Add()
01117   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
01118   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
01119   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
01120 
01121   // Update Python script
01122   TPythonDump() << aResGrp << " = " << _this() << ".IntersectGroups( "
01123                 << theGroup1 << ", " << theGroup2 << ", '" << theName << "')";
01124 
01125   return aResGrp._retn();
01126 }
01127 
01128 //=============================================================================
01136 //=============================================================================
01137 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectListOfGroups( 
01138   const SMESH::ListOfGroups& theGroups, const char* theName )
01139 throw (SALOME::SALOME_Exception)
01140 {
01141   if ( !theName )
01142     return SMESH::SMESH_Group::_nil();
01143 
01144   try
01145   {
01146     NCollection_DataMap< int, int > anIdToCount;
01147     SMESH::ElementType aType = SMESH::ALL;
01148     for ( int g = 0, n = theGroups.length(); g < n; g++ )
01149     {
01150       SMESH::SMESH_GroupBase_var aGrp = theGroups[ g ];
01151       if ( CORBA::is_nil( aGrp ) )
01152         continue;
01153 
01154       // check type
01155       SMESH::ElementType aCurrType = aGrp->GetType();
01156       if ( aType == SMESH::ALL )
01157         aType = aCurrType;
01158       else 
01159       {
01160         if ( aType != aCurrType )
01161           return SMESH::SMESH_Group::_nil();
01162       }
01163 
01164       // calculates number of occurance ids in groups
01165       SMESH::long_array_var aCurrIds = aGrp->GetListOfID();
01166       for ( int i = 0, n = aCurrIds->length(); i < n; i++ )
01167       {
01168         int aCurrId = aCurrIds[ i ];
01169         if ( !anIdToCount.IsBound( aCurrId ) )
01170           anIdToCount.Bind( aCurrId, 1 );
01171         else 
01172           anIdToCount( aCurrId ) = anIdToCount( aCurrId ) + 1;
01173       }
01174     }
01175     
01176     // create map of ids
01177     int nbGrp = theGroups.length();
01178     vector< int > anIds;
01179     NCollection_DataMap< int, int >::Iterator anIter( anIdToCount );
01180     for ( ; anIter.More(); anIter.Next() )
01181     {
01182       int aCurrId = anIter.Key();
01183       int aCurrNb = anIter.Value();
01184       if ( aCurrNb == nbGrp )
01185         anIds.push_back( aCurrId );
01186     }
01187 
01188     // Create group
01189     SMESH::SMESH_Group_var aResGrp = CreateGroup( aType, theName );
01190     if ( aResGrp->_is_nil() )
01191       return SMESH::SMESH_Group::_nil();
01192     
01193     // Create array of identifiers
01194     SMESH::long_array_var aResIds = new SMESH::long_array;
01195     aResIds->length( anIds.size() );
01196     
01197     //NCollection_Map< int >::Iterator aListIter( anIds );
01198     for ( int i = 0; i<anIds.size(); i++ )
01199     {
01200       aResIds[ i ] = anIds[i];
01201     }
01202     aResGrp->Add( aResIds );
01203 
01204     // Clear python lines, created by CreateGroup() and Add()
01205     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
01206     _gen_i->RemoveLastFromPythonScript( aStudy->StudyId() );
01207     _gen_i->RemoveLastFromPythonScript( aStudy->StudyId() );
01208 
01209     // Update Python script
01210     
01211     TPythonDump() << aResGrp << " = " << _this() << ".IntersectListOfGroups( "
01212                   << &theGroups << ", '" << theName << "' )";
01213 
01214     return aResGrp._retn();
01215   }
01216   catch( ... )
01217   {
01218     return SMESH::SMESH_Group::_nil();
01219   }
01220 }
01221 
01222 //=============================================================================
01227 //=============================================================================
01228 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
01229                                                 SMESH::SMESH_GroupBase_ptr theGroup2,
01230                                                 const char* theName )
01231   throw (SALOME::SALOME_Exception)
01232 {
01233   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
01234        theGroup1->GetType() != theGroup2->GetType() )
01235     return SMESH::SMESH_Group::_nil();
01236 
01237   // Perform Cutting
01238   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
01239   if ( aResGrp->_is_nil() )
01240     return aResGrp;
01241 
01242   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
01243   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
01244 
01245   TColStd_MapOfInteger aMap2;
01246 
01247   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
01248     aMap2.Add( anIds2[ i2 ] );
01249 
01250   TColStd_SequenceOfInteger aSeq;
01251   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
01252     if ( !aMap2.Contains( anIds1[ i1 ] ) )
01253       aSeq.Append( anIds1[ i1 ] );
01254 
01255   SMESH::long_array_var aResIds = new SMESH::long_array;
01256   aResIds->length( aSeq.Length() );
01257 
01258   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
01259     aResIds[ resI ] = aSeq( resI + 1 );
01260 
01261   aResGrp->Add( aResIds );
01262 
01263   // Clear python lines, created by CreateGroup() and Add()
01264   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
01265   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
01266   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
01267 
01268   // Update Python script
01269   TPythonDump() << aResGrp << " = " << _this() << ".CutGroups( "
01270                 << theGroup1 << ", " << theGroup2 << ", '"
01271                 << theName << "' )";
01272 
01273   return aResGrp._retn();
01274 }
01275 
01276 //=============================================================================
01285 //=============================================================================
01286 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutListOfGroups( 
01287   const SMESH::ListOfGroups& theMainGroups, 
01288   const SMESH::ListOfGroups& theToolGroups, 
01289   const char* theName )
01290   throw (SALOME::SALOME_Exception)
01291 {
01292   if ( !theName )
01293     return SMESH::SMESH_Group::_nil();
01294 
01295   try
01296   {
01297     set< int > aToolIds;
01298     SMESH::ElementType aType = SMESH::ALL;
01299     int g, n;
01300     // iterate through tool groups
01301     for ( g = 0, n = theToolGroups.length(); g < n; g++ )
01302     {
01303       SMESH::SMESH_GroupBase_var aGrp = theToolGroups[ g ];
01304       if ( CORBA::is_nil( aGrp ) )
01305         continue;
01306 
01307       // check type
01308       SMESH::ElementType aCurrType = aGrp->GetType();
01309       if ( aType == SMESH::ALL )
01310         aType = aCurrType;
01311       else 
01312       {
01313         if ( aType != aCurrType )
01314           return SMESH::SMESH_Group::_nil();
01315       }
01316 
01317       // unite tool ids
01318       SMESH::long_array_var aCurrIds = aGrp->GetListOfID();
01319       for ( int i = 0, n = aCurrIds->length(); i < n; i++ )
01320       {
01321         int aCurrId = aCurrIds[ i ];
01322         aToolIds.insert( aCurrId );
01323       }
01324     }
01325 
01326     vector< int > anIds; // result
01327 
01328     // Iterate through main group 
01329     for ( g = 0, n = theMainGroups.length(); g < n; g++ )
01330     {
01331       SMESH::SMESH_GroupBase_var aGrp = theMainGroups[ g ];
01332       if ( CORBA::is_nil( aGrp ) )
01333         continue;
01334 
01335       // check type
01336       SMESH::ElementType aCurrType = aGrp->GetType();
01337       if ( aType == SMESH::ALL )
01338         aType = aCurrType;
01339       else 
01340       {
01341         if ( aType != aCurrType )
01342           return SMESH::SMESH_Group::_nil();
01343       }
01344 
01345       // unite tool ids
01346       SMESH::long_array_var aCurrIds = aGrp->GetListOfID();
01347       for ( int i = 0, n = aCurrIds->length(); i < n; i++ )
01348       {
01349         int aCurrId = aCurrIds[ i ];
01350         if ( !aToolIds.count( aCurrId ) )
01351           anIds.push_back( aCurrId );
01352       }
01353     }
01354 
01355     // Create group
01356     SMESH::SMESH_Group_var aResGrp = CreateGroup( aType, theName );
01357     if ( aResGrp->_is_nil() )
01358       return SMESH::SMESH_Group::_nil();
01359     
01360     // Create array of identifiers
01361     SMESH::long_array_var aResIds = new SMESH::long_array;
01362     aResIds->length( anIds.size() );
01363     
01364     for (int i=0; i<anIds.size(); i++ )
01365     {
01366       aResIds[ i ] = anIds[i];
01367     }
01368     aResGrp->Add( aResIds );
01369 
01370     // Clear python lines, created by CreateGroup() and Add()
01371     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
01372     _gen_i->RemoveLastFromPythonScript( aStudy->StudyId() );
01373     _gen_i->RemoveLastFromPythonScript( aStudy->StudyId() );
01374 
01375     // Update Python script
01376 
01377     TPythonDump() << aResGrp << " = " << _this() << ".CutListOfGroups( "
01378                   << &theMainGroups << ", " << &theToolGroups << ", '"
01379                   << theName << "' )";
01380     
01381     return aResGrp._retn();
01382   }
01383   catch( ... )
01384   {
01385     return SMESH::SMESH_Group::_nil();
01386   }
01387 }
01388 
01389 //=============================================================================
01400 //=============================================================================
01401 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateDimGroup( 
01402   const SMESH::ListOfGroups& theGroups, 
01403   SMESH::ElementType         theElemType, 
01404   const char*                theName )
01405   throw (SALOME::SALOME_Exception)
01406 {
01407   SMESHDS_Mesh* aMeshDS = _impl->GetMeshDS();
01408 
01409   if ( !theName || !aMeshDS )
01410     return SMESH::SMESH_Group::_nil();
01411 
01412   SMDSAbs_ElementType anElemType = (SMDSAbs_ElementType)theElemType;
01413 
01414   try
01415   {
01416     // Create map of nodes from all groups 
01417 
01418     set< int > aNodeMap;
01419     
01420     for ( int g = 0, n = theGroups.length(); g < n; g++ )
01421     {
01422       SMESH::SMESH_GroupBase_var aGrp = theGroups[ g ];
01423       if ( CORBA::is_nil( aGrp ) )
01424         continue;
01425 
01426       SMESH::ElementType aType = aGrp->GetType();
01427       if ( aType == SMESH::ALL )
01428         continue;
01429       else if ( aType == SMESH::NODE )
01430       {
01431         SMESH::long_array_var aCurrIds = aGrp->GetListOfID();
01432         for ( int i = 0, n = aCurrIds->length(); i < n; i++ )
01433         {
01434           int aCurrId = aCurrIds[ i ];
01435           const SMDS_MeshNode* aNode = aMeshDS->FindNode( aCurrId );
01436           if ( aNode )
01437             aNodeMap.insert( aNode->GetID() );
01438         }
01439       }
01440       else 
01441       {
01442         SMESH::long_array_var aCurrIds = aGrp->GetListOfID();
01443         for ( int i = 0, n = aCurrIds->length(); i < n; i++ )
01444         {
01445           int aCurrId = aCurrIds[ i ];
01446           const SMDS_MeshElement* anElem = aMeshDS->FindElement( aCurrId );
01447           if ( !anElem )
01448             continue;
01449           SMDS_ElemIteratorPtr aNodeIter = anElem->nodesIterator();
01450           while( aNodeIter->more() )
01451           {
01452             const SMDS_MeshNode* aNode = 
01453               dynamic_cast<const SMDS_MeshNode*>( aNodeIter->next() );
01454             if ( aNode )
01455               aNodeMap.insert( aNode->GetID() );
01456           }
01457         }
01458       }
01459     }
01460 
01461     // Get result identifiers 
01462 
01463     vector< int > aResultIds;
01464     if ( theElemType == SMESH::NODE )
01465     {
01466       //NCollection_Map< int >::Iterator aNodeIter( aNodeMap );
01467       set<int>::iterator iter = aNodeMap.begin();
01468       for ( ; iter != aNodeMap.end(); iter++ )
01469         aResultIds.push_back( *iter);
01470     }
01471     else
01472     {
01473       // Create list of elements of given dimension constructed on the nodes
01474       vector< int > anElemList;
01475       //NCollection_Map< int >::Iterator aNodeIter( aNodeMap );
01476       //for ( ; aNodeIter.More(); aNodeIter.Next() )
01477       set<int>::iterator iter = aNodeMap.begin();
01478       for ( ; iter != aNodeMap.end(); iter++ )
01479       {
01480         const SMDS_MeshElement* aNode = 
01481           dynamic_cast<const SMDS_MeshElement*>( aMeshDS->FindNode( *iter ) );
01482         if ( !aNode )
01483           continue;
01484 
01485          SMDS_ElemIteratorPtr anElemIter = aNode->elementsIterator( anElemType );
01486         while( anElemIter->more() )
01487         {
01488           const SMDS_MeshElement* anElem = 
01489             dynamic_cast<const SMDS_MeshElement*>( anElemIter->next() );
01490           if ( anElem && anElem->GetType() == anElemType )
01491             anElemList.push_back( anElem->GetID() );
01492         }
01493       }
01494 
01495       // check whether all nodes of elements are present in nodes map
01496       //NCollection_Map< int >::Iterator anIter( anElemList );
01497       //for ( ; anIter.More(); anIter.Next() )
01498       for (int i=0; i< anElemList.size(); i++)
01499       {
01500         const SMDS_MeshElement* anElem = aMeshDS->FindElement( anElemList[i] );
01501         if ( !anElem )
01502           continue;
01503 
01504         bool isOk = true;
01505         SMDS_ElemIteratorPtr aNodeIter = anElem->nodesIterator();
01506         while( aNodeIter->more() )
01507         {
01508           const SMDS_MeshNode* aNode = 
01509             dynamic_cast<const SMDS_MeshNode*>( aNodeIter->next() );
01510           if ( !aNode || !aNodeMap.count( aNode->GetID() ) )
01511           {
01512             isOk = false;
01513             break;
01514           }
01515         } 
01516         if ( isOk )
01517           aResultIds.push_back( anElem->GetID() );
01518       }
01519     }
01520 
01521     // Create group
01522 
01523     SMESH::SMESH_Group_var aResGrp = CreateGroup( theElemType, theName );
01524     if ( aResGrp->_is_nil() )
01525       return SMESH::SMESH_Group::_nil();
01526     
01527     // Create array of identifiers
01528     SMESH::long_array_var aResIds = new SMESH::long_array;
01529     aResIds->length( aResultIds.size() );
01530     
01531     //NCollection_Map< int >::Iterator aResIter( aResultIds );
01532     //for ( int i = 0; aResIter.More(); aResIter.Next(), i++ )
01533     for (int i=0; i< aResultIds.size(); i++)
01534       aResIds[ i ] = aResultIds[i];
01535     aResGrp->Add( aResIds );
01536 
01537     // Remove strings corresponding to group creation
01538     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
01539     _gen_i->RemoveLastFromPythonScript( aStudy->StudyId() );
01540     _gen_i->RemoveLastFromPythonScript( aStudy->StudyId() );
01541 
01542     // Update Python script
01543     
01544     TPythonDump() << aResGrp << " = " << _this() << ".CreateDimGroup( "
01545                   << &theGroups << ", " << theElemType << ", '" << theName << "' )";
01546 
01547     return aResGrp._retn();
01548   }
01549   catch( ... )
01550   {
01551     return SMESH::SMESH_Group::_nil();
01552   }
01553 }
01554 
01555 //================================================================================
01559 //================================================================================
01560 
01561 void SMESH_Mesh_i::addGeomGroupData(GEOM::GEOM_Object_ptr theGeomObj,
01562                                     CORBA::Object_ptr     theSmeshObj)
01563 {
01564   if ( CORBA::is_nil( theGeomObj ) || theGeomObj->GetType() != GEOM_GROUP )
01565     return;
01566   // group SO
01567   SALOMEDS::Study_var   study  = _gen_i->GetCurrentStudy();
01568   SALOMEDS::SObject_var groupSO = _gen_i->ObjectToSObject( study, theGeomObj );
01569   if ( groupSO->_is_nil() )
01570     return;
01571   // group indices
01572   GEOM::GEOM_Gen_var geomGen = _gen_i->GetGeomEngine();
01573   GEOM::GEOM_IGroupOperations_var groupOp =
01574     geomGen->GetIGroupOperations( _gen_i->GetCurrentStudyID() );
01575   GEOM::ListOfLong_var ids = groupOp->GetObjects( theGeomObj );
01576 
01577   // store data
01578   _geomGroupData.push_back( TGeomGroupData() );
01579   TGeomGroupData & groupData = _geomGroupData.back();
01580   // entry
01581   CORBA::String_var entry = groupSO->GetID();
01582   groupData._groupEntry = entry.in();
01583   // indices
01584   for ( int i = 0; i < ids->length(); ++i )
01585     groupData._indices.insert( ids[i] );
01586   // SMESH object
01587   groupData._smeshObject = theSmeshObj;
01588 }
01589 
01590 //================================================================================
01594 //================================================================================
01595 
01596 void SMESH_Mesh_i::removeGeomGroupData(CORBA::Object_ptr theSmeshObj)
01597 {
01598   list<TGeomGroupData>::iterator
01599     data = _geomGroupData.begin(), dataEnd = _geomGroupData.end();
01600   for ( ; data != dataEnd; ++data ) {
01601     if ( theSmeshObj->_is_equivalent( data->_smeshObject )) {
01602       _geomGroupData.erase( data );
01603       return;
01604     }
01605   }
01606 }
01607 
01608 //================================================================================
01612 //================================================================================
01613 
01614 TopoDS_Shape SMESH_Mesh_i::newGroupShape( TGeomGroupData & groupData)
01615 {
01616   TopoDS_Shape newShape;
01617 
01618   // get geom group
01619   SALOMEDS::Study_var study = _gen_i->GetCurrentStudy();
01620   if ( study->_is_nil() ) return newShape; // means "not changed"
01621   SALOMEDS::SObject_var groupSO = study->FindObjectID( groupData._groupEntry.c_str() );
01622   if ( !groupSO->_is_nil() )
01623   {
01624     CORBA::Object_var groupObj = _gen_i->SObjectToObject( groupSO );
01625     if ( CORBA::is_nil( groupObj )) return newShape;
01626     GEOM::GEOM_Object_var geomGroup = GEOM::GEOM_Object::_narrow( groupObj );
01627 
01628     // get indices of group items
01629     set<int> curIndices;
01630     GEOM::GEOM_Gen_var geomGen = _gen_i->GetGeomEngine();
01631     GEOM::GEOM_IGroupOperations_var groupOp =
01632       geomGen->GetIGroupOperations( _gen_i->GetCurrentStudyID() );
01633     GEOM::ListOfLong_var ids = groupOp->GetObjects( geomGroup );
01634     for ( int i = 0; i < ids->length(); ++i )
01635       curIndices.insert( ids[i] );
01636 
01637     if ( groupData._indices == curIndices )
01638       return newShape; // group not changed
01639 
01640     // update data
01641     groupData._indices = curIndices;
01642 
01643     GEOM_Client* geomClient = _gen_i->GetShapeReader();
01644     if ( !geomClient ) return newShape;
01645     TCollection_AsciiString groupIOR = geomGen->GetStringFromIOR( geomGroup );
01646     geomClient->RemoveShapeFromBuffer( groupIOR );
01647     newShape = _gen_i->GeomObjectToShape( geomGroup );
01648   }    
01649 
01650   if ( newShape.IsNull() ) {
01651     // geom group becomes empty - return empty compound
01652     TopoDS_Compound compound;
01653     BRep_Builder().MakeCompound(compound);
01654     newShape = compound;
01655   }
01656   return newShape;
01657 }
01658 
01659 namespace {
01660   //=============================================================================
01664   //=============================================================================
01665   struct TIndexedShape {
01666     int          _index;
01667     TopoDS_Shape _shape;
01668     TIndexedShape( int i, const TopoDS_Shape& s ):_index(i), _shape(s) {}
01669   };
01670 }
01671 //=============================================================================
01678 //=============================================================================
01679 
01680 void SMESH_Mesh_i::CheckGeomGroupModif()
01681 {
01682   if ( !_impl->HasShapeToMesh() ) return;
01683 
01684   SALOMEDS::Study_var study = _gen_i->GetCurrentStudy();
01685   if ( study->_is_nil() ) return;
01686 
01687   CORBA::Long nbEntities = NbNodes() + NbElements();
01688 
01689   // Check if group contents changed
01690 
01691   typedef map< string, TopoDS_Shape > TEntry2Geom;
01692   TEntry2Geom newGroupContents;
01693 
01694   list<TGeomGroupData>::iterator
01695     data = _geomGroupData.begin(), dataEnd = _geomGroupData.end();
01696   for ( ; data != dataEnd; ++data )
01697   {
01698     pair< TEntry2Geom::iterator, bool > it_new =
01699       newGroupContents.insert( make_pair( data->_groupEntry, TopoDS_Shape() ));
01700     bool processedGroup    = !it_new.second;
01701     TopoDS_Shape& newShape = it_new.first->second;
01702     if ( !processedGroup )
01703       newShape = newGroupShape( *data );
01704     if ( newShape.IsNull() )
01705       continue; // no changes
01706 
01707     if ( processedGroup ) { // update group indices
01708       list<TGeomGroupData>::iterator data2 = data;
01709       for ( --data2; data2->_groupEntry != data->_groupEntry; --data2) {}
01710       data->_indices = data2->_indices;
01711     }
01712 
01713     // Update SMESH objects according to new GEOM group contents
01714 
01715     SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow( data->_smeshObject );
01716     if ( !submesh->_is_nil() ) // -------------- Sub mesh ---------------------
01717     {
01718       int oldID = submesh->GetId();
01719       if ( _mapSubMeshIor.find( oldID ) == _mapSubMeshIor.end() )
01720         continue;
01721       TopoDS_Shape oldShape = _mapSubMesh[oldID]->GetSubShape();
01722 
01723       // update hypotheses
01724       list <const SMESHDS_Hypothesis * > hyps = _impl->GetHypothesisList(oldShape);
01725       list <const SMESHDS_Hypothesis * >::iterator hypIt;
01726       for ( hypIt = hyps.begin(); hypIt != hyps.end(); ++hypIt )
01727       {
01728         _impl->RemoveHypothesis( oldShape, (*hypIt)->GetID());
01729         _impl->AddHypothesis   ( newShape, (*hypIt)->GetID());
01730       }
01731       // care of submeshes
01732       SMESH_subMesh* newSubmesh = _impl->GetSubMesh( newShape );
01733       int newID = newSubmesh->GetId();
01734       if ( newID != oldID ) {
01735         _mapSubMesh   [ newID ] = newSubmesh;
01736         _mapSubMesh_i [ newID ] = _mapSubMesh_i [ oldID ];
01737         _mapSubMeshIor[ newID ] = _mapSubMeshIor[ oldID ];
01738         _mapSubMesh.   erase(oldID);
01739         _mapSubMesh_i. erase(oldID);
01740         _mapSubMeshIor.erase(oldID);
01741         _mapSubMesh_i [ newID ]->changeLocalId( newID );
01742       }
01743       continue;
01744     }
01745 
01746     SMESH::SMESH_GroupOnGeom_var smeshGroup =
01747       SMESH::SMESH_GroupOnGeom::_narrow( data->_smeshObject );
01748     if ( !smeshGroup->_is_nil() ) // ------------ GROUP -----------------------
01749     {
01750       SMESH_GroupOnGeom_i* group_i = SMESH::DownCast<SMESH_GroupOnGeom_i*>( smeshGroup );
01751       if ( group_i ) {
01752         ::SMESH_Group* group = _impl->GetGroup( group_i->GetLocalID() );
01753         SMESHDS_GroupOnGeom* ds = static_cast<SMESHDS_GroupOnGeom*>( group->GetGroupDS() );
01754         ds->SetShape( newShape );
01755       }
01756       continue;
01757     }
01758 
01759     SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( data->_smeshObject );
01760     if ( !mesh->_is_nil() ) // -------------- MESH ----------------------------
01761     {
01762       // Remove groups and submeshes basing on removed sub-shapes
01763 
01764       TopTools_MapOfShape newShapeMap;
01765       TopoDS_Iterator shapeIt( newShape );
01766       for ( ; shapeIt.More(); shapeIt.Next() )
01767         newShapeMap.Add( shapeIt.Value() );
01768 
01769       SMESHDS_Mesh* meshDS = _impl->GetMeshDS();
01770       for ( shapeIt.Initialize( meshDS->ShapeToMesh() ); shapeIt.More(); shapeIt.Next() )
01771       {
01772         if ( newShapeMap.Contains( shapeIt.Value() ))
01773           continue;
01774         TopTools_IndexedMapOfShape oldShapeMap;
01775         TopExp::MapShapes( shapeIt.Value(), oldShapeMap );
01776         for ( int i = 1; i <= oldShapeMap.Extent(); ++i )
01777         {
01778           const TopoDS_Shape& oldShape = oldShapeMap(i);
01779           int oldInd = meshDS->ShapeToIndex( oldShape );
01780           // -- submeshes --
01781           map<int, SMESH::SMESH_subMesh_ptr>::iterator i_smIor = _mapSubMeshIor.find( oldInd );
01782           if ( i_smIor != _mapSubMeshIor.end() ) {
01783             RemoveSubMesh( i_smIor->second ); // one submesh per shape index
01784           }
01785           // --- groups ---
01786           map<int, SMESH::SMESH_GroupBase_ptr>::iterator i_grp = _mapGroups.begin();
01787           for ( ; i_grp != _mapGroups.end(); ++i_grp )
01788           {
01789             // check if a group bases on oldInd shape
01790             SMESHDS_GroupOnGeom* grpOnGeom = 0;
01791             if ( ::SMESH_Group* g = _impl->GetGroup( i_grp->first ))
01792               grpOnGeom = dynamic_cast<SMESHDS_GroupOnGeom*>( g->GetGroupDS() );
01793             if ( grpOnGeom && oldShape.IsSame( grpOnGeom->GetShape() ))
01794             { // remove
01795               RemoveGroup( i_grp->second ); // several groups can base on same shape
01796               i_grp = _mapGroups.begin(); // _mapGroups changed - restart iteration
01797             }
01798           }
01799         }
01800       }
01801       // Reassign hypotheses and update groups after setting the new shape to mesh
01802 
01803       // collect anassigned hypotheses
01804       typedef list< pair< TIndexedShape, list<const SMESHDS_Hypothesis*> > > TShapeHypList;
01805       list <const SMESHDS_Hypothesis * >::const_iterator hypIt;
01806       TShapeHypList assignedHyps;
01807       for ( int i = 1; i <= meshDS->MaxShapeIndex(); ++i )
01808       {
01809         const TopoDS_Shape& oldShape = meshDS->IndexToShape(i);
01810         list<const SMESHDS_Hypothesis*> hyps = meshDS->GetHypothesis( oldShape );// copy
01811         if ( !hyps.empty() ) {
01812           assignedHyps.push_back( make_pair( TIndexedShape(i,oldShape), hyps ));
01813           for ( hypIt = hyps.begin(); hypIt != hyps.end(); ++hypIt )
01814             _impl->RemoveHypothesis( oldShape, (*hypIt)->GetID());
01815         }
01816       }
01817       // collect shapes supporting groups
01818       typedef list < pair< TIndexedShape, SMDSAbs_ElementType > > TShapeTypeList;
01819       TShapeTypeList groupData;
01820       const set<SMESHDS_GroupBase*>& groups = meshDS->GetGroups();
01821       set<SMESHDS_GroupBase*>::const_iterator grIt = groups.begin();
01822       for ( ; grIt != groups.end(); ++grIt )
01823       {
01824         if ( SMESHDS_GroupOnGeom* gog = dynamic_cast<SMESHDS_GroupOnGeom*>( *grIt ))
01825           groupData.push_back
01826             ( make_pair( TIndexedShape( gog->GetID(),gog->GetShape()), gog->GetType()));
01827       }
01828       // set new shape to mesh -> DS of submeshes and geom groups is deleted
01829       _impl->ShapeToMesh( newShape );
01830       
01831       // reassign hypotheses
01832       TShapeHypList::iterator indS_hyps = assignedHyps.begin();
01833       for ( ; indS_hyps != assignedHyps.end(); ++indS_hyps )
01834       {
01835         TIndexedShape&                   geom = indS_hyps->first;
01836         list<const SMESHDS_Hypothesis*>& hyps = indS_hyps->second;
01837         int oldID = geom._index;
01838         int newID = meshDS->ShapeToIndex( geom._shape );
01839         if ( !newID )
01840           continue;
01841         if ( oldID == 1 ) { // main shape
01842           newID = 1;
01843           geom._shape = newShape;
01844         }
01845         for ( hypIt = hyps.begin(); hypIt != hyps.end(); ++hypIt )
01846           _impl->AddHypothesis( geom._shape, (*hypIt)->GetID());
01847         // care of submeshes
01848         SMESH_subMesh* newSubmesh = _impl->GetSubMesh( geom._shape );
01849         if ( newID != oldID ) {
01850           _mapSubMesh   [ newID ] = newSubmesh;
01851           _mapSubMesh_i [ newID ] = _mapSubMesh_i [ oldID ];
01852           _mapSubMeshIor[ newID ] = _mapSubMeshIor[ oldID ];
01853           _mapSubMesh.   erase(oldID);
01854           _mapSubMesh_i. erase(oldID);
01855           _mapSubMeshIor.erase(oldID);
01856           _mapSubMesh_i [ newID ]->changeLocalId( newID );
01857         }
01858       }
01859       // recreate groups
01860       TShapeTypeList::iterator geomType = groupData.begin();
01861       for ( ; geomType != groupData.end(); ++geomType )
01862       {
01863         const TIndexedShape& geom = geomType->first;
01864         int oldID = geom._index;
01865         if ( _mapGroups.find( oldID ) == _mapGroups.end() )
01866           continue;
01867         // get group name
01868         SALOMEDS::SObject_var groupSO = _gen_i->ObjectToSObject( study,_mapGroups[oldID] );
01869         CORBA::String_var     name    = groupSO->GetName();
01870         // update
01871         SMESH_GroupBase_i* group_i    = SMESH::DownCast<SMESH_GroupBase_i*>(_mapGroups[oldID] );
01872         int newID;
01873         if ( group_i && _impl->AddGroup( geomType->second, name.in(), newID, geom._shape ))
01874           group_i->changeLocalId( newID );
01875       }
01876 
01877       break; // everything has been updated
01878 
01879     } // update mesh
01880   } // loop on group data
01881 
01882   // Update icons
01883 
01884   CORBA::Long newNbEntities = NbNodes() + NbElements();
01885   list< SALOMEDS::SObject_var > soToUpdateIcons;
01886   if ( newNbEntities != nbEntities )
01887   {
01888     // Add all SObjects with icons
01889     soToUpdateIcons.push_back( _gen_i->ObjectToSObject( study, _this() )); // mesh
01890 
01891     for (map<int, SMESH::SMESH_subMesh_ptr>::iterator i_sm = _mapSubMeshIor.begin();
01892          i_sm != _mapSubMeshIor.end(); ++i_sm ) // submeshes
01893       soToUpdateIcons.push_back( _gen_i->ObjectToSObject( study, i_sm->second ));
01894 
01895     for ( map<int, SMESH::SMESH_GroupBase_ptr>::iterator i_gr = _mapGroups.begin();
01896           i_gr != _mapGroups.end(); ++i_gr ) // groups
01897       soToUpdateIcons.push_back( _gen_i->ObjectToSObject( study, i_gr->second ));
01898   }
01899 
01900   list< SALOMEDS::SObject_var >::iterator so = soToUpdateIcons.begin();
01901   for ( ; so != soToUpdateIcons.end(); ++so )
01902     _gen_i->SetPixMap( *so, "ICON_SMESH_TREE_MESH_WARN" );
01903 }
01904 
01905 //=============================================================================
01909 //=============================================================================
01910 
01911 SMESH::SMESH_Group_ptr SMESH_Mesh_i::ConvertToStandalone( SMESH::SMESH_GroupOnGeom_ptr theGroup )
01912 {
01913   SMESH::SMESH_Group_var aGroup;
01914   if ( theGroup->_is_nil() )
01915     return aGroup._retn();
01916 
01917   Unexpect aCatch(SALOME_SalomeException);
01918 
01919   SMESH_GroupBase_i* aGroupToRem =
01920     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
01921   if ( !aGroupToRem )
01922     return aGroup._retn();
01923 
01924   int anId = aGroupToRem->GetLocalID();
01925   if ( !_impl->ConvertToStandalone( anId ) )
01926     return aGroup._retn();
01927   removeGeomGroupData( theGroup );
01928 
01929   SMESH_GroupBase_i* aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
01930 
01931   // remove old instance of group from own map
01932   _mapGroups.erase( anId );
01933 
01934   SALOMEDS::StudyBuilder_var builder;
01935   SALOMEDS::SObject_var aGroupSO;
01936   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
01937   if ( !aStudy->_is_nil() )  {
01938     builder = aStudy->NewBuilder();
01939     aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
01940     if ( !aGroupSO->_is_nil() ) {
01941 
01942     // remove reference to geometry
01943     SALOMEDS::ChildIterator_var chItr = aStudy->NewChildIterator(aGroupSO);
01944     for ( ; chItr->More(); chItr->Next() )
01945       // Remove group's child SObject
01946       builder->RemoveObject( chItr->Value() );
01947 
01948       // Update Python script
01949       TPythonDump() << aGroupSO << " = " << _this() << ".ConvertToStandalone( "
01950                     << aGroupSO << " )";
01951     }
01952   }
01953 
01954   // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
01955   SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
01956   aGroupImpl->Register();
01957   // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
01958 
01959   // remember new group in own map
01960   aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
01961   _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
01962 
01963   // register CORBA object for persistence
01964   /*int nextId =*/ _gen_i->RegisterObject( aGroup );
01965 
01966   builder->SetIOR( aGroupSO, _gen_i->GetORB()->object_to_string( aGroup ) );
01967 
01968   return aGroup._retn();
01969 }
01970 
01971 //=============================================================================
01975 //=============================================================================
01976 
01977 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
01978 {
01979   if(MYDEBUG) MESSAGE( "createSubMesh" );
01980   TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
01981 
01982   ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
01983   int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
01984   SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
01985   SMESH::SMESH_subMesh_var subMesh
01986     = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
01987 
01988   _mapSubMesh[subMeshId] = mySubMesh;
01989   _mapSubMesh_i[subMeshId] = subMeshServant;
01990   _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
01991 
01992   // register CORBA object for persistence
01993   int nextId = _gen_i->RegisterObject( subMesh );
01994   if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
01995 
01996   // to track changes of GEOM groups
01997   addGeomGroupData( theSubShapeObject, subMesh );
01998 
01999   return subMesh._retn();
02000 }
02001 
02002 //=======================================================================
02003 //function : getSubMesh
02004 //purpose  :
02005 //=======================================================================
02006 
02007 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
02008 {
02009   map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
02010   if ( it == _mapSubMeshIor.end() )
02011     return SMESH::SMESH_subMesh::_nil();
02012 
02013   return SMESH::SMESH_subMesh::_duplicate( (*it).second );
02014 }
02015 
02016 
02017 //=============================================================================
02021 //=============================================================================
02022 
02023 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
02024                                   GEOM::GEOM_Object_ptr    theSubShapeObject )
02025 {
02026   MESSAGE("SMESH_Mesh_i::removeSubMesh()");
02027   if ( theSubMesh->_is_nil() /*|| theSubShapeObject->_is_nil()*/ )
02028     return;
02029 
02030   if ( theSubShapeObject->_is_nil() )  // not published shape (IPAL13617)
02031   {
02032     CORBA::Long shapeId = theSubMesh->GetId();
02033     if ( _mapSubMesh.find( shapeId ) != _mapSubMesh.end())
02034     {
02035       TopoDS_Shape S = _mapSubMesh[ shapeId ]->GetSubShape();
02036       if ( !S.IsNull() )
02037       {
02038         list<const SMESHDS_Hypothesis*> hyps = _impl->GetHypothesisList( S );
02039         list<const SMESHDS_Hypothesis*>::const_iterator hyp = hyps.begin();
02040         for ( ; hyp != hyps.end(); ++hyp )
02041           _impl->RemoveHypothesis(S, (*hyp)->GetID());
02042       }
02043     }
02044   }
02045   else
02046   {
02047     try {
02048       SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
02049       for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
02050         removeHypothesis( theSubShapeObject, aHypList[i] );
02051       }
02052     }
02053     catch( const SALOME::SALOME_Exception& ) {
02054       INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
02055     }
02056     removeGeomGroupData( theSubShapeObject );
02057   }
02058   int subMeshId = theSubMesh->GetId();
02059 
02060   _mapSubMesh.erase(subMeshId);
02061   _mapSubMesh_i.erase(subMeshId);
02062   _mapSubMeshIor.erase(subMeshId);
02063   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
02064 }
02065 
02066 //=============================================================================
02070 //=============================================================================
02071 
02072 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
02073                                                       const char*         theName,
02074                                                       const TopoDS_Shape& theShape )
02075 {
02076   std::string newName;
02077   if ( !theName || strlen( theName ) == 0 )
02078   {
02079     std::set< std::string > presentNames;
02080     std::map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i_gr = _mapGroups.begin();
02081     for ( ; i_gr != _mapGroups.end(); ++i_gr )
02082       presentNames.insert( i_gr->second->GetName() );
02083     do {
02084       newName = "noname_Group_" + SMESH_Comment( presentNames.size() + 1 );
02085     } while ( !presentNames.insert( newName ).second );
02086     theName = newName.c_str();
02087   }
02088   int anId;
02089   SMESH::SMESH_GroupBase_var aGroup;
02090   if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
02091     SMESH_GroupBase_i* aGroupImpl;
02092     if ( !theShape.IsNull() )
02093       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
02094     else
02095       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
02096 
02097     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
02098     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
02099     aGroupImpl->Register();
02100     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
02101 
02102     aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
02103     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
02104 
02105     // register CORBA object for persistence
02106     int nextId = _gen_i->RegisterObject( aGroup );
02107     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
02108 
02109     // to track changes of GEOM groups
02110     if ( !theShape.IsNull() ) {
02111       GEOM::GEOM_Object_var geom = _gen_i->ShapeToGeomObject( theShape );
02112       addGeomGroupData( geom, aGroup );
02113     }
02114   }
02115   return aGroup._retn();
02116 }
02117 
02118 //=============================================================================
02124 //=============================================================================
02125 
02126 void SMESH_Mesh_i::removeGroup( const int theId )
02127 {
02128   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );
02129   if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
02130     SMESH::SMESH_GroupBase_ptr group = _mapGroups[theId];
02131     _mapGroups.erase( theId );
02132     removeGeomGroupData( group );
02133     if (! _impl->RemoveGroup( theId ))
02134     {
02135       // it seems to be a call up from _impl caused by hyp modification (issue 0020918)
02136       RemoveGroup( group );
02137     }
02138   }
02139 }
02140 
02141 //=============================================================================
02145 //=============================================================================
02146 
02147 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
02148 throw(SALOME::SALOME_Exception)
02149 {
02150   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
02151 
02152   SMESH::log_array_var aLog;
02153   try{
02154     list < SMESHDS_Command * >logDS = _impl->GetLog();
02155     aLog = new SMESH::log_array;
02156     int indexLog = 0;
02157     int lg = logDS.size();
02158     SCRUTE(lg);
02159     aLog->length(lg);
02160     list < SMESHDS_Command * >::iterator its = logDS.begin();
02161     while(its != logDS.end()){
02162       SMESHDS_Command *com = *its;
02163       int comType = com->GetType();
02164       //SCRUTE(comType);
02165       int lgcom = com->GetNumber();
02166       //SCRUTE(lgcom);
02167       const list < int >&intList = com->GetIndexes();
02168       int inum = intList.size();
02169       //SCRUTE(inum);
02170       list < int >::const_iterator ii = intList.begin();
02171       const list < double >&coordList = com->GetCoords();
02172       int rnum = coordList.size();
02173       //SCRUTE(rnum);
02174       list < double >::const_iterator ir = coordList.begin();
02175       aLog[indexLog].commandType = comType;
02176       aLog[indexLog].number = lgcom;
02177       aLog[indexLog].coords.length(rnum);
02178       aLog[indexLog].indexes.length(inum);
02179       for(int i = 0; i < rnum; i++){
02180         aLog[indexLog].coords[i] = *ir;
02181         //MESSAGE(" "<<i<<" "<<ir.Value());
02182         ir++;
02183       }
02184       for(int i = 0; i < inum; i++){
02185         aLog[indexLog].indexes[i] = *ii;
02186         //MESSAGE(" "<<i<<" "<<ii.Value());
02187         ii++;
02188       }
02189       indexLog++;
02190       its++;
02191     }
02192     if(clearAfterGet)
02193       _impl->ClearLog();
02194   }
02195   catch(SALOME_Exception & S_ex){
02196     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
02197   }
02198   return aLog._retn();
02199 }
02200 
02201 
02202 //=============================================================================
02206 //=============================================================================
02207 
02208 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
02209 {
02210   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
02211   _impl->ClearLog();
02212 }
02213 
02214 //=============================================================================
02218 //=============================================================================
02219 
02220 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
02221 {
02222   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
02223   return _id;
02224 }
02225 
02226 //=============================================================================
02230 //=============================================================================
02231 
02232 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
02233 {
02234   return _studyId;
02235 }
02236 
02237 //=============================================================================
02238 namespace
02239 {
02241   // SMESH_Mesh::RemoveGroup() (issue 0020918)
02242   struct TRmGroupCallUp_i : public SMESH_Mesh::TRmGroupCallUp
02243   {
02244     SMESH_Mesh_i* _mesh;
02245     TRmGroupCallUp_i(SMESH_Mesh_i* mesh):_mesh(mesh) {}
02246     virtual void RemoveGroup (const int theGroupID) { _mesh->removeGroup( theGroupID ); }
02247   };
02248 }
02249 
02250 //=============================================================================
02254 //=============================================================================
02255 
02256 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
02257 {
02258   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
02259   _impl = impl;
02260   if ( _impl )
02261     _impl->SetRemoveGroupCallUp( new TRmGroupCallUp_i(this));
02262 }
02263 
02264 //=============================================================================
02268 //=============================================================================
02269 
02270 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
02271 {
02272   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
02273   return *_impl;
02274 }
02275 
02276 //=============================================================================
02280 //=============================================================================
02281 
02282 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
02283 {
02284   // Create MeshEditor
02285   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( this, false );
02286   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
02287 
02288   // Update Python script
02289   TPythonDump() << aMeshEditor << " = " << _this() << ".GetMeshEditor()";
02290 
02291   return aMesh._retn();
02292 }
02293 
02294 //=============================================================================
02298 //=============================================================================
02299 
02300 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditPreviewer()
02301 {
02302   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( this, true );
02303   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
02304   return aMesh._retn();
02305 }
02306 
02307 //================================================================================
02312 //================================================================================
02313 
02314 CORBA::Boolean SMESH_Mesh_i::HasModificationsToDiscard() throw(SALOME::SALOME_Exception)
02315 {
02316   Unexpect aCatch(SALOME_SalomeException);
02317   return _impl->HasModificationsToDiscard();
02318 }
02319 
02320 static SALOMEDS::Color getUniqueColor( const std::list<SALOMEDS::Color>& theReservedColors )
02321 {
02322   const int MAX_ATTEMPTS = 100;
02323   int cnt = 0;
02324   double tolerance = 0.5;
02325   SALOMEDS::Color col;
02326 
02327   bool ok = false;
02328   while ( !ok ) {
02329     // generate random color
02330     double red    = (double)rand() / RAND_MAX;
02331     double green  = (double)rand() / RAND_MAX;
02332     double blue   = (double)rand() / RAND_MAX;
02333     // check existence in the list of the existing colors
02334     bool matched = false;
02335     std::list<SALOMEDS::Color>::const_iterator it;
02336     for ( it = theReservedColors.begin(); it != theReservedColors.end() && !matched; ++it ) {
02337       SALOMEDS::Color color = *it;
02338       double tol = fabs( color.R - red ) + fabs( color.G - green ) + fabs( color.B  - blue  );
02339       matched = tol < tolerance;
02340     }
02341     if ( (cnt+1) % 20 == 0 ) tolerance = tolerance/2;
02342     ok = ( ++cnt == MAX_ATTEMPTS ) || !matched;
02343     col.R = red;
02344     col.G = green;
02345     col.B = blue;
02346   }
02347   return col;
02348 }
02349 
02350 //=============================================================================
02354 //=============================================================================
02355 void SMESH_Mesh_i::SetAutoColor(CORBA::Boolean theAutoColor) throw(SALOME::SALOME_Exception)
02356 {
02357   Unexpect aCatch(SALOME_SalomeException);
02358   _impl->SetAutoColor(theAutoColor);
02359 
02360   TPythonDump pyDump; // not to dump group->SetColor() from below code
02361   pyDump<<_this()<<".SetAutoColor( "<<theAutoColor<<" )";
02362 
02363   std::list<SALOMEDS::Color> aReservedColors;
02364   map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
02365   for ( ; it != _mapGroups.end(); it++ ) {
02366     if ( CORBA::is_nil( it->second )) continue;
02367     SALOMEDS::Color aColor = getUniqueColor( aReservedColors );
02368     it->second->SetColor( aColor );
02369     aReservedColors.push_back( aColor );
02370   }
02371 }
02372 
02373 //=============================================================================
02377 //=============================================================================
02378 CORBA::Boolean SMESH_Mesh_i::GetAutoColor() throw(SALOME::SALOME_Exception)
02379 {
02380   Unexpect aCatch(SALOME_SalomeException);
02381   return _impl->GetAutoColor();
02382 }
02383 
02384 
02385 //=============================================================================
02389 //=============================================================================
02390 
02391 CORBA::Boolean SMESH_Mesh_i::HasDuplicatedGroupNamesMED()
02392 {
02393   return _impl->HasDuplicatedGroupNamesMED();
02394 }
02395 
02396 void SMESH_Mesh_i::PrepareForWriting (const char* file, bool overwrite)
02397 {
02398   TCollection_AsciiString aFullName ((char*)file);
02399   OSD_Path aPath (aFullName);
02400   OSD_File aFile (aPath);
02401   if (aFile.Exists()) {
02402     // existing filesystem node
02403     if (aFile.KindOfFile() == OSD_FILE) {
02404       if (aFile.IsWriteable()) {
02405         if (overwrite) {
02406           aFile.Reset();
02407           aFile.Remove();
02408         }
02409         if (aFile.Failed()) {
02410           TCollection_AsciiString msg ("File ");
02411           msg += aFullName + " cannot be replaced.";
02412           THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
02413         }
02414       } else {
02415         TCollection_AsciiString msg ("File ");
02416         msg += aFullName + " cannot be overwritten.";
02417         THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
02418       }
02419     } else {
02420       TCollection_AsciiString msg ("Location ");
02421       msg += aFullName + " is not a file.";
02422       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
02423     }
02424   } else {
02425     // nonexisting file; check if it can be created
02426     aFile.Reset();
02427     aFile.Build(OSD_WriteOnly, OSD_Protection());
02428     if (aFile.Failed()) {
02429       TCollection_AsciiString msg ("You cannot create the file ");
02430       msg += aFullName + ". Check the directory existance and access rights.";
02431       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
02432     } else {
02433       aFile.Close();
02434       aFile.Remove();
02435     }
02436   }
02437 }
02438 
02439 void SMESH_Mesh_i::ExportToMEDX (const char* file,
02440                                  CORBA::Boolean auto_groups,
02441                                  SMESH::MED_VERSION theVersion,
02442                                  CORBA::Boolean overwrite)
02443   throw(SALOME::SALOME_Exception)
02444 {
02445   Unexpect aCatch(SALOME_SalomeException);
02446 
02447   // Perform Export
02448   PrepareForWriting(file, overwrite);
02449   const char* aMeshName = "Mesh";
02450   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
02451   if ( !aStudy->_is_nil() ) {
02452     SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
02453     if ( !aMeshSO->_is_nil() ) {
02454       aMeshName = aMeshSO->GetName();
02455       // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes
02456       if ( !aStudy->GetProperties()->IsLocked() )
02457         {
02458         SALOMEDS::GenericAttribute_var anAttr;
02459         SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
02460         SALOMEDS::AttributeExternalFileDef_var aFileName;
02461         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
02462         aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
02463         ASSERT(!aFileName->_is_nil());
02464         aFileName->SetValue(file);
02465         SALOMEDS::AttributeFileType_var aFileType;
02466         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
02467         aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
02468         ASSERT(!aFileType->_is_nil());
02469         aFileType->SetValue("FICHIERMED");
02470         }
02471     }
02472   }
02473   // Update Python script
02474   // set name of mesh before export
02475   TPythonDump() << _gen_i << ".SetName(" << _this() << ", '" << aMeshName << "')";
02476   
02477   // check names of groups
02478   checkGroupNames();
02479 
02480   TPythonDump() << _this() << ".ExportToMEDX( r'"
02481                 << file << "', " << auto_groups << ", " << theVersion << ", " << overwrite << " )";
02482 
02483   _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
02484 }
02485 
02486 void SMESH_Mesh_i::ExportToMED (const char* file,
02487                                 CORBA::Boolean auto_groups,
02488                                 SMESH::MED_VERSION theVersion)
02489   throw(SALOME::SALOME_Exception)
02490 {
02491   ExportToMEDX(file,auto_groups,theVersion,true);
02492 }
02493 
02494 void SMESH_Mesh_i::ExportMED (const char* file,
02495                               CORBA::Boolean auto_groups)
02496   throw(SALOME::SALOME_Exception)
02497 {
02498   ExportToMEDX(file,auto_groups,SMESH::MED_V2_2,true);
02499 }
02500 
02501 void SMESH_Mesh_i::ExportDAT (const char *file)
02502   throw(SALOME::SALOME_Exception)
02503 {
02504   Unexpect aCatch(SALOME_SalomeException);
02505 
02506   // Update Python script
02507   // check names of groups
02508   checkGroupNames();
02509   TPythonDump() << _this() << ".ExportDAT( r'" << file << "' )";
02510 
02511   // Perform Export
02512   PrepareForWriting(file);
02513   _impl->ExportDAT(file);
02514 }
02515 
02516 void SMESH_Mesh_i::ExportUNV (const char *file)
02517   throw(SALOME::SALOME_Exception)
02518 {
02519   Unexpect aCatch(SALOME_SalomeException);
02520 
02521   // Update Python script
02522   // check names of groups
02523   checkGroupNames();
02524   TPythonDump() << _this() << ".ExportUNV( r'" << file << "' )";
02525 
02526   // Perform Export
02527   PrepareForWriting(file);
02528   _impl->ExportUNV(file);
02529 }
02530 
02531 void SMESH_Mesh_i::ExportSTL (const char *file, const bool isascii)
02532   throw(SALOME::SALOME_Exception)
02533 {
02534   Unexpect aCatch(SALOME_SalomeException);
02535 
02536   // Update Python script
02537   // check names of groups
02538   checkGroupNames();
02539   TPythonDump() << _this() << ".ExportSTL( r'" << file << "', " << isascii << " )";
02540 
02541   // Perform Export
02542   PrepareForWriting(file);
02543   _impl->ExportSTL(file, isascii);
02544 }
02545 
02546 //=============================================================================
02550 //=============================================================================
02551 
02552 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
02553 {
02554   Unexpect aCatch(SALOME_SalomeException);
02555   SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
02556   SALOME_MED::MESH_var aMesh = aMedMesh->_this();
02557   return aMesh._retn();
02558 }
02559 
02560 //=============================================================================
02564 //=============================================================================
02565 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
02566 {
02567   Unexpect aCatch(SALOME_SalomeException);
02568   return _impl->NbNodes();
02569 }
02570 
02571 //=============================================================================
02575 //=============================================================================
02576 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
02577 {
02578   Unexpect aCatch(SALOME_SalomeException);
02579   return Nb0DElements() + NbEdges() + NbFaces() + NbVolumes();
02580 }
02581 
02582 //=============================================================================
02586 //=============================================================================
02587 CORBA::Long SMESH_Mesh_i::Nb0DElements()throw (SALOME::SALOME_Exception)
02588 {
02589   Unexpect aCatch(SALOME_SalomeException);
02590   return _impl->Nb0DElements();
02591 }
02592 
02593 //=============================================================================
02597 //=============================================================================
02598 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
02599 {
02600   Unexpect aCatch(SALOME_SalomeException);
02601   return _impl->NbEdges();
02602 }
02603 
02604 CORBA::Long SMESH_Mesh_i::NbEdgesOfOrder(SMESH::ElementOrder order)
02605   throw(SALOME::SALOME_Exception)
02606 {
02607   Unexpect aCatch(SALOME_SalomeException);
02608   return _impl->NbEdges( (SMDSAbs_ElementOrder) order);
02609 }
02610 
02611 //=============================================================================
02615 //=============================================================================
02616 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
02617 {
02618   Unexpect aCatch(SALOME_SalomeException);
02619   return _impl->NbFaces();
02620 }
02621 
02622 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
02623 {
02624   Unexpect aCatch(SALOME_SalomeException);
02625   return _impl->NbTriangles();
02626 }
02627 
02628 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
02629 {
02630   Unexpect aCatch(SALOME_SalomeException);
02631   return _impl->NbQuadrangles();
02632 }
02633 
02634 CORBA::Long SMESH_Mesh_i::NbPolygons()throw(SALOME::SALOME_Exception)
02635 {
02636   Unexpect aCatch(SALOME_SalomeException);
02637   return _impl->NbPolygons();
02638 }
02639 
02640 CORBA::Long SMESH_Mesh_i::NbFacesOfOrder(SMESH::ElementOrder order)
02641   throw(SALOME::SALOME_Exception)
02642 {
02643   Unexpect aCatch(SALOME_SalomeException);
02644   return _impl->NbFaces( (SMDSAbs_ElementOrder) order);
02645 }
02646 
02647 CORBA::Long SMESH_Mesh_i::NbTrianglesOfOrder(SMESH::ElementOrder order)
02648   throw(SALOME::SALOME_Exception)
02649 {
02650   Unexpect aCatch(SALOME_SalomeException);
02651   return _impl->NbTriangles( (SMDSAbs_ElementOrder) order);
02652 }
02653 
02654 CORBA::Long SMESH_Mesh_i::NbQuadranglesOfOrder(SMESH::ElementOrder order)
02655   throw(SALOME::SALOME_Exception)
02656 {
02657   Unexpect aCatch(SALOME_SalomeException);
02658   return _impl->NbQuadrangles( (SMDSAbs_ElementOrder) order);
02659 }
02660 
02661 //=============================================================================
02665 //=============================================================================
02666 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
02667 {
02668   Unexpect aCatch(SALOME_SalomeException);
02669   return _impl->NbVolumes();
02670 }
02671 
02672 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
02673 {
02674   Unexpect aCatch(SALOME_SalomeException);
02675   return _impl->NbTetras();
02676 }
02677 
02678 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
02679 {
02680   Unexpect aCatch(SALOME_SalomeException);
02681   return _impl->NbHexas();
02682 }
02683 
02684 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
02685 {
02686   Unexpect aCatch(SALOME_SalomeException);
02687   return _impl->NbPyramids();
02688 }
02689 
02690 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
02691 {
02692   Unexpect aCatch(SALOME_SalomeException);
02693   return _impl->NbPrisms();
02694 }
02695 
02696 CORBA::Long SMESH_Mesh_i::NbPolyhedrons()throw(SALOME::SALOME_Exception)
02697 {
02698   Unexpect aCatch(SALOME_SalomeException);
02699   return _impl->NbPolyhedrons();
02700 }
02701 
02702 CORBA::Long SMESH_Mesh_i::NbVolumesOfOrder(SMESH::ElementOrder order)
02703   throw(SALOME::SALOME_Exception)
02704 {
02705   Unexpect aCatch(SALOME_SalomeException);
02706   return _impl->NbVolumes( (SMDSAbs_ElementOrder) order);
02707 }
02708 
02709 CORBA::Long SMESH_Mesh_i::NbTetrasOfOrder(SMESH::ElementOrder order)
02710   throw(SALOME::SALOME_Exception)
02711 {
02712   Unexpect aCatch(SALOME_SalomeException);
02713   return _impl->NbTetras( (SMDSAbs_ElementOrder) order);
02714 }
02715 
02716 CORBA::Long SMESH_Mesh_i::NbHexasOfOrder(SMESH::ElementOrder order)
02717   throw(SALOME::SALOME_Exception)
02718 {
02719   Unexpect aCatch(SALOME_SalomeException);
02720   return _impl->NbHexas( (SMDSAbs_ElementOrder) order);
02721 }
02722 
02723 CORBA::Long SMESH_Mesh_i::NbPyramidsOfOrder(SMESH::ElementOrder order)
02724   throw(SALOME::SALOME_Exception)
02725 {
02726   Unexpect aCatch(SALOME_SalomeException);
02727   return _impl->NbPyramids( (SMDSAbs_ElementOrder) order);
02728 }
02729 
02730 CORBA::Long SMESH_Mesh_i::NbPrismsOfOrder(SMESH::ElementOrder order)
02731   throw(SALOME::SALOME_Exception)
02732 {
02733   Unexpect aCatch(SALOME_SalomeException);
02734   return _impl->NbPrisms( (SMDSAbs_ElementOrder) order);
02735 }
02736 
02737 //=============================================================================
02741 //=============================================================================
02742 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
02743 {
02744   Unexpect aCatch(SALOME_SalomeException);
02745   return _mapSubMesh_i.size();
02746 }
02747 
02748 //=============================================================================
02752 //=============================================================================
02753 char* SMESH_Mesh_i::Dump()
02754 {
02755   ostringstream os;
02756   _impl->Dump( os );
02757   return CORBA::string_dup( os.str().c_str() );
02758 }
02759 
02760 //=============================================================================
02764 //=============================================================================
02765 SMESH::long_array* SMESH_Mesh_i::GetIDs()
02766 {
02767 //   SMESH::long_array_var aResult = new SMESH::long_array();
02768 //   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
02769 //   int aMinId = aSMESHDS_Mesh->MinElementID();
02770 //   int aMaxId =  aSMESHDS_Mesh->MaxElementID();
02771 
02772 //   aResult->length(aMaxId - aMinId + 1);
02773 
02774 //   for (int i = 0, id = aMinId; id <= aMaxId; id++  )
02775 //     aResult[i++] = id;
02776 
02777 //   return aResult._retn();
02778   // PAL12398
02779   return GetElementsId();
02780 }
02781 
02782 //=============================================================================
02786 //=============================================================================
02787 
02788 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
02789      throw (SALOME::SALOME_Exception)
02790 {
02791   Unexpect aCatch(SALOME_SalomeException);
02792   MESSAGE("SMESH_Mesh_i::GetElementsId");
02793   SMESH::long_array_var aResult = new SMESH::long_array();
02794   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
02795 
02796   if ( aSMESHDS_Mesh == NULL )
02797     return aResult._retn();
02798 
02799   long nbElements = NbElements();
02800   aResult->length( nbElements );
02801   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
02802   for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
02803     aResult[i] = anIt->next()->GetID();
02804 
02805   return aResult._retn();
02806 }
02807 
02808 
02809 //=============================================================================
02813 //=============================================================================
02814 
02815 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
02816     throw (SALOME::SALOME_Exception)
02817 {
02818   Unexpect aCatch(SALOME_SalomeException);
02819   MESSAGE("SMESH_subMesh_i::GetElementsByType");
02820   SMESH::long_array_var aResult = new SMESH::long_array();
02821   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
02822 
02823   if ( aSMESHDS_Mesh == NULL )
02824     return aResult._retn();
02825 
02826   long nbElements = NbElements();
02827 
02828   // No sense in returning ids of elements along with ids of nodes:
02829   // when theElemType == SMESH::ALL, return node ids only if
02830   // there are no elements
02831   if ( theElemType == SMESH::NODE || (theElemType == SMESH::ALL && nbElements == 0) )
02832     return GetNodesId();
02833 
02834   aResult->length( nbElements );
02835 
02836   int i = 0;
02837 
02838   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
02839   while ( i < nbElements && anIt->more() ) {
02840     const SMDS_MeshElement* anElem = anIt->next();
02841     if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
02842       aResult[i++] = anElem->GetID();
02843   }
02844 
02845   aResult->length( i );
02846 
02847   return aResult._retn();
02848 }
02849 
02850 //=============================================================================
02854 //=============================================================================
02855 
02856 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
02857   throw (SALOME::SALOME_Exception)
02858 {
02859   Unexpect aCatch(SALOME_SalomeException);
02860   MESSAGE("SMESH_subMesh_i::GetNodesId");
02861   SMESH::long_array_var aResult = new SMESH::long_array();
02862   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
02863 
02864   if ( aSMESHDS_Mesh == NULL )
02865     return aResult._retn();
02866 
02867   long nbNodes = NbNodes();
02868   aResult->length( nbNodes );
02869   SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator(/*idInceasingOrder=*/true);
02870   for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
02871     aResult[i] = anIt->next()->GetID();
02872 
02873   return aResult._retn();
02874 }
02875 
02876 //=============================================================================
02880 //=============================================================================
02881 
02882 SMESH::ElementType SMESH_Mesh_i::GetElementType( const CORBA::Long id, const bool iselem )
02883   throw (SALOME::SALOME_Exception)
02884 {
02885   return ( SMESH::ElementType )_impl->GetElementType( id, iselem );
02886 }
02887 
02888 //=============================================================================
02892 //=============================================================================
02893 
02894 SMESH::EntityType SMESH_Mesh_i::GetElementGeomType( const CORBA::Long id )
02895   throw (SALOME::SALOME_Exception)
02896 {
02897   const SMDS_MeshElement* e = _impl->GetMeshDS()->FindElement(id);
02898   if ( !e )
02899     THROW_SALOME_CORBA_EXCEPTION( "invalid element id", SALOME::BAD_PARAM );
02900 
02901   return ( SMESH::EntityType ) e->GetEntityType();
02902 }
02903 
02904 //=============================================================================
02908 //=============================================================================
02909 SMESH::long_array* SMESH_Mesh_i::GetSubMeshElementsId(const CORBA::Long ShapeID)
02910      throw (SALOME::SALOME_Exception)
02911 {
02912   SMESH::long_array_var aResult = new SMESH::long_array();
02913 
02914   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
02915   if(!SM) return aResult._retn();
02916 
02917   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
02918   if(!SDSM) return aResult._retn();
02919 
02920   aResult->length(SDSM->NbElements());
02921 
02922   SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
02923   int i = 0;
02924   while ( eIt->more() ) {
02925     aResult[i++] = eIt->next()->GetID();
02926   }
02927 
02928   return aResult._retn();
02929 }
02930 
02931 
02932 //=============================================================================
02938 //=============================================================================
02939 SMESH::long_array* SMESH_Mesh_i::GetSubMeshNodesId(const CORBA::Long ShapeID, CORBA::Boolean all)
02940      throw (SALOME::SALOME_Exception)
02941 {
02942   SMESH::long_array_var aResult = new SMESH::long_array();
02943 
02944   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
02945   if(!SM) return aResult._retn();
02946 
02947   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
02948   if(!SDSM) return aResult._retn();
02949 
02950   set<int> theElems;
02951   if( !all || (SDSM->NbElements()==0) ) { // internal nodes or vertex submesh
02952     SMDS_NodeIteratorPtr nIt = SDSM->GetNodes();
02953     while ( nIt->more() ) {
02954       const SMDS_MeshNode* elem = nIt->next();
02955       theElems.insert( elem->GetID() );
02956     }
02957   }
02958   else { // all nodes of submesh elements
02959     SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
02960     while ( eIt->more() ) {
02961       const SMDS_MeshElement* anElem = eIt->next();
02962       SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
02963       while ( nIt->more() ) {
02964         const SMDS_MeshElement* elem = nIt->next();
02965         theElems.insert( elem->GetID() );
02966       }
02967     }
02968   }
02969 
02970   aResult->length(theElems.size());
02971   set<int>::iterator itElem;
02972   int i = 0;
02973   for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
02974     aResult[i++] = *itElem;
02975 
02976   return aResult._retn();
02977 }
02978   
02979 
02980 //=============================================================================
02984 //=============================================================================
02985 SMESH::ElementType SMESH_Mesh_i::GetSubMeshElementType(const CORBA::Long ShapeID)
02986      throw (SALOME::SALOME_Exception)
02987 {
02988   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
02989   if(!SM) return SMESH::ALL;
02990 
02991   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
02992   if(!SDSM) return SMESH::ALL;
02993 
02994   if(SDSM->NbElements()==0)
02995     return (SM->GetSubShape().ShapeType() == TopAbs_VERTEX) ? SMESH::NODE : SMESH::ALL;
02996 
02997   SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
02998   const SMDS_MeshElement* anElem = eIt->next();
02999   return ( SMESH::ElementType ) anElem->GetType();
03000 }
03001   
03002 
03003 //=============================================================================
03007 //=============================================================================
03008 
03009 CORBA::LongLong SMESH_Mesh_i::GetMeshPtr()
03010 {
03011   CORBA::LongLong pointeur = CORBA::LongLong(_impl);
03012   if ( MYDEBUG )
03013     MESSAGE("CORBA::LongLong SMESH_Mesh_i::GetMeshPtr() "<<pointeur);
03014   return pointeur;
03015 }
03016 
03017 
03018 //=============================================================================
03023 //=============================================================================
03024 
03025 SMESH::double_array* SMESH_Mesh_i::GetNodeXYZ(const CORBA::Long id)
03026 {
03027   SMESH::double_array_var aResult = new SMESH::double_array();
03028   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03029   if ( aSMESHDS_Mesh == NULL )
03030     return aResult._retn();
03031 
03032   // find node
03033   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
03034   if(!aNode)
03035     return aResult._retn();
03036 
03037   // add coordinates
03038   aResult->length(3);
03039   aResult[0] = aNode->X();
03040   aResult[1] = aNode->Y();
03041   aResult[2] = aNode->Z();
03042   return aResult._retn();
03043 }
03044 
03045 
03046 //=============================================================================
03051 //=============================================================================
03052 
03053 SMESH::long_array* SMESH_Mesh_i::GetNodeInverseElements(const CORBA::Long id)
03054 {
03055   SMESH::long_array_var aResult = new SMESH::long_array();
03056   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03057   if ( aSMESHDS_Mesh == NULL )
03058     return aResult._retn();
03059 
03060   // find node
03061   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
03062   if(!aNode)
03063     return aResult._retn();
03064 
03065   // find inverse elements
03066   SMDS_ElemIteratorPtr eIt = aNode->GetInverseElementIterator();
03067   TColStd_SequenceOfInteger IDs;
03068   while(eIt->more()) {
03069     const SMDS_MeshElement* elem = eIt->next();
03070     IDs.Append(elem->GetID());
03071   }
03072   if(IDs.Length()>0) {
03073     aResult->length(IDs.Length());
03074     int i = 1;
03075     for(; i<=IDs.Length(); i++) {
03076       aResult[i-1] = IDs.Value(i);
03077     }
03078   }
03079   return aResult._retn();
03080 }
03081 
03082 //=============================================================================
03086 //=============================================================================
03087 
03088 SMESH::NodePosition* SMESH_Mesh_i::GetNodePosition(CORBA::Long NodeID)
03089 {
03090   SMESH::NodePosition* aNodePosition = new SMESH::NodePosition();
03091   aNodePosition->shapeID = 0;
03092   aNodePosition->shapeType = GEOM::SHAPE;
03093 
03094   SMESHDS_Mesh* mesh = _impl->GetMeshDS();
03095   if ( !mesh ) return aNodePosition;
03096 
03097   if ( const SMDS_MeshNode* aNode = mesh->FindNode(NodeID) )
03098   {
03099     if ( SMDS_PositionPtr pos = aNode->GetPosition() )
03100     {
03101       aNodePosition->shapeID = aNode->getshapeId();
03102       switch ( pos->GetTypeOfPosition() ) {
03103       case SMDS_TOP_EDGE:
03104         aNodePosition->shapeType = GEOM::EDGE;
03105         aNodePosition->params.length(1);
03106         aNodePosition->params[0] =
03107           static_cast<SMDS_EdgePosition*>( pos )->GetUParameter();
03108         break;
03109       case SMDS_TOP_FACE:
03110         aNodePosition->shapeType = GEOM::FACE;
03111         aNodePosition->params.length(2);
03112         aNodePosition->params[0] =
03113           static_cast<SMDS_FacePosition*>( pos )->GetUParameter();
03114         aNodePosition->params[1] =
03115           static_cast<SMDS_FacePosition*>( pos )->GetVParameter();
03116         break;
03117       case SMDS_TOP_VERTEX:
03118         aNodePosition->shapeType = GEOM::VERTEX;
03119         break;
03120       case SMDS_TOP_3DSPACE:
03121         if ( TopExp_Explorer(_impl->GetShapeToMesh(), TopAbs_SOLID).More() )
03122           aNodePosition->shapeType = GEOM::SOLID;
03123         else if ( TopExp_Explorer(_impl->GetShapeToMesh(), TopAbs_SHELL).More() )
03124           aNodePosition->shapeType = GEOM::SHELL;
03125         break;
03126       default:;
03127       }
03128     }
03129   }
03130   return aNodePosition;
03131 }
03132 
03133 //=============================================================================
03138 //=============================================================================
03139 
03140 CORBA::Long SMESH_Mesh_i::GetShapeID(const CORBA::Long id)
03141 {
03142   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03143   if ( aSMESHDS_Mesh == NULL )
03144     return -1;
03145 
03146   // try to find node
03147   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
03148   if(aNode) {
03149     return aNode->getshapeId();
03150   }
03151 
03152   return -1;
03153 }
03154 
03155 
03156 //=============================================================================
03162 //=============================================================================
03163 
03164 CORBA::Long SMESH_Mesh_i::GetShapeIDForElem(const CORBA::Long id)
03165 {
03166   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03167   if ( aSMESHDS_Mesh == NULL )
03168     return -1;
03169 
03170   // try to find element
03171   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
03172   if(!elem)
03173     return -1;
03174 
03175   //SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
03176   ::SMESH_MeshEditor aMeshEditor(_impl);
03177   int index = aMeshEditor.FindShape( elem );
03178   if(index>0)
03179     return index;
03180 
03181   return -1;
03182 }
03183 
03184 
03185 //=============================================================================
03190 //=============================================================================
03191 
03192 CORBA::Long SMESH_Mesh_i::GetElemNbNodes(const CORBA::Long id)
03193 {
03194   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03195   if ( aSMESHDS_Mesh == NULL ) return -1;
03196   // try to find element
03197   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
03198   if(!elem) return -1;
03199   return elem->NbNodes();
03200 }
03201 
03202 
03203 //=============================================================================
03209 //=============================================================================
03210 
03211 CORBA::Long SMESH_Mesh_i::GetElemNode(const CORBA::Long id, const CORBA::Long index)
03212 {
03213   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03214   if ( aSMESHDS_Mesh == NULL ) return -1;
03215   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
03216   if(!elem) return -1;
03217   if( index>=elem->NbNodes() || index<0 ) return -1;
03218   return elem->GetNode(index)->GetID();
03219 }
03220 
03221 //=============================================================================
03225 //=============================================================================
03226 
03227 SMESH::long_array* SMESH_Mesh_i::GetElemNodes(const CORBA::Long id)
03228 {
03229   SMESH::long_array_var aResult = new SMESH::long_array();
03230   if ( SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS() )
03231   {
03232     if ( const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id) )
03233     {
03234       aResult->length( elem->NbNodes() );
03235       for ( int i = 0; i < elem->NbNodes(); ++i )
03236         aResult[ i ] = elem->GetNode( i )->GetID();
03237     }
03238   }
03239   return aResult._retn();
03240 }
03241 
03242 //=============================================================================
03247 //=============================================================================
03248 
03249 CORBA::Boolean SMESH_Mesh_i::IsMediumNode(const CORBA::Long ide, const CORBA::Long idn)
03250 {
03251   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03252   if ( aSMESHDS_Mesh == NULL ) return false;
03253   // try to find node
03254   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(idn);
03255   if(!aNode) return false;
03256   // try to find element
03257   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(ide);
03258   if(!elem) return false;
03259 
03260   return elem->IsMediumNode(aNode);
03261 }
03262 
03263 
03264 //=============================================================================
03269 //=============================================================================
03270 
03271 CORBA::Boolean SMESH_Mesh_i::IsMediumNodeOfAnyElem(const CORBA::Long idn,
03272                                                    SMESH::ElementType theElemType)
03273 {
03274   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03275   if ( aSMESHDS_Mesh == NULL ) return false;
03276 
03277   // try to find node
03278   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(idn);
03279   if(!aNode) return false;
03280 
03281   SMESH_MesherHelper aHelper( *(_impl) );
03282 
03283   SMDSAbs_ElementType aType;
03284   if(theElemType==SMESH::EDGE) aType = SMDSAbs_Edge;
03285   else if(theElemType==SMESH::FACE) aType = SMDSAbs_Face;
03286   else if(theElemType==SMESH::VOLUME) aType = SMDSAbs_Volume;
03287   else aType = SMDSAbs_All;
03288 
03289   return aHelper.IsMedium(aNode,aType);
03290 }
03291 
03292 
03293 //=============================================================================
03297 //=============================================================================
03298 
03299 CORBA::Long SMESH_Mesh_i::ElemNbEdges(const CORBA::Long id)
03300 {
03301   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03302   if ( aSMESHDS_Mesh == NULL ) return -1;
03303   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
03304   if(!elem) return -1;
03305   return elem->NbEdges();
03306 }
03307 
03308 
03309 //=============================================================================
03313 //=============================================================================
03314 
03315 CORBA::Long SMESH_Mesh_i::ElemNbFaces(const CORBA::Long id)
03316 {
03317   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03318   if ( aSMESHDS_Mesh == NULL ) return -1;
03319   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
03320   if(!elem) return -1;
03321   return elem->NbFaces();
03322 }
03323 
03324 //=======================================================================
03325 //function : GetElemFaceNodes
03326 //purpose  : Returns nodes of given face (counted from zero) for given element.
03327 //=======================================================================
03328 
03329 SMESH::long_array* SMESH_Mesh_i::GetElemFaceNodes(CORBA::Long  elemId,
03330                                                   CORBA::Short faceIndex)
03331 {
03332   SMESH::long_array_var aResult = new SMESH::long_array();
03333   if ( SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS() )
03334   {
03335     if ( const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(elemId) )
03336     {
03337       SMDS_VolumeTool vtool( elem );
03338       if ( faceIndex < vtool.NbFaces() )
03339       {
03340         aResult->length( vtool.NbFaceNodes( faceIndex ));
03341         const SMDS_MeshNode** nn = vtool.GetFaceNodes( faceIndex );
03342         for ( int i = 0; i < aResult->length(); ++i )
03343           aResult[ i ] = nn[ i ]->GetID();
03344       }
03345     }
03346   }
03347   return aResult._retn();
03348 }
03349 
03350 //=======================================================================
03351 //function : FindElementByNodes
03352 //purpose  : Returns an element based on all given nodes.
03353 //=======================================================================
03354 
03355 CORBA::Long SMESH_Mesh_i::FindElementByNodes(const SMESH::long_array& nodes)
03356 {
03357   CORBA::Long elemID(0);
03358   if ( SMESHDS_Mesh* mesh = _impl->GetMeshDS() )
03359   {
03360     vector< const SMDS_MeshNode * > nn( nodes.length() );
03361     for ( int i = 0; i < nodes.length(); ++i )
03362       if ( !( nn[i] = mesh->FindNode( nodes[i] )))
03363         return elemID;
03364 
03365     const SMDS_MeshElement* elem = mesh->FindElement( nn );
03366     if ( !elem && ( _impl->NbEdges( ORDER_QUADRATIC ) ||
03367                     _impl->NbFaces( ORDER_QUADRATIC ) ||
03368                     _impl->NbVolumes( ORDER_QUADRATIC )))
03369       elem = mesh->FindElement( nn, SMDSAbs_All, /*noMedium=*/true );
03370 
03371     if ( elem ) elemID = CORBA::Long( elem->GetID() );
03372   }
03373   return elemID;
03374 }
03375 
03376 //=============================================================================
03380 //=============================================================================
03381 
03382 CORBA::Boolean SMESH_Mesh_i::IsPoly(const CORBA::Long id)
03383 {
03384   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03385   if ( aSMESHDS_Mesh == NULL ) return false;
03386   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
03387   if(!elem) return false;
03388   return elem->IsPoly();
03389 }
03390 
03391 
03392 //=============================================================================
03396 //=============================================================================
03397 
03398 CORBA::Boolean SMESH_Mesh_i::IsQuadratic(const CORBA::Long id)
03399 {
03400   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03401   if ( aSMESHDS_Mesh == NULL ) return false;
03402   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
03403   if(!elem) return false;
03404   return elem->IsQuadratic();
03405 }
03406 
03407 
03408 //=============================================================================
03412 //=============================================================================
03413 
03414 SMESH::double_array* SMESH_Mesh_i::BaryCenter(const CORBA::Long id)
03415 {
03416   SMESH::double_array_var aResult = new SMESH::double_array();
03417   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
03418   if ( aSMESHDS_Mesh == NULL )
03419     return aResult._retn();
03420 
03421   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
03422   if(!elem)
03423     return aResult._retn();
03424 
03425   if(elem->GetType()==SMDSAbs_Volume) {
03426     SMDS_VolumeTool aTool;
03427     if(aTool.Set(elem)) {
03428       aResult->length(3);
03429       if (!aTool.GetBaryCenter( aResult[0], aResult[1], aResult[2]) )
03430         aResult->length(0);
03431     }
03432   }
03433   else {
03434     SMDS_ElemIteratorPtr anIt = elem->nodesIterator();
03435     int nbn = 0;
03436     double x=0., y=0., z=0.;
03437     for(; anIt->more(); ) {
03438       nbn++;
03439       const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(anIt->next());
03440       x += aNode->X();
03441       y += aNode->Y();
03442       z += aNode->Z();
03443     }
03444     if(nbn>0) {
03445       // add coordinates
03446       aResult->length(3);
03447       aResult[0] = x/nbn;
03448       aResult[1] = y/nbn;
03449       aResult[2] = z/nbn;
03450     }
03451   }
03452 
03453   return aResult._retn();
03454 }
03455 
03456 
03457 //=============================================================================
03461 //=============================================================================
03462 
03463 void SMESH_Mesh_i::CreateGroupServants() 
03464 {
03465   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
03466 
03467   set<int> addedIDs;
03468   ::SMESH_Mesh::GroupIteratorPtr groupIt = _impl->GetGroups();
03469   while ( groupIt->more() )
03470   {
03471     ::SMESH_Group* group = groupIt->next();
03472     int            anId = group->GetGroupDS()->GetID();
03473 
03474     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.find(anId);
03475     if ( it != _mapGroups.end() && !CORBA::is_nil( it->second ))
03476       continue;
03477     addedIDs.insert( anId );
03478 
03479     SMESH_GroupBase_i* aGroupImpl;
03480     TopoDS_Shape       shape;
03481     if ( SMESHDS_GroupOnGeom* groupOnGeom =
03482          dynamic_cast<SMESHDS_GroupOnGeom*>( group->GetGroupDS() ))
03483     {
03484       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
03485       shape      = groupOnGeom->GetShape();
03486     }
03487     else {
03488       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
03489     }
03490 
03491     // To ensure correct mapping of servant and correct reference counting in GenericObj_i
03492     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
03493     aGroupImpl->Register();
03494 
03495     SMESH::SMESH_GroupBase_var groupVar =
03496       SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
03497     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( groupVar );
03498 
03499     // register CORBA object for persistence
03500     int nextId = _gen_i->RegisterObject( groupVar );
03501     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
03502 
03503     // publishing the groups in the study
03504     if ( !aStudy->_is_nil() ) {
03505       GEOM::GEOM_Object_var shapeVar = _gen_i->ShapeToGeomObject( shape );
03506       _gen_i->PublishGroup( aStudy, _this(), groupVar, shapeVar, groupVar->GetName());
03507     }
03508   }
03509   if ( !addedIDs.empty() )
03510   {
03511     // python dump
03512     set<int>::iterator id = addedIDs.begin();
03513     for ( ; id != addedIDs.end(); ++id )
03514     {
03515       map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.find(*id);
03516       int i = std::distance( _mapGroups.begin(), it );
03517       TPythonDump() << it->second << " = " << _this() << ".GetGroups()[ "<< i << " ]";
03518     }
03519   }
03520 }
03521 
03522 //=============================================================================
03526 //=============================================================================
03527 
03528 SMESH::ListOfGroups* SMESH_Mesh_i::GetGroups(const list<int>& groupIDs) const
03529 {
03530   int nbGroups = groupIDs.size();
03531   SMESH::ListOfGroups_var aList = new SMESH::ListOfGroups();
03532   aList->length( nbGroups );
03533 
03534   list<int>::const_iterator ids = groupIDs.begin();
03535   for ( nbGroups = 0; ids != groupIDs.end(); ++ids )
03536   {
03537     map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = _mapGroups.find( *ids );
03538     if ( it != _mapGroups.end() && !CORBA::is_nil( it->second ))
03539       aList[nbGroups++] = SMESH::SMESH_GroupBase::_duplicate( it->second );
03540   }
03541   aList->length( nbGroups );
03542   return aList._retn();
03543 }
03544 
03545 //=============================================================================
03549 //=============================================================================
03550 
03551 SALOME_MED::MedFileInfo* SMESH_Mesh_i::GetMEDFileInfo()
03552 {
03553   SALOME_MED::MedFileInfo_var res( myFileInfo );
03554   if ( !res.operator->() ) {
03555     res = new SALOME_MED::MedFileInfo;
03556     res->fileName = "";
03557     res->fileSize = res->major = res->minor = res->release = -1;
03558   }
03559   return res._retn();
03560 }
03561 
03562 //=============================================================================
03566 //=============================================================================
03567 
03568 void SMESH_Mesh_i::checkGroupNames()
03569 {
03570   int nbGrp = NbGroups();
03571   if ( !nbGrp )
03572     return;
03573 
03574   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
03575   if ( aStudy->_is_nil() )
03576     return; // nothing to do
03577   
03578   SMESH::ListOfGroups* grpList = 0;
03579   // avoid dump of "GetGroups"
03580   {
03581     // store python dump into a local variable inside local scope
03582     SMESH::TPythonDump pDump; // do not delete this line of code
03583     grpList = GetGroups();
03584   }
03585 
03586   for ( int gIndx = 0; gIndx < nbGrp; gIndx++ ) {
03587     SMESH::SMESH_GroupBase_ptr aGrp = (*grpList)[ gIndx ];
03588     if ( !aGrp )
03589       continue;
03590     SALOMEDS::SObject_var aGrpSO = _gen_i->ObjectToSObject( aStudy, aGrp );
03591     if ( aGrpSO->_is_nil() )
03592       continue;
03593     // correct name of the mesh group if necessary
03594     const char* guiName = aGrpSO->GetName();
03595     if ( strcmp(guiName, aGrp->GetName()) )
03596       aGrp->SetName( guiName );
03597   }
03598 }
03599 
03600 //=============================================================================
03604 //=============================================================================
03605 void SMESH_Mesh_i::SetParameters(const char* theParameters)
03606 {
03607   SMESH_Gen_i::GetSMESHGen()->UpdateParameters(SMESH::SMESH_Mesh::_narrow(_this()),
03608                                                CORBA::string_dup(theParameters));
03609 }
03610 
03611 //=============================================================================
03615 //=============================================================================
03616 char* SMESH_Mesh_i::GetParameters()
03617 {
03618   SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen();
03619   return CORBA::string_dup(gen->GetParameters(SMESH::SMESH_Mesh::_narrow(_this())));
03620 }
03621 
03622 //=============================================================================
03626 //=============================================================================
03627 SMESH::string_array* SMESH_Mesh_i::GetLastParameters()
03628 {
03629   SMESH::string_array_var aResult = new SMESH::string_array();
03630   SMESH_Gen_i *gen = SMESH_Gen_i::GetSMESHGen();
03631   if(gen) {
03632     char *aParameters = GetParameters();
03633     SALOMEDS::Study_ptr aStudy = gen->GetCurrentStudy();
03634     if(!aStudy->_is_nil()) {
03635       SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters); 
03636       if(aSections->length() > 0) {
03637         SALOMEDS::ListOfStrings aVars = aSections[aSections->length()-1];
03638         aResult->length(aVars.length());
03639         for(int i = 0;i < aVars.length();i++)
03640           aResult[i] = CORBA::string_dup( aVars[i]);
03641       }
03642     }
03643   }
03644   return aResult._retn();
03645 }
03646 
03647 //=======================================================================
03648 //function : GetTypes
03649 //purpose  : Returns types of elements it contains
03650 //=======================================================================
03651 
03652 SMESH::array_of_ElementType* SMESH_Mesh_i::GetTypes()
03653 {
03654   SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
03655 
03656   types->length( 4 );
03657   int nbTypes = 0;
03658   if (_impl->NbEdges())
03659     types[nbTypes++] = SMESH::EDGE;
03660   if (_impl->NbFaces())
03661     types[nbTypes++] = SMESH::FACE;
03662   if (_impl->NbVolumes())
03663     types[nbTypes++] = SMESH::VOLUME;
03664   if (_impl->Nb0DElements())
03665     types[nbTypes++] = SMESH::ELEM0D;
03666   types->length( nbTypes );
03667 
03668   return types._retn();
03669 }
03670 
03671 //=======================================================================
03672 //function : GetMesh
03673 //purpose  : Returns self
03674 //=======================================================================
03675 
03676 SMESH::SMESH_Mesh_ptr SMESH_Mesh_i::GetMesh()
03677 {
03678   return SMESH::SMESH_Mesh::_duplicate( _this() );
03679 }
03680 
03681 //=============================================================================
03685 //=============================================================================
03686 SMESH::long_array* SMESH_Mesh_i::GetMeshInfo()
03687 {
03688   SMESH::long_array_var aRes = new SMESH::long_array();
03689   aRes->length(SMESH::Entity_Last);
03690   for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
03691     aRes[i] = 0;
03692   SMESHDS_Mesh* aMeshDS = _impl->GetMeshDS();
03693   if (!aMeshDS)
03694     return aRes._retn();
03695   const SMDS_MeshInfo& aMeshInfo = aMeshDS->GetMeshInfo();
03696   for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
03697     aRes[i] = aMeshInfo.NbEntities((SMDSAbs_EntityType)i);
03698   return aRes._retn();
03699 }
03700 
03701 //=============================================================================
03705 //=============================================================================
03706 void SMESH_Mesh_i::CollectMeshInfo(const SMDS_ElemIteratorPtr theItr,
03707                                    SMESH::long_array&         theInfo)
03708 {
03709   if (!theItr) return;
03710   while (theItr->more())
03711     theInfo[ theItr->next()->GetEntityType() ]++;
03712 }
03713 
03714 //=============================================================================
03718 //=============================================================================
03719 static TopAbs_ShapeEnum shapeTypeByDim(const int theDim)
03720 {
03721   TopAbs_ShapeEnum aType = TopAbs_SOLID;
03722   switch ( theDim ) {
03723   case 0: aType = TopAbs_VERTEX; break;
03724   case 1: aType = TopAbs_EDGE; break;
03725   case 2: aType = TopAbs_FACE; break;
03726   case 3:
03727   default:aType = TopAbs_SOLID; break;
03728   }
03729   return aType;
03730 }
03731 
03732 //=============================================================================
03741 //=============================================================================
03742 
03743 class SMESH_DimHyp
03744 {
03745  public:
03747   int _dim;    
03748   int _ownDim; 
03749   TopTools_MapOfShape _shapeMap;
03750   SMESH_subMesh*      _subMesh;
03751   list<const SMESHDS_Hypothesis*> _hypothesises; 
03752 
03754   SMESH_DimHyp(const SMESH_subMesh*  theSubMesh,
03755                const int             theDim,
03756                const TopoDS_Shape&   theShape)
03757   {
03758     _subMesh = (SMESH_subMesh*)theSubMesh;
03759     SetShape( theDim, theShape );
03760   }
03761 
03763   void SetShape(const int theDim,
03764                 const TopoDS_Shape& theShape)
03765   {
03766     _dim = theDim;
03767     _ownDim = (int)SMESH_Gen::GetShapeDim(theShape);
03768     if (_dim >= _ownDim)
03769       _shapeMap.Add( theShape );
03770     else {
03771       TopExp_Explorer anExp( theShape, shapeTypeByDim(theDim) );
03772       for( ; anExp.More(); anExp.Next() )
03773         _shapeMap.Add( anExp.Current() );
03774     }
03775   }
03776 
03778   static bool isShareSubShapes(const TopTools_MapOfShape& theToCheck,
03779                                const TopTools_MapOfShape& theToFind,
03780                                const TopAbs_ShapeEnum     theType)
03781   {
03782     bool isShared = false;
03783     TopTools_MapIteratorOfMapOfShape anItr( theToCheck );
03784     for (; !isShared && anItr.More(); anItr.Next() ) {
03785       const TopoDS_Shape aSubSh = anItr.Key();
03786       // check for case when concurrent dimensions are same
03787       isShared = theToFind.Contains( aSubSh );
03788       // check for subshape with concurrent dimension
03789       TopExp_Explorer anExp( aSubSh, theType );
03790       for ( ; !isShared && anExp.More(); anExp.Next() )
03791         isShared = theToFind.Contains( anExp.Current() );
03792     }
03793     return isShared;
03794   }
03795   
03797   static bool checkAlgo(const SMESHDS_Hypothesis* theA1,
03798                         const SMESHDS_Hypothesis* theA2)
03799   {
03800     if ( theA1->GetType() == SMESHDS_Hypothesis::PARAM_ALGO ||
03801          theA2->GetType() == SMESHDS_Hypothesis::PARAM_ALGO )
03802       return false; // one of the hypothesis is not algorithm
03803     // check algorithm names (should be equal)
03804     return strcmp( theA1->GetName(), theA2->GetName() ) == 0;
03805   }
03806 
03807   
03809   bool IsConcurrent(const SMESH_DimHyp* theOther) const
03810   {
03811     if ( _subMesh == theOther->_subMesh )
03812       return false; // same subshape - should not be
03813 
03814     // if ( <own dim of either of submeshes> == <concurrent dim> &&
03815     //      any of the two submeshes is not on COMPOUND shape )
03816     //  -> no concurrency
03817     bool meIsCompound = (_subMesh->GetSubMeshDS() && _subMesh->GetSubMeshDS()->IsComplexSubmesh());
03818     bool otherIsCompound = (theOther->_subMesh->GetSubMeshDS() && theOther->_subMesh->GetSubMeshDS()->IsComplexSubmesh());
03819     if ( (_ownDim == _dim  || theOther->_ownDim == _dim ) && (!meIsCompound || !otherIsCompound))
03820       return false;
03821 
03822 //     bool checkSubShape = ( _dim >= theOther->_dim )
03823 //       ? isShareSubShapes( _shapeMap, theOther->_shapeMap, shapeTypeByDim(theOther->_dim) )
03824 //       : isShareSubShapes( theOther->_shapeMap, _shapeMap, shapeTypeByDim(_dim) ) ;
03825     bool checkSubShape = isShareSubShapes( _shapeMap, theOther->_shapeMap, shapeTypeByDim(_dim));
03826     if ( !checkSubShape )
03827         return false;
03828 
03829     // check algorithms to be same
03830     if (!checkAlgo( _hypothesises.front(), theOther->_hypothesises.front() ))
03831       return true; // different algorithms
03832     
03833     // check hypothesises for concurrence (skip first as algorithm)
03834     int nbSame = 0;
03835     // pointers should be same, becase it is referenes from mesh hypothesis partition
03836     list <const SMESHDS_Hypothesis*>::const_iterator hypIt = _hypothesises.begin();
03837     list <const SMESHDS_Hypothesis*>::const_iterator otheEndIt = theOther->_hypothesises.end();
03838     for ( hypIt++ /*skip first as algo*/; hypIt != _hypothesises.end(); hypIt++ )
03839       if ( find( theOther->_hypothesises.begin(), otheEndIt, *hypIt ) != otheEndIt )
03840         nbSame++;
03841     // the submeshes are concurrent if their algorithms has different parameters
03842     return nbSame != theOther->_hypothesises.size() - 1;
03843   }
03844   
03845 }; // end of SMESH_DimHyp
03846 
03847 typedef list<SMESH_DimHyp*> TDimHypList;
03848 
03849 static void addDimHypInstance(const int               theDim, 
03850                               const TopoDS_Shape&     theShape,
03851                               const SMESH_Algo*       theAlgo,
03852                               const SMESH_subMesh*    theSubMesh,
03853                               const list <const SMESHDS_Hypothesis*>& theHypList,
03854                               TDimHypList*            theDimHypListArr )
03855 {
03856   TDimHypList& listOfdimHyp = theDimHypListArr[theDim];
03857   if ( listOfdimHyp.empty() || listOfdimHyp.back()->_subMesh != theSubMesh ) {
03858     SMESH_DimHyp* dimHyp = new SMESH_DimHyp( theSubMesh, theDim, theShape );
03859     listOfdimHyp.push_back( dimHyp );
03860   }
03861   
03862   SMESH_DimHyp* dimHyp = listOfdimHyp.back();
03863   dimHyp->_hypothesises.push_front(theAlgo);
03864   list <const SMESHDS_Hypothesis*>::const_iterator hypIt = theHypList.begin();
03865   for( ; hypIt != theHypList.end(); hypIt++ )
03866     dimHyp->_hypothesises.push_back( *hypIt );
03867 }
03868 
03869 static void findConcurrents(const SMESH_DimHyp* theDimHyp,
03870                             const TDimHypList&  theListOfDimHyp,
03871                             TListOfInt&         theListOfConcurr )
03872 {
03873   TDimHypList::const_reverse_iterator rIt = theListOfDimHyp.rbegin();
03874   for ( ; rIt != theListOfDimHyp.rend(); rIt++ ) {
03875     const SMESH_DimHyp* curDimHyp = *rIt;
03876     if ( curDimHyp == theDimHyp )
03877       break; // meet own dimHyp pointer in same dimension
03878     else if ( theDimHyp->IsConcurrent( curDimHyp ) )
03879       if ( find( theListOfConcurr.begin(),
03880                  theListOfConcurr.end(),
03881                  curDimHyp->_subMesh->GetId() ) == theListOfConcurr.end() )
03882         theListOfConcurr.push_back( curDimHyp->_subMesh->GetId() );
03883   }
03884 }
03885 
03886 static void unionLists(TListOfInt&       theListOfId,
03887                        TListOfListOfInt& theListOfListOfId,
03888                        const int         theIndx )
03889 {
03890   TListOfListOfInt::iterator it = theListOfListOfId.begin();
03891   for ( int i = 0; it != theListOfListOfId.end(); it++, i++ ) {
03892     if ( i < theIndx )
03893       continue; //skip already treated lists
03894     // check if other list has any same submesh object
03895     TListOfInt& otherListOfId = *it;
03896     if ( find_first_of( theListOfId.begin(), theListOfId.end(),
03897                         otherListOfId.begin(), otherListOfId.end() ) == theListOfId.end() )
03898       continue;
03899          
03900     // union two lists (from source into target)
03901     TListOfInt::iterator it2 = otherListOfId.begin();
03902     for ( ; it2 != otherListOfId.end(); it2++ ) {
03903       if ( find( theListOfId.begin(), theListOfId.end(), (*it2) ) == theListOfId.end() )
03904         theListOfId.push_back(*it2);
03905     }
03906     // clear source list
03907     otherListOfId.clear();
03908   }
03909 }
03910 
03912 static void removeDimHyps( TDimHypList* theArrOfList )
03913 {
03914   for (int i = 0; i < 4; i++ ) {
03915     TDimHypList& listOfdimHyp = theArrOfList[i];
03916     TDimHypList::const_iterator it = listOfdimHyp.begin();
03917     for ( ; it != listOfdimHyp.end(); it++ )
03918       delete (*it);
03919   }
03920 }
03921 
03922 //=============================================================================
03926 //=============================================================================
03927 
03928 SMESH::submesh_array_array* SMESH_Mesh_i::GetMeshOrder()
03929 {
03930   SMESH::submesh_array_array_var aResult = new SMESH::submesh_array_array();
03931 
03932   SMESHDS_Mesh* aMeshDS = _impl->GetMeshDS();
03933   if ( !aMeshDS )
03934     return aResult._retn();
03935   
03936   ::SMESH_Mesh& mesh = GetImpl();
03937   TListOfListOfInt anOrder = mesh.GetMeshOrder(); // is there already defined order?
03938   if ( !anOrder.size() ) {
03939 
03940     // collect submeshes detecting concurrent algorithms and hypothesises
03941     TDimHypList dimHypListArr[4]; // dimHyp list for each shape dimension
03942     
03943     map<int, ::SMESH_subMesh*>::iterator i_sm = _mapSubMesh.begin();
03944     for ( ; i_sm != _mapSubMesh.end(); i_sm++ ) {
03945       ::SMESH_subMesh* sm = (*i_sm).second;
03946       // shape of submesh
03947       const TopoDS_Shape& aSubMeshShape = sm->GetSubShape();
03948       
03949       // list of assigned hypothesises
03950       const list <const SMESHDS_Hypothesis*>& hypList = mesh.GetHypothesisList(aSubMeshShape);
03951       // Find out dimensions where the submesh can be concurrent.
03952       // We define the dimensions by algo of each of hypotheses in hypList
03953       list <const SMESHDS_Hypothesis*>::const_iterator hypIt = hypList.begin();
03954       for( ; hypIt != hypList.end(); hypIt++ ) {
03955         SMESH_Algo* anAlgo = 0;
03956         const SMESH_Hypothesis* hyp = dynamic_cast<const SMESH_Hypothesis*>(*hypIt);
03957         if ( hyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO )
03958           // hyp it-self is algo
03959           anAlgo = (SMESH_Algo*)dynamic_cast<const SMESH_Algo*>(hyp);
03960         else {
03961           // try to find algorithm with help of subshapes
03962           TopExp_Explorer anExp( aSubMeshShape, shapeTypeByDim(hyp->GetDim()) );
03963           for ( ; !anAlgo && anExp.More(); anExp.Next() )
03964             anAlgo = mesh.GetGen()->GetAlgo( mesh, anExp.Current() );
03965         }
03966         if (!anAlgo)
03967           continue; // no assigned algorithm to current submesh
03968 
03969         int dim = anAlgo->GetDim(); // top concurrent dimension (see comment to SMESH_DimHyp)
03970         // the submesh can concurrent at <dim> (or lower dims if !anAlgo->NeedDescretBoundary())
03971 
03972         // create instance of dimension-hypothesis for found concurrent dimension(s) and algorithm
03973         for ( int j = anAlgo->NeedDescretBoundary() ? dim : 1, jn = dim; j <= jn; j++ )
03974           addDimHypInstance( j, aSubMeshShape, anAlgo, sm, hypList, dimHypListArr );
03975       }
03976     } // end iterations on submesh
03977     
03978     // iterate on created dimension-hypotheses and check for concurrents
03979     for ( int i = 0; i < 4; i++ ) {
03980       const list<SMESH_DimHyp*>& listOfDimHyp = dimHypListArr[i];
03981       // check for concurrents in own and other dimensions (step-by-step)
03982       TDimHypList::const_iterator dhIt = listOfDimHyp.begin();
03983       for ( ; dhIt != listOfDimHyp.end(); dhIt++ ) {
03984         const SMESH_DimHyp* dimHyp = *dhIt;
03985         TListOfInt listOfConcurr;
03986         // looking for concurrents and collect into own list
03987         for ( int j = i; j < 4; j++ )
03988           findConcurrents( dimHyp, dimHypListArr[j], listOfConcurr );
03989         // check if any concurrents found
03990         if ( listOfConcurr.size() > 0 ) {
03991           // add own submesh to list of concurrent
03992           listOfConcurr.push_front( dimHyp->_subMesh->GetId() );
03993           anOrder.push_back( listOfConcurr );
03994         }
03995       }
03996     }
03997     
03998     removeDimHyps(dimHypListArr);
03999     
04000     // now, minimise the number of concurrent groups
04001     // Here we assume that lists of submhes can has same submesh
04002     // in case of multi-dimension algorithms, as result
04003     //  list with common submesh have to be union into one list
04004     int listIndx = 0;
04005     TListOfListOfInt::iterator listIt = anOrder.begin();
04006     for(; listIt != anOrder.end(); listIt++, listIndx++ )
04007       unionLists( *listIt,  anOrder, listIndx + 1 );
04008   }
04009   // convert submesh ids into interface instances
04010   //  and dump command into python
04011   convertMeshOrder( anOrder, aResult, true );
04012 
04013   return aResult._retn();
04014 }
04015 
04016 //=============================================================================
04023 //=============================================================================
04024 
04025 static void findCommonSubMesh (list<const SMESH_subMesh*>& theSubMeshList,
04026                                const SMESH_subMesh*        theSubMesh,
04027                                set<const SMESH_subMesh*>&  theCommon )
04028 {
04029   if ( !theSubMesh )
04030     return;
04031   list<const SMESH_subMesh*>::const_iterator it = theSubMeshList.begin();
04032   for ( ; it != theSubMeshList.end(); it++ )
04033     theSubMesh->FindIntersection( *it, theCommon );
04034   theSubMeshList.push_back( theSubMesh );
04035   //theCommon.insert( theSubMesh );
04036 }
04037 
04038 //=============================================================================
04043 //=============================================================================
04044 
04045 ::CORBA::Boolean SMESH_Mesh_i::SetMeshOrder(const SMESH::submesh_array_array& theSubMeshArray)
04046 {
04047   bool res = false;
04048   ::SMESH_Mesh& mesh = GetImpl();
04049 
04050   TPythonDump aPythonDump; // prevent dump of called methods
04051   aPythonDump << "isDone = " << _this() << ".SetMeshOrder( [ ";
04052 
04053   TListOfListOfInt subMeshOrder;
04054   for ( int i = 0, n = theSubMeshArray.length(); i < n; i++ )
04055   {
04056     const SMESH::submesh_array& aSMArray = theSubMeshArray[i];
04057     TListOfInt subMeshIds;
04058     aPythonDump << "[ ";
04059     // Collect subMeshes which should be clear
04060     //  do it list-by-list, because modification of submesh order
04061     //  take effect between concurrent submeshes only
04062     set<const SMESH_subMesh*> subMeshToClear;
04063     list<const SMESH_subMesh*> subMeshList;
04064     for ( int j = 0, jn = aSMArray.length(); j < jn; j++ )
04065     {
04066       const SMESH::SMESH_subMesh_var subMesh = SMESH::SMESH_subMesh::_duplicate(aSMArray[j]);
04067       if ( j > 0 )
04068         aPythonDump << ", ";
04069       aPythonDump << subMesh;
04070       subMeshIds.push_back( subMesh->GetId() );
04071       // detect common parts of submeshes
04072       if ( _mapSubMesh.find(subMesh->GetId()) != _mapSubMesh.end() )
04073         findCommonSubMesh( subMeshList, _mapSubMesh[ subMesh->GetId() ], subMeshToClear );
04074     }
04075     aPythonDump << " ]";
04076     subMeshOrder.push_back( subMeshIds );
04077 
04078     // clear collected submeshes
04079     set<const SMESH_subMesh*>::iterator clrIt = subMeshToClear.begin();
04080     for ( ; clrIt != subMeshToClear.end(); clrIt++ ) {
04081       SMESH_subMesh* sm = (SMESH_subMesh*)*clrIt;
04082         if ( sm )
04083           sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
04084         // ClearSubMesh( *clrIt );
04085       }
04086   }
04087   aPythonDump << " ])";
04088 
04089   mesh.SetMeshOrder( subMeshOrder );
04090   res = true;
04091   
04092   return res;
04093 }
04094 
04095 //=============================================================================
04099 //=============================================================================
04100 
04101 void SMESH_Mesh_i::convertMeshOrder
04102 (const TListOfListOfInt& theIdsOrder,
04103  SMESH::submesh_array_array& theResOrder,
04104  const bool theIsDump)
04105 {
04106   int nbSet = theIdsOrder.size();
04107   TPythonDump aPythonDump; // prevent dump of called methods
04108   if ( theIsDump )
04109     aPythonDump << "[ ";
04110   theResOrder.length(nbSet);
04111   TListOfListOfInt::const_iterator it = theIdsOrder.begin();
04112   int listIndx = 0;
04113   for( ; it != theIdsOrder.end(); it++ ) {
04114     // translate submesh identificators into submesh objects
04115     //  takeing into account real number of concurrent lists
04116     const TListOfInt& aSubOrder = (*it);
04117     if (!aSubOrder.size())
04118       continue;
04119     if ( theIsDump )
04120       aPythonDump << "[ ";
04121     // convert shape indeces into interfaces
04122     SMESH::submesh_array_var aResSubSet = new SMESH::submesh_array();
04123     aResSubSet->length(aSubOrder.size());
04124     TListOfInt::const_iterator subIt = aSubOrder.begin();
04125     for( int j = 0; subIt != aSubOrder.end(); subIt++ ) {
04126       if ( _mapSubMeshIor.find(*subIt) == _mapSubMeshIor.end() )
04127         continue;
04128       SMESH::SMESH_subMesh_var subMesh =
04129         SMESH::SMESH_subMesh::_duplicate( _mapSubMeshIor[*subIt] );
04130       if ( theIsDump ) {
04131         if ( j > 0 )
04132           aPythonDump << ", ";
04133         aPythonDump << subMesh;
04134       }
04135       aResSubSet[ j++ ] = subMesh;
04136     }
04137     if ( theIsDump )
04138       aPythonDump << " ]";
04139     theResOrder[ listIndx++ ] = aResSubSet;
04140   }
04141   // correct number of lists
04142   theResOrder.length( listIndx );
04143 
04144   if ( theIsDump ) {
04145     // finilise python dump
04146     aPythonDump << " ]";
04147     aPythonDump << " = " << _this() << ".GetMeshOrder()";
04148   }
04149 }
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS