]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESH_I/SMESH_Mesh_i.cxx
Salome HOME
0020279: [CEA 334] control the "random" use when using mesh algorithms
[modules/smesh.git] / src / SMESH_I / SMESH_Mesh_i.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
23 //  File   : SMESH_Mesh_i.cxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : SMESH
26 //
27 #include "SMESH_Mesh_i.hxx"
28
29 #include "SMESH_Filter_i.hxx"
30 #include "SMESH_Gen_i.hxx"
31 #include "SMESH_Group_i.hxx"
32 #include "SMESH_MEDMesh_i.hxx"
33 #include "SMESH_MeshEditor_i.hxx"
34 #include "SMESH_PythonDump.hxx"
35 #include "SMESH_subMesh_i.hxx"
36
37 #include "DriverMED_R_SMESHDS_Mesh.h"
38 #include "DriverMED_W_SMESHDS_Mesh.h"
39 #include "SMDS_VolumeTool.hxx"
40 #include "SMESHDS_Command.hxx"
41 #include "SMESHDS_CommandType.hxx"
42 #include "SMESHDS_GroupOnGeom.hxx"
43 #include "SMESH_Group.hxx"
44 #include "SMESH_MeshEditor.hxx"
45 #include "SMESH_MesherHelper.hxx"
46 #include "SMDS_EdgePosition.hxx"
47 #include "SMDS_FacePosition.hxx"
48
49 #include "OpUtil.hxx"
50 #include "SALOME_NamingService.hxx"
51 #include "Utils_CorbaException.hxx"
52 #include "Utils_ExceptHandlers.hxx"
53 #include "Utils_SINGLETON.hxx"
54 #include "utilities.h"
55 #include "GEOMImpl_Types.hxx"
56
57 // OCCT Includes
58 #include <BRep_Builder.hxx>
59 #include <OSD_Directory.hxx>
60 #include <OSD_File.hxx>
61 #include <OSD_Path.hxx>
62 #include <OSD_Protection.hxx>
63 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
64 #include <TColStd_MapOfInteger.hxx>
65 #include <TColStd_SequenceOfInteger.hxx>
66 #include <TCollection_AsciiString.hxx>
67 #include <TopExp_Explorer.hxx>
68 #include <TopoDS_Compound.hxx>
69
70 // STL Includes
71 #include <string>
72 #include <iostream>
73 #include <sstream>
74 #include <sys/stat.h>
75
76 #ifdef _DEBUG_
77 static int MYDEBUG = 0;
78 #else
79 static int MYDEBUG = 0;
80 #endif
81
82 using namespace std;
83 using SMESH::TPythonDump;
84
85 int SMESH_Mesh_i::myIdGenerator = 0;
86
87
88
89 //=============================================================================
90 /*!
91  *  Constructor
92  */
93 //=============================================================================
94
95 SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
96                             SMESH_Gen_i*            gen_i,
97                             CORBA::Long studyId )
98 : SALOME::GenericObj_i( thePOA )
99 {
100   MESSAGE("SMESH_Mesh_i");
101   _impl = NULL;
102   _gen_i = gen_i;
103   _id = myIdGenerator++;
104   _studyId = studyId;
105 }
106
107 //=============================================================================
108 /*!
109  *  Destructor
110  */
111 //=============================================================================
112
113 SMESH_Mesh_i::~SMESH_Mesh_i()
114 {
115   INFOS("~SMESH_Mesh_i");
116   map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
117   for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
118     SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
119     if ( aGroup ) {
120       // this method is colled from destructor of group (PAL6331)
121       //_impl->RemoveGroup( aGroup->GetLocalID() );
122       
123       aGroup->Destroy();
124     }
125   }
126   _mapGroups.clear();
127 }
128
129 //=============================================================================
130 /*!
131  *  SetShape
132  *
133  *  Associates <this> mesh with <theShape> and puts a reference
134  *  to <theShape> into the current study;
135  *  the previous shape is substituted by the new one.
136  */
137 //=============================================================================
138
139 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
140     throw (SALOME::SALOME_Exception)
141 {
142   Unexpect aCatch(SALOME_SalomeException);
143   try {
144     _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
145   }
146   catch(SALOME_Exception & S_ex) {
147     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
148   }
149   // to track changes of GEOM groups
150   addGeomGroupData( theShapeObject, _this() );
151 }
152
153 //================================================================================
154 /*!
155  * \brief return true if mesh has a shape to build a shape on
156  */
157 //================================================================================
158
159 CORBA::Boolean SMESH_Mesh_i::HasShapeToMesh()
160   throw (SALOME::SALOME_Exception)
161 {
162   Unexpect aCatch(SALOME_SalomeException);
163   bool res = false;
164   try {
165     res = _impl->HasShapeToMesh();
166   }
167   catch(SALOME_Exception & S_ex) {
168     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
169   }
170   return res;
171 }
172
173 //=======================================================================
174 //function : GetShapeToMesh
175 //purpose  :
176 //=======================================================================
177
178 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
179   throw (SALOME::SALOME_Exception)
180 {
181   Unexpect aCatch(SALOME_SalomeException);
182   GEOM::GEOM_Object_var aShapeObj;
183   try {
184     TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
185     if ( !S.IsNull() )
186       aShapeObj = _gen_i->ShapeToGeomObject( S );
187   }
188   catch(SALOME_Exception & S_ex) {
189     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
190   }
191   return aShapeObj._retn();
192 }
193
194 //================================================================================
195 /*!
196  * \brief Remove all nodes and elements
197  */
198 //================================================================================
199
200 void SMESH_Mesh_i::Clear() throw (SALOME::SALOME_Exception)
201 {
202   Unexpect aCatch(SALOME_SalomeException);
203   try {
204     _impl->Clear();
205   }
206   catch(SALOME_Exception & S_ex) {
207     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
208   }
209   TPythonDump() <<  _this() << ".Clear()";
210 }
211
212 //=============================================================================
213 /*!
214  *
215  */
216 //=============================================================================
217
218 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
219 {
220   SMESH::DriverMED_ReadStatus res;
221   switch (theStatus)
222   {
223   case DriverMED_R_SMESHDS_Mesh::DRS_OK:
224     res = SMESH::DRS_OK; break;
225   case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
226     res = SMESH::DRS_EMPTY; break;
227   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
228     res = SMESH::DRS_WARN_RENUMBER; break;
229   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
230     res = SMESH::DRS_WARN_SKIP_ELEM; break;
231   case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
232   default:
233     res = SMESH::DRS_FAIL; break;
234   }
235   return res;
236 }
237
238 //=============================================================================
239 /*!
240  *  ImportMEDFile
241  *
242  *  Imports mesh data from MED file
243  */
244 //=============================================================================
245
246 SMESH::DriverMED_ReadStatus
247 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
248   throw ( SALOME::SALOME_Exception )
249 {
250   Unexpect aCatch(SALOME_SalomeException);
251   int status;
252   try {
253     status = _impl->MEDToMesh( theFileName, theMeshName );
254   }
255   catch( SALOME_Exception& S_ex ) {
256     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
257   }
258   catch ( ... ) {
259     THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
260   }
261
262   CreateGroupServants();
263
264   int major, minor, release;
265   if( !MED::getMEDVersion( theFileName, major, minor, release ) )
266     major = minor = release = -1;
267   myFileInfo           = new SALOME_MED::MedFileInfo();
268   myFileInfo->fileName = theFileName;
269   myFileInfo->fileSize = 0;
270 #ifdef WIN32
271   struct _stati64 d;
272   if ( ::_stati64( theFileName, &d ) != -1 )
273 #else
274   struct stat64 d;
275   if ( ::stat64( theFileName, &d ) != -1 )
276 #endif
277     myFileInfo->fileSize = d.st_size;
278   myFileInfo->major    = major;
279   myFileInfo->minor    = minor;
280   myFileInfo->release  = release;
281
282   return ConvertDriverMEDReadStatus(status);
283 }
284
285 //================================================================================
286 /*!
287  * \brief Return string representation of a MED file version comprising nbDigits
288  */
289 //================================================================================
290
291 char* SMESH_Mesh_i::GetVersionString(SMESH::MED_VERSION version, CORBA::Short nbDigits)
292 {
293   std::string ver = DriverMED_W_SMESHDS_Mesh::GetVersionString(MED::EVersion(version),
294                                                                nbDigits);
295   return CORBA::string_dup( ver.c_str() );
296 }
297
298 //=============================================================================
299 /*!
300  *  ImportUNVFile
301  *
302  *  Imports mesh data from MED file
303  */
304 //=============================================================================
305
306 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
307   throw ( SALOME::SALOME_Exception )
308 {
309   // Read mesh with name = <theMeshName> into SMESH_Mesh
310   _impl->UNVToMesh( theFileName );
311
312   CreateGroupServants();
313
314   return 1;
315 }
316
317 //=============================================================================
318 /*!
319  *  ImportSTLFile
320  *
321  *  Imports mesh data from STL file
322  */
323 //=============================================================================
324 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
325   throw ( SALOME::SALOME_Exception )
326 {
327   // Read mesh with name = <theMeshName> into SMESH_Mesh
328   _impl->STLToMesh( theFileName );
329
330   return 1;
331 }
332
333 //=============================================================================
334 /*!
335  *  importMEDFile
336  *
337  *  Imports mesh data from MED file
338  */
339 //=============================================================================
340
341 // int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
342 // {
343 //   // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
344 //   int status = _impl->MEDToMesh( theFileName, theMeshName );
345 //   CreateGroupServants();
346
347 //   return status;
348 // }
349
350 //=============================================================================
351 /*!
352  *
353  */
354 //=============================================================================
355
356 #define RETURNCASE(hyp_stat) case SMESH_Hypothesis::hyp_stat: return SMESH::hyp_stat;
357
358 SMESH::Hypothesis_Status SMESH_Mesh_i::ConvertHypothesisStatus
359                          (SMESH_Hypothesis::Hypothesis_Status theStatus)
360 {
361   switch (theStatus) {
362   RETURNCASE( HYP_OK            );
363   RETURNCASE( HYP_MISSING       );
364   RETURNCASE( HYP_CONCURENT     );
365   RETURNCASE( HYP_BAD_PARAMETER );
366   RETURNCASE( HYP_HIDDEN_ALGO   );
367   RETURNCASE( HYP_HIDING_ALGO   );
368   RETURNCASE( HYP_UNKNOWN_FATAL );
369   RETURNCASE( HYP_INCOMPATIBLE  );
370   RETURNCASE( HYP_NOTCONFORM    );
371   RETURNCASE( HYP_ALREADY_EXIST );
372   RETURNCASE( HYP_BAD_DIM       );
373   RETURNCASE( HYP_BAD_SUBSHAPE  );
374   RETURNCASE( HYP_BAD_GEOMETRY  );
375   RETURNCASE( HYP_NEED_SHAPE    );
376   default:;
377   }
378   return SMESH::HYP_UNKNOWN_FATAL;
379 }
380
381 //=============================================================================
382 /*!
383  *  AddHypothesis
384  *
385  *  calls internal addHypothesis() and then adds a reference to <anHyp> under
386  *  the SObject actually having a reference to <aSubShape>.
387  *  NB: For this method to work, it is necessary to add a reference to sub-shape first.
388  */
389 //=============================================================================
390
391 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
392                                                      SMESH::SMESH_Hypothesis_ptr anHyp)
393   throw(SALOME::SALOME_Exception)
394 {
395   Unexpect aCatch(SALOME_SalomeException);
396   SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
397
398   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
399     _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
400                                  aSubShapeObject, anHyp );
401
402   if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
403
404   // Update Python script
405   if(_impl->HasShapeToMesh()) {
406     TPythonDump() << "status = " << _this() << ".AddHypothesis( "
407                   << aSubShapeObject << ", " << anHyp << " )";
408   }
409   else {
410     TPythonDump() << "status = " << _this() << ".AddHypothesis( "<< anHyp << " )";
411   }
412   
413   return ConvertHypothesisStatus(status);
414 }
415
416 //=============================================================================
417 /*!
418  *
419  */
420 //=============================================================================
421
422 SMESH_Hypothesis::Hypothesis_Status
423   SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr       aSubShapeObject,
424                               SMESH::SMESH_Hypothesis_ptr anHyp)
425 {
426   if(MYDEBUG) MESSAGE("addHypothesis");
427
428   if (CORBA::is_nil(aSubShapeObject) && HasShapeToMesh())
429     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
430                                  SALOME::BAD_PARAM);
431
432   SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
433   if (CORBA::is_nil(myHyp))
434     THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
435                                  SALOME::BAD_PARAM);
436
437   SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
438   try
439   {
440     TopoDS_Shape myLocSubShape;
441     //use PseudoShape in case if mesh has no shape
442     if(HasShapeToMesh())
443       myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
444     else              
445       myLocSubShape = _impl->GetShapeToMesh();
446     
447     int hypId = myHyp->GetId();
448     status = _impl->AddHypothesis(myLocSubShape, hypId);
449     if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
450       _mapHypo[hypId] = SMESH::SMESH_Hypothesis::_duplicate( myHyp );
451       // assure there is a corresponding submesh
452       if ( !_impl->IsMainShape( myLocSubShape )) {
453         int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
454         if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
455           createSubMesh( aSubShapeObject );
456       }
457     }
458   }
459   catch(SALOME_Exception & S_ex)
460   {
461     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
462   }
463   return status;
464 }
465
466 //=============================================================================
467 /*!
468  *
469  */
470 //=============================================================================
471
472 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
473                                                         SMESH::SMESH_Hypothesis_ptr anHyp)
474      throw(SALOME::SALOME_Exception)
475 {
476   Unexpect aCatch(SALOME_SalomeException);
477   SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
478
479   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
480     _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
481                                       aSubShapeObject, anHyp );
482
483   // Update Python script
484     // Update Python script
485   if(_impl->HasShapeToMesh()) {
486   TPythonDump() << "status = " << _this() << ".RemoveHypothesis( "
487                 << aSubShapeObject << ", " << anHyp << " )";
488   }
489   else {
490     TPythonDump() << "status = " << _this() << ".RemoveHypothesis( "
491                   << anHyp << " )";
492   }
493
494   return ConvertHypothesisStatus(status);
495 }
496
497 //=============================================================================
498 /*!
499  *
500  */
501 //=============================================================================
502
503 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
504                                  SMESH::SMESH_Hypothesis_ptr anHyp)
505 {
506         if(MYDEBUG) MESSAGE("removeHypothesis()");
507         // **** proposer liste de subShape (selection multiple)
508
509         if (CORBA::is_nil(aSubShapeObject) && HasShapeToMesh())
510                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
511                         SALOME::BAD_PARAM);
512
513         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
514         if (CORBA::is_nil(myHyp))
515           THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
516                         SALOME::BAD_PARAM);
517
518         SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
519         try
520         {
521                 TopoDS_Shape myLocSubShape;
522                 //use PseudoShape in case if mesh has no shape
523                 if(HasShapeToMesh())
524                   myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
525                 else
526                   myLocSubShape = _impl->GetShapeToMesh();
527                 
528                 int hypId = myHyp->GetId();
529                 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
530                 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
531                   _mapHypo.erase( hypId );
532         }
533         catch(SALOME_Exception & S_ex)
534         {
535                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
536         }
537         return status;
538 }
539
540 //=============================================================================
541 /*!
542  *
543  */
544 //=============================================================================
545
546 SMESH::ListOfHypothesis *
547         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
548 throw(SALOME::SALOME_Exception)
549 {
550   Unexpect aCatch(SALOME_SalomeException);
551   if (MYDEBUG) MESSAGE("GetHypothesisList");
552   if (_impl->HasShapeToMesh() && CORBA::is_nil(aSubShapeObject))
553     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
554                                  SALOME::BAD_PARAM);
555
556   SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
557
558   try {
559     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
560     if ( myLocSubShape.IsNull() && !_impl->HasShapeToMesh() )
561       myLocSubShape = _impl->GetShapeToMesh();
562     const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
563     int i = 0, n = aLocalList.size();
564     aList->length( n );
565
566     for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
567       SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
568       if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
569         aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
570     }
571
572     aList->length( i );
573   }
574   catch(SALOME_Exception & S_ex) {
575     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
576   }
577
578   return aList._retn();
579 }
580
581 //=============================================================================
582 /*!
583  *
584  */
585 //=============================================================================
586 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
587                                                   const char*           theName )
588      throw(SALOME::SALOME_Exception)
589 {
590   Unexpect aCatch(SALOME_SalomeException);
591   MESSAGE("SMESH_Mesh_i::GetSubMesh");
592   if (CORBA::is_nil(aSubShapeObject))
593     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
594                                  SALOME::BAD_PARAM);
595
596   SMESH::SMESH_subMesh_var subMesh;
597   SMESH::SMESH_Mesh_var    aMesh = SMESH::SMESH_Mesh::_narrow(_this());
598   try {
599     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
600
601     //Get or Create the SMESH_subMesh object implementation
602
603     int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
604     subMesh = getSubMesh( subMeshId );
605
606     // create a new subMesh object servant if there is none for the shape
607     if ( subMesh->_is_nil() )
608       subMesh = createSubMesh( aSubShapeObject );
609     if ( _gen_i->CanPublishInStudy( subMesh )) {
610       SALOMEDS::SObject_var aSO =
611         _gen_i->PublishSubMesh(_gen_i->GetCurrentStudy(), aMesh,
612                                subMesh, aSubShapeObject, theName );
613       if ( !aSO->_is_nil()) {
614         // Update Python script
615         TPythonDump() << aSO << " = " << _this() << ".GetSubMesh( "
616                       << aSubShapeObject << ", '" << theName << "' )";
617       }
618     }
619   }
620   catch(SALOME_Exception & S_ex) {
621     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
622   }
623   return subMesh._retn();
624 }
625
626 //=============================================================================
627 /*!
628  *
629  */
630 //=============================================================================
631
632 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
633      throw (SALOME::SALOME_Exception)
634 {
635   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
636   if ( theSubMesh->_is_nil() )
637     return;
638
639   GEOM::GEOM_Object_var aSubShapeObject;
640   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
641   if ( !aStudy->_is_nil() )  {
642     // Remove submesh's SObject
643     SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
644     if ( !anSO->_is_nil() ) {
645       long aTag = SMESH_Gen_i::GetRefOnShapeTag();
646       SALOMEDS::SObject_var anObj, aRef;
647       if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
648         aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
649
650       aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
651
652       // Update Python script
653       TPythonDump() << _this() << ".RemoveSubMesh( " << anSO << " )";
654     }
655   }
656
657   removeSubMesh( theSubMesh, aSubShapeObject.in() );
658 }
659
660 //=============================================================================
661 /*!
662  *  ElementTypeString
663  */
664 //=============================================================================
665 #define CASE2STRING(enum) case SMESH::enum: return "SMESH."#enum;
666 inline TCollection_AsciiString ElementTypeString (SMESH::ElementType theElemType)
667 {
668   switch (theElemType) {
669     CASE2STRING( ALL );
670     CASE2STRING( NODE );
671     CASE2STRING( EDGE );
672     CASE2STRING( FACE );
673     CASE2STRING( VOLUME );
674   default:;
675   }
676   return "";
677 }
678
679 //=============================================================================
680 /*!
681  *
682  */
683 //=============================================================================
684
685 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
686                                                  const char*         theName )
687      throw(SALOME::SALOME_Exception)
688 {
689   Unexpect aCatch(SALOME_SalomeException);
690   SMESH::SMESH_Group_var aNewGroup =
691     SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
692
693   if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
694     SALOMEDS::SObject_var aSO =
695       _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
696                            aNewGroup, GEOM::GEOM_Object::_nil(), theName);
697     if ( !aSO->_is_nil()) {
698       // Update Python script
699       TPythonDump() << aSO << " = " << _this() << ".CreateGroup( "
700                     << ElementTypeString(theElemType) << ", '" << theName << "' )";
701     }
702   }
703   return aNewGroup._retn();
704 }
705
706
707 //=============================================================================
708 /*!
709  *
710  */
711 //=============================================================================
712 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM (SMESH::ElementType    theElemType,
713                                                                 const char*           theName,
714                                                                 GEOM::GEOM_Object_ptr theGeomObj)
715      throw(SALOME::SALOME_Exception)
716 {
717   Unexpect aCatch(SALOME_SalomeException);
718   SMESH::SMESH_GroupOnGeom_var aNewGroup;
719
720   TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
721   if ( !aShape.IsNull() )
722   {
723     aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
724       ( createGroup( theElemType, theName, aShape ));
725
726     if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
727       SALOMEDS::SObject_var aSO =
728         _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
729                              aNewGroup, theGeomObj, theName);
730       if ( !aSO->_is_nil()) {
731         // Update Python script
732         TPythonDump() << aSO << " = " << _this() << ".CreateGroupFromGEOM("
733                       << ElementTypeString(theElemType) << ", '" << theName << "', "
734                       << theGeomObj << " )";
735       }
736     }
737   }
738
739   return aNewGroup._retn();
740 }
741
742 //=============================================================================
743 /*!
744  *
745  */
746 //=============================================================================
747
748 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
749      throw (SALOME::SALOME_Exception)
750 {
751   if ( theGroup->_is_nil() )
752     return;
753
754   SMESH_GroupBase_i* aGroup =
755     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
756   if ( !aGroup )
757     return;
758
759   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
760   if ( !aStudy->_is_nil() )  {
761     SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
762
763     if ( !aGroupSO->_is_nil() ) {
764       // Update Python script
765       TPythonDump() << _this() << ".RemoveGroup( " << aGroupSO << " )";
766
767       // Remove group's SObject
768       aStudy->NewBuilder()->RemoveObjectWithChildren( aGroupSO );
769     }
770   }
771
772   // Remove the group from SMESH data structures
773   removeGroup( aGroup->GetLocalID() );
774 }
775
776 //=============================================================================
777 /*! RemoveGroupWithContents
778  *  Remove group with its contents
779  */
780 //=============================================================================
781 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
782   throw (SALOME::SALOME_Exception)
783 {
784   if ( theGroup->_is_nil() )
785     return;
786
787   SMESH_GroupBase_i* aGroup =
788     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
789   if ( !aGroup )
790     return;
791
792   SMESH::long_array_var anIds = aGroup->GetListOfID();
793   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
794
795   // Update Python script
796   TPythonDump() << _this() << ".RemoveGroupWithContents( " << theGroup << " )";
797
798   // Remove contents
799   if ( aGroup->GetType() == SMESH::NODE )
800     aMeshEditor->RemoveNodes( anIds );
801   else
802     aMeshEditor->RemoveElements( anIds );
803
804   // Remove group
805   RemoveGroup( theGroup );
806
807   // Clear python lines, created by RemoveNodes/Elements() and RemoveGroup()
808   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
809   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
810 }
811
812
813 //================================================================================
814 /*!
815  * \brief Get the list of groups existing in the mesh
816   * \retval SMESH::ListOfGroups * - list of groups
817  */
818 //================================================================================
819
820 SMESH::ListOfGroups * SMESH_Mesh_i::GetGroups() throw(SALOME::SALOME_Exception)
821 {
822   Unexpect aCatch(SALOME_SalomeException);
823   if (MYDEBUG) MESSAGE("GetGroups");
824
825   SMESH::ListOfGroups_var aList = new SMESH::ListOfGroups();
826
827   // Python Dump
828   TPythonDump aPythonDump;
829   if ( !_mapGroups.empty() ) // (IMP13463) avoid "SyntaxError: can't assign to []"
830     aPythonDump << "[ ";
831
832   try {
833     aList->length( _mapGroups.size() );
834     int i = 0;
835     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
836     for ( ; it != _mapGroups.end(); it++ ) {
837       if ( CORBA::is_nil( it->second )) continue;
838       aList[i++] = SMESH::SMESH_GroupBase::_duplicate( it->second );
839       // Python Dump
840       if (i > 1) aPythonDump << ", ";
841       aPythonDump << it->second;
842     }
843     aList->length( i );
844   }
845   catch(SALOME_Exception & S_ex) {
846     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
847   }
848
849   // Update Python script
850   if ( !_mapGroups.empty() ) // (IMP13463) avoid "SyntaxError: can't assign to []"
851     aPythonDump << " ] = " << _this() << ".GetGroups()";
852
853   return aList._retn();
854 }
855 //=============================================================================
856 /*!
857  *  Get number of groups existing in the mesh
858  */
859 //=============================================================================
860
861 CORBA::Long SMESH_Mesh_i::NbGroups() throw (SALOME::SALOME_Exception)
862 {
863   Unexpect aCatch(SALOME_SalomeException);
864   return _mapGroups.size();
865 }
866
867 //=============================================================================
868 /*! UnionGroups
869  *  New group is created. All mesh elements that are
870  *  present in initial groups are added to the new one
871  */
872 //=============================================================================
873 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
874                                                   SMESH::SMESH_GroupBase_ptr theGroup2,
875                                                   const char* theName )
876   throw (SALOME::SALOME_Exception)
877 {
878   try
879   {
880     if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
881          theGroup1->GetType() != theGroup2->GetType() )
882       return SMESH::SMESH_Group::_nil();
883
884     // Create Union
885     SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
886     if ( aResGrp->_is_nil() )
887       return SMESH::SMESH_Group::_nil();
888
889     SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
890     SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
891
892     TColStd_MapOfInteger aResMap;
893
894     for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
895       aResMap.Add( anIds1[ i1 ] );
896
897     for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
898       aResMap.Add( anIds2[ i2 ] );
899
900     SMESH::long_array_var aResIds = new SMESH::long_array;
901     aResIds->length( aResMap.Extent() );
902
903     int resI = 0;
904     TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
905     for( ; anIter.More(); anIter.Next() )
906       aResIds[ resI++ ] = anIter.Key();
907
908     aResGrp->Add( aResIds );
909
910     // Clear python lines, created by CreateGroup() and Add()
911     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
912     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
913     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
914
915     // Update Python script
916     TPythonDump() << aResGrp << " = " << _this() << ".UnionGroups( "
917                   << theGroup1 << ", " << theGroup2 << ", '"
918                   << theName << "' )";
919
920     return aResGrp._retn();
921   }
922   catch( ... )
923   {
924     return SMESH::SMESH_Group::_nil();
925   }
926 }
927
928 //=============================================================================
929 /*! IntersectGroups
930  *  New group is created. All mesh elements that are
931  *  present in both initial groups are added to the new one.
932  */
933 //=============================================================================
934 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
935                                                       SMESH::SMESH_GroupBase_ptr theGroup2,
936                                                       const char* theName )
937   throw (SALOME::SALOME_Exception)
938 {
939   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
940        theGroup1->GetType() != theGroup2->GetType() )
941     return SMESH::SMESH_Group::_nil();
942
943   // Create Intersection
944   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
945   if ( aResGrp->_is_nil() )
946     return aResGrp;
947
948   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
949   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
950
951   TColStd_MapOfInteger aMap1;
952
953   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
954     aMap1.Add( anIds1[ i1 ] );
955
956   TColStd_SequenceOfInteger aSeq;
957
958   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
959     if ( aMap1.Contains( anIds2[ i2 ] ) )
960       aSeq.Append( anIds2[ i2 ] );
961
962   SMESH::long_array_var aResIds = new SMESH::long_array;
963   aResIds->length( aSeq.Length() );
964
965   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
966     aResIds[ resI ] = aSeq( resI + 1 );
967
968   aResGrp->Add( aResIds );
969
970   // Clear python lines, created by CreateGroup() and Add()
971   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
972   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
973   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
974
975   // Update Python script
976   TPythonDump() << aResGrp << " = " << _this() << ".IntersectGroups( "
977                 << theGroup1 << ", " << theGroup2 << ", '" << theName << "')";
978
979   return aResGrp._retn();
980 }
981
982 //=============================================================================
983 /*! CutGroups
984  *  New group is created. All mesh elements that are present in
985  *  main group but do not present in tool group are added to the new one
986  */
987 //=============================================================================
988 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
989                                                 SMESH::SMESH_GroupBase_ptr theGroup2,
990                                                 const char* theName )
991   throw (SALOME::SALOME_Exception)
992 {
993   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
994        theGroup1->GetType() != theGroup2->GetType() )
995     return SMESH::SMESH_Group::_nil();
996
997   // Perform Cutting
998   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
999   if ( aResGrp->_is_nil() )
1000     return aResGrp;
1001
1002   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
1003   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
1004
1005   TColStd_MapOfInteger aMap2;
1006
1007   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
1008     aMap2.Add( anIds2[ i2 ] );
1009
1010   TColStd_SequenceOfInteger aSeq;
1011   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
1012     if ( !aMap2.Contains( anIds1[ i1 ] ) )
1013       aSeq.Append( anIds1[ i1 ] );
1014
1015   SMESH::long_array_var aResIds = new SMESH::long_array;
1016   aResIds->length( aSeq.Length() );
1017
1018   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
1019     aResIds[ resI ] = aSeq( resI + 1 );
1020
1021   aResGrp->Add( aResIds );
1022
1023   // Clear python lines, created by CreateGroup() and Add()
1024   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1025   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
1026   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
1027
1028   // Update Python script
1029   TPythonDump() << aResGrp << " = " << _this() << ".CutGroups( "
1030                 << theGroup1 << ", " << theGroup2 << ", '"
1031                 << theName << "' )";
1032
1033   return aResGrp._retn();
1034 }
1035
1036 //================================================================================
1037 /*!
1038  * \brief Remember GEOM group data
1039  */
1040 //================================================================================
1041
1042 void SMESH_Mesh_i::addGeomGroupData(GEOM::GEOM_Object_ptr theGeomObj,
1043                                     CORBA::Object_ptr     theSmeshObj)
1044 {
1045   if ( CORBA::is_nil( theGeomObj ) || theGeomObj->GetType() != GEOM_GROUP )
1046     return;
1047   // group SO
1048   SALOMEDS::Study_var   study  = _gen_i->GetCurrentStudy();
1049   SALOMEDS::SObject_var groupSO = _gen_i->ObjectToSObject( study, theGeomObj );
1050   if ( groupSO->_is_nil() )
1051     return;
1052   // group indices
1053   GEOM::GEOM_Gen_var geomGen = _gen_i->GetGeomEngine();
1054   GEOM::GEOM_IGroupOperations_var groupOp =
1055     geomGen->GetIGroupOperations( _gen_i->GetCurrentStudyID() );
1056   GEOM::ListOfLong_var ids = groupOp->GetObjects( theGeomObj );
1057
1058   // store data
1059   _geomGroupData.push_back( TGeomGroupData() );
1060   TGeomGroupData & groupData = _geomGroupData.back();
1061   // entry
1062   CORBA::String_var entry = groupSO->GetID();
1063   groupData._groupEntry = entry.in();
1064   // indices
1065   for ( int i = 0; i < ids->length(); ++i )
1066     groupData._indices.insert( ids[i] );
1067   // SMESH object
1068   groupData._smeshObject = theSmeshObj;
1069 }
1070
1071 //================================================================================
1072 /*!
1073  * Remove GEOM group data relating to removed smesh object
1074  */
1075 //================================================================================
1076
1077 void SMESH_Mesh_i::removeGeomGroupData(CORBA::Object_ptr theSmeshObj)
1078 {
1079   list<TGeomGroupData>::iterator
1080     data = _geomGroupData.begin(), dataEnd = _geomGroupData.end();
1081   for ( ; data != dataEnd; ++data ) {
1082     if ( theSmeshObj->_is_equivalent( data->_smeshObject )) {
1083       _geomGroupData.erase( data );
1084       return;
1085     }
1086   }
1087 }
1088
1089 //================================================================================
1090 /*!
1091  * \brief Return new group contents if it has been changed and update group data
1092  */
1093 //================================================================================
1094
1095 TopoDS_Shape SMESH_Mesh_i::newGroupShape( TGeomGroupData & groupData)
1096 {
1097   TopoDS_Shape newShape;
1098
1099   // get geom group
1100   SALOMEDS::Study_var study = _gen_i->GetCurrentStudy();
1101   if ( study->_is_nil() ) return newShape; // means "not changed"
1102   SALOMEDS::SObject_var groupSO = study->FindObjectID( groupData._groupEntry.c_str() );
1103   if ( !groupSO->_is_nil() )
1104   {
1105     CORBA::Object_var groupObj = _gen_i->SObjectToObject( groupSO );
1106     if ( CORBA::is_nil( groupObj )) return newShape;
1107     GEOM::GEOM_Object_var geomGroup = GEOM::GEOM_Object::_narrow( groupObj );
1108     
1109     // get indices of group items
1110     set<int> curIndices;
1111     GEOM::GEOM_Gen_var geomGen = _gen_i->GetGeomEngine();
1112     GEOM::GEOM_IGroupOperations_var groupOp =
1113       geomGen->GetIGroupOperations( _gen_i->GetCurrentStudyID() );
1114     GEOM::ListOfLong_var ids = groupOp->GetObjects( geomGroup );
1115     for ( int i = 0; i < ids->length(); ++i )
1116       curIndices.insert( ids[i] );
1117
1118     if ( groupData._indices == curIndices )
1119       return newShape; // group not changed
1120
1121     // update data
1122     groupData._indices = curIndices;
1123
1124     GEOM_Client* geomClient = _gen_i->GetShapeReader();
1125     if ( !geomClient ) return newShape;
1126     TCollection_AsciiString groupIOR = geomGen->GetStringFromIOR( geomGroup );
1127     geomClient->RemoveShapeFromBuffer( groupIOR );
1128     newShape = _gen_i->GeomObjectToShape( geomGroup );
1129   }    
1130
1131   if ( newShape.IsNull() ) {
1132     // geom group becomes empty - return empty compound
1133     TopoDS_Compound compound;
1134     BRep_Builder    builder;
1135     builder.MakeCompound(compound);
1136     newShape = compound;
1137   }
1138   return newShape;
1139 }
1140
1141 //=============================================================================
1142 /*!
1143  * \brief Update objects depending on changed geom groups
1144  * 
1145  * NPAL16168: geometrical group edition from a submesh don't modifiy mesh computation
1146  * issue 0020210: Update of a smesh group after modification of the associated geom group
1147  */
1148 //=============================================================================
1149
1150 void SMESH_Mesh_i::CheckGeomGroupModif()
1151 {
1152   if ( !_impl->HasShapeToMesh() ) return;
1153
1154   SALOMEDS::Study_var study = _gen_i->GetCurrentStudy();
1155   if ( study->_is_nil() ) return;
1156
1157   CORBA::Long nbEntities = NbNodes() + NbElements();
1158
1159   // Check if group contents changed
1160
1161   typedef map< string, TopoDS_Shape > TEntry2Geom;
1162   TEntry2Geom newGroupContents;
1163
1164   list<TGeomGroupData>::iterator
1165     data = _geomGroupData.begin(), dataEnd = _geomGroupData.end();
1166   for ( ; data != dataEnd; ++data )
1167   {
1168     pair< TEntry2Geom::iterator, bool > it_new =
1169       newGroupContents.insert( make_pair( data->_groupEntry, TopoDS_Shape() ));
1170     bool processedGroup    = !it_new.second;
1171     TopoDS_Shape& newShape = it_new.first->second;
1172     if ( !processedGroup )
1173       newShape = newGroupShape( *data );
1174     if ( newShape.IsNull() )
1175       continue; // no changes
1176
1177     if ( processedGroup ) { // update group indices
1178       list<TGeomGroupData>::iterator data2 = data;
1179       for ( --data2; data2->_groupEntry != data->_groupEntry; --data2)
1180         data->_indices = data2->_indices;
1181     }
1182
1183     // Update SMESH objects according to new GEOM group contents
1184
1185     SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( data->_smeshObject );
1186     if ( !mesh->_is_nil() ) // -------------- MESH ----------------------------
1187     {
1188       // TODO
1189       continue;
1190     }
1191
1192     SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow( data->_smeshObject );
1193     if ( !submesh->_is_nil() ) // -------------- Sub mesh ---------------------
1194     {
1195       int oldID = submesh->GetId();
1196       if ( _mapSubMeshIor.find( oldID ) == _mapSubMeshIor.end() )
1197         continue;
1198       TopoDS_Shape oldShape = _mapSubMesh[oldID]->GetSubShape();
1199
1200       // update hypotheses
1201       list <const SMESHDS_Hypothesis * > hyps = _impl->GetHypothesisList(oldShape);
1202       list <const SMESHDS_Hypothesis * >::iterator hypIt;
1203       for ( hypIt = hyps.begin(); hypIt != hyps.end(); ++hypIt )
1204       {
1205         _impl->RemoveHypothesis( oldShape, (*hypIt)->GetID());
1206         _impl->AddHypothesis   ( newShape, (*hypIt)->GetID());
1207       }
1208       // care of submeshes
1209       SMESH_subMesh* newSubmesh = _impl->GetSubMesh( newShape );
1210       int newID = newSubmesh->GetId();
1211       if ( newID != oldID ) {
1212         _mapSubMesh   [ newID ] = newSubmesh;
1213         _mapSubMesh_i [ newID ] = _mapSubMesh_i [ oldID ];
1214         _mapSubMeshIor[ newID ] = _mapSubMeshIor[ oldID ];
1215         _mapSubMesh.   erase(oldID);
1216         _mapSubMesh_i. erase(oldID);
1217         _mapSubMeshIor.erase(oldID);
1218         _mapSubMesh_i [ newID ]->changeLocalId( newID );
1219       }
1220       continue;
1221     }
1222
1223     SMESH::SMESH_GroupOnGeom_var smeshGroup =
1224       SMESH::SMESH_GroupOnGeom::_narrow( data->_smeshObject );
1225     if ( !smeshGroup->_is_nil() ) // ------------ GROUP -----------------------
1226     {
1227       SMESH_GroupOnGeom_i* group_i = SMESH::DownCast<SMESH_GroupOnGeom_i*>( smeshGroup );
1228       if ( group_i ) {
1229         ::SMESH_Group* group = _impl->GetGroup( group_i->GetLocalID() );
1230         SMESHDS_GroupOnGeom* ds = static_cast<SMESHDS_GroupOnGeom*>( group->GetGroupDS() );
1231         ds->SetShape( newShape );
1232       }
1233       continue;
1234     }
1235   }
1236
1237   // Update icons
1238
1239   CORBA::Long newNbEntities = NbNodes() + NbElements();
1240   list< SALOMEDS::SObject_var > soToUpdateIcons;
1241   if ( newNbEntities != nbEntities )
1242   {
1243     // Add all SObjects with icons
1244     soToUpdateIcons.push_back( _gen_i->ObjectToSObject( study, _this() )); // mesh
1245
1246     for (map<int, SMESH::SMESH_subMesh_ptr>::iterator i_sm = _mapSubMeshIor.begin();
1247          i_sm != _mapSubMeshIor.end(); ++i_sm ) // submeshes
1248       soToUpdateIcons.push_back( _gen_i->ObjectToSObject( study, i_sm->second ));
1249
1250     for ( map<int, SMESH::SMESH_GroupBase_ptr>::iterator i_gr = _mapGroups.begin();
1251           i_gr != _mapGroups.end(); ++i_gr ) // groups
1252       soToUpdateIcons.push_back( _gen_i->ObjectToSObject( study, i_gr->second ));
1253   }
1254
1255   list< SALOMEDS::SObject_var >::iterator so = soToUpdateIcons.begin();
1256   for ( ; so != soToUpdateIcons.end(); ++so )
1257     _gen_i->SetPixMap( *so, "ICON_SMESH_TREE_MESH_WARN" );
1258 }
1259
1260 //=============================================================================
1261 /*!
1262  *
1263  */
1264 //=============================================================================
1265
1266 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
1267 {
1268   if(MYDEBUG) MESSAGE( "createSubMesh" );
1269   TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
1270
1271   ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
1272   int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
1273   SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
1274   SMESH::SMESH_subMesh_var subMesh
1275     = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
1276
1277   _mapSubMesh[subMeshId] = mySubMesh;
1278   _mapSubMesh_i[subMeshId] = subMeshServant;
1279   _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
1280
1281   // register CORBA object for persistence
1282   int nextId = _gen_i->RegisterObject( subMesh );
1283   if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
1284
1285   // to track changes of GEOM groups
1286   addGeomGroupData( theSubShapeObject, subMesh );
1287
1288   return subMesh._retn();
1289 }
1290
1291 //=======================================================================
1292 //function : getSubMesh
1293 //purpose  :
1294 //=======================================================================
1295
1296 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
1297 {
1298   map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
1299   if ( it == _mapSubMeshIor.end() )
1300     return SMESH::SMESH_subMesh::_nil();
1301
1302   return SMESH::SMESH_subMesh::_duplicate( (*it).second );
1303 }
1304
1305
1306 //=============================================================================
1307 /*!
1308  *
1309  */
1310 //=============================================================================
1311
1312 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
1313                                   GEOM::GEOM_Object_ptr    theSubShapeObject )
1314 {
1315   MESSAGE("SMESH_Mesh_i::removeSubMesh()");
1316   if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
1317     return;
1318
1319   try {
1320     SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
1321     for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
1322       removeHypothesis( theSubShapeObject, aHypList[i] );
1323     }
1324   }
1325   catch( const SALOME::SALOME_Exception& ) {
1326     INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
1327   }
1328   removeGeomGroupData( theSubShapeObject );
1329
1330   int subMeshId = theSubMesh->GetId();
1331
1332   _mapSubMesh.erase(subMeshId);
1333   _mapSubMesh_i.erase(subMeshId);
1334   _mapSubMeshIor.erase(subMeshId);
1335   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
1336 }
1337
1338 //=============================================================================
1339 /*!
1340  *
1341  */
1342 //=============================================================================
1343
1344 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
1345                                                       const char*         theName,
1346                                                       const TopoDS_Shape& theShape )
1347 {
1348   int anId;
1349   SMESH::SMESH_GroupBase_var aGroup;
1350   if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
1351     SMESH_GroupBase_i* aGroupImpl;
1352     if ( !theShape.IsNull() )
1353       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
1354     else
1355       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
1356
1357     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
1358     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
1359     aGroupImpl->Register();
1360     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
1361
1362     aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
1363     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
1364
1365     // register CORBA object for persistence
1366     int nextId = _gen_i->RegisterObject( aGroup );
1367     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
1368
1369     // to track changes of GEOM groups
1370     if ( !theShape.IsNull() ) {
1371       GEOM::GEOM_Object_var geom = _gen_i->ShapeToGeomObject( theShape );
1372       addGeomGroupData( geom, aGroup );
1373     }
1374   }
1375   return aGroup._retn();
1376 }
1377
1378 //=============================================================================
1379 /*!
1380  * SMESH_Mesh_i::removeGroup
1381  *
1382  * Should be called by ~SMESH_Group_i()
1383  */
1384 //=============================================================================
1385
1386 void SMESH_Mesh_i::removeGroup( const int theId )
1387 {
1388   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );
1389   if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
1390     removeGeomGroupData( _mapGroups[theId] );
1391     _mapGroups.erase( theId );
1392     _impl->RemoveGroup( theId );
1393   }
1394 }
1395
1396
1397 //=============================================================================
1398 /*!
1399  *
1400  */
1401 //=============================================================================
1402
1403 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
1404 throw(SALOME::SALOME_Exception)
1405 {
1406   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
1407
1408   SMESH::log_array_var aLog;
1409   try{
1410     list < SMESHDS_Command * >logDS = _impl->GetLog();
1411     aLog = new SMESH::log_array;
1412     int indexLog = 0;
1413     int lg = logDS.size();
1414     SCRUTE(lg);
1415     aLog->length(lg);
1416     list < SMESHDS_Command * >::iterator its = logDS.begin();
1417     while(its != logDS.end()){
1418       SMESHDS_Command *com = *its;
1419       int comType = com->GetType();
1420       //SCRUTE(comType);
1421       int lgcom = com->GetNumber();
1422       //SCRUTE(lgcom);
1423       const list < int >&intList = com->GetIndexes();
1424       int inum = intList.size();
1425       //SCRUTE(inum);
1426       list < int >::const_iterator ii = intList.begin();
1427       const list < double >&coordList = com->GetCoords();
1428       int rnum = coordList.size();
1429       //SCRUTE(rnum);
1430       list < double >::const_iterator ir = coordList.begin();
1431       aLog[indexLog].commandType = comType;
1432       aLog[indexLog].number = lgcom;
1433       aLog[indexLog].coords.length(rnum);
1434       aLog[indexLog].indexes.length(inum);
1435       for(int i = 0; i < rnum; i++){
1436         aLog[indexLog].coords[i] = *ir;
1437         //MESSAGE(" "<<i<<" "<<ir.Value());
1438         ir++;
1439       }
1440       for(int i = 0; i < inum; i++){
1441         aLog[indexLog].indexes[i] = *ii;
1442         //MESSAGE(" "<<i<<" "<<ii.Value());
1443         ii++;
1444       }
1445       indexLog++;
1446       its++;
1447     }
1448     if(clearAfterGet)
1449       _impl->ClearLog();
1450   }
1451   catch(SALOME_Exception & S_ex){
1452     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1453   }
1454   return aLog._retn();
1455 }
1456
1457
1458 //=============================================================================
1459 /*!
1460  *
1461  */
1462 //=============================================================================
1463
1464 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
1465 {
1466   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
1467   // ****
1468 }
1469
1470 //=============================================================================
1471 /*!
1472  *
1473  */
1474 //=============================================================================
1475
1476 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1477 {
1478   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1479   return _id;
1480 }
1481
1482 //=============================================================================
1483 /*!
1484  *
1485  */
1486 //=============================================================================
1487
1488 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1489 {
1490   return _studyId;
1491 }
1492
1493 //=============================================================================
1494 /*!
1495  *
1496  */
1497 //=============================================================================
1498
1499 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1500 {
1501   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1502   _impl = impl;
1503 }
1504
1505 //=============================================================================
1506 /*!
1507  *
1508  */
1509 //=============================================================================
1510
1511 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1512 {
1513   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1514   return *_impl;
1515 }
1516
1517 //=============================================================================
1518 /*!
1519  * Return mesh editor
1520  */
1521 //=============================================================================
1522
1523 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1524 {
1525   // Create MeshEditor
1526   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( this, false );
1527   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1528
1529   // Update Python script
1530   TPythonDump() << aMeshEditor << " = " << _this() << ".GetMeshEditor()";
1531
1532   return aMesh._retn();
1533 }
1534
1535 //=============================================================================
1536 /*!
1537  * Return mesh edition previewer
1538  */
1539 //=============================================================================
1540
1541 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditPreviewer()
1542 {
1543   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( this, true );
1544   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1545   return aMesh._retn();
1546 }
1547
1548 //=============================================================================
1549 /*!
1550  *
1551  */
1552 //=============================================================================
1553 void SMESH_Mesh_i::SetAutoColor(CORBA::Boolean theAutoColor) throw(SALOME::SALOME_Exception)
1554 {
1555   Unexpect aCatch(SALOME_SalomeException);
1556   _impl->SetAutoColor(theAutoColor);
1557 }
1558
1559 //=============================================================================
1560 /*!
1561  *
1562  */
1563 //=============================================================================
1564 CORBA::Boolean SMESH_Mesh_i::GetAutoColor() throw(SALOME::SALOME_Exception)
1565 {
1566   Unexpect aCatch(SALOME_SalomeException);
1567   return _impl->GetAutoColor();
1568 }
1569
1570
1571 //=============================================================================
1572 /*!
1573  *  Export in different formats
1574  */
1575 //=============================================================================
1576
1577 CORBA::Boolean SMESH_Mesh_i::HasDuplicatedGroupNamesMED()
1578 {
1579   return _impl->HasDuplicatedGroupNamesMED();
1580 }
1581
1582 void SMESH_Mesh_i::PrepareForWriting (const char* file)
1583 {
1584   TCollection_AsciiString aFullName ((char*)file);
1585   OSD_Path aPath (aFullName);
1586   OSD_File aFile (aPath);
1587   if (aFile.Exists()) {
1588     // existing filesystem node
1589     if (aFile.KindOfFile() == OSD_FILE) {
1590       if (aFile.IsWriteable()) {
1591         aFile.Reset();
1592         aFile.Remove();
1593         if (aFile.Failed()) {
1594           TCollection_AsciiString msg ("File ");
1595           msg += aFullName + " cannot be replaced.";
1596           THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1597         }
1598       } else {
1599         TCollection_AsciiString msg ("File ");
1600         msg += aFullName + " cannot be overwritten.";
1601         THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1602       }
1603     } else {
1604       TCollection_AsciiString msg ("Location ");
1605       msg += aFullName + " is not a file.";
1606       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1607     }
1608   } else {
1609     // nonexisting file; check if it can be created
1610     aFile.Reset();
1611     aFile.Build(OSD_WriteOnly, OSD_Protection());
1612     if (aFile.Failed()) {
1613       TCollection_AsciiString msg ("You cannot create the file ");
1614       msg += aFullName + ". Check the directory existance and access rights.";
1615       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1616     } else {
1617       aFile.Close();
1618       aFile.Remove();
1619     }
1620   }
1621 }
1622
1623 void SMESH_Mesh_i::ExportToMED (const char* file,
1624                                 CORBA::Boolean auto_groups,
1625                                 SMESH::MED_VERSION theVersion)
1626   throw(SALOME::SALOME_Exception)
1627 {
1628   Unexpect aCatch(SALOME_SalomeException);
1629
1630   // Update Python script
1631   TPythonDump() << _this() << ".ExportToMED( '"
1632                 << file << "', " << auto_groups << ", " << theVersion << " )";
1633
1634   // Perform Export
1635   PrepareForWriting(file);
1636   char* aMeshName = "Mesh";
1637   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1638   if ( !aStudy->_is_nil() ) {
1639     SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1640     if ( !aMeshSO->_is_nil() ) {
1641       aMeshName = aMeshSO->GetName();
1642       //SCRUTE(file);
1643       //SCRUTE(aMeshName);
1644       //SCRUTE(aMeshSO->GetID());
1645
1646       // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes
1647       if ( !aStudy->GetProperties()->IsLocked() )
1648         {
1649         SALOMEDS::GenericAttribute_var anAttr;
1650         SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1651         SALOMEDS::AttributeExternalFileDef_var aFileName;
1652         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1653         aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1654         ASSERT(!aFileName->_is_nil());
1655         aFileName->SetValue(file);
1656         SALOMEDS::AttributeFileType_var aFileType;
1657         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1658         aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1659         ASSERT(!aFileType->_is_nil());
1660         aFileType->SetValue("FICHIERMED");
1661         }
1662     }
1663   }
1664   _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1665 }
1666
1667 void SMESH_Mesh_i::ExportMED (const char* file,
1668                               CORBA::Boolean auto_groups)
1669   throw(SALOME::SALOME_Exception)
1670 {
1671   ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1672 }
1673
1674 void SMESH_Mesh_i::ExportDAT (const char *file)
1675   throw(SALOME::SALOME_Exception)
1676 {
1677   Unexpect aCatch(SALOME_SalomeException);
1678
1679   // Update Python script
1680   TPythonDump() << _this() << ".ExportDAT( '" << file << "' )";
1681
1682   // Perform Export
1683   PrepareForWriting(file);
1684   _impl->ExportDAT(file);
1685 }
1686
1687 void SMESH_Mesh_i::ExportUNV (const char *file)
1688   throw(SALOME::SALOME_Exception)
1689 {
1690   Unexpect aCatch(SALOME_SalomeException);
1691
1692   // Update Python script
1693   TPythonDump() << _this() << ".ExportUNV( '" << file << "' )";
1694
1695   // Perform Export
1696   PrepareForWriting(file);
1697   _impl->ExportUNV(file);
1698 }
1699
1700 void SMESH_Mesh_i::ExportSTL (const char *file, const bool isascii)
1701   throw(SALOME::SALOME_Exception)
1702 {
1703   Unexpect aCatch(SALOME_SalomeException);
1704
1705   // Update Python script
1706   TPythonDump() << _this() << ".ExportSTL( '" << file << "', " << isascii << " )";
1707
1708   // Perform Export
1709   PrepareForWriting(file);
1710   _impl->ExportSTL(file, isascii);
1711 }
1712
1713 //=============================================================================
1714 /*!
1715  *
1716  */
1717 //=============================================================================
1718
1719 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1720 {
1721   Unexpect aCatch(SALOME_SalomeException);
1722   SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1723   SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1724   return aMesh._retn();
1725 }
1726
1727 //=============================================================================
1728 /*!
1729  *
1730  */
1731 //=============================================================================
1732 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1733 {
1734   Unexpect aCatch(SALOME_SalomeException);
1735   return _impl->NbNodes();
1736 }
1737
1738 //=============================================================================
1739 /*!
1740  *
1741  */
1742 //=============================================================================
1743 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1744 {
1745   Unexpect aCatch(SALOME_SalomeException);
1746   return NbEdges() + NbFaces() + NbVolumes();
1747 }
1748
1749 //=============================================================================
1750 /*!
1751  *
1752  */
1753 //=============================================================================
1754 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1755 {
1756   Unexpect aCatch(SALOME_SalomeException);
1757   return _impl->NbEdges();
1758 }
1759
1760 CORBA::Long SMESH_Mesh_i::NbEdgesOfOrder(SMESH::ElementOrder order)
1761   throw(SALOME::SALOME_Exception)
1762 {
1763   Unexpect aCatch(SALOME_SalomeException);
1764   return _impl->NbEdges( (SMDSAbs_ElementOrder) order);
1765 }
1766
1767 //=============================================================================
1768 /*!
1769  *
1770  */
1771 //=============================================================================
1772 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1773 {
1774   Unexpect aCatch(SALOME_SalomeException);
1775   return _impl->NbFaces();
1776 }
1777
1778 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1779 {
1780   Unexpect aCatch(SALOME_SalomeException);
1781   return _impl->NbTriangles();
1782 }
1783
1784 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1785 {
1786   Unexpect aCatch(SALOME_SalomeException);
1787   return _impl->NbQuadrangles();
1788 }
1789
1790 CORBA::Long SMESH_Mesh_i::NbPolygons()throw(SALOME::SALOME_Exception)
1791 {
1792   Unexpect aCatch(SALOME_SalomeException);
1793   return _impl->NbPolygons();
1794 }
1795
1796 CORBA::Long SMESH_Mesh_i::NbFacesOfOrder(SMESH::ElementOrder order)
1797   throw(SALOME::SALOME_Exception)
1798 {
1799   Unexpect aCatch(SALOME_SalomeException);
1800   return _impl->NbFaces( (SMDSAbs_ElementOrder) order);
1801 }
1802
1803 CORBA::Long SMESH_Mesh_i::NbTrianglesOfOrder(SMESH::ElementOrder order)
1804   throw(SALOME::SALOME_Exception)
1805 {
1806   Unexpect aCatch(SALOME_SalomeException);
1807   return _impl->NbTriangles( (SMDSAbs_ElementOrder) order);
1808 }
1809
1810 CORBA::Long SMESH_Mesh_i::NbQuadranglesOfOrder(SMESH::ElementOrder order)
1811   throw(SALOME::SALOME_Exception)
1812 {
1813   Unexpect aCatch(SALOME_SalomeException);
1814   return _impl->NbQuadrangles( (SMDSAbs_ElementOrder) order);
1815 }
1816
1817 //=============================================================================
1818 /*!
1819  *
1820  */
1821 //=============================================================================
1822 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1823 {
1824   Unexpect aCatch(SALOME_SalomeException);
1825   return _impl->NbVolumes();
1826 }
1827
1828 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1829 {
1830   Unexpect aCatch(SALOME_SalomeException);
1831   return _impl->NbTetras();
1832 }
1833
1834 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1835 {
1836   Unexpect aCatch(SALOME_SalomeException);
1837   return _impl->NbHexas();
1838 }
1839
1840 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1841 {
1842   Unexpect aCatch(SALOME_SalomeException);
1843   return _impl->NbPyramids();
1844 }
1845
1846 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1847 {
1848   Unexpect aCatch(SALOME_SalomeException);
1849   return _impl->NbPrisms();
1850 }
1851
1852 CORBA::Long SMESH_Mesh_i::NbPolyhedrons()throw(SALOME::SALOME_Exception)
1853 {
1854   Unexpect aCatch(SALOME_SalomeException);
1855   return _impl->NbPolyhedrons();
1856 }
1857
1858 CORBA::Long SMESH_Mesh_i::NbVolumesOfOrder(SMESH::ElementOrder order)
1859   throw(SALOME::SALOME_Exception)
1860 {
1861   Unexpect aCatch(SALOME_SalomeException);
1862   return _impl->NbVolumes( (SMDSAbs_ElementOrder) order);
1863 }
1864
1865 CORBA::Long SMESH_Mesh_i::NbTetrasOfOrder(SMESH::ElementOrder order)
1866   throw(SALOME::SALOME_Exception)
1867 {
1868   Unexpect aCatch(SALOME_SalomeException);
1869   return _impl->NbTetras( (SMDSAbs_ElementOrder) order);
1870 }
1871
1872 CORBA::Long SMESH_Mesh_i::NbHexasOfOrder(SMESH::ElementOrder order)
1873   throw(SALOME::SALOME_Exception)
1874 {
1875   Unexpect aCatch(SALOME_SalomeException);
1876   return _impl->NbHexas( (SMDSAbs_ElementOrder) order);
1877 }
1878
1879 CORBA::Long SMESH_Mesh_i::NbPyramidsOfOrder(SMESH::ElementOrder order)
1880   throw(SALOME::SALOME_Exception)
1881 {
1882   Unexpect aCatch(SALOME_SalomeException);
1883   return _impl->NbPyramids( (SMDSAbs_ElementOrder) order);
1884 }
1885
1886 CORBA::Long SMESH_Mesh_i::NbPrismsOfOrder(SMESH::ElementOrder order)
1887   throw(SALOME::SALOME_Exception)
1888 {
1889   Unexpect aCatch(SALOME_SalomeException);
1890   return _impl->NbPrisms( (SMDSAbs_ElementOrder) order);
1891 }
1892
1893 //=============================================================================
1894 /*!
1895  *
1896  */
1897 //=============================================================================
1898 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1899 {
1900   Unexpect aCatch(SALOME_SalomeException);
1901   return _mapSubMesh_i.size();
1902 }
1903
1904 //=============================================================================
1905 /*!
1906  *
1907  */
1908 //=============================================================================
1909 char* SMESH_Mesh_i::Dump()
1910 {
1911   std::ostringstream os;
1912   _impl->Dump( os );
1913   return CORBA::string_dup( os.str().c_str() );
1914 }
1915
1916 //=============================================================================
1917 /*!
1918  *
1919  */
1920 //=============================================================================
1921 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1922 {
1923 //   SMESH::long_array_var aResult = new SMESH::long_array();
1924 //   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1925 //   int aMinId = aSMESHDS_Mesh->MinElementID();
1926 //   int aMaxId =  aSMESHDS_Mesh->MaxElementID();
1927
1928 //   aResult->length(aMaxId - aMinId + 1);
1929
1930 //   for (int i = 0, id = aMinId; id <= aMaxId; id++  )
1931 //     aResult[i++] = id;
1932
1933 //   return aResult._retn();
1934   // PAL12398
1935   return GetElementsId();
1936 }
1937
1938 //=============================================================================
1939 /*!
1940  *
1941  */
1942 //=============================================================================
1943
1944 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1945      throw (SALOME::SALOME_Exception)
1946 {
1947   Unexpect aCatch(SALOME_SalomeException);
1948   MESSAGE("SMESH_Mesh_i::GetElementsId");
1949   SMESH::long_array_var aResult = new SMESH::long_array();
1950   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1951
1952   if ( aSMESHDS_Mesh == NULL )
1953     return aResult._retn();
1954
1955   long nbElements = NbElements();
1956   aResult->length( nbElements );
1957   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1958   for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1959     aResult[i] = anIt->next()->GetID();
1960
1961   return aResult._retn();
1962 }
1963
1964
1965 //=============================================================================
1966 /*!
1967  *
1968  */
1969 //=============================================================================
1970
1971 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1972     throw (SALOME::SALOME_Exception)
1973 {
1974   Unexpect aCatch(SALOME_SalomeException);
1975   MESSAGE("SMESH_subMesh_i::GetElementsByType");
1976   SMESH::long_array_var aResult = new SMESH::long_array();
1977   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1978
1979   if ( aSMESHDS_Mesh == NULL )
1980     return aResult._retn();
1981
1982   long nbElements = NbElements();
1983
1984   // No sense in returning ids of elements along with ids of nodes:
1985   // when theElemType == SMESH::ALL, return node ids only if
1986   // there are no elements
1987   if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1988     return GetNodesId();
1989
1990   aResult->length( nbElements );
1991
1992   int i = 0;
1993
1994   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1995   while ( i < nbElements && anIt->more() ) {
1996     const SMDS_MeshElement* anElem = anIt->next();
1997     if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1998       aResult[i++] = anElem->GetID();
1999   }
2000
2001   aResult->length( i );
2002
2003   return aResult._retn();
2004 }
2005
2006 //=============================================================================
2007 /*!
2008  *
2009  */
2010 //=============================================================================
2011
2012 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
2013   throw (SALOME::SALOME_Exception)
2014 {
2015   Unexpect aCatch(SALOME_SalomeException);
2016   MESSAGE("SMESH_subMesh_i::GetNodesId");
2017   SMESH::long_array_var aResult = new SMESH::long_array();
2018   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2019
2020   if ( aSMESHDS_Mesh == NULL )
2021     return aResult._retn();
2022
2023   long nbNodes = NbNodes();
2024   aResult->length( nbNodes );
2025   SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
2026   for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
2027     aResult[i] = anIt->next()->GetID();
2028
2029   return aResult._retn();
2030 }
2031
2032 //=============================================================================
2033 /*!
2034  *
2035  */
2036 //=============================================================================
2037
2038 SMESH::ElementType SMESH_Mesh_i::GetElementType( const CORBA::Long id, const bool iselem )
2039   throw (SALOME::SALOME_Exception)
2040 {
2041   return ( SMESH::ElementType )_impl->GetElementType( id, iselem );
2042 }
2043
2044
2045 //=============================================================================
2046 /*!
2047  * Returns ID of elements for given submesh
2048  */
2049 //=============================================================================
2050 SMESH::long_array* SMESH_Mesh_i::GetSubMeshElementsId(const CORBA::Long ShapeID)
2051      throw (SALOME::SALOME_Exception)
2052 {
2053   SMESH::long_array_var aResult = new SMESH::long_array();
2054
2055   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
2056   if(!SM) return aResult._retn();
2057
2058   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
2059   if(!SDSM) return aResult._retn();
2060
2061   aResult->length(SDSM->NbElements());
2062
2063   SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
2064   int i = 0;
2065   while ( eIt->more() ) {
2066     aResult[i++] = eIt->next()->GetID();
2067   }
2068
2069   return aResult._retn();
2070 }
2071
2072
2073 //=============================================================================
2074 /*!
2075  * Returns ID of nodes for given submesh
2076  * If param all==true - returns all nodes, else -
2077  * returns only nodes on shapes.
2078  */
2079 //=============================================================================
2080 SMESH::long_array* SMESH_Mesh_i::GetSubMeshNodesId(const CORBA::Long ShapeID, CORBA::Boolean all)
2081      throw (SALOME::SALOME_Exception)
2082 {
2083   SMESH::long_array_var aResult = new SMESH::long_array();
2084
2085   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
2086   if(!SM) return aResult._retn();
2087
2088   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
2089   if(!SDSM) return aResult._retn();
2090
2091   set<int> theElems;
2092   if( !all || (SDSM->NbElements()==0) ) { // internal nodes or vertex submesh
2093     SMDS_NodeIteratorPtr nIt = SDSM->GetNodes();
2094     while ( nIt->more() ) {
2095       const SMDS_MeshNode* elem = nIt->next();
2096       theElems.insert( elem->GetID() );
2097     }
2098   }
2099   else { // all nodes of submesh elements
2100     SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
2101     while ( eIt->more() ) {
2102       const SMDS_MeshElement* anElem = eIt->next();
2103       SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
2104       while ( nIt->more() ) {
2105         const SMDS_MeshElement* elem = nIt->next();
2106         theElems.insert( elem->GetID() );
2107       }
2108     }
2109   }
2110
2111   aResult->length(theElems.size());
2112   set<int>::iterator itElem;
2113   int i = 0;
2114   for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
2115     aResult[i++] = *itElem;
2116
2117   return aResult._retn();
2118 }
2119   
2120
2121 //=============================================================================
2122 /*!
2123  * Returns type of elements for given submesh
2124  */
2125 //=============================================================================
2126 SMESH::ElementType SMESH_Mesh_i::GetSubMeshElementType(const CORBA::Long ShapeID)
2127      throw (SALOME::SALOME_Exception)
2128 {
2129   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
2130   if(!SM) return SMESH::ALL;
2131
2132   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
2133   if(!SDSM) return SMESH::ALL;
2134
2135   if(SDSM->NbElements()==0)
2136     return (SM->GetSubShape().ShapeType() == TopAbs_VERTEX) ? SMESH::NODE : SMESH::ALL;
2137
2138   SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
2139   const SMDS_MeshElement* anElem = eIt->next();
2140   return ( SMESH::ElementType ) anElem->GetType();
2141 }
2142   
2143
2144 //=============================================================================
2145 /*!
2146  *
2147  */
2148 //=============================================================================
2149
2150 CORBA::LongLong SMESH_Mesh_i::GetMeshPtr()
2151 {
2152   CORBA::LongLong pointeur = CORBA::LongLong(_impl);
2153   if ( MYDEBUG )
2154     MESSAGE("CORBA::LongLong SMESH_Mesh_i::GetMeshPtr() "<<pointeur);
2155   return pointeur;
2156 }
2157
2158
2159 //=============================================================================
2160 /*!
2161  * Get XYZ coordinates of node as list of double
2162  * If there is not node for given ID - returns empty list
2163  */
2164 //=============================================================================
2165
2166 SMESH::double_array* SMESH_Mesh_i::GetNodeXYZ(const CORBA::Long id)
2167 {
2168   SMESH::double_array_var aResult = new SMESH::double_array();
2169   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2170   if ( aSMESHDS_Mesh == NULL )
2171     return aResult._retn();
2172
2173   // find node
2174   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
2175   if(!aNode)
2176     return aResult._retn();
2177
2178   // add coordinates
2179   aResult->length(3);
2180   aResult[0] = aNode->X();
2181   aResult[1] = aNode->Y();
2182   aResult[2] = aNode->Z();
2183   return aResult._retn();
2184 }
2185
2186
2187 //=============================================================================
2188 /*!
2189  * For given node returns list of IDs of inverse elements
2190  * If there is not node for given ID - returns empty list
2191  */
2192 //=============================================================================
2193
2194 SMESH::long_array* SMESH_Mesh_i::GetNodeInverseElements(const CORBA::Long id)
2195 {
2196   SMESH::long_array_var aResult = new SMESH::long_array();
2197   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2198   if ( aSMESHDS_Mesh == NULL )
2199     return aResult._retn();
2200
2201   // find node
2202   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
2203   if(!aNode)
2204     return aResult._retn();
2205
2206   // find inverse elements
2207   SMDS_ElemIteratorPtr eIt = aNode->GetInverseElementIterator();
2208   TColStd_SequenceOfInteger IDs;
2209   while(eIt->more()) {
2210     const SMDS_MeshElement* elem = eIt->next();
2211     IDs.Append(elem->GetID());
2212   }
2213   if(IDs.Length()>0) {
2214     aResult->length(IDs.Length());
2215     int i = 1;
2216     for(; i<=IDs.Length(); i++) {
2217       aResult[i-1] = IDs.Value(i);
2218     }
2219   }
2220   return aResult._retn();
2221 }
2222
2223 //=============================================================================
2224 /*!
2225  * \brief Return position of a node on shape
2226  */
2227 //=============================================================================
2228
2229 SMESH::NodePosition* SMESH_Mesh_i::GetNodePosition(CORBA::Long NodeID)
2230 {
2231   SMESH::NodePosition* aNodePosition = new SMESH::NodePosition();
2232   aNodePosition->shapeID = 0;
2233   aNodePosition->shapeType = GEOM::SHAPE;
2234
2235   SMESHDS_Mesh* mesh = _impl->GetMeshDS();
2236   if ( !mesh ) return aNodePosition;
2237
2238   if ( const SMDS_MeshNode* aNode = mesh->FindNode(NodeID) )
2239   {
2240     if ( SMDS_PositionPtr pos = aNode->GetPosition() )
2241     {
2242       aNodePosition->shapeID = pos->GetShapeId();
2243       switch ( pos->GetTypeOfPosition() ) {
2244       case SMDS_TOP_EDGE:
2245         aNodePosition->shapeType = GEOM::EDGE;
2246         aNodePosition->params.length(1);
2247         aNodePosition->params[0] =
2248           static_cast<SMDS_EdgePosition*>( pos.get() )->GetUParameter();
2249         break;
2250       case SMDS_TOP_FACE:
2251         aNodePosition->shapeType = GEOM::FACE;
2252         aNodePosition->params.length(2);
2253         aNodePosition->params[0] =
2254           static_cast<SMDS_FacePosition*>( pos.get() )->GetUParameter();
2255         aNodePosition->params[1] =
2256           static_cast<SMDS_FacePosition*>( pos.get() )->GetVParameter();
2257         break;
2258       case SMDS_TOP_VERTEX:
2259         aNodePosition->shapeType = GEOM::VERTEX;
2260         break;
2261       case SMDS_TOP_3DSPACE:
2262         if ( TopExp_Explorer(_impl->GetShapeToMesh(), TopAbs_SOLID).More() )
2263           aNodePosition->shapeType = GEOM::SOLID;
2264         else if ( TopExp_Explorer(_impl->GetShapeToMesh(), TopAbs_SHELL).More() )
2265           aNodePosition->shapeType = GEOM::SHELL;
2266         break;
2267       default:;
2268       }
2269     }
2270   }
2271   return aNodePosition;
2272 }
2273
2274 //=============================================================================
2275 /*!
2276  * If given element is node returns IDs of shape from position
2277  * If there is not node for given ID - returns -1
2278  */
2279 //=============================================================================
2280
2281 CORBA::Long SMESH_Mesh_i::GetShapeID(const CORBA::Long id)
2282 {
2283   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2284   if ( aSMESHDS_Mesh == NULL )
2285     return -1;
2286
2287   // try to find node
2288   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
2289   if(aNode) {
2290     SMDS_PositionPtr pos = aNode->GetPosition();
2291     if(!pos)
2292       return -1;
2293     else
2294       return pos->GetShapeId();
2295   }
2296
2297   return -1;
2298 }
2299
2300
2301 //=============================================================================
2302 /*!
2303  * For given element returns ID of result shape after 
2304  * ::FindShape() from SMESH_MeshEditor
2305  * If there is not element for given ID - returns -1
2306  */
2307 //=============================================================================
2308
2309 CORBA::Long SMESH_Mesh_i::GetShapeIDForElem(const CORBA::Long id)
2310 {
2311   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2312   if ( aSMESHDS_Mesh == NULL )
2313     return -1;
2314
2315   // try to find element
2316   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2317   if(!elem)
2318     return -1;
2319
2320   //SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
2321   ::SMESH_MeshEditor aMeshEditor(_impl);
2322   int index = aMeshEditor.FindShape( elem );
2323   if(index>0)
2324     return index;
2325
2326   return -1;
2327 }
2328
2329
2330 //=============================================================================
2331 /*!
2332  * Returns number of nodes for given element
2333  * If there is not element for given ID - returns -1
2334  */
2335 //=============================================================================
2336
2337 CORBA::Long SMESH_Mesh_i::GetElemNbNodes(const CORBA::Long id)
2338 {
2339   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2340   if ( aSMESHDS_Mesh == NULL ) return -1;
2341   // try to find element
2342   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2343   if(!elem) return -1;
2344   return elem->NbNodes();
2345 }
2346
2347
2348 //=============================================================================
2349 /*!
2350  * Returns ID of node by given index for given element
2351  * If there is not element for given ID - returns -1
2352  * If there is not node for given index - returns -2
2353  */
2354 //=============================================================================
2355
2356 CORBA::Long SMESH_Mesh_i::GetElemNode(const CORBA::Long id, const CORBA::Long index)
2357 {
2358   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2359   if ( aSMESHDS_Mesh == NULL ) return -1;
2360   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2361   if(!elem) return -1;
2362   if( index>=elem->NbNodes() || index<0 ) return -1;
2363   return elem->GetNode(index)->GetID();
2364 }
2365
2366 //=============================================================================
2367 /*!
2368  * Returns IDs of nodes of given element
2369  */
2370 //=============================================================================
2371
2372 SMESH::long_array* SMESH_Mesh_i::GetElemNodes(const CORBA::Long id)
2373 {
2374   SMESH::long_array_var aResult = new SMESH::long_array();
2375   if ( SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS() )
2376   {
2377     if ( const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id) )
2378     {
2379       aResult->length( elem->NbNodes() );
2380       for ( int i = 0; i < elem->NbNodes(); ++i )
2381         aResult[ i ] = elem->GetNode( i )->GetID();
2382     }
2383   }
2384   return aResult._retn();
2385 }
2386
2387 //=============================================================================
2388 /*!
2389  * Returns true if given node is medium node
2390  * in given quadratic element
2391  */
2392 //=============================================================================
2393
2394 CORBA::Boolean SMESH_Mesh_i::IsMediumNode(const CORBA::Long ide, const CORBA::Long idn)
2395 {
2396   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2397   if ( aSMESHDS_Mesh == NULL ) return false;
2398   // try to find node
2399   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(idn);
2400   if(!aNode) return false;
2401   // try to find element
2402   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(ide);
2403   if(!elem) return false;
2404
2405   return elem->IsMediumNode(aNode);
2406 }
2407
2408
2409 //=============================================================================
2410 /*!
2411  * Returns true if given node is medium node
2412  * in one of quadratic elements
2413  */
2414 //=============================================================================
2415
2416 CORBA::Boolean SMESH_Mesh_i::IsMediumNodeOfAnyElem(const CORBA::Long idn,
2417                                                    SMESH::ElementType theElemType)
2418 {
2419   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2420   if ( aSMESHDS_Mesh == NULL ) return false;
2421
2422   // try to find node
2423   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(idn);
2424   if(!aNode) return false;
2425
2426   SMESH_MesherHelper aHelper( *(_impl) );
2427
2428   SMDSAbs_ElementType aType;
2429   if(theElemType==SMESH::EDGE) aType = SMDSAbs_Edge;
2430   else if(theElemType==SMESH::FACE) aType = SMDSAbs_Face;
2431   else if(theElemType==SMESH::VOLUME) aType = SMDSAbs_Volume;
2432   else aType = SMDSAbs_All;
2433
2434   return aHelper.IsMedium(aNode,aType);
2435 }
2436
2437
2438 //=============================================================================
2439 /*!
2440  * Returns number of edges for given element
2441  */
2442 //=============================================================================
2443
2444 CORBA::Long SMESH_Mesh_i::ElemNbEdges(const CORBA::Long id)
2445 {
2446   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2447   if ( aSMESHDS_Mesh == NULL ) return -1;
2448   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2449   if(!elem) return -1;
2450   return elem->NbEdges();
2451 }
2452
2453
2454 //=============================================================================
2455 /*!
2456  * Returns number of faces for given element
2457  */
2458 //=============================================================================
2459
2460 CORBA::Long SMESH_Mesh_i::ElemNbFaces(const CORBA::Long id)
2461 {
2462   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2463   if ( aSMESHDS_Mesh == NULL ) return -1;
2464   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2465   if(!elem) return -1;
2466   return elem->NbFaces();
2467 }
2468
2469
2470 //=============================================================================
2471 /*!
2472  * Returns true if given element is polygon
2473  */
2474 //=============================================================================
2475
2476 CORBA::Boolean SMESH_Mesh_i::IsPoly(const CORBA::Long id)
2477 {
2478   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2479   if ( aSMESHDS_Mesh == NULL ) return false;
2480   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2481   if(!elem) return false;
2482   return elem->IsPoly();
2483 }
2484
2485
2486 //=============================================================================
2487 /*!
2488  * Returns true if given element is quadratic
2489  */
2490 //=============================================================================
2491
2492 CORBA::Boolean SMESH_Mesh_i::IsQuadratic(const CORBA::Long id)
2493 {
2494   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2495   if ( aSMESHDS_Mesh == NULL ) return false;
2496   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2497   if(!elem) return false;
2498   return elem->IsQuadratic();
2499 }
2500
2501
2502 //=============================================================================
2503 /*!
2504  * Returns bary center for given element
2505  */
2506 //=============================================================================
2507
2508 SMESH::double_array* SMESH_Mesh_i::BaryCenter(const CORBA::Long id)
2509 {
2510   SMESH::double_array_var aResult = new SMESH::double_array();
2511   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2512   if ( aSMESHDS_Mesh == NULL )
2513     return aResult._retn();
2514
2515   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2516   if(!elem)
2517     return aResult._retn();
2518
2519   if(elem->GetType()==SMDSAbs_Volume) {
2520     SMDS_VolumeTool aTool;
2521     if(aTool.Set(elem)) {
2522       aResult->length(3);
2523       if (!aTool.GetBaryCenter( aResult[0], aResult[1], aResult[2]) )
2524         aResult->length(0);
2525     }
2526   }
2527   else {
2528     SMDS_ElemIteratorPtr anIt = elem->nodesIterator();
2529     int nbn = 0;
2530     double x=0., y=0., z=0.;
2531     for(; anIt->more(); ) {
2532       nbn++;
2533       const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(anIt->next());
2534       x += aNode->X();
2535       y += aNode->Y();
2536       z += aNode->Z();
2537     }
2538     if(nbn>0) {
2539       // add coordinates
2540       aResult->length(3);
2541       aResult[0] = x/nbn;
2542       aResult[1] = y/nbn;
2543       aResult[2] = z/nbn;
2544     }
2545   }
2546
2547   return aResult._retn();
2548 }
2549
2550
2551 //=============================================================================
2552 /*!
2553  * Create and publish group servants if any groups were imported or created anyhow
2554  */
2555 //=============================================================================
2556
2557 void SMESH_Mesh_i::CreateGroupServants() 
2558 {
2559   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
2560
2561   ::SMESH_Mesh::GroupIteratorPtr groupIt = _impl->GetGroups();
2562   while ( groupIt->more() )
2563   {
2564     ::SMESH_Group* group = groupIt->next();
2565     int            anId = group->GetGroupDS()->GetID();
2566
2567     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.find(anId);
2568     if ( it != _mapGroups.end() && !CORBA::is_nil( it->second ))
2569       continue;
2570
2571     SMESH_GroupBase_i* aGroupImpl;
2572     TopoDS_Shape       shape;
2573     if ( SMESHDS_GroupOnGeom* groupOnGeom =
2574          dynamic_cast<SMESHDS_GroupOnGeom*>( group->GetGroupDS() ))
2575     {
2576       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
2577       shape      = groupOnGeom->GetShape();
2578     }
2579     else {
2580       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
2581     }
2582
2583     // To ensure correct mapping of servant and correct reference counting in GenericObj_i
2584     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
2585     aGroupImpl->Register();
2586
2587     SMESH::SMESH_GroupBase_var groupVar =
2588       SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
2589     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( groupVar );
2590
2591     // register CORBA object for persistence
2592     int nextId = _gen_i->RegisterObject( groupVar );
2593     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
2594
2595     // publishing of the groups in the study
2596     if ( !aStudy->_is_nil() ) {
2597       GEOM::GEOM_Object_var shapeVar = _gen_i->ShapeToGeomObject( shape );
2598       _gen_i->PublishGroup( aStudy, _this(), groupVar, shapeVar, groupVar->GetName());
2599     }
2600   }
2601 }
2602
2603 //=============================================================================
2604 /*!
2605  * \brief Return groups cantained in _mapGroups by their IDs
2606  */
2607 //=============================================================================
2608
2609 SMESH::ListOfGroups* SMESH_Mesh_i::GetGroups(const list<int>& groupIDs) const
2610 {
2611   int nbGroups = groupIDs.size();
2612   SMESH::ListOfGroups_var aList = new SMESH::ListOfGroups();
2613   aList->length( nbGroups );
2614
2615   list<int>::const_iterator ids = groupIDs.begin();
2616   for ( nbGroups = 0; ids != groupIDs.end(); ++ids )
2617   {
2618     map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = _mapGroups.find( *ids );
2619     if ( it != _mapGroups.end() && !CORBA::is_nil( it->second ))
2620       aList[nbGroups++] = SMESH::SMESH_GroupBase::_duplicate( it->second );
2621   }
2622   aList->length( nbGroups );
2623   return aList._retn();
2624 }
2625
2626 //=============================================================================
2627 /*!
2628  * \brief Return information about imported file
2629  */
2630 //=============================================================================
2631
2632 SALOME_MED::MedFileInfo* SMESH_Mesh_i::GetMEDFileInfo()
2633 {
2634   SALOME_MED::MedFileInfo_var res( myFileInfo );
2635   if ( !res.operator->() ) {
2636     res = new SALOME_MED::MedFileInfo;
2637     res->fileName = "";
2638     res->fileSize = res->major = res->minor = res->release = -1;
2639   }
2640   return res._retn();
2641 }