Salome HOME
Merging from V3_2_6pre4
[modules/smesh.git] / src / SMESH_I / SMESH_Mesh_i.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  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 //
23 //
24 //  File   : SMESH_Mesh_i.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESH_Mesh_i.hxx"
30 #include "SMESH_subMesh_i.hxx"
31 #include "SMESH_MEDMesh_i.hxx"
32 #include "SMESH_Group_i.hxx"
33 #include "SMESH_Filter_i.hxx"
34 #include "SMESH_PythonDump.hxx"
35
36 #include "Utils_CorbaException.hxx"
37 #include "Utils_ExceptHandlers.hxx"
38 #include "utilities.h"
39
40 #include "SALOME_NamingService.hxx"
41 #include "Utils_SINGLETON.hxx"
42 #include "OpUtil.hxx"
43
44 #include "SMESHDS_Command.hxx"
45 #include "SMESHDS_CommandType.hxx"
46 #include "SMESH_MeshEditor_i.hxx"
47 #include "SMESH_Gen_i.hxx"
48 #include "DriverMED_R_SMESHDS_Mesh.h"
49 //#include "SMDS_ElemIterator.hxx"
50 #include "SMDS_VolumeTool.hxx"
51 #include "SMESH_MesherHelper.hxx"
52 #include "SMESH_MeshEditor.hxx"
53
54 // OCCT Includes
55 #include <OSD_Path.hxx>
56 #include <OSD_File.hxx>
57 #include <OSD_Directory.hxx>
58 #include <OSD_Protection.hxx>
59 #include <TColStd_MapOfInteger.hxx>
60 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
61 #include <TColStd_SequenceOfInteger.hxx>
62 #include "TCollection_AsciiString.hxx"
63
64 // STL Includes
65 #include <string>
66 #include <iostream>
67 #include <sstream>
68
69 #ifdef _DEBUG_
70 static int MYDEBUG = 0;
71 #else
72 static int MYDEBUG = 0;
73 #endif
74
75 using namespace std;
76 using SMESH::TPythonDump;
77
78 int SMESH_Mesh_i::myIdGenerator = 0;
79
80
81
82 //=============================================================================
83 /*!
84  *  Constructor
85  */
86 //=============================================================================
87
88 SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
89                             SMESH_Gen_i*            gen_i,
90                             CORBA::Long studyId )
91 : SALOME::GenericObj_i( thePOA )
92 {
93   MESSAGE("SMESH_Mesh_i");
94   _impl = NULL;
95   _gen_i = gen_i;
96   _id = myIdGenerator++;
97   _studyId = studyId;
98 }
99
100 //=============================================================================
101 /*!
102  *  Destructor
103  */
104 //=============================================================================
105
106 SMESH_Mesh_i::~SMESH_Mesh_i()
107 {
108   INFOS("~SMESH_Mesh_i");
109   map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
110   for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
111     SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
112     if ( aGroup ) {
113
114       // this method is colled from destructor of group (PAL6331)
115       //_impl->RemoveGroup( aGroup->GetLocalID() );
116
117       aGroup->Destroy();
118     }
119   }
120   _mapGroups.clear();
121 }
122
123 //=============================================================================
124 /*!
125  *  SetShape
126  *
127  *  Associates <this> mesh with <theShape> and puts a reference
128  *  to <theShape> into the current study;
129  *  the previous shape is substituted by the new one.
130  */
131 //=============================================================================
132
133 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
134     throw (SALOME::SALOME_Exception)
135 {
136   Unexpect aCatch(SALOME_SalomeException);
137   try {
138     _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
139   }
140   catch(SALOME_Exception & S_ex) {
141     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
142   }
143 }
144
145 //================================================================================
146 /*!
147  * \brief return true if mesh has a shape to build a shape on
148  */
149 //================================================================================
150
151 CORBA::Boolean SMESH_Mesh_i::HasShapeToMesh()
152   throw (SALOME::SALOME_Exception)
153 {
154   Unexpect aCatch(SALOME_SalomeException);
155   bool res = false;
156   try {
157     res = _impl->HasShapeToMesh();
158   }
159   catch(SALOME_Exception & S_ex) {
160     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
161   }
162   return res;
163 }
164
165 //=======================================================================
166 //function : GetShapeToMesh
167 //purpose  :
168 //=======================================================================
169
170 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
171     throw (SALOME::SALOME_Exception)
172 {
173   Unexpect aCatch(SALOME_SalomeException);
174   GEOM::GEOM_Object_var aShapeObj;
175   try {
176     TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
177     if ( !S.IsNull() )
178       aShapeObj = _gen_i->ShapeToGeomObject( S );
179   }
180   catch(SALOME_Exception & S_ex) {
181     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
182   }
183   return aShapeObj._retn();
184 }
185
186 //=============================================================================
187 /*!
188  *
189  */
190 //=============================================================================
191
192 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
193 {
194   SMESH::DriverMED_ReadStatus res;
195   switch (theStatus)
196   {
197   case DriverMED_R_SMESHDS_Mesh::DRS_OK:
198     res = SMESH::DRS_OK; break;
199   case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
200     res = SMESH::DRS_EMPTY; break;
201   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
202     res = SMESH::DRS_WARN_RENUMBER; break;
203   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
204     res = SMESH::DRS_WARN_SKIP_ELEM; break;
205   case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
206   default:
207     res = SMESH::DRS_FAIL; break;
208   }
209   return res;
210 }
211
212 //=============================================================================
213 /*!
214  *  ImportMEDFile
215  *
216  *  Imports mesh data from MED file
217  */
218 //=============================================================================
219
220 SMESH::DriverMED_ReadStatus
221 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
222   throw ( SALOME::SALOME_Exception )
223 {
224   Unexpect aCatch(SALOME_SalomeException);
225   int status;
226   try {
227     status = importMEDFile( theFileName, theMeshName );
228   }
229   catch( SALOME_Exception& S_ex ) {
230     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
231   }
232   catch ( ... ) {
233     THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
234   }
235
236   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
237   if ( !aStudy->_is_nil() ) {
238     // publishing of the groups in the study (sub-meshes are out of scope of MED import)
239     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
240     for (; it != _mapGroups.end(); it++ ) {
241       SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
242       _gen_i->PublishGroup( aStudy, _this(), aGroup,
243                            GEOM::GEOM_Object::_nil(), aGroup->GetName());
244     }
245   }
246   return ConvertDriverMEDReadStatus(status);
247 }
248
249 //=============================================================================
250 /*!
251  *  ImportUNVFile
252  *
253  *  Imports mesh data from MED file
254  */
255 //=============================================================================
256
257 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
258   throw ( SALOME::SALOME_Exception )
259 {
260   // Read mesh with name = <theMeshName> into SMESH_Mesh
261   _impl->UNVToMesh( theFileName );
262
263   CreateGroupServants();
264
265   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
266   if ( !aStudy->_is_nil() ) {
267     // publishing of the groups in the study (sub-meshes are out of scope of UNV import)
268     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
269     for (; it != _mapGroups.end(); it++ ) {
270       SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
271       _gen_i->PublishGroup( aStudy, _this(), aGroup,
272                            GEOM::GEOM_Object::_nil(), aGroup->GetName());
273     }
274   }
275   return 1;
276 }
277
278 //=============================================================================
279 /*!
280  *  ImportSTLFile
281  *
282  *  Imports mesh data from STL file
283  */
284 //=============================================================================
285 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
286   throw ( SALOME::SALOME_Exception )
287 {
288   // Read mesh with name = <theMeshName> into SMESH_Mesh
289   _impl->STLToMesh( theFileName );
290
291   return 1;
292 }
293
294 //=============================================================================
295 /*!
296  *  importMEDFile
297  *
298  *  Imports mesh data from MED file
299  */
300 //=============================================================================
301
302 int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
303 {
304   // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
305   int status = _impl->MEDToMesh( theFileName, theMeshName );
306   CreateGroupServants();
307
308   return status;
309 }
310
311 //=============================================================================
312 /*!
313  *
314  */
315 //=============================================================================
316
317 #define RETURNCASE(hyp_stat) case SMESH_Hypothesis::hyp_stat: return SMESH::hyp_stat;
318
319 SMESH::Hypothesis_Status SMESH_Mesh_i::ConvertHypothesisStatus
320                          (SMESH_Hypothesis::Hypothesis_Status theStatus)
321 {
322   switch (theStatus) {
323   RETURNCASE( HYP_OK            );
324   RETURNCASE( HYP_MISSING       );
325   RETURNCASE( HYP_CONCURENT     );
326   RETURNCASE( HYP_BAD_PARAMETER );
327   RETURNCASE( HYP_HIDDEN_ALGO   );
328   RETURNCASE( HYP_HIDING_ALGO   );
329   RETURNCASE( HYP_UNKNOWN_FATAL );
330   RETURNCASE( HYP_INCOMPATIBLE  );
331   RETURNCASE( HYP_NOTCONFORM    );
332   RETURNCASE( HYP_ALREADY_EXIST );
333   RETURNCASE( HYP_BAD_DIM       );
334   RETURNCASE( HYP_BAD_SUBSHAPE  );
335   RETURNCASE( HYP_BAD_GEOMETRY  );
336   default:;
337   }
338   return SMESH::HYP_UNKNOWN_FATAL;
339 }
340
341 //=============================================================================
342 /*!
343  *  AddHypothesis
344  *
345  *  calls internal addHypothesis() and then adds a reference to <anHyp> under
346  *  the SObject actually having a reference to <aSubShape>.
347  *  NB: For this method to work, it is necessary to add a reference to sub-shape first.
348  */
349 //=============================================================================
350
351 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
352                                                      SMESH::SMESH_Hypothesis_ptr anHyp)
353   throw(SALOME::SALOME_Exception)
354 {
355   Unexpect aCatch(SALOME_SalomeException);
356   SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
357
358   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
359     _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
360                                  aSubShapeObject, anHyp );
361
362   if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
363
364   // Update Python script
365   TPythonDump() << "status = " << _this() << ".AddHypothesis( "
366                 << aSubShapeObject << ", " << anHyp << " )";
367
368   return ConvertHypothesisStatus(status);
369 }
370
371 //=============================================================================
372 /*!
373  *
374  */
375 //=============================================================================
376
377 SMESH_Hypothesis::Hypothesis_Status
378   SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr       aSubShapeObject,
379                               SMESH::SMESH_Hypothesis_ptr anHyp)
380 {
381   if(MYDEBUG) MESSAGE("addHypothesis");
382
383   if (CORBA::is_nil(aSubShapeObject))
384     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
385                                  SALOME::BAD_PARAM);
386
387   SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
388   if (CORBA::is_nil(myHyp))
389     THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
390                                  SALOME::BAD_PARAM);
391
392   SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
393   try
394   {
395     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
396     int hypId = myHyp->GetId();
397     status = _impl->AddHypothesis(myLocSubShape, hypId);
398     if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
399       _mapHypo[hypId] = SMESH::SMESH_Hypothesis::_duplicate( myHyp );
400       // assure there is a corresponding submesh
401       if ( !_impl->IsMainShape( myLocSubShape )) {
402         int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
403         if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
404           createSubMesh( aSubShapeObject );
405       }
406     }
407   }
408   catch(SALOME_Exception & S_ex)
409   {
410     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
411   }
412   return status;
413 }
414
415 //=============================================================================
416 /*!
417  *
418  */
419 //=============================================================================
420
421 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
422                                                         SMESH::SMESH_Hypothesis_ptr anHyp)
423      throw(SALOME::SALOME_Exception)
424 {
425   Unexpect aCatch(SALOME_SalomeException);
426   SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
427
428   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
429     _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
430                                       aSubShapeObject, anHyp );
431
432   // Update Python script
433   TPythonDump() << "status = " << _this() << ".RemoveHypothesis( "
434                 << aSubShapeObject << ", " << anHyp << " )";
435
436   return ConvertHypothesisStatus(status);
437 }
438
439 //=============================================================================
440 /*!
441  *
442  */
443 //=============================================================================
444
445 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
446                                  SMESH::SMESH_Hypothesis_ptr anHyp)
447 {
448         if(MYDEBUG) MESSAGE("removeHypothesis()");
449         // **** proposer liste de subShape (selection multiple)
450
451         if (CORBA::is_nil(aSubShapeObject))
452                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
453                         SALOME::BAD_PARAM);
454
455         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
456         if (CORBA::is_nil(myHyp))
457           THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
458                         SALOME::BAD_PARAM);
459
460         SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
461         try
462         {
463                 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
464                 int hypId = myHyp->GetId();
465                 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
466                 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
467                   _mapHypo.erase( hypId );
468         }
469         catch(SALOME_Exception & S_ex)
470         {
471                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
472         }
473         return status;
474 }
475
476 //=============================================================================
477 /*!
478  *
479  */
480 //=============================================================================
481
482 SMESH::ListOfHypothesis *
483         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
484 throw(SALOME::SALOME_Exception)
485 {
486   Unexpect aCatch(SALOME_SalomeException);
487   if (MYDEBUG) MESSAGE("GetHypothesisList");
488   if (CORBA::is_nil(aSubShapeObject))
489     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
490                                  SALOME::BAD_PARAM);
491
492   SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
493
494   try {
495     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
496     const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
497     int i = 0, n = aLocalList.size();
498     aList->length( n );
499
500     for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
501       SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
502       if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
503         aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
504     }
505
506     aList->length( i );
507   }
508   catch(SALOME_Exception & S_ex) {
509     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
510   }
511
512   return aList._retn();
513 }
514
515 //=============================================================================
516 /*!
517  *
518  */
519 //=============================================================================
520 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
521                                                   const char*           theName )
522      throw(SALOME::SALOME_Exception)
523 {
524   Unexpect aCatch(SALOME_SalomeException);
525   MESSAGE("SMESH_Mesh_i::GetSubMesh");
526   if (CORBA::is_nil(aSubShapeObject))
527     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
528                                  SALOME::BAD_PARAM);
529
530   SMESH::SMESH_subMesh_var subMesh;
531   SMESH::SMESH_Mesh_var    aMesh = SMESH::SMESH_Mesh::_narrow(_this());
532   try {
533     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
534
535     //Get or Create the SMESH_subMesh object implementation
536
537     int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
538     subMesh = getSubMesh( subMeshId );
539
540     // create a new subMesh object servant if there is none for the shape
541     if ( subMesh->_is_nil() )
542       subMesh = createSubMesh( aSubShapeObject );
543
544     if ( _gen_i->CanPublishInStudy( subMesh )) {
545       SALOMEDS::SObject_var aSO =
546         _gen_i->PublishSubMesh(_gen_i->GetCurrentStudy(), aMesh,
547                                subMesh, aSubShapeObject, theName );
548       if ( !aSO->_is_nil()) {
549         // Update Python script
550         TPythonDump() << aSO << " = " << _this() << ".GetSubMesh( "
551                       << aSubShapeObject << ", '" << theName << "' )";
552       }
553     }
554   }
555   catch(SALOME_Exception & S_ex) {
556     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
557   }
558   return subMesh._retn();
559 }
560
561 //=============================================================================
562 /*!
563  *
564  */
565 //=============================================================================
566
567 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
568      throw (SALOME::SALOME_Exception)
569 {
570   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
571   if ( theSubMesh->_is_nil() )
572     return;
573
574   GEOM::GEOM_Object_var aSubShapeObject;
575   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
576   if ( !aStudy->_is_nil() )  {
577     // Remove submesh's SObject
578     SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
579     if ( !anSO->_is_nil() ) {
580       long aTag = SMESH_Gen_i::GetRefOnShapeTag();
581       SALOMEDS::SObject_var anObj, aRef;
582       if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
583         aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
584
585       aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
586
587       // Update Python script
588       TPythonDump() << _this() << ".RemoveSubMesh( " << anSO << " )";
589     }
590   }
591
592   removeSubMesh( theSubMesh, aSubShapeObject.in() );
593 }
594
595 //=============================================================================
596 /*!
597  *  ElementTypeString
598  */
599 //=============================================================================
600 #define CASE2STRING(enum) case SMESH::enum: return "SMESH."#enum;
601 inline TCollection_AsciiString ElementTypeString (SMESH::ElementType theElemType)
602 {
603   switch (theElemType) {
604     CASE2STRING( ALL );
605     CASE2STRING( NODE );
606     CASE2STRING( EDGE );
607     CASE2STRING( FACE );
608     CASE2STRING( VOLUME );
609   default:;
610   }
611   return "";
612 }
613
614 //=============================================================================
615 /*!
616  *
617  */
618 //=============================================================================
619
620 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
621                                                  const char*         theName )
622      throw(SALOME::SALOME_Exception)
623 {
624   Unexpect aCatch(SALOME_SalomeException);
625   SMESH::SMESH_Group_var aNewGroup =
626     SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
627
628   if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
629     SALOMEDS::SObject_var aSO =
630       _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
631                            aNewGroup, GEOM::GEOM_Object::_nil(), theName);
632     if ( !aSO->_is_nil()) {
633       // Update Python script
634       TPythonDump() << aSO << " = " << _this() << ".CreateGroup( "
635                     << ElementTypeString(theElemType) << ", '" << theName << "' )";
636     }
637   }
638   return aNewGroup._retn();
639 }
640
641
642 //=============================================================================
643 /*!
644  *
645  */
646 //=============================================================================
647 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM (SMESH::ElementType    theElemType,
648                                                                 const char*           theName,
649                                                                 GEOM::GEOM_Object_ptr theGeomObj)
650      throw(SALOME::SALOME_Exception)
651 {
652   Unexpect aCatch(SALOME_SalomeException);
653   SMESH::SMESH_GroupOnGeom_var aNewGroup;
654
655   TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
656   if ( !aShape.IsNull() ) {
657     aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
658       ( createGroup( theElemType, theName, aShape ));
659     if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
660       SALOMEDS::SObject_var aSO =
661         _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
662                              aNewGroup, theGeomObj, theName);
663       if ( !aSO->_is_nil()) {
664         // Update Python script
665         TPythonDump() << aSO << " = " << _this() << ".CreateGroupFromGEOM("
666                       << ElementTypeString(theElemType) << ", '" << theName << "', "
667                       << theGeomObj << " )";
668       }
669     }
670   }
671
672   return aNewGroup._retn();
673 }
674
675 //=============================================================================
676 /*!
677  *
678  */
679 //=============================================================================
680
681 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
682      throw (SALOME::SALOME_Exception)
683 {
684   if ( theGroup->_is_nil() )
685     return;
686
687   SMESH_GroupBase_i* aGroup =
688     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
689   if ( !aGroup )
690     return;
691
692   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
693   if ( !aStudy->_is_nil() )  {
694     SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
695
696     if ( !aGroupSO->_is_nil() ) {
697       // Update Python script
698       TPythonDump() << _this() << ".RemoveGroup( " << aGroupSO << " )";
699
700       // Remove group's SObject
701       aStudy->NewBuilder()->RemoveObject( aGroupSO );
702     }
703   }
704
705   // Remove the group from SMESH data structures
706   removeGroup( aGroup->GetLocalID() );
707 }
708
709 //=============================================================================
710 /*! RemoveGroupWithContents
711  *  Remove group with its contents
712  */
713 //=============================================================================
714 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
715   throw (SALOME::SALOME_Exception)
716 {
717   if ( theGroup->_is_nil() )
718     return;
719
720   SMESH_GroupBase_i* aGroup =
721     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
722   if ( !aGroup )
723     return;
724
725   SMESH::long_array_var anIds = aGroup->GetListOfID();
726   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
727
728   // Update Python script
729   TPythonDump() << _this() << ".RemoveGroupWithContents( " << theGroup << " )";
730
731   // Remove contents
732   if ( aGroup->GetType() == SMESH::NODE )
733     aMeshEditor->RemoveNodes( anIds );
734   else
735     aMeshEditor->RemoveElements( anIds );
736
737   // Remove group
738   RemoveGroup( theGroup );
739
740   // Clear python lines, created by RemoveNodes/Elements() and RemoveGroup()
741   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
742   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
743 }
744
745
746 //================================================================================
747 /*!
748  * \brief Get the list of groups existing in the mesh
749   * \retval SMESH::ListOfGroups * - list of groups
750  */
751 //================================================================================
752
753 SMESH::ListOfGroups * SMESH_Mesh_i::GetGroups() throw(SALOME::SALOME_Exception)
754 {
755   Unexpect aCatch(SALOME_SalomeException);
756   if (MYDEBUG) MESSAGE("GetGroups");
757
758   SMESH::ListOfGroups_var aList = new SMESH::ListOfGroups();
759
760   // Python Dump
761   TPythonDump aPythonDump;
762   if ( !_mapGroups.empty() ) // (IMP13463) avoid "SyntaxError: can't assign to []"
763     aPythonDump << "[ ";
764
765   try {
766     aList->length( _mapGroups.size() );
767     int i = 0;
768     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
769     for ( ; it != _mapGroups.end(); it++ ) {
770       if ( CORBA::is_nil( it->second )) continue;
771       aList[i++] = SMESH::SMESH_GroupBase::_duplicate( it->second );
772       // Python Dump
773       if (i > 1) aPythonDump << ", ";
774       aPythonDump << it->second;
775     }
776     aList->length( i );
777   }
778   catch(SALOME_Exception & S_ex) {
779     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
780   }
781
782   // Update Python script
783   if ( !_mapGroups.empty() ) // (IMP13463) avoid "SyntaxError: can't assign to []"
784     aPythonDump << " ] = " << _this() << ".GetGroups()";
785
786   return aList._retn();
787 }
788
789 //=============================================================================
790 /*! UnionGroups
791  *  New group is created. All mesh elements that are
792  *  present in initial groups are added to the new one
793  */
794 //=============================================================================
795 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
796                                                   SMESH::SMESH_GroupBase_ptr theGroup2,
797                                                   const char* theName )
798   throw (SALOME::SALOME_Exception)
799 {
800   try
801   {
802     if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
803          theGroup1->GetType() != theGroup2->GetType() )
804       return SMESH::SMESH_Group::_nil();
805
806     // Create Union
807     SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
808     if ( aResGrp->_is_nil() )
809       return SMESH::SMESH_Group::_nil();
810
811     SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
812     SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
813
814     TColStd_MapOfInteger aResMap;
815
816     for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
817       aResMap.Add( anIds1[ i1 ] );
818
819     for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
820       aResMap.Add( anIds2[ i2 ] );
821
822     SMESH::long_array_var aResIds = new SMESH::long_array;
823     aResIds->length( aResMap.Extent() );
824
825     int resI = 0;
826     TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
827     for( ; anIter.More(); anIter.Next() )
828       aResIds[ resI++ ] = anIter.Key();
829
830     aResGrp->Add( aResIds );
831
832     // Clear python lines, created by CreateGroup() and Add()
833     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
834     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
835     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
836
837     // Update Python script
838     TPythonDump() << aResGrp << " = " << _this() << ".UnionGroups( "
839                   << theGroup1 << ", " << theGroup2 << ", '"
840                   << theName << "' )";
841
842     return aResGrp._retn();
843   }
844   catch( ... )
845   {
846     return SMESH::SMESH_Group::_nil();
847   }
848 }
849
850 //=============================================================================
851 /*! IntersectGroups
852  *  New group is created. All mesh elements that are
853  *  present in both initial groups are added to the new one.
854  */
855 //=============================================================================
856 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
857                                                       SMESH::SMESH_GroupBase_ptr theGroup2,
858                                                       const char* theName )
859   throw (SALOME::SALOME_Exception)
860 {
861   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
862        theGroup1->GetType() != theGroup2->GetType() )
863     return SMESH::SMESH_Group::_nil();
864
865   // Create Intersection
866   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
867   if ( aResGrp->_is_nil() )
868     return aResGrp;
869
870   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
871   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
872
873   TColStd_MapOfInteger aMap1;
874
875   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
876     aMap1.Add( anIds1[ i1 ] );
877
878   TColStd_SequenceOfInteger aSeq;
879
880   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
881     if ( aMap1.Contains( anIds2[ i2 ] ) )
882       aSeq.Append( anIds2[ i2 ] );
883
884   SMESH::long_array_var aResIds = new SMESH::long_array;
885   aResIds->length( aSeq.Length() );
886
887   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
888     aResIds[ resI ] = aSeq( resI + 1 );
889
890   aResGrp->Add( aResIds );
891
892   // Clear python lines, created by CreateGroup() and Add()
893   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
894   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
895   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
896
897   // Update Python script
898   TPythonDump() << aResGrp << " = " << _this() << ".IntersectGroups( "
899                 << theGroup1 << ", " << theGroup2 << ", '" << theName << "')";
900
901   return aResGrp._retn();
902 }
903
904 //=============================================================================
905 /*! CutGroups
906  *  New group is created. All mesh elements that are present in
907  *  main group but do not present in tool group are added to the new one
908  */
909 //=============================================================================
910 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1,
911                                                 SMESH::SMESH_GroupBase_ptr theGroup2,
912                                                 const char* theName )
913   throw (SALOME::SALOME_Exception)
914 {
915   if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
916        theGroup1->GetType() != theGroup2->GetType() )
917     return SMESH::SMESH_Group::_nil();
918
919   // Perform Cutting
920   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
921   if ( aResGrp->_is_nil() )
922     return aResGrp;
923
924   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
925   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
926
927   TColStd_MapOfInteger aMap2;
928
929   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
930     aMap2.Add( anIds2[ i2 ] );
931
932
933   TColStd_SequenceOfInteger aSeq;
934   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
935     if ( !aMap2.Contains( anIds1[ i1 ] ) )
936       aSeq.Append( anIds1[ i1 ] );
937
938   SMESH::long_array_var aResIds = new SMESH::long_array;
939   aResIds->length( aSeq.Length() );
940
941   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
942     aResIds[ resI ] = aSeq( resI + 1 );
943
944   aResGrp->Add( aResIds );
945
946   // Clear python lines, created by CreateGroup() and Add()
947   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
948   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
949   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
950
951   // Update Python script
952   TPythonDump() << aResGrp << " = " << _this() << ".CutGroups( "
953                 << theGroup1 << ", " << theGroup2 << ", '"
954                 << theName << "' )";
955
956   return aResGrp._retn();
957 }
958
959 //=============================================================================
960 /*!
961  *
962  */
963 //=============================================================================
964
965 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
966 {
967   if(MYDEBUG) MESSAGE( "createSubMesh" );
968   TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
969
970   ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
971   int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
972   SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
973   SMESH::SMESH_subMesh_var subMesh
974     = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
975
976   _mapSubMesh[subMeshId] = mySubMesh;
977   _mapSubMesh_i[subMeshId] = subMeshServant;
978   _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
979
980   // register CORBA object for persistence
981   int nextId = _gen_i->RegisterObject( subMesh );
982   if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
983
984   return subMesh._retn();
985 }
986
987 //=======================================================================
988 //function : getSubMesh
989 //purpose  :
990 //=======================================================================
991
992 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
993 {
994   map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
995   if ( it == _mapSubMeshIor.end() )
996     return SMESH::SMESH_subMesh::_nil();
997
998   return SMESH::SMESH_subMesh::_duplicate( (*it).second );
999 }
1000
1001
1002 //=============================================================================
1003 /*!
1004  *
1005  */
1006 //=============================================================================
1007
1008 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
1009                                   GEOM::GEOM_Object_ptr    theSubShapeObject )
1010 {
1011   MESSAGE("SMESH_Mesh_i::removeSubMesh()");
1012   if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
1013     return;
1014
1015   try {
1016     SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
1017     for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
1018       removeHypothesis( theSubShapeObject, aHypList[i] );
1019     }
1020   }
1021   catch( const SALOME::SALOME_Exception& ) {
1022     INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
1023   }
1024
1025   int subMeshId = theSubMesh->GetId();
1026
1027   _mapSubMesh.erase(subMeshId);
1028   _mapSubMesh_i.erase(subMeshId);
1029   _mapSubMeshIor.erase(subMeshId);
1030   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
1031 }
1032
1033 //=============================================================================
1034 /*!
1035  *
1036  */
1037 //=============================================================================
1038
1039 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
1040                                                       const char*         theName,
1041                                                       const TopoDS_Shape& theShape )
1042 {
1043   int anId;
1044   SMESH::SMESH_GroupBase_var aGroup;
1045   if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
1046     SMESH_GroupBase_i* aGroupImpl;
1047     if ( !theShape.IsNull() )
1048       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
1049     else
1050       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
1051
1052     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
1053     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
1054     aGroupImpl->Register();
1055     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
1056
1057     aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
1058     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
1059
1060     // register CORBA object for persistence
1061     int nextId = _gen_i->RegisterObject( aGroup );
1062     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
1063   }
1064   return aGroup._retn();
1065 }
1066
1067 //=============================================================================
1068 /*!
1069  * SMESH_Mesh_i::removeGroup
1070  *
1071  * Should be called by ~SMESH_Group_i()
1072  */
1073 //=============================================================================
1074
1075 void SMESH_Mesh_i::removeGroup( const int theId )
1076 {
1077   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );
1078   if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
1079     _mapGroups.erase( theId );
1080     _impl->RemoveGroup( theId );
1081   }
1082 }
1083
1084
1085 //=============================================================================
1086 /*!
1087  *
1088  */
1089 //=============================================================================
1090
1091 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
1092 throw(SALOME::SALOME_Exception)
1093 {
1094   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
1095
1096   SMESH::log_array_var aLog;
1097   try{
1098     list < SMESHDS_Command * >logDS = _impl->GetLog();
1099     aLog = new SMESH::log_array;
1100     int indexLog = 0;
1101     int lg = logDS.size();
1102     SCRUTE(lg);
1103     aLog->length(lg);
1104     list < SMESHDS_Command * >::iterator its = logDS.begin();
1105     while(its != logDS.end()){
1106       SMESHDS_Command *com = *its;
1107       int comType = com->GetType();
1108       //SCRUTE(comType);
1109       int lgcom = com->GetNumber();
1110       //SCRUTE(lgcom);
1111       const list < int >&intList = com->GetIndexes();
1112       int inum = intList.size();
1113       //SCRUTE(inum);
1114       list < int >::const_iterator ii = intList.begin();
1115       const list < double >&coordList = com->GetCoords();
1116       int rnum = coordList.size();
1117       //SCRUTE(rnum);
1118       list < double >::const_iterator ir = coordList.begin();
1119       aLog[indexLog].commandType = comType;
1120       aLog[indexLog].number = lgcom;
1121       aLog[indexLog].coords.length(rnum);
1122       aLog[indexLog].indexes.length(inum);
1123       for(int i = 0; i < rnum; i++){
1124         aLog[indexLog].coords[i] = *ir;
1125         //MESSAGE(" "<<i<<" "<<ir.Value());
1126         ir++;
1127       }
1128       for(int i = 0; i < inum; i++){
1129         aLog[indexLog].indexes[i] = *ii;
1130         //MESSAGE(" "<<i<<" "<<ii.Value());
1131         ii++;
1132       }
1133       indexLog++;
1134       its++;
1135     }
1136     if(clearAfterGet)
1137       _impl->ClearLog();
1138   }
1139   catch(SALOME_Exception & S_ex){
1140     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1141   }
1142   return aLog._retn();
1143 }
1144
1145
1146 //=============================================================================
1147 /*!
1148  *
1149  */
1150 //=============================================================================
1151
1152 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
1153 {
1154   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
1155   // ****
1156 }
1157
1158 //=============================================================================
1159 /*!
1160  *
1161  */
1162 //=============================================================================
1163
1164 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1165 {
1166   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1167   return _id;
1168 }
1169
1170 //=============================================================================
1171 /*!
1172  *
1173  */
1174 //=============================================================================
1175
1176 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1177 {
1178   return _studyId;
1179 }
1180
1181 //=============================================================================
1182 /*!
1183  *
1184  */
1185 //=============================================================================
1186
1187 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1188 {
1189   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1190   _impl = impl;
1191 }
1192
1193 //=============================================================================
1194 /*!
1195  *
1196  */
1197 //=============================================================================
1198
1199 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1200 {
1201   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1202   return *_impl;
1203 }
1204
1205 //=============================================================================
1206 /*!
1207  * Return mesh editor
1208  */
1209 //=============================================================================
1210
1211 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1212 {
1213   // Create MeshEditor
1214   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl, false );
1215   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1216
1217   // Update Python script
1218   TPythonDump() << aMeshEditor << " = " << _this() << ".GetMeshEditor()";
1219
1220   return aMesh._retn();
1221 }
1222
1223 //=============================================================================
1224 /*!
1225  * Return mesh edition previewer
1226  */
1227 //=============================================================================
1228
1229 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditPreviewer()
1230 {
1231   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl, true );
1232   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1233   return aMesh._retn();
1234 }
1235
1236 //=============================================================================
1237 /*!
1238  *  Export in different formats
1239  */
1240 //=============================================================================
1241
1242 CORBA::Boolean SMESH_Mesh_i::HasDuplicatedGroupNamesMED()
1243 {
1244   return _impl->HasDuplicatedGroupNamesMED();
1245 }
1246
1247 static void PrepareForWriting (const char* file)
1248 {
1249   TCollection_AsciiString aFullName ((char*)file);
1250   OSD_Path aPath (aFullName);
1251   OSD_File aFile (aPath);
1252   if (aFile.Exists()) {
1253     // existing filesystem node
1254     if (aFile.KindOfFile() == OSD_FILE) {
1255       if (aFile.IsWriteable()) {
1256         aFile.Reset();
1257         aFile.Remove();
1258         if (aFile.Failed()) {
1259           TCollection_AsciiString msg ("File ");
1260           msg += aFullName + " cannot be replaced.";
1261           THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1262         }
1263       } else {
1264         TCollection_AsciiString msg ("File ");
1265         msg += aFullName + " cannot be overwritten.";
1266         THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1267       }
1268     } else {
1269       TCollection_AsciiString msg ("Location ");
1270       msg += aFullName + " is not a file.";
1271       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1272     }
1273   } else {
1274     // nonexisting file; check if it can be created
1275     aFile.Reset();
1276     aFile.Build(OSD_WriteOnly, OSD_Protection());
1277     if (aFile.Failed()) {
1278       TCollection_AsciiString msg ("You cannot create the file ");
1279       msg += aFullName + ". Check the directory existance and access rights.";
1280       THROW_SALOME_CORBA_EXCEPTION(msg.ToCString(), SALOME::BAD_PARAM);
1281     } else {
1282       aFile.Close();
1283       aFile.Remove();
1284     }
1285   }
1286 }
1287
1288 void SMESH_Mesh_i::ExportToMED (const char* file,
1289                                 CORBA::Boolean auto_groups,
1290                                 SMESH::MED_VERSION theVersion)
1291   throw(SALOME::SALOME_Exception)
1292 {
1293   Unexpect aCatch(SALOME_SalomeException);
1294
1295   // Update Python script
1296   TPythonDump() << _this() << ".ExportToMED( '"
1297                 << file << "', " << auto_groups << ", " << theVersion << " )";
1298
1299   // Perform Export
1300   PrepareForWriting(file);
1301   char* aMeshName = "Mesh";
1302   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1303   if ( !aStudy->_is_nil() ) {
1304     SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1305     if ( !aMeshSO->_is_nil() ) {
1306       aMeshName = aMeshSO->GetName();
1307       //SCRUTE(file);
1308       //SCRUTE(aMeshName);
1309       //SCRUTE(aMeshSO->GetID());
1310
1311       // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes
1312       if ( !aStudy->GetProperties()->IsLocked() )
1313         {
1314         SALOMEDS::GenericAttribute_var anAttr;
1315         SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1316         SALOMEDS::AttributeExternalFileDef_var aFileName;
1317         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1318         aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1319         ASSERT(!aFileName->_is_nil());
1320         aFileName->SetValue(file);
1321         SALOMEDS::AttributeFileType_var aFileType;
1322         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1323         aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1324         ASSERT(!aFileType->_is_nil());
1325         aFileType->SetValue("FICHIERMED");
1326         }
1327     }
1328   }
1329   _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1330 }
1331
1332 void SMESH_Mesh_i::ExportMED (const char* file,
1333                               CORBA::Boolean auto_groups)
1334   throw(SALOME::SALOME_Exception)
1335 {
1336   ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1337 }
1338
1339 void SMESH_Mesh_i::ExportDAT (const char *file)
1340   throw(SALOME::SALOME_Exception)
1341 {
1342   Unexpect aCatch(SALOME_SalomeException);
1343
1344   // Update Python script
1345   TPythonDump() << _this() << ".ExportDAT( '" << file << "' )";
1346
1347   // Perform Export
1348   PrepareForWriting(file);
1349   _impl->ExportDAT(file);
1350 }
1351
1352 void SMESH_Mesh_i::ExportUNV (const char *file)
1353   throw(SALOME::SALOME_Exception)
1354 {
1355   Unexpect aCatch(SALOME_SalomeException);
1356
1357   // Update Python script
1358   TPythonDump() << _this() << ".ExportUNV( '" << file << "' )";
1359
1360   // Perform Export
1361   PrepareForWriting(file);
1362   _impl->ExportUNV(file);
1363 }
1364
1365 void SMESH_Mesh_i::ExportSTL (const char *file, const bool isascii)
1366   throw(SALOME::SALOME_Exception)
1367 {
1368   Unexpect aCatch(SALOME_SalomeException);
1369
1370   // Update Python script
1371   TPythonDump() << _this() << ".ExportSTL( '" << file << "', " << isascii << " )";
1372
1373   // Perform Export
1374   PrepareForWriting(file);
1375   _impl->ExportSTL(file, isascii);
1376 }
1377
1378 //=============================================================================
1379 /*!
1380  *
1381  */
1382 //=============================================================================
1383
1384 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1385 {
1386   Unexpect aCatch(SALOME_SalomeException);
1387   SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1388   SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1389   return aMesh._retn();
1390 }
1391
1392 //=============================================================================
1393 /*!
1394  *
1395  */
1396 //=============================================================================
1397 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1398 {
1399   Unexpect aCatch(SALOME_SalomeException);
1400   return _impl->NbNodes();
1401 }
1402
1403 //=============================================================================
1404 /*!
1405  *
1406  */
1407 //=============================================================================
1408 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1409 {
1410   Unexpect aCatch(SALOME_SalomeException);
1411   return NbEdges() + NbFaces() + NbVolumes();
1412 }
1413
1414 //=============================================================================
1415 /*!
1416  *
1417  */
1418 //=============================================================================
1419 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1420 {
1421   Unexpect aCatch(SALOME_SalomeException);
1422   return _impl->NbEdges();
1423 }
1424
1425 CORBA::Long SMESH_Mesh_i::NbEdgesOfOrder(SMESH::ElementOrder order)
1426   throw(SALOME::SALOME_Exception)
1427 {
1428   Unexpect aCatch(SALOME_SalomeException);
1429   return _impl->NbEdges( (::SMESH_Mesh::ElementOrder) order);
1430 }
1431
1432 //=============================================================================
1433 /*!
1434  *
1435  */
1436 //=============================================================================
1437 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1438 {
1439   Unexpect aCatch(SALOME_SalomeException);
1440   return _impl->NbFaces();
1441 }
1442
1443 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1444 {
1445   Unexpect aCatch(SALOME_SalomeException);
1446   return _impl->NbTriangles();
1447 }
1448
1449 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1450 {
1451   Unexpect aCatch(SALOME_SalomeException);
1452   return _impl->NbQuadrangles();
1453 }
1454
1455 CORBA::Long SMESH_Mesh_i::NbPolygons()throw(SALOME::SALOME_Exception)
1456 {
1457   Unexpect aCatch(SALOME_SalomeException);
1458   return _impl->NbPolygons();
1459 }
1460
1461 CORBA::Long SMESH_Mesh_i::NbFacesOfOrder(SMESH::ElementOrder order)
1462   throw(SALOME::SALOME_Exception)
1463 {
1464   Unexpect aCatch(SALOME_SalomeException);
1465   return _impl->NbFaces( (::SMESH_Mesh::ElementOrder) order);
1466 }
1467
1468 CORBA::Long SMESH_Mesh_i::NbTrianglesOfOrder(SMESH::ElementOrder order)
1469   throw(SALOME::SALOME_Exception)
1470 {
1471   Unexpect aCatch(SALOME_SalomeException);
1472   return _impl->NbTriangles( (::SMESH_Mesh::ElementOrder) order);
1473 }
1474
1475 CORBA::Long SMESH_Mesh_i::NbQuadranglesOfOrder(SMESH::ElementOrder order)
1476   throw(SALOME::SALOME_Exception)
1477 {
1478   Unexpect aCatch(SALOME_SalomeException);
1479   return _impl->NbQuadrangles( (::SMESH_Mesh::ElementOrder) order);
1480 }
1481
1482 //=============================================================================
1483 /*!
1484  *
1485  */
1486 //=============================================================================
1487 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1488 {
1489   Unexpect aCatch(SALOME_SalomeException);
1490   return _impl->NbVolumes();
1491 }
1492
1493 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1494 {
1495   Unexpect aCatch(SALOME_SalomeException);
1496   return _impl->NbTetras();
1497 }
1498
1499 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1500 {
1501   Unexpect aCatch(SALOME_SalomeException);
1502   return _impl->NbHexas();
1503 }
1504
1505 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1506 {
1507   Unexpect aCatch(SALOME_SalomeException);
1508   return _impl->NbPyramids();
1509 }
1510
1511 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1512 {
1513   Unexpect aCatch(SALOME_SalomeException);
1514   return _impl->NbPrisms();
1515 }
1516
1517 CORBA::Long SMESH_Mesh_i::NbPolyhedrons()throw(SALOME::SALOME_Exception)
1518 {
1519   Unexpect aCatch(SALOME_SalomeException);
1520   return _impl->NbPolyhedrons();
1521 }
1522
1523 CORBA::Long SMESH_Mesh_i::NbVolumesOfOrder(SMESH::ElementOrder order)
1524   throw(SALOME::SALOME_Exception)
1525 {
1526   Unexpect aCatch(SALOME_SalomeException);
1527   return _impl->NbVolumes( (::SMESH_Mesh::ElementOrder) order);
1528 }
1529
1530 CORBA::Long SMESH_Mesh_i::NbTetrasOfOrder(SMESH::ElementOrder order)
1531   throw(SALOME::SALOME_Exception)
1532 {
1533   Unexpect aCatch(SALOME_SalomeException);
1534   return _impl->NbTetras( (::SMESH_Mesh::ElementOrder) order);
1535 }
1536
1537 CORBA::Long SMESH_Mesh_i::NbHexasOfOrder(SMESH::ElementOrder order)
1538   throw(SALOME::SALOME_Exception)
1539 {
1540   Unexpect aCatch(SALOME_SalomeException);
1541   return _impl->NbHexas( (::SMESH_Mesh::ElementOrder) order);
1542 }
1543
1544 CORBA::Long SMESH_Mesh_i::NbPyramidsOfOrder(SMESH::ElementOrder order)
1545   throw(SALOME::SALOME_Exception)
1546 {
1547   Unexpect aCatch(SALOME_SalomeException);
1548   return _impl->NbPyramids( (::SMESH_Mesh::ElementOrder) order);
1549 }
1550
1551 CORBA::Long SMESH_Mesh_i::NbPrismsOfOrder(SMESH::ElementOrder order)
1552   throw(SALOME::SALOME_Exception)
1553 {
1554   Unexpect aCatch(SALOME_SalomeException);
1555   return _impl->NbPrisms( (::SMESH_Mesh::ElementOrder) order);
1556 }
1557
1558 //=============================================================================
1559 /*!
1560  *
1561  */
1562 //=============================================================================
1563 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1564 {
1565   Unexpect aCatch(SALOME_SalomeException);
1566   return _impl->NbSubMesh();
1567 }
1568
1569 //=============================================================================
1570 /*!
1571  *
1572  */
1573 //=============================================================================
1574 char* SMESH_Mesh_i::Dump()
1575 {
1576   std::ostringstream os;
1577   _impl->Dump( os );
1578   return CORBA::string_dup( os.str().c_str() );
1579 }
1580
1581 //=============================================================================
1582 /*!
1583  *
1584  */
1585 //=============================================================================
1586 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1587 {
1588 //   SMESH::long_array_var aResult = new SMESH::long_array();
1589 //   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1590 //   int aMinId = aSMESHDS_Mesh->MinElementID();
1591 //   int aMaxId =  aSMESHDS_Mesh->MaxElementID();
1592
1593 //   aResult->length(aMaxId - aMinId + 1);
1594
1595 //   for (int i = 0, id = aMinId; id <= aMaxId; id++  )
1596 //     aResult[i++] = id;
1597
1598 //   return aResult._retn();
1599   // PAL12398
1600   return GetElementsId();
1601 }
1602
1603 //=============================================================================
1604 /*!
1605  *
1606  */
1607 //=============================================================================
1608
1609 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1610      throw (SALOME::SALOME_Exception)
1611 {
1612   Unexpect aCatch(SALOME_SalomeException);
1613   MESSAGE("SMESH_Mesh_i::GetElementsId");
1614   SMESH::long_array_var aResult = new SMESH::long_array();
1615   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1616
1617   if ( aSMESHDS_Mesh == NULL )
1618     return aResult._retn();
1619
1620   long nbElements = NbElements();
1621   aResult->length( nbElements );
1622   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1623   for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1624     aResult[i] = anIt->next()->GetID();
1625
1626   return aResult._retn();
1627 }
1628
1629
1630 //=============================================================================
1631 /*!
1632  *
1633  */
1634 //=============================================================================
1635
1636 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1637     throw (SALOME::SALOME_Exception)
1638 {
1639   Unexpect aCatch(SALOME_SalomeException);
1640   MESSAGE("SMESH_subMesh_i::GetElementsByType");
1641   SMESH::long_array_var aResult = new SMESH::long_array();
1642   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1643
1644   if ( aSMESHDS_Mesh == NULL )
1645     return aResult._retn();
1646
1647   long nbElements = NbElements();
1648
1649   // No sense in returning ids of elements along with ids of nodes:
1650   // when theElemType == SMESH::ALL, return node ids only if
1651   // there are no elements
1652   if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1653     return GetNodesId();
1654
1655   aResult->length( nbElements );
1656
1657   int i = 0;
1658
1659   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1660   while ( i < nbElements && anIt->more() ) {
1661     const SMDS_MeshElement* anElem = anIt->next();
1662     if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1663       aResult[i++] = anElem->GetID();
1664   }
1665
1666   aResult->length( i );
1667
1668   return aResult._retn();
1669 }
1670
1671 //=============================================================================
1672 /*!
1673  *
1674  */
1675 //=============================================================================
1676
1677 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1678   throw (SALOME::SALOME_Exception)
1679 {
1680   Unexpect aCatch(SALOME_SalomeException);
1681   MESSAGE("SMESH_subMesh_i::GetNodesId");
1682   SMESH::long_array_var aResult = new SMESH::long_array();
1683   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1684
1685   if ( aSMESHDS_Mesh == NULL )
1686     return aResult._retn();
1687
1688   long nbNodes = NbNodes();
1689   aResult->length( nbNodes );
1690   SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1691   for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1692     aResult[i] = anIt->next()->GetID();
1693
1694   return aResult._retn();
1695 }
1696
1697 //=============================================================================
1698 /*!
1699  *
1700  */
1701 //=============================================================================
1702
1703 SMESH::ElementType SMESH_Mesh_i::GetElementType( const CORBA::Long id, const bool iselem )
1704   throw (SALOME::SALOME_Exception)
1705 {
1706   return ( SMESH::ElementType )_impl->GetElementType( id, iselem );
1707 }
1708
1709
1710 //=============================================================================
1711 /*!
1712  * Returns ID of elements for given submesh
1713  */
1714 //=============================================================================
1715 SMESH::long_array* SMESH_Mesh_i::GetSubMeshElementsId(const CORBA::Long ShapeID)
1716      throw (SALOME::SALOME_Exception)
1717 {
1718   SMESH::long_array_var aResult = new SMESH::long_array();
1719
1720   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
1721   if(!SM) return aResult._retn();
1722
1723   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
1724   if(!SDSM) return aResult._retn();
1725
1726   aResult->length(SDSM->NbElements());
1727
1728   SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
1729   int i = 0;
1730   while ( eIt->more() ) {
1731     aResult[i++] = eIt->next()->GetID();
1732   }
1733
1734   return aResult._retn();
1735 }
1736
1737
1738 //=============================================================================
1739 /*!
1740  * Returns ID of nodes for given submesh
1741  * If param all==true - returns all nodes, else -
1742  * returns only nodes on shapes.
1743  */
1744 //=============================================================================
1745 SMESH::long_array* SMESH_Mesh_i::GetSubMeshNodesId(const CORBA::Long ShapeID, CORBA::Boolean all)
1746      throw (SALOME::SALOME_Exception)
1747 {
1748   SMESH::long_array_var aResult = new SMESH::long_array();
1749
1750   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
1751   if(!SM) return aResult._retn();
1752
1753   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
1754   if(!SDSM) return aResult._retn();
1755
1756   map<int,const SMDS_MeshElement*> theElems;
1757   if( !all || (SDSM->NbElements()==0 && SDSM->NbNodes()==1) ) {
1758     SMDS_NodeIteratorPtr nIt = SDSM->GetNodes();
1759     while ( nIt->more() ) {
1760       const SMDS_MeshNode* elem = nIt->next();
1761       theElems.insert( make_pair(elem->GetID(),elem) );
1762     }
1763   }
1764   else { // all nodes of submesh elements
1765     SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
1766     while ( eIt->more() ) {
1767       const SMDS_MeshElement* anElem = eIt->next();
1768       SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
1769       while ( nIt->more() ) {
1770         const SMDS_MeshElement* elem = nIt->next();
1771         theElems.insert( make_pair(elem->GetID(),elem) );
1772       }
1773     }
1774   }
1775
1776   aResult->length(theElems.size());
1777   map<int, const SMDS_MeshElement * >::iterator itElem;
1778   int i = 0;
1779   for ( itElem = theElems.begin(); itElem != theElems.end(); itElem++ )
1780     aResult[i++] = (*itElem).first;
1781
1782   return aResult._retn();
1783 }
1784   
1785
1786 //=============================================================================
1787 /*!
1788  * Returns type of elements for given submesh
1789  */
1790 //=============================================================================
1791 SMESH::ElementType SMESH_Mesh_i::GetSubMeshElementType(const CORBA::Long ShapeID)
1792      throw (SALOME::SALOME_Exception)
1793 {
1794   SMESH_subMesh* SM = _impl->GetSubMeshContaining(ShapeID);
1795   if(!SM) return SMESH::ALL;
1796
1797   SMESHDS_SubMesh* SDSM = SM->GetSubMeshDS();
1798   if(!SDSM) return SMESH::ALL;
1799
1800   if(SDSM->NbElements()==0)
1801     return (SM->GetSubShape().ShapeType() == TopAbs_VERTEX) ? SMESH::NODE : SMESH::ALL;
1802
1803   SMDS_ElemIteratorPtr eIt = SDSM->GetElements();
1804   const SMDS_MeshElement* anElem = eIt->next();
1805   return ( SMESH::ElementType ) anElem->GetType();
1806 }
1807   
1808
1809 //=============================================================================
1810 /*!
1811  *
1812  */
1813 //=============================================================================
1814
1815 CORBA::LongLong SMESH_Mesh_i::GetMeshPtr()
1816 {
1817   CORBA::LongLong pointeur = CORBA::LongLong(_impl);
1818   cerr << "CORBA::LongLong SMESH_Mesh_i::GetMeshPtr() " << pointeur << endl;
1819   return pointeur;
1820 }
1821
1822
1823 //=============================================================================
1824 /*!
1825  * Get XYZ coordinates of node as list of double
1826  * If there is not node for given ID - returns empty list
1827  */
1828 //=============================================================================
1829
1830 SMESH::double_array* SMESH_Mesh_i::GetNodeXYZ(const CORBA::Long id)
1831 {
1832   SMESH::double_array_var aResult = new SMESH::double_array();
1833   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1834   if ( aSMESHDS_Mesh == NULL )
1835     return aResult._retn();
1836
1837   // find node
1838   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
1839   if(!aNode)
1840     return aResult._retn();
1841
1842   // add coordinates
1843   aResult->length(3);
1844   aResult[0] = aNode->X();
1845   aResult[1] = aNode->Y();
1846   aResult[2] = aNode->Z();
1847   return aResult._retn();
1848 }
1849
1850
1851 //=============================================================================
1852 /*!
1853  * For given node returns list of IDs of inverse elements
1854  * If there is not node for given ID - returns empty list
1855  */
1856 //=============================================================================
1857
1858 SMESH::long_array* SMESH_Mesh_i::GetNodeInverseElements(const CORBA::Long id)
1859 {
1860   SMESH::long_array_var aResult = new SMESH::long_array();
1861   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1862   if ( aSMESHDS_Mesh == NULL )
1863     return aResult._retn();
1864
1865   // find node
1866   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
1867   if(!aNode)
1868     return aResult._retn();
1869
1870   // find inverse elements
1871   SMDS_ElemIteratorPtr eIt = aNode->GetInverseElementIterator();
1872   TColStd_SequenceOfInteger IDs;
1873   while(eIt->more()) {
1874     const SMDS_MeshElement* elem = eIt->next();
1875     IDs.Append(elem->GetID());
1876   }
1877   if(IDs.Length()>0) {
1878     aResult->length(IDs.Length());
1879     int i = 1;
1880     for(; i<=IDs.Length(); i++) {
1881       aResult[i-1] = IDs.Value(i);
1882     }
1883   }
1884   return aResult._retn();
1885 }
1886
1887
1888 //=============================================================================
1889 /*!
1890  * If given element is node returns IDs of shape from position
1891  * If there is not node for given ID - returns -1
1892  */
1893 //=============================================================================
1894
1895 CORBA::Long SMESH_Mesh_i::GetShapeID(const CORBA::Long id)
1896 {
1897   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1898   if ( aSMESHDS_Mesh == NULL )
1899     return -1;
1900
1901   // try to find node
1902   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(id);
1903   if(aNode) {
1904     SMDS_PositionPtr pos = aNode->GetPosition();
1905     if(!pos)
1906       return -1;
1907     else
1908       return pos->GetShapeId();
1909   }
1910
1911   return -1;
1912 }
1913
1914
1915 //=============================================================================
1916 /*!
1917  * For given element returns ID of result shape after 
1918  * ::FindShape() from SMESH_MeshEditor
1919  * If there is not element for given ID - returns -1
1920  */
1921 //=============================================================================
1922
1923 CORBA::Long SMESH_Mesh_i::GetShapeIDForElem(const CORBA::Long id)
1924 {
1925   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1926   if ( aSMESHDS_Mesh == NULL )
1927     return -1;
1928
1929   // try to find element
1930   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
1931   if(!elem)
1932     return -1;
1933
1934   //SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
1935   ::SMESH_MeshEditor aMeshEditor(_impl);
1936   int index = aMeshEditor.FindShape( elem );
1937   if(index>0)
1938     return index;
1939
1940   return -1;
1941 }
1942
1943
1944 //=============================================================================
1945 /*!
1946  * Returns number of nodes for given element
1947  * If there is not element for given ID - returns -1
1948  */
1949 //=============================================================================
1950
1951 CORBA::Long SMESH_Mesh_i::GetElemNbNodes(const CORBA::Long id)
1952 {
1953   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1954   if ( aSMESHDS_Mesh == NULL ) return -1;
1955   // try to find element
1956   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
1957   if(!elem) return -1;
1958   return elem->NbNodes();
1959 }
1960
1961
1962 //=============================================================================
1963 /*!
1964  * Returns ID of node by given index for given element
1965  * If there is not element for given ID - returns -1
1966  * If there is not node for given index - returns -2
1967  */
1968 //=============================================================================
1969
1970 CORBA::Long SMESH_Mesh_i::GetElemNode(const CORBA::Long id, const CORBA::Long index)
1971 {
1972   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1973   if ( aSMESHDS_Mesh == NULL ) return -1;
1974   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
1975   if(!elem) return -1;
1976   if( index>=elem->NbNodes() || index<0 ) return -1;
1977   return elem->GetNode(index)->GetID();
1978 }
1979
1980 //=============================================================================
1981 /*!
1982  * Returns IDs of nodes of given element
1983  */
1984 //=============================================================================
1985
1986 SMESH::long_array* SMESH_Mesh_i::GetElemNodes(const CORBA::Long id)
1987 {
1988   SMESH::long_array_var aResult = new SMESH::long_array();
1989   if ( SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS() )
1990   {
1991     if ( const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id) )
1992     {
1993       aResult->length( elem->NbNodes() );
1994       for ( int i = 0; i < elem->NbNodes(); ++i )
1995         aResult[ i ] = elem->GetNode( i )->GetID();
1996     }
1997   }
1998   return aResult._retn();
1999 }
2000
2001 //=============================================================================
2002 /*!
2003  * Returns true if given node is medium node
2004  * in given quadratic element
2005  */
2006 //=============================================================================
2007
2008 CORBA::Boolean SMESH_Mesh_i::IsMediumNode(const CORBA::Long ide, const CORBA::Long idn)
2009 {
2010   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2011   if ( aSMESHDS_Mesh == NULL ) return false;
2012   // try to find node
2013   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(idn);
2014   if(!aNode) return false;
2015   // try to find element
2016   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(ide);
2017   if(!elem) return false;
2018
2019   return elem->IsMediumNode(aNode);
2020 }
2021
2022
2023 //=============================================================================
2024 /*!
2025  * Returns true if given node is medium node
2026  * in one of quadratic elements
2027  */
2028 //=============================================================================
2029
2030 CORBA::Boolean SMESH_Mesh_i::IsMediumNodeOfAnyElem(const CORBA::Long idn,
2031                                                    SMESH::ElementType theElemType)
2032 {
2033   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2034   if ( aSMESHDS_Mesh == NULL ) return false;
2035
2036   // try to find node
2037   const SMDS_MeshNode* aNode = aSMESHDS_Mesh->FindNode(idn);
2038   if(!aNode) return false;
2039
2040   SMESH_MesherHelper aHelper( *(_impl) );
2041
2042   SMDSAbs_ElementType aType;
2043   if(theElemType==SMESH::EDGE) aType = SMDSAbs_Edge;
2044   else if(theElemType==SMESH::FACE) aType = SMDSAbs_Face;
2045   else if(theElemType==SMESH::VOLUME) aType = SMDSAbs_Volume;
2046   else aType = SMDSAbs_All;
2047
2048   return aHelper.IsMedium(aNode,aType);
2049 }
2050
2051
2052 //=============================================================================
2053 /*!
2054  * Returns number of edges for given element
2055  */
2056 //=============================================================================
2057
2058 CORBA::Long SMESH_Mesh_i::ElemNbEdges(const CORBA::Long id)
2059 {
2060   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2061   if ( aSMESHDS_Mesh == NULL ) return -1;
2062   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2063   if(!elem) return -1;
2064   return elem->NbEdges();
2065 }
2066
2067
2068 //=============================================================================
2069 /*!
2070  * Returns number of faces for given element
2071  */
2072 //=============================================================================
2073
2074 CORBA::Long SMESH_Mesh_i::ElemNbFaces(const CORBA::Long id)
2075 {
2076   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2077   if ( aSMESHDS_Mesh == NULL ) return -1;
2078   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2079   if(!elem) return -1;
2080   return elem->NbFaces();
2081 }
2082
2083
2084 //=============================================================================
2085 /*!
2086  * Returns true if given element is polygon
2087  */
2088 //=============================================================================
2089
2090 CORBA::Boolean SMESH_Mesh_i::IsPoly(const CORBA::Long id)
2091 {
2092   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2093   if ( aSMESHDS_Mesh == NULL ) return false;
2094   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2095   if(!elem) return false;
2096   return elem->IsPoly();
2097 }
2098
2099
2100 //=============================================================================
2101 /*!
2102  * Returns true if given element is quadratic
2103  */
2104 //=============================================================================
2105
2106 CORBA::Boolean SMESH_Mesh_i::IsQuadratic(const CORBA::Long id)
2107 {
2108   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2109   if ( aSMESHDS_Mesh == NULL ) return false;
2110   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2111   if(!elem) return false;
2112   return elem->IsQuadratic();
2113 }
2114
2115
2116 //=============================================================================
2117 /*!
2118  * Returns bary center for given element
2119  */
2120 //=============================================================================
2121
2122 SMESH::double_array* SMESH_Mesh_i::BaryCenter(const CORBA::Long id)
2123 {
2124   SMESH::double_array_var aResult = new SMESH::double_array();
2125   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
2126   if ( aSMESHDS_Mesh == NULL )
2127     return aResult._retn();
2128
2129   const SMDS_MeshElement* elem = aSMESHDS_Mesh->FindElement(id);
2130   if(!elem)
2131     return aResult._retn();
2132
2133   if(elem->GetType()==SMDSAbs_Volume) {
2134     // use SMDS_VolumeTool
2135     SMDS_VolumeTool aTool;
2136     if(aTool.Set(elem)) {
2137       double x=0., y=0., z=0.;
2138       if(aTool.GetBaryCenter(x,y,z)) {
2139         // add coordinates
2140         aResult->length(3);
2141         aResult[0] = x;
2142         aResult[1] = y;
2143         aResult[2] = z;
2144       }
2145     }
2146   }
2147   else {
2148     SMDS_ElemIteratorPtr anIt = elem->nodesIterator();
2149     int nbn = 0;
2150     double x=0., y=0., z=0.;
2151     for(; anIt->more(); ) {
2152       nbn++;
2153       const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(anIt->next());
2154       x += aNode->X();
2155       y += aNode->Y();
2156       z += aNode->Z();
2157     }
2158     if(nbn>0) {
2159       // add coordinates
2160       aResult->length(3);
2161       aResult[0] = x/nbn;
2162       aResult[1] = y/nbn;
2163       aResult[2] = z/nbn;
2164     }
2165   }
2166
2167   return aResult._retn();
2168 }
2169
2170
2171 //=============================================================================
2172 /*!
2173  *
2174  */
2175 //=============================================================================
2176 void SMESH_Mesh_i::CreateGroupServants() 
2177 {
2178   // Create group servants, if any groups were imported
2179   list<int> aGroupIds = _impl->GetGroupIds();
2180   for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
2181     SMESH_Group_i* aGroupImpl     = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
2182
2183     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
2184     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
2185     aGroupImpl->Register();
2186     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
2187
2188     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
2189     _mapGroups[*it]               = SMESH::SMESH_Group::_duplicate( aGroup );
2190
2191     // register CORBA object for persistence
2192     int nextId = _gen_i->RegisterObject( aGroup );
2193     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
2194   }
2195 }
2196