Salome HOME
PAL8581. Really check smoothing
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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
35 #include "Utils_CorbaException.hxx"
36 #include "Utils_ExceptHandlers.hxx"
37 #include "utilities.h"
38
39 #include "SALOME_NamingService.hxx"
40 #include "Utils_SINGLETON.hxx"
41 #include "OpUtil.hxx"
42
43 #include "TCollection_AsciiString.hxx"
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
50 #include <TColStd_MapOfInteger.hxx>
51 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
52 #include <TColStd_SequenceOfInteger.hxx>
53
54 #include <string>
55 #include <iostream>
56 #include <sstream>
57
58 #ifdef _DEBUG_
59 static int MYDEBUG = 0;
60 #else
61 static int MYDEBUG = 0;
62 #endif
63
64 using namespace std;
65
66 int SMESH_Mesh_i::myIdGenerator = 0;
67
68 //=============================================================================
69 /*!
70  *  Constructor
71  */
72 //=============================================================================
73
74 SMESH_Mesh_i::SMESH_Mesh_i( PortableServer::POA_ptr thePOA,
75                             SMESH_Gen_i*            gen_i,
76                             CORBA::Long studyId )
77 : SALOME::GenericObj_i( thePOA )
78 {
79   INFOS("SMESH_Mesh_i");
80   _gen_i = gen_i;
81   _id = myIdGenerator++;
82   _studyId = studyId;
83   thePOA->activate_object( this );
84 }
85
86 //=============================================================================
87 /*!
88  *  Destructor
89  */
90 //=============================================================================
91
92 SMESH_Mesh_i::~SMESH_Mesh_i()
93 {
94   INFOS("~SMESH_Mesh_i");
95   map<int, SMESH::SMESH_GroupBase_ptr>::iterator it;
96   for ( it = _mapGroups.begin(); it != _mapGroups.end(); it++ ) {
97     SMESH_GroupBase_i* aGroup = dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( it->second ).in() );
98     if ( aGroup ) {
99
100       // this method is colled from destructor of group (PAL6331)
101       //_impl->RemoveGroup( aGroup->GetLocalID() );
102       
103       aGroup->Destroy();
104     }
105   }
106   _mapGroups.clear();
107 }
108
109 //=============================================================================
110 /*!
111  *  SetShape
112  *
113  *  Associates <this> mesh with <theShape> and puts a reference  
114  *  to <theShape> into the current study; 
115  *  the previous shape is substituted by the new one.
116  */
117 //=============================================================================
118
119 void SMESH_Mesh_i::SetShape( GEOM::GEOM_Object_ptr theShapeObject )
120     throw (SALOME::SALOME_Exception)
121 {
122   Unexpect aCatch(SALOME_SalomeException);
123   try {
124     _impl->ShapeToMesh( _gen_i->GeomObjectToShape( theShapeObject ));
125   }
126   catch(SALOME_Exception & S_ex) {
127     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
128   }
129 }
130
131 //=======================================================================
132 //function : GetShapeToMesh
133 //purpose  : 
134 //=======================================================================
135
136 GEOM::GEOM_Object_ptr SMESH_Mesh_i::GetShapeToMesh()
137     throw (SALOME::SALOME_Exception)
138 {
139   Unexpect aCatch(SALOME_SalomeException);
140   GEOM::GEOM_Object_var aShapeObj;
141   try {
142     TopoDS_Shape S = _impl->GetMeshDS()->ShapeToMesh();
143     if ( !S.IsNull() )
144       aShapeObj = _gen_i->ShapeToGeomObject( S );
145   }
146   catch(SALOME_Exception & S_ex) {
147     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
148   }
149   return aShapeObj._retn();
150 }
151
152 //=============================================================================
153 /*!
154  *  
155  */
156 //=============================================================================
157
158 static SMESH::DriverMED_ReadStatus ConvertDriverMEDReadStatus (int theStatus)
159 {
160   SMESH::DriverMED_ReadStatus res;
161   switch (theStatus)
162   {
163   case DriverMED_R_SMESHDS_Mesh::DRS_OK:
164     res = SMESH::DRS_OK; break;
165   case DriverMED_R_SMESHDS_Mesh::DRS_EMPTY:
166     res = SMESH::DRS_EMPTY; break;
167   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_RENUMBER:
168     res = SMESH::DRS_WARN_RENUMBER; break;
169   case DriverMED_R_SMESHDS_Mesh::DRS_WARN_SKIP_ELEM:
170     res = SMESH::DRS_WARN_SKIP_ELEM; break;
171   case DriverMED_R_SMESHDS_Mesh::DRS_FAIL:
172   default:
173     res = SMESH::DRS_FAIL; break;
174   }
175   return res;
176 }
177
178 //=============================================================================
179 /*!
180  *  ImportMEDFile
181  *
182  *  Imports mesh data from MED file
183  */
184 //=============================================================================
185
186 SMESH::DriverMED_ReadStatus
187 SMESH_Mesh_i::ImportMEDFile( const char* theFileName, const char* theMeshName )
188   throw ( SALOME::SALOME_Exception )
189 {
190   Unexpect aCatch(SALOME_SalomeException);
191   int status;
192   try {
193     status = importMEDFile( theFileName, theMeshName );
194   }
195   catch( SALOME_Exception& S_ex ) {
196     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
197   }  
198   catch ( ... ) {
199     THROW_SALOME_CORBA_EXCEPTION("ImportMEDFile(): unknown exception", SALOME::BAD_PARAM);
200   }
201
202   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
203   if ( !aStudy->_is_nil() ) {
204     // publishing of the groups in the study (sub-meshes are out of scope of MED import)
205     map<int, SMESH::SMESH_GroupBase_ptr>::iterator it = _mapGroups.begin();
206     for (; it != _mapGroups.end(); it++ ) {
207       SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_duplicate( it->second );
208       _gen_i->PublishGroup( aStudy, _this(), aGroup,
209                            GEOM::GEOM_Object::_nil(), aGroup->GetName());
210     }
211   }
212   return ConvertDriverMEDReadStatus(status);
213 }
214
215 //=============================================================================
216 /*!
217  *  ImportUNVFile
218  *
219  *  Imports mesh data from MED file
220  */
221 //=============================================================================
222
223 int SMESH_Mesh_i::ImportUNVFile( const char* theFileName )
224   throw ( SALOME::SALOME_Exception )
225 {
226   // Read mesh with name = <theMeshName> into SMESH_Mesh
227   _impl->UNVToMesh( theFileName );
228
229   return 1;
230 }
231
232 //=============================================================================
233 /*!
234  *  ImportSTLFile
235  *
236  *  Imports mesh data from STL file
237  */
238 //=============================================================================
239 int SMESH_Mesh_i::ImportSTLFile( const char* theFileName )
240   throw ( SALOME::SALOME_Exception )
241 {
242   // Read mesh with name = <theMeshName> into SMESH_Mesh
243   _impl->STLToMesh( theFileName );
244
245   return 1;
246 }
247
248 //=============================================================================
249 /*!
250  *  importMEDFile
251  *
252  *  Imports mesh data from MED file
253  */
254 //=============================================================================
255
256 int SMESH_Mesh_i::importMEDFile( const char* theFileName, const char* theMeshName )
257 {
258   // Read mesh with name = <theMeshName> and all its groups into SMESH_Mesh
259   int status = _impl->MEDToMesh( theFileName, theMeshName );
260
261   // Create group servants, if any groups were imported
262   list<int> aGroupIds = _impl->GetGroupIds();
263   for ( list<int>::iterator it = aGroupIds.begin(); it != aGroupIds.end(); it++ ) {
264     SMESH_Group_i* aGroupImpl     = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, *it );
265
266     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
267     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
268     aGroupImpl->Register();
269     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
270
271     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( aGroupImpl->_this() );
272     _mapGroups[*it]               = SMESH::SMESH_Group::_duplicate( aGroup );
273
274     // register CORBA object for persistence
275     int nextId = _gen_i->RegisterObject( aGroup );
276     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
277   }
278
279   return status;
280 }
281
282 //=============================================================================
283 /*!
284  *  
285  */
286 //=============================================================================
287
288 static SMESH::Hypothesis_Status ConvertHypothesisStatus
289                          (SMESH_Hypothesis::Hypothesis_Status theStatus)
290 {
291   SMESH::Hypothesis_Status res;
292   switch (theStatus)
293   {
294   case SMESH_Hypothesis::HYP_OK:
295     res = SMESH::HYP_OK; break;
296   case SMESH_Hypothesis::HYP_MISSING:
297     res = SMESH::HYP_MISSING; break;
298   case SMESH_Hypothesis::HYP_CONCURENT:
299     res = SMESH::HYP_CONCURENT; break;
300   case SMESH_Hypothesis::HYP_BAD_PARAMETER:
301     res = SMESH::HYP_BAD_PARAMETER; break;
302   case SMESH_Hypothesis::HYP_INCOMPATIBLE:
303     res = SMESH::HYP_INCOMPATIBLE; break;
304   case SMESH_Hypothesis::HYP_NOTCONFORM:
305     res = SMESH::HYP_NOTCONFORM; break;
306   case SMESH_Hypothesis::HYP_ALREADY_EXIST:
307     res = SMESH::HYP_ALREADY_EXIST; break;
308   case SMESH_Hypothesis::HYP_BAD_DIM:
309     res = SMESH::HYP_BAD_DIM; break;
310   default:
311     res = SMESH::HYP_UNKNOWN_FATAL;
312   }
313   return res;
314 }
315
316 //=============================================================================
317 /*!
318  *  AddHypothesis
319  *
320  *  calls internal addHypothesis() and then adds a reference to <anHyp> under 
321  *  the SObject actually having a reference to <aSubShape>.
322  *  NB: For this method to work, it is necessary to add a reference to sub-shape first.
323  */
324 //=============================================================================
325
326 SMESH::Hypothesis_Status SMESH_Mesh_i::AddHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
327                                                      SMESH::SMESH_Hypothesis_ptr anHyp)
328   throw(SALOME::SALOME_Exception)
329 {
330   Unexpect aCatch(SALOME_SalomeException);
331   SMESH_Hypothesis::Hypothesis_Status status = addHypothesis( aSubShapeObject, anHyp );
332
333   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
334     _gen_i->AddHypothesisToShape(_gen_i->GetCurrentStudy(), _this(),
335                                  aSubShapeObject, anHyp );
336
337   if(MYDEBUG) MESSAGE( " AddHypothesis(): status = " << status );
338
339   // Update Python script
340   TCollection_AsciiString aStr ("status = ");
341   SMESH_Gen_i::AddObject(aStr, _this()) += ".AddHypothesis(";
342   SMESH_Gen_i::AddObject(aStr, aSubShapeObject) += ", ";
343   SMESH_Gen_i::AddObject(aStr, anHyp) += ")";
344
345   SMESH_Gen_i::AddToCurrentPyScript(aStr);
346
347   return ConvertHypothesisStatus(status);
348 }
349
350 //=============================================================================
351 /*!
352  *  
353  */
354 //=============================================================================
355
356 SMESH_Hypothesis::Hypothesis_Status
357   SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr       aSubShapeObject,
358                               SMESH::SMESH_Hypothesis_ptr anHyp)
359 {
360   if(MYDEBUG) MESSAGE("addHypothesis");
361
362   if (CORBA::is_nil(aSubShapeObject))
363     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
364                                  SALOME::BAD_PARAM);
365
366   SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
367   if (CORBA::is_nil(myHyp))
368     THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
369                                  SALOME::BAD_PARAM);
370
371   SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
372   try
373   {
374     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
375     int hypId = myHyp->GetId();
376     status = _impl->AddHypothesis(myLocSubShape, hypId);
377     if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
378       _mapHypo[hypId] = myHyp;
379       // assure there is a corresponding submesh
380       if ( !_impl->IsMainShape( myLocSubShape )) {
381         int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
382         if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
383           createSubMesh( aSubShapeObject );
384       }
385     }
386   }
387   catch(SALOME_Exception & S_ex)
388   {
389     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
390   }
391   return status;
392 }
393
394 //=============================================================================
395 /*!
396  *  
397  */
398 //=============================================================================
399
400 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
401                                                         SMESH::SMESH_Hypothesis_ptr anHyp)
402      throw(SALOME::SALOME_Exception)
403 {
404   Unexpect aCatch(SALOME_SalomeException);
405   SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
406
407   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
408     _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
409                                       aSubShapeObject, anHyp );
410
411   // Update Python script
412   TCollection_AsciiString aStr ("status = ");
413   SMESH_Gen_i::AddObject(aStr, _this()) += ".RemoveHypothesis(";
414   SMESH_Gen_i::AddObject(aStr, aSubShapeObject) += ", ";
415   SMESH_Gen_i::AddObject(aStr, anHyp) += ")";
416
417   SMESH_Gen_i::AddToCurrentPyScript(aStr);
418
419   return ConvertHypothesisStatus(status);
420 }
421
422 //=============================================================================
423 /*!
424  *  
425  */
426 //=============================================================================
427
428 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
429                                  SMESH::SMESH_Hypothesis_ptr anHyp)
430 {
431         if(MYDEBUG) MESSAGE("removeHypothesis()");
432         // **** proposer liste de subShape (selection multiple)
433
434         if (CORBA::is_nil(aSubShapeObject))
435                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
436                         SALOME::BAD_PARAM);
437
438         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
439         if (CORBA::is_nil(myHyp))
440           THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
441                         SALOME::BAD_PARAM);
442
443         SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
444         try
445         {
446                 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
447                 int hypId = myHyp->GetId();
448                 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
449                 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
450                   _mapHypo.erase( hypId );
451         }
452         catch(SALOME_Exception & S_ex)
453         {
454                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
455         }
456         return status;
457 }
458
459 //=============================================================================
460 /*!
461  *  
462  */
463 //=============================================================================
464
465 SMESH::ListOfHypothesis *
466         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
467 throw(SALOME::SALOME_Exception)
468 {
469   Unexpect aCatch(SALOME_SalomeException);
470   if (MYDEBUG) MESSAGE("GetHypothesisList");
471   if (CORBA::is_nil(aSubShapeObject))
472     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
473                                  SALOME::BAD_PARAM);
474   
475   SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
476
477   try {
478     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
479     const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
480     int i = 0, n = aLocalList.size();
481     aList->length( n );
482
483     for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
484       SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
485       if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
486         aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
487     }
488
489     aList->length( i );
490   }
491   catch(SALOME_Exception & S_ex) {
492     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
493   }
494   
495   return aList._retn();
496 }
497
498 //=============================================================================
499 /*!
500  *  
501  */
502 //=============================================================================
503 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
504                                                   const char*           theName ) 
505      throw(SALOME::SALOME_Exception)
506 {
507   Unexpect aCatch(SALOME_SalomeException);
508   MESSAGE("SMESH_Mesh_i::GetSubMesh");
509   if (CORBA::is_nil(aSubShapeObject))
510     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
511                                  SALOME::BAD_PARAM);
512   
513   SMESH::SMESH_subMesh_var subMesh;
514   SMESH::SMESH_Mesh_var    aMesh = SMESH::SMESH_Mesh::_narrow(_this());
515   try {
516     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
517     
518     //Get or Create the SMESH_subMesh object implementation
519     
520     int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
521     subMesh = getSubMesh( subMeshId );
522
523     // create a new subMesh object servant if there is none for the shape
524     if ( subMesh->_is_nil() )
525       subMesh = createSubMesh( aSubShapeObject );
526
527     if ( _gen_i->CanPublishInStudy( subMesh )) {
528       SALOMEDS::SObject_var aSO =
529         _gen_i->PublishSubMesh(_gen_i->GetCurrentStudy(), aMesh,
530                                subMesh, aSubShapeObject, theName );
531       if ( !aSO->_is_nil()) {
532         // Update Python script
533         TCollection_AsciiString aStr (aSO->GetID());
534         aStr += " = ";
535         SMESH_Gen_i::AddObject(aStr, _this()) += ".GetSubMesh(";
536         SMESH_Gen_i::AddObject(aStr, aSubShapeObject) += ", \"";
537         aStr += (char*)theName;
538         aStr += "\")";
539
540         SMESH_Gen_i::AddToCurrentPyScript(aStr);
541       }
542     }
543   }
544   catch(SALOME_Exception & S_ex) {
545     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
546   }
547   return subMesh._retn();
548 }
549
550 //=============================================================================
551 /*!
552  *  
553  */
554 //=============================================================================
555
556 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
557      throw (SALOME::SALOME_Exception)
558 {
559   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
560   if ( theSubMesh->_is_nil() )
561     return;
562
563   GEOM::GEOM_Object_var aSubShapeObject;
564   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
565   if ( !aStudy->_is_nil() )  {
566     // Remove submesh's SObject
567     SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
568     if ( !anSO->_is_nil() ) {
569       long aTag = SMESH_Gen_i::GetRefOnShapeTag(); 
570       SALOMEDS::SObject_var anObj, aRef;
571       if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
572         aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
573
574       aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
575
576       // Update Python script
577       TCollection_AsciiString aStr;
578       SMESH_Gen_i::AddObject(aStr, _this()) += ".RemoveSubMesh(";
579       aStr += anSO->GetID();
580       aStr += ")";
581
582       SMESH_Gen_i::AddToCurrentPyScript(aStr);
583     }
584   }
585
586   removeSubMesh( theSubMesh, aSubShapeObject.in() );
587 }
588
589 //=============================================================================
590 /*!
591  *  ElementTypeString
592  */
593 //=============================================================================
594 inline TCollection_AsciiString ElementTypeString (SMESH::ElementType theElemType)
595 {
596   TCollection_AsciiString aStr;
597   switch (theElemType) {
598   case SMESH::ALL:
599     aStr = "SMESH.ALL";
600     break;
601   case SMESH::NODE:
602     aStr = "SMESH.NODE";
603     break;
604   case SMESH::EDGE:
605     aStr = "SMESH.EDGE";
606     break;
607   case SMESH::FACE:
608     aStr = "SMESH.FACE";
609     break;
610   case SMESH::VOLUME:
611     aStr = "SMESH.VOLUME";
612     break;
613   default:
614     break;
615   }
616   return aStr;
617 }
618
619 //=============================================================================
620 /*!
621  *  
622  */
623 //=============================================================================
624
625 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
626                                                  const char*         theName )
627      throw(SALOME::SALOME_Exception)
628 {
629   Unexpect aCatch(SALOME_SalomeException);
630   SMESH::SMESH_Group_var aNewGroup =
631     SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
632
633   if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
634     SALOMEDS::SObject_var aSO =
635       _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(),
636                            aNewGroup, GEOM::GEOM_Object::_nil(), theName);
637     if ( !aSO->_is_nil()) {
638       // Update Python script
639       TCollection_AsciiString aStr (aSO->GetID());
640       aStr += " = ";
641       SMESH_Gen_i::AddObject(aStr, _this()) += ".CreateGroup(";
642       aStr += ElementTypeString(theElemType) + ", \"" + (char*)theName + "\")";
643
644       SMESH_Gen_i::AddToCurrentPyScript(aStr);
645     }
646   }
647
648   return aNewGroup._retn();
649 }
650
651
652 //=============================================================================
653 /*!
654  *  
655  */
656 //=============================================================================
657 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM (SMESH::ElementType    theElemType,
658                                                                 const char*           theName,
659                                                                 GEOM::GEOM_Object_ptr theGeomObj)
660      throw(SALOME::SALOME_Exception)
661 {
662   Unexpect aCatch(SALOME_SalomeException);
663   SMESH::SMESH_GroupOnGeom_var aNewGroup;
664
665   TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
666   if ( !aShape.IsNull() ) {
667     aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
668       ( createGroup( theElemType, theName, aShape ));
669     if ( _gen_i->CanPublishInStudy( aNewGroup ) ) {
670       SALOMEDS::SObject_var aSO =
671         _gen_i->PublishGroup(_gen_i->GetCurrentStudy(), _this(), 
672                              aNewGroup, theGeomObj, theName);
673       if ( !aSO->_is_nil()) {
674         // Update Python script
675         TCollection_AsciiString aStr (aSO->GetID());
676         aStr += " = ";
677         SMESH_Gen_i::AddObject(aStr, _this()) += ".CreateGroupFromGEOM(";
678         aStr += ElementTypeString(theElemType) + ", \"" + (char*)theName + "\", ";
679         SMESH_Gen_i::AddObject(aStr, theGeomObj) += ")";
680
681         SMESH_Gen_i::AddToCurrentPyScript(aStr);
682       }
683     }
684   }
685
686   return aNewGroup._retn();
687 }
688
689 //=============================================================================
690 /*!
691  *  
692  */
693 //=============================================================================
694
695 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
696      throw (SALOME::SALOME_Exception)
697 {
698   if ( theGroup->_is_nil() )
699     return;
700
701   SMESH_GroupBase_i* aGroup =
702     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
703   if ( !aGroup )
704     return;
705
706   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
707   if ( !aStudy->_is_nil() )  {
708     SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
709
710     if ( !aGroupSO->_is_nil() ) {
711       // Update Python script
712       TCollection_AsciiString aStr;
713       SMESH_Gen_i::AddObject(aStr, _this()) += ".RemoveGroup(";
714       aStr += aGroupSO->GetID();
715       aStr += ")";
716
717       SMESH_Gen_i::AddToCurrentPyScript(aStr);
718
719       // Remove group's SObject
720       aStudy->NewBuilder()->RemoveObject( aGroupSO );
721     }
722   }
723
724   // Remove the group from SMESH data structures
725   removeGroup( aGroup->GetLocalID() );
726 }
727
728 //=============================================================================
729 /*! RemoveGroupWithContents
730  *  Remove group with its contents
731  */ 
732 //=============================================================================
733 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
734   throw (SALOME::SALOME_Exception)
735 {
736   if ( theGroup->_is_nil() )
737     return;
738
739   SMESH_GroupBase_i* aGroup =
740     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
741   if ( !aGroup )
742     return;
743   
744   SMESH::long_array_var anIds = aGroup->GetListOfID();
745   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
746
747   // Update Python script
748   TCollection_AsciiString aStr;
749   SMESH_Gen_i::AddObject(aStr, _this()) += ".RemoveGroupWithContents(";
750   SMESH_Gen_i::AddObject(aStr, theGroup) += ")";
751
752   SMESH_Gen_i::AddToCurrentPyScript(aStr);
753
754   // Remove contents
755   if ( aGroup->GetType() == SMESH::NODE )
756     aMeshEditor->RemoveNodes( anIds );
757   else
758     aMeshEditor->RemoveElements( anIds );
759
760   // Remove group
761   RemoveGroup( theGroup );
762
763   // Clear python lines, created by RemoveNodes/Elements() and RemoveGroup()
764   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
765   _gen_i->RemoveLastFromPythonScript(_gen_i->GetCurrentStudy()->StudyId());
766 }
767
768 //=============================================================================
769 /*! UnionGroups
770  *  New group is created. All mesh elements that are 
771  *  present in initial groups are added to the new one
772  */
773 //=============================================================================
774 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1, 
775                                                   SMESH::SMESH_GroupBase_ptr theGroup2, 
776                                                   const char* theName )
777   throw (SALOME::SALOME_Exception)
778 {
779   try
780   {
781     if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
782          theGroup1->GetType() != theGroup2->GetType() )
783       return SMESH::SMESH_Group::_nil();
784
785     // Create Union
786     SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
787     if ( aResGrp->_is_nil() )
788       return SMESH::SMESH_Group::_nil();
789
790     SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
791     SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
792
793     TColStd_MapOfInteger aResMap;
794
795     for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
796       aResMap.Add( anIds1[ i1 ] );
797
798     for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
799       aResMap.Add( anIds2[ i2 ] );
800
801     SMESH::long_array_var aResIds = new SMESH::long_array;
802     aResIds->length( aResMap.Extent() );
803
804     int resI = 0;
805     TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
806     for( ; anIter.More(); anIter.Next() )
807       aResIds[ resI++ ] = anIter.Key();
808
809     aResGrp->Add( aResIds );
810
811     // Clear python lines, created by CreateGroup() and Add()
812     SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
813     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
814     _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
815
816     // Update Python script
817     TCollection_AsciiString aStr;
818     SMESH_Gen_i::AddObject(aStr, aResGrp) += " = ";
819     SMESH_Gen_i::AddObject(aStr, _this()) += ".UnionGroups(";
820     SMESH_Gen_i::AddObject(aStr, theGroup1) += ", ";
821     SMESH_Gen_i::AddObject(aStr, theGroup2) += ", \"";
822     aStr += TCollection_AsciiString((char*)theName) + "\")";
823
824     SMESH_Gen_i::AddToCurrentPyScript(aStr);
825
826     return aResGrp._retn();
827   }
828   catch( ... )
829   {
830     return SMESH::SMESH_Group::_nil();
831   }
832 }
833   
834 //=============================================================================
835 /*! IntersectGroups
836  *  New group is created. All mesh elements that are 
837  *  present in both initial groups are added to the new one.
838  */
839 //=============================================================================
840 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1, 
841                                                       SMESH::SMESH_GroupBase_ptr theGroup2, 
842                                                       const char* theName )
843   throw (SALOME::SALOME_Exception)
844 {
845   if ( theGroup1->_is_nil() || theGroup2->_is_nil() || 
846        theGroup1->GetType() != theGroup2->GetType() )
847     return SMESH::SMESH_Group::_nil();
848
849   // Create Intersection
850   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
851   if ( aResGrp->_is_nil() )
852     return aResGrp;
853
854   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
855   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
856
857   TColStd_MapOfInteger aMap1;
858
859   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
860     aMap1.Add( anIds1[ i1 ] );
861
862   TColStd_SequenceOfInteger aSeq;
863
864   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
865     if ( aMap1.Contains( anIds2[ i2 ] ) )
866       aSeq.Append( anIds2[ i2 ] );
867
868   SMESH::long_array_var aResIds = new SMESH::long_array;
869   aResIds->length( aSeq.Length() );
870
871   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
872     aResIds[ resI ] = aSeq( resI + 1 );
873
874   aResGrp->Add( aResIds );
875
876   // Clear python lines, created by CreateGroup() and Add()
877   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
878   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
879   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
880
881   // Update Python script
882   TCollection_AsciiString aStr;
883   SMESH_Gen_i::AddObject(aStr, aResGrp) += " = ";
884   SMESH_Gen_i::AddObject(aStr, _this()) += ".IntersectGroups(";
885   SMESH_Gen_i::AddObject(aStr, theGroup1) += ", ";
886   SMESH_Gen_i::AddObject(aStr, theGroup2) += ", \"";
887   aStr += TCollection_AsciiString((char*)theName) + "\")";
888
889   SMESH_Gen_i::AddToCurrentPyScript(aStr);
890
891   return aResGrp._retn();
892 }
893
894 //=============================================================================
895 /*! CutGroups
896  *  New group is created. All mesh elements that are present in 
897  *  main group but do not present in tool group are added to the new one 
898  */
899 //=============================================================================
900 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1, 
901                                                 SMESH::SMESH_GroupBase_ptr theGroup2, 
902                                                 const char* theName )
903   throw (SALOME::SALOME_Exception)
904 {
905   if ( theGroup1->_is_nil() || theGroup2->_is_nil() || 
906        theGroup1->GetType() != theGroup2->GetType() )
907     return SMESH::SMESH_Group::_nil();
908
909   // Perform Cutting
910   SMESH::SMESH_Group_var aResGrp = CreateGroup( theGroup1->GetType(), theName );
911   if ( aResGrp->_is_nil() )
912     return aResGrp;
913
914   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
915   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
916
917   TColStd_MapOfInteger aMap2;
918
919   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
920     aMap2.Add( anIds2[ i2 ] );
921
922
923   TColStd_SequenceOfInteger aSeq;
924   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
925     if ( !aMap2.Contains( anIds1[ i1 ] ) )
926       aSeq.Append( anIds1[ i1 ] );
927
928   SMESH::long_array_var aResIds = new SMESH::long_array;
929   aResIds->length( aSeq.Length() );
930
931   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
932     aResIds[ resI ] = aSeq( resI + 1 );  
933
934   aResGrp->Add( aResIds );
935
936   // Clear python lines, created by CreateGroup() and Add()
937   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
938   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
939   _gen_i->RemoveLastFromPythonScript(aStudy->StudyId());
940
941   // Update Python script
942   TCollection_AsciiString aStr;
943   SMESH_Gen_i::AddObject(aStr, aResGrp) += " = ";
944   SMESH_Gen_i::AddObject(aStr, _this()) += ".CutGroups(";
945   SMESH_Gen_i::AddObject(aStr, theGroup1) += ", ";
946   SMESH_Gen_i::AddObject(aStr, theGroup2) += ", \"";
947   aStr += TCollection_AsciiString((char*)theName) + "\")";
948
949   SMESH_Gen_i::AddToCurrentPyScript(aStr);
950
951   return aResGrp._retn();
952 }
953
954 //=============================================================================
955 /*!
956  *  
957  */
958 //=============================================================================
959
960 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
961 {
962   if(MYDEBUG) MESSAGE( "createSubMesh" );
963   TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
964
965   ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
966   int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
967   SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
968   SMESH::SMESH_subMesh_var subMesh
969     = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
970
971   _mapSubMesh[subMeshId] = mySubMesh;
972   _mapSubMesh_i[subMeshId] = subMeshServant;
973   _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
974
975   // register CORBA object for persistence
976   int nextId = _gen_i->RegisterObject( subMesh );
977   if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
978
979   return subMesh._retn(); 
980 }
981
982 //=======================================================================
983 //function : getSubMesh
984 //purpose  : 
985 //=======================================================================
986
987 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
988 {
989   map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
990   if ( it == _mapSubMeshIor.end() )
991     return SMESH::SMESH_subMesh::_nil();
992
993   return SMESH::SMESH_subMesh::_duplicate( (*it).second );
994 }
995
996
997 //=============================================================================
998 /*!
999  *  
1000  */
1001 //=============================================================================
1002
1003 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
1004                                   GEOM::GEOM_Object_ptr    theSubShapeObject )
1005 {
1006   MESSAGE("SMESH_Mesh_i::removeSubMesh()");
1007   if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
1008     return;
1009
1010   try {
1011     SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
1012     for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
1013       removeHypothesis( theSubShapeObject, aHypList[i] );
1014     }
1015   }
1016   catch( const SALOME::SALOME_Exception& ) {
1017     INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
1018   }
1019
1020   int subMeshId = theSubMesh->GetId();
1021
1022   _mapSubMesh.erase(subMeshId);
1023   _mapSubMesh_i.erase(subMeshId);
1024   _mapSubMeshIor.erase(subMeshId);
1025   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
1026 }
1027
1028 //=============================================================================
1029 /*!
1030  *  
1031  */
1032 //=============================================================================
1033
1034 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
1035                                                       const char*         theName,
1036                                                       const TopoDS_Shape& theShape )
1037 {
1038   int anId;
1039   SMESH::SMESH_GroupBase_var aGroup;
1040   if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
1041     SMESH_GroupBase_i* aGroupImpl;
1042     if ( !theShape.IsNull() )
1043       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
1044     else
1045       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
1046
1047     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
1048     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
1049     aGroupImpl->Register();
1050     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
1051
1052     aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
1053     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
1054
1055     // register CORBA object for persistence
1056     int nextId = _gen_i->RegisterObject( aGroup );
1057     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
1058   }
1059   return aGroup._retn();
1060 }
1061
1062 //=============================================================================
1063 /*!
1064  * SMESH_Mesh_i::removeGroup
1065  *
1066  * Should be called by ~SMESH_Group_i() 
1067  */
1068 //=============================================================================
1069
1070 void SMESH_Mesh_i::removeGroup( const int theId )
1071 {
1072   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );  
1073   if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
1074     _mapGroups.erase( theId );
1075     _impl->RemoveGroup( theId );
1076   }
1077 }
1078
1079
1080 //=============================================================================
1081 /*!
1082  *  
1083  */
1084 //=============================================================================
1085
1086 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
1087 throw(SALOME::SALOME_Exception)
1088 {
1089   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
1090   
1091   SMESH::log_array_var aLog;
1092   try{
1093     list < SMESHDS_Command * >logDS = _impl->GetLog();
1094     aLog = new SMESH::log_array;
1095     int indexLog = 0;
1096     int lg = logDS.size();
1097     SCRUTE(lg);
1098     aLog->length(lg);
1099     list < SMESHDS_Command * >::iterator its = logDS.begin();
1100     while(its != logDS.end()){
1101       SMESHDS_Command *com = *its;
1102       int comType = com->GetType();
1103       //SCRUTE(comType);
1104       int lgcom = com->GetNumber();
1105       //SCRUTE(lgcom);
1106       const list < int >&intList = com->GetIndexes();
1107       int inum = intList.size();
1108       //SCRUTE(inum);
1109       list < int >::const_iterator ii = intList.begin();
1110       const list < double >&coordList = com->GetCoords();
1111       int rnum = coordList.size();
1112       //SCRUTE(rnum);
1113       list < double >::const_iterator ir = coordList.begin();
1114       aLog[indexLog].commandType = comType;
1115       aLog[indexLog].number = lgcom;
1116       aLog[indexLog].coords.length(rnum);
1117       aLog[indexLog].indexes.length(inum);
1118       for(int i = 0; i < rnum; i++){
1119         aLog[indexLog].coords[i] = *ir;
1120         //MESSAGE(" "<<i<<" "<<ir.Value());
1121         ir++;
1122       }
1123       for(int i = 0; i < inum; i++){
1124         aLog[indexLog].indexes[i] = *ii;
1125         //MESSAGE(" "<<i<<" "<<ii.Value());
1126         ii++;
1127       }
1128       indexLog++;
1129       its++;
1130     }
1131     if(clearAfterGet)
1132       _impl->ClearLog();
1133   }
1134   catch(SALOME_Exception & S_ex){
1135     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1136   }
1137   return aLog._retn();
1138 }
1139
1140
1141 //=============================================================================
1142 /*!
1143  *  
1144  */
1145 //=============================================================================
1146
1147 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
1148 {
1149   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
1150   // ****
1151 }
1152
1153 //=============================================================================
1154 /*!
1155  *  
1156  */
1157 //=============================================================================
1158
1159 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1160 {
1161   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1162   return _id;
1163 }
1164
1165 //=============================================================================
1166 /*!
1167  *  
1168  */
1169 //=============================================================================
1170
1171 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1172 {
1173   return _studyId;
1174 }
1175
1176 //=============================================================================
1177 /*!
1178  *  
1179  */
1180 //=============================================================================
1181
1182 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1183 {
1184   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1185   _impl = impl;
1186 }
1187
1188 //=============================================================================
1189 /*!
1190  *  
1191  */
1192 //=============================================================================
1193
1194 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1195 {
1196   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1197   return *_impl;
1198 }
1199
1200
1201 //=============================================================================
1202 /*!
1203  *  
1204  */
1205 //=============================================================================
1206
1207 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1208 {
1209   // Update Python script
1210   TCollection_AsciiString aStr ("mesh_editor = ");
1211   SMESH_Gen_i::AddObject(aStr, _this()) += ".GetMeshEditor()";
1212
1213   SMESH_Gen_i::AddToCurrentPyScript(aStr);
1214
1215   // Create MeshEditor
1216   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl );
1217   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1218   return aMesh._retn();
1219 }
1220
1221 //=============================================================================
1222 /*!
1223  *  Export in different formats
1224  */
1225 //=============================================================================
1226
1227 void SMESH_Mesh_i::ExportToMED (const char* file, 
1228                                 CORBA::Boolean auto_groups, 
1229                                 SMESH::MED_VERSION theVersion)
1230   throw(SALOME::SALOME_Exception)
1231 {
1232   Unexpect aCatch(SALOME_SalomeException);
1233
1234   // Update Python script
1235   TCollection_AsciiString aStr;
1236   SMESH_Gen_i::AddObject(aStr, _this()) += ".ExportToMED(\"";
1237   aStr += TCollection_AsciiString((char*)file) + "\", ";
1238   aStr += TCollection_AsciiString((int)auto_groups) + ", ";
1239   switch (theVersion) {
1240   case SMESH::MED_V2_1:
1241     aStr += "SMESH.MED_V2_1)";
1242     break;
1243   case SMESH::MED_V2_2:
1244     aStr += "SMESH.MED_V2_2)";
1245     break;
1246   default:
1247     aStr += TCollection_AsciiString(theVersion) + ")";
1248     break;
1249   }
1250
1251   SMESH_Gen_i::AddToCurrentPyScript(aStr);
1252
1253   // Perform Export
1254   char* aMeshName = "Mesh";
1255   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1256   if ( !aStudy->_is_nil() ) {
1257     SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1258     if ( !aMeshSO->_is_nil() ) {
1259       aMeshName = aMeshSO->GetName();
1260       //SCRUTE(file);
1261       //SCRUTE(aMeshName);
1262       //SCRUTE(aMeshSO->GetID());
1263       
1264       // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes 
1265       if ( !aStudy->GetProperties()->IsLocked() ) 
1266         {
1267         SALOMEDS::GenericAttribute_var anAttr;
1268         SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1269         SALOMEDS::AttributeExternalFileDef_var aFileName;
1270         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1271         aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1272         ASSERT(!aFileName->_is_nil());
1273         aFileName->SetValue(file);
1274         SALOMEDS::AttributeFileType_var aFileType;
1275         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1276         aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1277         ASSERT(!aFileType->_is_nil());
1278         aFileType->SetValue("FICHIERMED");
1279         }
1280     }
1281   }
1282   _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1283 }
1284
1285 void SMESH_Mesh_i::ExportMED (const char* file, 
1286                               CORBA::Boolean auto_groups)
1287   throw(SALOME::SALOME_Exception)
1288 {
1289   ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1290 }
1291
1292 void SMESH_Mesh_i::ExportDAT (const char *file)
1293   throw(SALOME::SALOME_Exception)
1294 {
1295   Unexpect aCatch(SALOME_SalomeException);
1296
1297   // Update Python script
1298   TCollection_AsciiString aStr;
1299   SMESH_Gen_i::AddObject(aStr, _this()) += ".ExportDAT(\"";
1300   aStr += TCollection_AsciiString((char*)file) + "\")";
1301
1302   SMESH_Gen_i::AddToCurrentPyScript(aStr);
1303
1304   // Perform Export
1305   _impl->ExportDAT(file);
1306 }
1307
1308 void SMESH_Mesh_i::ExportUNV (const char *file)
1309   throw(SALOME::SALOME_Exception)
1310 {
1311   Unexpect aCatch(SALOME_SalomeException);
1312
1313   // Update Python script
1314   TCollection_AsciiString aStr;
1315   SMESH_Gen_i::AddObject(aStr, _this()) += ".ExportUNV(\"";
1316   aStr += TCollection_AsciiString((char*)file) + "\")";
1317
1318   SMESH_Gen_i::AddToCurrentPyScript(aStr);
1319
1320   // Perform Export
1321   _impl->ExportUNV(file);
1322 }
1323
1324 void SMESH_Mesh_i::ExportSTL (const char *file, const bool isascii)
1325   throw(SALOME::SALOME_Exception)
1326 {
1327   Unexpect aCatch(SALOME_SalomeException);
1328
1329   // Update Python script
1330   TCollection_AsciiString aStr;
1331   SMESH_Gen_i::AddObject(aStr, _this()) += ".ExportToMED(\"";
1332   aStr += TCollection_AsciiString((char*)file) + "\", ";
1333   aStr += TCollection_AsciiString((int)isascii) + ")";
1334
1335   SMESH_Gen_i::AddToCurrentPyScript(aStr);
1336
1337   // Perform Export
1338   _impl->ExportSTL(file, isascii);
1339 }
1340
1341 //=============================================================================
1342 /*!
1343  *  
1344  */
1345 //=============================================================================
1346
1347 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1348 {
1349   Unexpect aCatch(SALOME_SalomeException);
1350   SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1351   SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1352   return aMesh._retn();
1353 }
1354
1355 //=============================================================================
1356 /*!
1357  *  
1358  */
1359 //=============================================================================
1360 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1361 {
1362   Unexpect aCatch(SALOME_SalomeException);
1363   return _impl->NbNodes();
1364 }
1365
1366 //=============================================================================
1367 /*!
1368  *  
1369  */
1370 //=============================================================================
1371 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1372 {
1373   Unexpect aCatch(SALOME_SalomeException);
1374   return NbEdges() + NbFaces() + NbVolumes();
1375 }
1376   
1377 //=============================================================================
1378 /*!
1379  *  
1380  */
1381 //=============================================================================
1382 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1383 {
1384   Unexpect aCatch(SALOME_SalomeException);
1385   return _impl->NbEdges();
1386 }
1387
1388 //=============================================================================
1389 /*!
1390  *  
1391  */
1392 //=============================================================================
1393 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1394 {
1395   Unexpect aCatch(SALOME_SalomeException);
1396   return _impl->NbFaces();
1397 }
1398
1399 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1400 {
1401   Unexpect aCatch(SALOME_SalomeException);
1402   return _impl->NbTriangles();
1403 }
1404
1405 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1406 {
1407   Unexpect aCatch(SALOME_SalomeException);
1408   return _impl->NbQuadrangles();
1409 }
1410
1411 CORBA::Long SMESH_Mesh_i::NbPolygons()throw(SALOME::SALOME_Exception)
1412 {
1413   Unexpect aCatch(SALOME_SalomeException);
1414   return _impl->NbPolygons();
1415 }
1416
1417 //=============================================================================
1418 /*!
1419  *  
1420  */
1421 //=============================================================================
1422 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1423 {
1424   Unexpect aCatch(SALOME_SalomeException);
1425   return _impl->NbVolumes();
1426 }
1427
1428 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1429 {
1430   Unexpect aCatch(SALOME_SalomeException);
1431   return _impl->NbTetras();
1432 }
1433
1434 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1435 {
1436   Unexpect aCatch(SALOME_SalomeException);
1437   return _impl->NbHexas();
1438 }
1439
1440 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1441 {
1442   Unexpect aCatch(SALOME_SalomeException);
1443   return _impl->NbPyramids();
1444 }
1445
1446 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1447 {
1448   Unexpect aCatch(SALOME_SalomeException);
1449   return _impl->NbPrisms();
1450 }
1451
1452 CORBA::Long SMESH_Mesh_i::NbPolyhedrons()throw(SALOME::SALOME_Exception)
1453 {
1454   Unexpect aCatch(SALOME_SalomeException);
1455   return _impl->NbPolyhedrons();
1456 }
1457
1458 //=============================================================================
1459 /*!
1460  *  
1461  */
1462 //=============================================================================
1463 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1464 {
1465   Unexpect aCatch(SALOME_SalomeException);
1466   return _impl->NbSubMesh();
1467 }
1468
1469 //=============================================================================
1470 /*!
1471  *  
1472  */
1473 //=============================================================================
1474 char* SMESH_Mesh_i::Dump()
1475 {
1476   std::ostringstream os;
1477   _impl->Dump( os );
1478   return CORBA::string_dup( os.str().c_str() );
1479 }
1480
1481 //=============================================================================
1482 /*!
1483  *  
1484  */
1485 //=============================================================================
1486 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1487 {
1488   SMESH::long_array_var aResult = new SMESH::long_array();
1489   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1490   int aMinId = aSMESHDS_Mesh->MinElementID();
1491   int aMaxId =  aSMESHDS_Mesh->MaxElementID();
1492
1493   aResult->length(aMaxId - aMinId + 1);
1494   
1495   for (int i = 0, id = aMinId; id <= aMaxId; id++  )
1496     aResult[i++] = id;
1497   
1498   return aResult._retn();
1499 }
1500
1501 //=============================================================================
1502 /*!
1503  *  
1504  */
1505 //=============================================================================
1506   
1507 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1508      throw (SALOME::SALOME_Exception)
1509 {
1510   Unexpect aCatch(SALOME_SalomeException);
1511   MESSAGE("SMESH_Mesh_i::GetElementsId");
1512   SMESH::long_array_var aResult = new SMESH::long_array();
1513   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1514
1515   if ( aSMESHDS_Mesh == NULL )
1516     return aResult._retn();
1517
1518   long nbElements = NbElements();
1519   aResult->length( nbElements );
1520   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1521   for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1522     aResult[i] = anIt->next()->GetID();
1523
1524   return aResult._retn();
1525 }
1526
1527
1528 //=============================================================================
1529 /*!
1530  *  
1531  */
1532 //=============================================================================
1533
1534 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1535     throw (SALOME::SALOME_Exception)
1536 {
1537   Unexpect aCatch(SALOME_SalomeException);
1538   MESSAGE("SMESH_subMesh_i::GetElementsByType");
1539   SMESH::long_array_var aResult = new SMESH::long_array();
1540   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1541
1542   if ( aSMESHDS_Mesh == NULL )
1543     return aResult._retn();
1544
1545   long nbElements = NbElements();
1546
1547   // No sense in returning ids of elements along with ids of nodes:
1548   // when theElemType == SMESH::ALL, return node ids only if
1549   // there are no elements
1550   if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1551     return GetNodesId();
1552
1553   aResult->length( nbElements );
1554
1555   int i = 0;
1556
1557   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1558   while ( i < nbElements && anIt->more() ) {
1559     const SMDS_MeshElement* anElem = anIt->next();
1560     if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1561       aResult[i++] = anElem->GetID();
1562   }
1563
1564   aResult->length( i );
1565
1566   return aResult._retn();
1567 }
1568
1569 //=============================================================================
1570 /*!
1571  *  
1572  */
1573 //=============================================================================
1574   
1575 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1576   throw (SALOME::SALOME_Exception)
1577 {
1578   Unexpect aCatch(SALOME_SalomeException);
1579   MESSAGE("SMESH_subMesh_i::GetNodesId");
1580   SMESH::long_array_var aResult = new SMESH::long_array();
1581   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1582
1583   if ( aSMESHDS_Mesh == NULL )
1584     return aResult._retn();
1585
1586   long nbNodes = NbNodes();
1587   aResult->length( nbNodes );
1588   SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1589   for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1590     aResult[i] = anIt->next()->GetID();
1591
1592   return aResult._retn();
1593 }
1594