Salome HOME
Dump Python. Add AddToCurrentPyScript(), AddArray(), AddObject()
[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   SALOMEDS::SObject_var aMeshSO = SMESH_Gen_i::ObjectToSObject(_gen_i->GetCurrentStudy(), _this());
341   SALOMEDS::SObject_var aHypoSO = SMESH_Gen_i::ObjectToSObject(_gen_i->GetCurrentStudy(), anHyp);
342
343   TCollection_AsciiString aStr ("status = ");
344   aStr += aMeshSO->GetID();
345   aStr += ".AddHypothesis(salome.IDToObject(\"";
346   aStr += aSubShapeObject->GetStudyEntry();
347   aStr += "\"), ";
348   aStr += aHypoSO->GetID();
349   aStr += ")";
350
351   _gen_i->AddToPythonScript(_gen_i->GetCurrentStudy()->StudyId(), aStr);
352
353   aStr = "print \"AddHypothesis: \", status";
354   _gen_i->AddToPythonScript(_gen_i->GetCurrentStudy()->StudyId(), aStr);
355
356   return ConvertHypothesisStatus(status);
357 }
358
359 //=============================================================================
360 /*!
361  *  
362  */
363 //=============================================================================
364
365 SMESH_Hypothesis::Hypothesis_Status
366   SMESH_Mesh_i::addHypothesis(GEOM::GEOM_Object_ptr       aSubShapeObject,
367                               SMESH::SMESH_Hypothesis_ptr anHyp)
368 {
369   if(MYDEBUG) MESSAGE("addHypothesis");
370
371   if (CORBA::is_nil(aSubShapeObject))
372     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
373                                  SALOME::BAD_PARAM);
374
375   SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
376   if (CORBA::is_nil(myHyp))
377     THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
378                                  SALOME::BAD_PARAM);
379
380   SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
381   try
382   {
383     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape( aSubShapeObject);
384     int hypId = myHyp->GetId();
385     status = _impl->AddHypothesis(myLocSubShape, hypId);
386     if ( !SMESH_Hypothesis::IsStatusFatal(status) ) {
387       _mapHypo[hypId] = myHyp;
388       // assure there is a corresponding submesh
389       if ( !_impl->IsMainShape( myLocSubShape )) {
390         int shapeId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
391         if ( _mapSubMesh_i.find( shapeId ) == _mapSubMesh_i.end() )
392           createSubMesh( aSubShapeObject );
393       }
394     }
395   }
396   catch(SALOME_Exception & S_ex)
397   {
398     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
399   }
400   return status;
401 }
402
403 //=============================================================================
404 /*!
405  *  
406  */
407 //=============================================================================
408
409 SMESH::Hypothesis_Status SMESH_Mesh_i::RemoveHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
410                                                         SMESH::SMESH_Hypothesis_ptr anHyp)
411      throw(SALOME::SALOME_Exception)
412 {
413   Unexpect aCatch(SALOME_SalomeException);
414   SMESH_Hypothesis::Hypothesis_Status status = removeHypothesis( aSubShapeObject, anHyp );
415
416   if ( !SMESH_Hypothesis::IsStatusFatal(status) )
417     _gen_i->RemoveHypothesisFromShape(_gen_i->GetCurrentStudy(), _this(),
418                                       aSubShapeObject, anHyp );
419
420   return ConvertHypothesisStatus(status);
421 }
422
423 //=============================================================================
424 /*!
425  *  
426  */
427 //=============================================================================
428
429 SMESH_Hypothesis::Hypothesis_Status SMESH_Mesh_i::removeHypothesis(GEOM::GEOM_Object_ptr aSubShapeObject,
430                                  SMESH::SMESH_Hypothesis_ptr anHyp)
431 {
432         if(MYDEBUG) MESSAGE("removeHypothesis()");
433         // **** proposer liste de subShape (selection multiple)
434
435         if (CORBA::is_nil(aSubShapeObject))
436                 THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
437                         SALOME::BAD_PARAM);
438
439         SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(anHyp);
440         if (CORBA::is_nil(myHyp))
441           THROW_SALOME_CORBA_EXCEPTION("bad hypothesis reference",
442                         SALOME::BAD_PARAM);
443
444         SMESH_Hypothesis::Hypothesis_Status status = SMESH_Hypothesis::HYP_OK;
445         try
446         {
447                 TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
448                 int hypId = myHyp->GetId();
449                 status = _impl->RemoveHypothesis(myLocSubShape, hypId);
450                 if ( !SMESH_Hypothesis::IsStatusFatal(status) )
451                   _mapHypo.erase( hypId );
452         }
453         catch(SALOME_Exception & S_ex)
454         {
455                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
456         }
457         return status;
458 }
459
460 //=============================================================================
461 /*!
462  *  
463  */
464 //=============================================================================
465
466 SMESH::ListOfHypothesis *
467         SMESH_Mesh_i::GetHypothesisList(GEOM::GEOM_Object_ptr aSubShapeObject)
468 throw(SALOME::SALOME_Exception)
469 {
470   Unexpect aCatch(SALOME_SalomeException);
471   if (MYDEBUG) MESSAGE("GetHypothesisList");
472   if (CORBA::is_nil(aSubShapeObject))
473     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
474                                  SALOME::BAD_PARAM);
475   
476   SMESH::ListOfHypothesis_var aList = new SMESH::ListOfHypothesis();
477
478   try {
479     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
480     const list<const SMESHDS_Hypothesis*>& aLocalList = _impl->GetHypothesisList( myLocSubShape );
481     int i = 0, n = aLocalList.size();
482     aList->length( n );
483
484     for ( list<const SMESHDS_Hypothesis*>::const_iterator anIt = aLocalList.begin(); i < n && anIt != aLocalList.end(); anIt++ ) {
485       SMESHDS_Hypothesis* aHyp = (SMESHDS_Hypothesis*)(*anIt);
486       if ( _mapHypo.find( aHyp->GetID() ) != _mapHypo.end() )
487         aList[i++] = SMESH::SMESH_Hypothesis::_narrow( _mapHypo[aHyp->GetID()] );
488     }
489
490     aList->length( i );
491   }
492   catch(SALOME_Exception & S_ex) {
493     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
494   }
495   
496   return aList._retn();
497 }
498
499 //=============================================================================
500 /*!
501  *  
502  */
503 //=============================================================================
504 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::GetSubMesh(GEOM::GEOM_Object_ptr aSubShapeObject,
505                                                   const char*           theName ) 
506      throw(SALOME::SALOME_Exception)
507 {
508   Unexpect aCatch(SALOME_SalomeException);
509   MESSAGE("SMESH_Mesh_i::GetSubMesh");
510   if (CORBA::is_nil(aSubShapeObject))
511     THROW_SALOME_CORBA_EXCEPTION("bad subShape reference",
512                                  SALOME::BAD_PARAM);
513   
514   SMESH::SMESH_subMesh_var subMesh;
515   SMESH::SMESH_Mesh_var    aMesh = SMESH::SMESH_Mesh::_narrow(_this());
516   try {
517     TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(aSubShapeObject);
518     
519     //Get or Create the SMESH_subMesh object implementation
520     
521     int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
522     subMesh = getSubMesh( subMeshId );
523
524     // create a new subMesh object servant if there is none for the shape
525     if ( subMesh->_is_nil() )
526       subMesh = createSubMesh( aSubShapeObject );
527
528     if ( _gen_i->CanPublishInStudy( subMesh ))
529       _gen_i->PublishSubMesh (_gen_i->GetCurrentStudy(), aMesh,
530                               subMesh, aSubShapeObject, theName );
531   }
532   catch(SALOME_Exception & S_ex) {
533     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
534   }
535   return subMesh._retn();
536 }
537
538 //=============================================================================
539 /*!
540  *  
541  */
542 //=============================================================================
543
544 void SMESH_Mesh_i::RemoveSubMesh( SMESH::SMESH_subMesh_ptr theSubMesh )
545      throw (SALOME::SALOME_Exception)
546 {
547   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::RemoveSubMesh");
548   if ( theSubMesh->_is_nil() )
549     return;
550
551   GEOM::GEOM_Object_var aSubShapeObject;
552   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
553   if ( !aStudy->_is_nil() )  {
554     // Remove submesh's SObject
555     SALOMEDS::SObject_var anSO = _gen_i->ObjectToSObject( aStudy, theSubMesh );
556     if ( !anSO->_is_nil() ) {
557       long aTag = SMESH_Gen_i::GetRefOnShapeTag(); 
558       SALOMEDS::SObject_var anObj, aRef;
559       if ( anSO->FindSubObject( aTag, anObj ) && anObj->ReferencedObject( aRef ) )
560         aSubShapeObject = GEOM::GEOM_Object::_narrow( aRef->GetObject() );
561
562       aStudy->NewBuilder()->RemoveObjectWithChildren( anSO );
563     }
564   }
565
566   removeSubMesh( theSubMesh, aSubShapeObject.in() );
567 }
568
569
570 //=============================================================================
571 /*!
572  *  
573  */
574 //=============================================================================
575
576 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CreateGroup( SMESH::ElementType theElemType,
577                                                  const char*         theName )
578      throw(SALOME::SALOME_Exception)
579 {
580   Unexpect aCatch(SALOME_SalomeException);
581   SMESH::SMESH_Group_var aNewGroup =
582     SMESH::SMESH_Group::_narrow( createGroup( theElemType, theName ));
583
584   _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(),
585                        aNewGroup, GEOM::GEOM_Object::_nil(), theName);
586
587   return aNewGroup._retn();
588 }
589
590
591 //=============================================================================
592 /*!
593  *  
594  */
595 //=============================================================================
596 SMESH::SMESH_GroupOnGeom_ptr SMESH_Mesh_i::CreateGroupFromGEOM( SMESH::ElementType    theElemType,
597                                                                const char*           theName,
598                                                                GEOM::GEOM_Object_ptr theGeomObj)
599      throw(SALOME::SALOME_Exception)
600 {
601   Unexpect aCatch(SALOME_SalomeException);
602   SMESH::SMESH_GroupOnGeom_var aNewGroup;
603
604   TopoDS_Shape aShape = _gen_i->GeomObjectToShape( theGeomObj );
605   if ( !aShape.IsNull() ) {
606     aNewGroup = SMESH::SMESH_GroupOnGeom::_narrow
607       ( createGroup( theElemType, theName, aShape ));
608     if ( _gen_i->CanPublishInStudy( aNewGroup ) )
609       _gen_i->PublishGroup( _gen_i->GetCurrentStudy(), _this(), 
610                            aNewGroup, theGeomObj, theName );
611   }
612
613   return aNewGroup._retn();
614 }
615 //=============================================================================
616 /*!
617  *  
618  */
619 //=============================================================================
620
621 void SMESH_Mesh_i::RemoveGroup( SMESH::SMESH_GroupBase_ptr theGroup )
622      throw (SALOME::SALOME_Exception)
623 {
624   if ( theGroup->_is_nil() )
625     return;
626
627   SMESH_GroupBase_i* aGroup =
628     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
629   if ( !aGroup )
630     return;
631
632   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
633   if ( !aStudy->_is_nil() )  {
634     // Remove group's SObject
635     SALOMEDS::SObject_var aGroupSO = _gen_i->ObjectToSObject( aStudy, theGroup );
636     if ( !aGroupSO->_is_nil() )
637       aStudy->NewBuilder()->RemoveObject( aGroupSO );
638   }
639
640   // Remove the group from SMESH data structures
641   removeGroup( aGroup->GetLocalID() );
642 }
643
644 //=============================================================================
645 /*! RemoveGroupWithContents
646  *  Remove group with its contents
647  */ 
648 //=============================================================================
649 void SMESH_Mesh_i::RemoveGroupWithContents( SMESH::SMESH_GroupBase_ptr theGroup )
650   throw (SALOME::SALOME_Exception)
651 {
652   if ( theGroup->_is_nil() )
653     return;
654
655   SMESH_GroupBase_i* aGroup =
656     dynamic_cast<SMESH_GroupBase_i*>( SMESH_Gen_i::GetServant( theGroup ).in() );
657   if ( !aGroup )
658     return;
659   
660   SMESH::long_array_var anIds = aGroup->GetListOfID();
661   SMESH::SMESH_MeshEditor_var aMeshEditor = SMESH_Mesh_i::GetMeshEditor();
662     
663   if ( aGroup->GetType() == SMESH::NODE )
664     aMeshEditor->RemoveNodes( anIds );
665   else
666     aMeshEditor->RemoveElements( anIds );
667   
668   RemoveGroup( theGroup );
669 }
670
671 //=============================================================================
672 /*! UnionGroups
673  *  New group is created. All mesh elements that are 
674  *  present in initial groups are added to the new one
675  */
676 //=============================================================================
677 SMESH::SMESH_Group_ptr SMESH_Mesh_i::UnionGroups( SMESH::SMESH_GroupBase_ptr theGroup1, 
678                                                   SMESH::SMESH_GroupBase_ptr theGroup2, 
679                                                   const char* theName )
680   throw (SALOME::SALOME_Exception)
681 {
682   try
683   {
684     SMESH::SMESH_Group_var aResGrp;
685
686     if ( theGroup1->_is_nil() || theGroup2->_is_nil() ||
687          theGroup1->GetType() != theGroup2->GetType() )
688       return SMESH::SMESH_Group::_nil();
689
690     aResGrp = CreateGroup( theGroup1->GetType(), theName );
691     if ( aResGrp->_is_nil() )
692       return SMESH::SMESH_Group::_nil();
693
694     SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
695     SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
696
697     TColStd_MapOfInteger aResMap;
698
699     for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
700       aResMap.Add( anIds1[ i1 ] );
701
702     for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
703       aResMap.Add( anIds2[ i2 ] );
704
705     SMESH::long_array_var aResIds = new SMESH::long_array;
706     aResIds->length( aResMap.Extent() );
707
708     int resI = 0;
709     TColStd_MapIteratorOfMapOfInteger anIter( aResMap );
710     for( ; anIter.More(); anIter.Next() )
711       aResIds[ resI++ ] = anIter.Key();
712
713     aResGrp->Add( aResIds );
714
715     return aResGrp._retn();
716   }
717   catch( ... )
718   {
719     return SMESH::SMESH_Group::_nil();
720   }
721 }
722   
723 //=============================================================================
724 /*! IntersectGroups
725  *  New group is created. All mesh elements that are 
726  *  present in both initial groups are added to the new one.
727  */
728 //=============================================================================
729 SMESH::SMESH_Group_ptr SMESH_Mesh_i::IntersectGroups( SMESH::SMESH_GroupBase_ptr theGroup1, 
730                                                       SMESH::SMESH_GroupBase_ptr theGroup2, 
731                                                       const char* theName )
732   throw (SALOME::SALOME_Exception)
733 {
734   SMESH::SMESH_Group_var aResGrp;
735   
736   if ( theGroup1->_is_nil() || theGroup2->_is_nil() || 
737        theGroup1->GetType() != theGroup2->GetType() )
738     return aResGrp;
739   
740   aResGrp = CreateGroup( theGroup1->GetType(), theName );
741   if ( aResGrp->_is_nil() )
742     return aResGrp;
743   
744   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
745   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
746   
747   TColStd_MapOfInteger aMap1;
748   
749   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
750     aMap1.Add( anIds1[ i1 ] );
751
752   TColStd_SequenceOfInteger aSeq;
753
754   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
755     if ( aMap1.Contains( anIds2[ i2 ] ) )
756       aSeq.Append( anIds2[ i2 ] );
757   
758   SMESH::long_array_var aResIds = new SMESH::long_array;
759   aResIds->length( aSeq.Length() );
760   
761   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
762     aResIds[ resI ] = aSeq( resI + 1 );
763   
764   aResGrp->Add( aResIds );
765   
766   return aResGrp._retn();
767 }
768
769 //=============================================================================
770 /*! CutGroups
771  *  New group is created. All mesh elements that are present in 
772  *  main group but do not present in tool group are added to the new one 
773  */
774 //=============================================================================
775 SMESH::SMESH_Group_ptr SMESH_Mesh_i::CutGroups( SMESH::SMESH_GroupBase_ptr theGroup1, 
776                                                 SMESH::SMESH_GroupBase_ptr theGroup2, 
777                                                 const char* theName )
778   throw (SALOME::SALOME_Exception)
779 {
780   SMESH::SMESH_Group_var aResGrp;
781   
782   if ( theGroup1->_is_nil() || theGroup2->_is_nil() || 
783        theGroup1->GetType() != theGroup2->GetType() )
784     return aResGrp;
785   
786   aResGrp = CreateGroup( theGroup1->GetType(), theName );
787   if ( aResGrp->_is_nil() )
788     return aResGrp;
789   
790   SMESH::long_array_var anIds1 = theGroup1->GetListOfID();
791   SMESH::long_array_var anIds2 = theGroup2->GetListOfID();
792   
793   TColStd_MapOfInteger aMap2;
794   
795   for ( int i2 = 0, n2 = anIds2->length(); i2 < n2; i2++ )
796     aMap2.Add( anIds2[ i2 ] );
797
798
799   TColStd_SequenceOfInteger aSeq;
800   for ( int i1 = 0, n1 = anIds1->length(); i1 < n1; i1++ )
801     if ( !aMap2.Contains( anIds1[ i1 ] ) )
802       aSeq.Append( anIds1[ i1 ] );
803
804   SMESH::long_array_var aResIds = new SMESH::long_array;
805   aResIds->length( aSeq.Length() );
806
807   for ( int resI = 0, resN = aSeq.Length(); resI < resN; resI++ )
808     aResIds[ resI ] = aSeq( resI + 1 );  
809   
810   aResGrp->Add( aResIds );
811   
812   return aResGrp._retn();
813 }
814
815 //=============================================================================
816 /*!
817  *  
818  */
819 //=============================================================================
820
821 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::createSubMesh( GEOM::GEOM_Object_ptr theSubShapeObject )
822 {
823   if(MYDEBUG) MESSAGE( "createSubMesh" );
824   TopoDS_Shape myLocSubShape = _gen_i->GeomObjectToShape(theSubShapeObject);
825
826   ::SMESH_subMesh * mySubMesh = _impl->GetSubMesh(myLocSubShape);
827   int subMeshId = _impl->GetMeshDS()->ShapeToIndex( myLocSubShape );
828   SMESH_subMesh_i *subMeshServant = new SMESH_subMesh_i(myPOA, _gen_i, this, subMeshId);
829   SMESH::SMESH_subMesh_var subMesh
830     = SMESH::SMESH_subMesh::_narrow(subMeshServant->_this());
831
832   _mapSubMesh[subMeshId] = mySubMesh;
833   _mapSubMesh_i[subMeshId] = subMeshServant;
834   _mapSubMeshIor[subMeshId] = SMESH::SMESH_subMesh::_duplicate(subMesh);
835
836   // register CORBA object for persistence
837   int nextId = _gen_i->RegisterObject( subMesh );
838   if(MYDEBUG) MESSAGE( "Add submesh to map with id = "<< nextId);
839
840   return subMesh._retn(); 
841 }
842
843 //=======================================================================
844 //function : getSubMesh
845 //purpose  : 
846 //=======================================================================
847
848 SMESH::SMESH_subMesh_ptr SMESH_Mesh_i::getSubMesh(int shapeID)
849 {
850   map<int, SMESH::SMESH_subMesh_ptr>::iterator it = _mapSubMeshIor.find( shapeID );
851   if ( it == _mapSubMeshIor.end() )
852     return SMESH::SMESH_subMesh::_nil();
853
854   return SMESH::SMESH_subMesh::_duplicate( (*it).second );
855 }
856
857
858 //=============================================================================
859 /*!
860  *  
861  */
862 //=============================================================================
863
864 void SMESH_Mesh_i::removeSubMesh (SMESH::SMESH_subMesh_ptr theSubMesh,
865                                   GEOM::GEOM_Object_ptr    theSubShapeObject )
866 {
867   MESSAGE("SMESH_Mesh_i::removeSubMesh()");
868   if ( theSubMesh->_is_nil() || theSubShapeObject->_is_nil() )
869     return;
870
871   try {
872     SMESH::ListOfHypothesis_var aHypList = GetHypothesisList( theSubShapeObject );
873     for ( int i = 0, n = aHypList->length(); i < n; i++ ) {
874       removeHypothesis( theSubShapeObject, aHypList[i] );
875     }
876   }
877   catch( const SALOME::SALOME_Exception& ) {
878     INFOS("SMESH_Mesh_i::removeSubMesh(): exception caught!");
879   }
880
881   int subMeshId = theSubMesh->GetId();
882
883   _mapSubMesh.erase(subMeshId);
884   _mapSubMesh_i.erase(subMeshId);
885   _mapSubMeshIor.erase(subMeshId);
886   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeSubMesh() completed");
887 }
888
889 //=============================================================================
890 /*!
891  *  
892  */
893 //=============================================================================
894
895 SMESH::SMESH_GroupBase_ptr SMESH_Mesh_i::createGroup (SMESH::ElementType theElemType,
896                                                       const char*         theName,
897                                                       const TopoDS_Shape& theShape )
898 {
899   int anId;
900   SMESH::SMESH_GroupBase_var aGroup;
901   if ( _impl->AddGroup( (SMDSAbs_ElementType)theElemType, theName, anId, theShape )) {
902     SMESH_GroupBase_i* aGroupImpl;
903     if ( !theShape.IsNull() )
904       aGroupImpl = new SMESH_GroupOnGeom_i( SMESH_Gen_i::GetPOA(), this, anId );
905     else
906       aGroupImpl = new SMESH_Group_i( SMESH_Gen_i::GetPOA(), this, anId );
907
908     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
909     SMESH_Gen_i::GetPOA()->activate_object( aGroupImpl );
910     aGroupImpl->Register();
911     // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i
912
913     aGroup = SMESH::SMESH_GroupBase::_narrow( aGroupImpl->_this() );
914     _mapGroups[anId] = SMESH::SMESH_GroupBase::_duplicate( aGroup );
915
916     // register CORBA object for persistence
917     int nextId = _gen_i->RegisterObject( aGroup );
918     if(MYDEBUG) MESSAGE( "Add group to map with id = "<< nextId);
919   }
920   return aGroup._retn();
921 }
922
923 //=============================================================================
924 /*!
925  * SMESH_Mesh_i::removeGroup
926  *
927  * Should be called by ~SMESH_Group_i() 
928  */
929 //=============================================================================
930
931 void SMESH_Mesh_i::removeGroup( const int theId )
932 {
933   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::removeGroup()" );  
934   if ( _mapGroups.find( theId ) != _mapGroups.end() ) {
935     _mapGroups.erase( theId );
936     _impl->RemoveGroup( theId );
937   }
938 }
939
940
941 //=============================================================================
942 /*!
943  *  
944  */
945 //=============================================================================
946
947 SMESH::log_array * SMESH_Mesh_i::GetLog(CORBA::Boolean clearAfterGet)
948 throw(SALOME::SALOME_Exception)
949 {
950   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetLog");
951   
952   SMESH::log_array_var aLog;
953   try{
954     list < SMESHDS_Command * >logDS = _impl->GetLog();
955     aLog = new SMESH::log_array;
956     int indexLog = 0;
957     int lg = logDS.size();
958     SCRUTE(lg);
959     aLog->length(lg);
960     list < SMESHDS_Command * >::iterator its = logDS.begin();
961     while(its != logDS.end()){
962       SMESHDS_Command *com = *its;
963       int comType = com->GetType();
964       //SCRUTE(comType);
965       int lgcom = com->GetNumber();
966       //SCRUTE(lgcom);
967       const list < int >&intList = com->GetIndexes();
968       int inum = intList.size();
969       //SCRUTE(inum);
970       list < int >::const_iterator ii = intList.begin();
971       const list < double >&coordList = com->GetCoords();
972       int rnum = coordList.size();
973       //SCRUTE(rnum);
974       list < double >::const_iterator ir = coordList.begin();
975       aLog[indexLog].commandType = comType;
976       aLog[indexLog].number = lgcom;
977       aLog[indexLog].coords.length(rnum);
978       aLog[indexLog].indexes.length(inum);
979       for(int i = 0; i < rnum; i++){
980         aLog[indexLog].coords[i] = *ir;
981         //MESSAGE(" "<<i<<" "<<ir.Value());
982         ir++;
983       }
984       for(int i = 0; i < inum; i++){
985         aLog[indexLog].indexes[i] = *ii;
986         //MESSAGE(" "<<i<<" "<<ii.Value());
987         ii++;
988       }
989       indexLog++;
990       its++;
991     }
992     if(clearAfterGet)
993       _impl->ClearLog();
994   }
995   catch(SALOME_Exception & S_ex){
996     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
997   }
998   return aLog._retn();
999 }
1000
1001
1002 //=============================================================================
1003 /*!
1004  *  
1005  */
1006 //=============================================================================
1007
1008 void SMESH_Mesh_i::ClearLog() throw(SALOME::SALOME_Exception)
1009 {
1010   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::ClearLog");
1011   // ****
1012 }
1013
1014 //=============================================================================
1015 /*!
1016  *  
1017  */
1018 //=============================================================================
1019
1020 CORBA::Long SMESH_Mesh_i::GetId()throw(SALOME::SALOME_Exception)
1021 {
1022   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetId");
1023   return _id;
1024 }
1025
1026 //=============================================================================
1027 /*!
1028  *  
1029  */
1030 //=============================================================================
1031
1032 CORBA::Long SMESH_Mesh_i::GetStudyId()throw(SALOME::SALOME_Exception)
1033 {
1034   return _studyId;
1035 }
1036
1037 //=============================================================================
1038 /*!
1039  *  
1040  */
1041 //=============================================================================
1042
1043 void SMESH_Mesh_i::SetImpl(::SMESH_Mesh * impl)
1044 {
1045   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::SetImpl");
1046   _impl = impl;
1047 }
1048
1049 //=============================================================================
1050 /*!
1051  *  
1052  */
1053 //=============================================================================
1054
1055 ::SMESH_Mesh & SMESH_Mesh_i::GetImpl()
1056 {
1057   if(MYDEBUG) MESSAGE("SMESH_Mesh_i::GetImpl()");
1058   return *_impl;
1059 }
1060
1061
1062 //=============================================================================
1063 /*!
1064  *  
1065  */
1066 //=============================================================================
1067
1068 SMESH::SMESH_MeshEditor_ptr SMESH_Mesh_i::GetMeshEditor()
1069 {
1070   // Update Python script
1071   SALOMEDS::SObject_var aSO =
1072     SMESH_Gen_i::ObjectToSObject(_gen_i->GetCurrentStudy(), _this());
1073   TCollection_AsciiString aStr ("mesh_editor = ");
1074   aStr += aSO->GetID();
1075   aStr += ".GetMeshEditor()";
1076
1077   _gen_i->AddToPythonScript(_gen_i->GetCurrentStudy()->StudyId(), aStr);
1078
1079   // Create MeshEditor
1080   SMESH_MeshEditor_i *aMeshEditor = new SMESH_MeshEditor_i( _impl );
1081   SMESH::SMESH_MeshEditor_var aMesh = aMeshEditor->_this();
1082   return aMesh._retn();
1083 }
1084
1085 //=============================================================================
1086 /*!
1087  *  
1088  */
1089 //=============================================================================
1090
1091 void SMESH_Mesh_i::ExportToMED( const char* file, 
1092                                 CORBA::Boolean auto_groups, 
1093                                 SMESH::MED_VERSION theVersion )
1094   throw(SALOME::SALOME_Exception)
1095 {
1096   Unexpect aCatch(SALOME_SalomeException);
1097   
1098   char* aMeshName = "Mesh";
1099   SALOMEDS::Study_ptr aStudy = _gen_i->GetCurrentStudy();
1100   if ( !aStudy->_is_nil() ) {
1101     SALOMEDS::SObject_var aMeshSO = _gen_i->ObjectToSObject( aStudy, _this() );
1102     if ( !aMeshSO->_is_nil() ) {
1103       aMeshName = aMeshSO->GetName();
1104       //SCRUTE(file);
1105       //SCRUTE(aMeshName);
1106       //SCRUTE(aMeshSO->GetID());
1107       
1108       // asv : 27.10.04 : fix of 6903: check for StudyLocked before adding attributes 
1109       if ( !aStudy->GetProperties()->IsLocked() ) 
1110         {
1111         SALOMEDS::GenericAttribute_var anAttr;
1112         SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1113         SALOMEDS::AttributeExternalFileDef_var aFileName;
1114         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeExternalFileDef");
1115         aFileName = SALOMEDS::AttributeExternalFileDef::_narrow(anAttr);
1116         ASSERT(!aFileName->_is_nil());
1117         aFileName->SetValue(file);
1118         SALOMEDS::AttributeFileType_var aFileType;
1119         anAttr=aStudyBuilder->FindOrCreateAttribute(aMeshSO, "AttributeFileType");
1120         aFileType = SALOMEDS::AttributeFileType::_narrow(anAttr);
1121         ASSERT(!aFileType->_is_nil());
1122         aFileType->SetValue("FICHIERMED");
1123         }
1124     }
1125   }
1126   _impl->ExportMED( file, aMeshName, auto_groups, theVersion );
1127 }
1128
1129 void SMESH_Mesh_i::ExportMED( const char* file, 
1130                               CORBA::Boolean auto_groups)
1131   throw(SALOME::SALOME_Exception)
1132 {
1133   ExportToMED(file,auto_groups,SMESH::MED_V2_1);
1134 }
1135
1136 void SMESH_Mesh_i::ExportDAT(const char *file) throw(SALOME::SALOME_Exception)
1137 {
1138   Unexpect aCatch(SALOME_SalomeException);
1139   _impl->ExportDAT(file);
1140 }
1141 void SMESH_Mesh_i::ExportUNV(const char *file) throw(SALOME::SALOME_Exception)
1142 {
1143   Unexpect aCatch(SALOME_SalomeException);
1144   _impl->ExportUNV(file);
1145 }
1146
1147 void SMESH_Mesh_i::ExportSTL(const char *file, const bool isascii) throw(SALOME::SALOME_Exception)
1148 {
1149   Unexpect aCatch(SALOME_SalomeException);
1150   _impl->ExportSTL(file, isascii);
1151 }
1152
1153 //=============================================================================
1154 /*!
1155  *  
1156  */
1157 //=============================================================================
1158
1159 SALOME_MED::MESH_ptr SMESH_Mesh_i::GetMEDMesh()throw(SALOME::SALOME_Exception)
1160 {
1161   Unexpect aCatch(SALOME_SalomeException);
1162   SMESH_MEDMesh_i *aMedMesh = new SMESH_MEDMesh_i(this);
1163   SALOME_MED::MESH_var aMesh = aMedMesh->_this();
1164   return aMesh._retn();
1165 }
1166
1167 //=============================================================================
1168 /*!
1169  *  
1170  */
1171 //=============================================================================
1172 CORBA::Long SMESH_Mesh_i::NbNodes()throw(SALOME::SALOME_Exception)
1173 {
1174   Unexpect aCatch(SALOME_SalomeException);
1175   return _impl->NbNodes();
1176 }
1177
1178 //=============================================================================
1179 /*!
1180  *  
1181  */
1182 //=============================================================================
1183 CORBA::Long SMESH_Mesh_i::NbElements()throw (SALOME::SALOME_Exception)
1184 {
1185   Unexpect aCatch(SALOME_SalomeException);
1186   return NbEdges() + NbFaces() + NbVolumes();
1187 }
1188   
1189 //=============================================================================
1190 /*!
1191  *  
1192  */
1193 //=============================================================================
1194 CORBA::Long SMESH_Mesh_i::NbEdges()throw(SALOME::SALOME_Exception)
1195 {
1196   Unexpect aCatch(SALOME_SalomeException);
1197   return _impl->NbEdges();
1198 }
1199
1200 //=============================================================================
1201 /*!
1202  *  
1203  */
1204 //=============================================================================
1205 CORBA::Long SMESH_Mesh_i::NbFaces()throw(SALOME::SALOME_Exception)
1206 {
1207   Unexpect aCatch(SALOME_SalomeException);
1208   return _impl->NbFaces();
1209 }
1210
1211 CORBA::Long SMESH_Mesh_i::NbTriangles()throw(SALOME::SALOME_Exception)
1212 {
1213   Unexpect aCatch(SALOME_SalomeException);
1214   return _impl->NbTriangles();
1215 }
1216
1217 CORBA::Long SMESH_Mesh_i::NbQuadrangles()throw(SALOME::SALOME_Exception)
1218 {
1219   Unexpect aCatch(SALOME_SalomeException);
1220   return _impl->NbQuadrangles();
1221 }
1222
1223 CORBA::Long SMESH_Mesh_i::NbPolygons()throw(SALOME::SALOME_Exception)
1224 {
1225   Unexpect aCatch(SALOME_SalomeException);
1226   return _impl->NbPolygons();
1227 }
1228
1229 //=============================================================================
1230 /*!
1231  *  
1232  */
1233 //=============================================================================
1234 CORBA::Long SMESH_Mesh_i::NbVolumes()throw(SALOME::SALOME_Exception)
1235 {
1236   Unexpect aCatch(SALOME_SalomeException);
1237   return _impl->NbVolumes();
1238 }
1239
1240 CORBA::Long SMESH_Mesh_i::NbTetras()throw(SALOME::SALOME_Exception)
1241 {
1242   Unexpect aCatch(SALOME_SalomeException);
1243   return _impl->NbTetras();
1244 }
1245
1246 CORBA::Long SMESH_Mesh_i::NbHexas()throw(SALOME::SALOME_Exception)
1247 {
1248   Unexpect aCatch(SALOME_SalomeException);
1249   return _impl->NbHexas();
1250 }
1251
1252 CORBA::Long SMESH_Mesh_i::NbPyramids()throw(SALOME::SALOME_Exception)
1253 {
1254   Unexpect aCatch(SALOME_SalomeException);
1255   return _impl->NbPyramids();
1256 }
1257
1258 CORBA::Long SMESH_Mesh_i::NbPrisms()throw(SALOME::SALOME_Exception)
1259 {
1260   Unexpect aCatch(SALOME_SalomeException);
1261   return _impl->NbPrisms();
1262 }
1263
1264 CORBA::Long SMESH_Mesh_i::NbPolyhedrons()throw(SALOME::SALOME_Exception)
1265 {
1266   Unexpect aCatch(SALOME_SalomeException);
1267   return _impl->NbPolyhedrons();
1268 }
1269
1270 //=============================================================================
1271 /*!
1272  *  
1273  */
1274 //=============================================================================
1275 CORBA::Long SMESH_Mesh_i::NbSubMesh()throw(SALOME::SALOME_Exception)
1276 {
1277   Unexpect aCatch(SALOME_SalomeException);
1278   return _impl->NbSubMesh();
1279 }
1280
1281 //=============================================================================
1282 /*!
1283  *  
1284  */
1285 //=============================================================================
1286 char* SMESH_Mesh_i::Dump()
1287 {
1288   std::ostringstream os;
1289   _impl->Dump( os );
1290   return CORBA::string_dup( os.str().c_str() );
1291 }
1292
1293 //=============================================================================
1294 /*!
1295  *  
1296  */
1297 //=============================================================================
1298 SMESH::long_array* SMESH_Mesh_i::GetIDs()
1299 {
1300   SMESH::long_array_var aResult = new SMESH::long_array();
1301   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1302   int aMinId = aSMESHDS_Mesh->MinElementID();
1303   int aMaxId =  aSMESHDS_Mesh->MaxElementID();
1304
1305   aResult->length(aMaxId - aMinId + 1);
1306   
1307   for (int i = 0, id = aMinId; id <= aMaxId; id++  )
1308     aResult[i++] = id;
1309   
1310   return aResult._retn();
1311 }
1312
1313 //=============================================================================
1314 /*!
1315  *  
1316  */
1317 //=============================================================================
1318   
1319 SMESH::long_array* SMESH_Mesh_i::GetElementsId()
1320      throw (SALOME::SALOME_Exception)
1321 {
1322   Unexpect aCatch(SALOME_SalomeException);
1323   MESSAGE("SMESH_Mesh_i::GetElementsId");
1324   SMESH::long_array_var aResult = new SMESH::long_array();
1325   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1326
1327   if ( aSMESHDS_Mesh == NULL )
1328     return aResult._retn();
1329
1330   long nbElements = NbElements();
1331   aResult->length( nbElements );
1332   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1333   for ( int i = 0, n = nbElements; i < n && anIt->more(); i++ )
1334     aResult[i] = anIt->next()->GetID();
1335
1336   return aResult._retn();
1337 }
1338
1339
1340 //=============================================================================
1341 /*!
1342  *  
1343  */
1344 //=============================================================================
1345
1346 SMESH::long_array* SMESH_Mesh_i::GetElementsByType( SMESH::ElementType theElemType )
1347     throw (SALOME::SALOME_Exception)
1348 {
1349   Unexpect aCatch(SALOME_SalomeException);
1350   MESSAGE("SMESH_subMesh_i::GetElementsByType");
1351   SMESH::long_array_var aResult = new SMESH::long_array();
1352   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1353
1354   if ( aSMESHDS_Mesh == NULL )
1355     return aResult._retn();
1356
1357   long nbElements = NbElements();
1358
1359   // No sense in returning ids of elements along with ids of nodes:
1360   // when theElemType == SMESH::ALL, return node ids only if
1361   // there are no elements
1362   if ( theElemType == SMESH::NODE || theElemType == SMESH::ALL && nbElements == 0 )
1363     return GetNodesId();
1364
1365   aResult->length( nbElements );
1366
1367   int i = 0;
1368
1369   SMDS_ElemIteratorPtr anIt = aSMESHDS_Mesh->elementsIterator();
1370   while ( i < nbElements && anIt->more() ) {
1371     const SMDS_MeshElement* anElem = anIt->next();
1372     if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
1373       aResult[i++] = anElem->GetID();
1374   }
1375
1376   aResult->length( i );
1377
1378   return aResult._retn();
1379 }
1380
1381 //=============================================================================
1382 /*!
1383  *  
1384  */
1385 //=============================================================================
1386   
1387 SMESH::long_array* SMESH_Mesh_i::GetNodesId()
1388   throw (SALOME::SALOME_Exception)
1389 {
1390   Unexpect aCatch(SALOME_SalomeException);
1391   MESSAGE("SMESH_subMesh_i::GetNodesId");
1392   SMESH::long_array_var aResult = new SMESH::long_array();
1393   SMESHDS_Mesh* aSMESHDS_Mesh = _impl->GetMeshDS();
1394
1395   if ( aSMESHDS_Mesh == NULL )
1396     return aResult._retn();
1397
1398   long nbNodes = NbNodes();
1399   aResult->length( nbNodes );
1400   SMDS_NodeIteratorPtr anIt = aSMESHDS_Mesh->nodesIterator();
1401   for ( int i = 0, n = nbNodes; i < n && anIt->more(); i++ )
1402     aResult[i] = anIt->next()->GetID();
1403
1404   return aResult._retn();
1405 }
1406