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