Salome HOME
Update copyright information
[modules/smesh.git] / src / SMESH_I / SMESH_Group_i.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's classes
23 //  File   : SMESH_Group_i.cxx
24 //  Author : Sergey ANIKIN, OCC
25 //  Module : SMESH
26 //  $Header$
27 //
28 #include "SMESH_Group_i.hxx"
29 #include "SMESH_Mesh_i.hxx"
30 #include "SMESH_Gen_i.hxx"
31 #include "SMESH_Group.hxx"
32 #include "SMESHDS_Group.hxx"
33 #include "SMESHDS_GroupOnGeom.hxx"
34 #include "SMDSAbs_ElementType.hxx"
35
36 #include "SMESH_Filter_i.hxx"
37 #include "SMESH_PythonDump.hxx"
38
39 #include "utilities.h"
40
41 using namespace SMESH;
42
43 //=============================================================================
44 /*!
45  *  
46  */
47 //=============================================================================
48
49 SMESH_GroupBase_i::SMESH_GroupBase_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
50 : SALOME::GenericObj_i( thePOA ),
51   myMeshServant( theMeshServant ), 
52   myLocalID( theLocalID )
53 {
54   // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i,
55   // servant activation is performed by SMESH_Mesh_i::createGroup()
56   // thePOA->activate_object( this );
57 }
58
59 SMESH_Group_i::SMESH_Group_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
60      : SALOME::GenericObj_i( thePOA ),
61        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
62 {
63   //MESSAGE("SMESH_Group_i; this = "<<this );
64 }
65
66 SMESH_GroupOnGeom_i::SMESH_GroupOnGeom_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
67      : SALOME::GenericObj_i( thePOA ),
68        SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
69 {
70   //MESSAGE("SMESH_GroupOnGeom_i; this = "<<this );
71 }
72
73 //=============================================================================
74 /*!
75  *  
76  */
77 //=============================================================================
78
79 SMESH_GroupBase_i::~SMESH_GroupBase_i()
80 {
81   MESSAGE("~SMESH_GroupBase_i; this = "<<this );
82   if ( myMeshServant )
83     myMeshServant->removeGroup(myLocalID);
84 }
85
86 //=======================================================================
87 //function : GetSmeshGroup
88 //purpose  : 
89 //=======================================================================
90
91 ::SMESH_Group* SMESH_GroupBase_i::GetSmeshGroup() const
92 {
93   if ( myMeshServant ) {
94     ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
95     return aMesh.GetGroup(myLocalID);
96   }
97   return 0;
98 }
99
100 //=======================================================================
101 //function : GetGroupDS
102 //purpose  : 
103 //=======================================================================
104
105 SMESHDS_GroupBase* SMESH_GroupBase_i::GetGroupDS() const
106 {
107   ::SMESH_Group* aGroup = GetSmeshGroup();
108   if ( aGroup )
109     return aGroup->GetGroupDS();
110   return 0;
111 }
112
113 //=============================================================================
114 /*!
115  *  
116  */
117 //=============================================================================
118
119 void SMESH_GroupBase_i::SetName( const char* theName )
120 {
121   // Update Python script
122   TPythonDump() <<  _this() << ".SetName( '" << theName << "' )";
123
124   // Perform renaming
125   ::SMESH_Group* aGroup = GetSmeshGroup();
126   if (aGroup) {
127     aGroup->SetName(theName);
128
129     // Update group name in a study
130     SMESH_Gen_i* aGen = myMeshServant->GetGen();
131     aGen->SetName( aGen->ObjectToSObject( aGen->GetCurrentStudy(), _this() ), theName );
132     return;
133   }
134   MESSAGE("can't set name of a vague group");
135 }
136
137 //=============================================================================
138 /*!
139  *  
140  */
141 //=============================================================================
142
143 char* SMESH_GroupBase_i::GetName()
144 {
145   ::SMESH_Group* aGroup = GetSmeshGroup();
146   if (aGroup)
147     return CORBA::string_dup (aGroup->GetName());
148   MESSAGE("get name of a vague group");
149   return CORBA::string_dup( "NO_NAME" );
150 }
151
152 //=============================================================================
153 /*!
154  *  
155  */
156 //=============================================================================
157
158 SMESH::ElementType SMESH_GroupBase_i::GetType()
159 {
160   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
161   if (aGroupDS) {
162     SMDSAbs_ElementType aSMDSType = aGroupDS->GetType();
163     SMESH::ElementType aType;
164     switch (aSMDSType) {
165     case SMDSAbs_Node:   aType = SMESH::NODE; break;
166     case SMDSAbs_Edge:   aType = SMESH::EDGE; break;
167     case SMDSAbs_Face:   aType = SMESH::FACE; break;
168     case SMDSAbs_Volume: aType = SMESH::VOLUME; break;
169     default:             aType = SMESH::ALL; break;
170     }
171     return aType;
172   }
173   MESSAGE("get type of a vague group");
174   return SMESH::ALL;
175 }
176
177
178 //=============================================================================
179 /*!
180  *  
181  */
182 //=============================================================================
183
184 CORBA::Long SMESH_GroupBase_i::Size()
185 {
186   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
187   if (aGroupDS)
188     return aGroupDS->Extent();
189   MESSAGE("get size of a vague group");
190   return 0;
191 }
192
193 //=============================================================================
194 /*!
195  *  
196  */
197 //=============================================================================
198
199 CORBA::Boolean SMESH_GroupBase_i::IsEmpty()
200 {
201   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
202   if (aGroupDS)
203     return aGroupDS->IsEmpty();
204   MESSAGE("checking IsEmpty of a vague group");
205   return true;
206 }
207
208 //=============================================================================
209 /*!
210  *  
211  */
212 //=============================================================================
213
214 void SMESH_Group_i::Clear()
215 {
216   // Update Python script
217   TPythonDump() << _this() << ".Clear()";
218
219   // Clear the group
220   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
221   if (aGroupDS) {
222     aGroupDS->Clear();
223     return;
224   }
225   MESSAGE("attempt to clear a vague group");
226 }
227
228 //=============================================================================
229 /*!
230  *  
231  */
232 //=============================================================================
233
234 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
235 {
236   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
237   if (aGroupDS)
238     return aGroupDS->Contains(theID);
239   MESSAGE("attempt to check contents of a vague group");
240   return false;
241 }
242
243 //=============================================================================
244 /*!
245  *  
246  */
247 //=============================================================================
248
249 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
250 {
251   // Update Python script
252   TPythonDump() << "nbAdd = " << _this() << ".Add( " << theIDs << " )";
253
254   // Add elements to the group
255   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
256   if (aGroupDS) {
257     int nbAdd = 0;
258     for (int i = 0; i < theIDs.length(); i++) {
259       int anID = (int) theIDs[i];
260       if (aGroupDS->Add(anID))
261         nbAdd++;
262     }
263     return nbAdd;
264   }
265   MESSAGE("attempt to add elements to a vague group");
266   return 0;
267 }
268
269 //=============================================================================
270 /*!
271  *  
272  */
273 //=============================================================================
274
275 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
276 {
277   // Update Python script
278   TPythonDump() << "nbDel = " << _this() << ".Remove( " << theIDs << " )";
279
280   // Remove elements from the group
281   SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
282   if (aGroupDS) {
283     int nbDel = 0;
284     for (int i = 0; i < theIDs.length(); i++) {
285       int anID = (int) theIDs[i];
286       if (aGroupDS->Remove(anID))
287         nbDel++;
288     }
289     return nbDel;
290   }
291   MESSAGE("attempt to remove elements from a vague group");
292   return 0;
293 }
294
295 //=============================================================================
296 /*!
297  *  
298  */
299 //=============================================================================
300
301 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
302
303 CORBA::Long 
304 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
305                    SMESHDS_GroupBase* theGroupBase,
306                    TFunChangeGroup theFun)
307 {
308   CORBA::Long aNb = 0;
309   if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
310     SMESH::Controls::Filter::TIdSequence aSequence;
311     const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
312     SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
313     
314     CORBA::Long i = 0, iEnd = aSequence.size();
315     for(; i < iEnd; i++)
316       if((aGroupDS->*theFun)(aSequence[i]))
317         aNb++;
318     return aNb;
319   }
320   return aNb;
321 }
322
323 CORBA::Long 
324 SMESH_Group_i::
325 AddByPredicate( SMESH::Predicate_ptr thePredicate )
326 {
327   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
328     TPythonDump()<<_this()<<".AddByPredicate("<<aPredicate<<")";
329     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Add);
330   }
331   return 0;
332 }
333
334 CORBA::Long 
335 SMESH_Group_i::
336 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
337 {
338   if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
339     TPythonDump()<<_this()<<".RemoveByPredicate("<<aPredicate<<")";
340     return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Remove);
341   }
342   return 0;
343 }
344
345 //=============================================================================
346 /*!
347  *  
348  */
349 //=============================================================================
350
351 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
352 {
353   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
354   if (aGroupDS)
355     return aGroupDS->GetID(theIndex);
356   MESSAGE("attempt to iterate on a vague group");
357   return -1;
358 }
359
360 //=============================================================================
361 /*!
362  *  
363  */
364 //=============================================================================
365
366 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
367 {
368   SMESH::long_array_var aRes = new SMESH::long_array();
369   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
370   if (aGroupDS) {
371     int aSize = aGroupDS->Extent();
372     aRes->length(aSize);
373     for (int i = 0; i < aSize; i++)
374       aRes[i] = aGroupDS->GetID(i+1);
375     return aRes._retn();
376   }
377   MESSAGE("get list of IDs of a vague group");
378   return aRes._retn();
379 }
380
381 //=============================================================================
382 /*!
383  *  
384  */
385 //=============================================================================
386 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
387 {
388   SMESH::SMESH_Mesh_var aMesh;
389   if ( myMeshServant )
390     aMesh = SMESH::SMESH_Mesh::_narrow( myMeshServant->_this() );
391   return aMesh._retn();
392 }
393
394 //=============================================================================
395 /*!
396  *  
397  */
398 //=============================================================================
399 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
400 {
401   SMESH::long_array_var aResult = GetListOfID();
402   return aResult._retn();
403 }
404
405 //=======================================================================
406 //function : GetShape
407 //purpose  : 
408 //=======================================================================
409
410 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
411 {
412   GEOM::GEOM_Object_var aGeomObj;
413   SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
414   if ( aGroupDS ) {
415     SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
416     aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
417   }
418   return aGeomObj._retn();
419 }
420
421 //=============================================================================
422 /*!
423  *
424  */
425 //=============================================================================
426 SALOMEDS::Color SMESH_GroupBase_i::GetColor()
427 {
428   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
429   if (aGroupDS)
430   {
431     Quantity_Color aQColor = aGroupDS->GetColor();
432     SALOMEDS::Color aColor;
433     aColor.R = aQColor.Red();
434     aColor.G = aQColor.Green();
435     aColor.B = aQColor.Blue();
436
437     return aColor;
438   }
439   MESSAGE("get color of a group");
440   return SALOMEDS::Color();
441 }
442
443 //=============================================================================
444 /*!
445  *
446  */
447 //=============================================================================
448 void SMESH_GroupBase_i::SetColor(const SALOMEDS::Color& color)
449 {
450   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
451   if (aGroupDS)
452   {
453     Quantity_Color aQColor( color.R, color.G, color.B, Quantity_TOC_RGB );
454     return aGroupDS->SetColor(aQColor);
455   }
456   MESSAGE("set color of a group");
457   return ;
458 }
459
460 //=============================================================================
461 /*!
462  *
463  */
464 //=============================================================================
465 CORBA::Long SMESH_GroupBase_i::GetColorNumber()
466 {
467   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
468   if (aGroupDS)
469     return aGroupDS->GetColorGroup();
470   MESSAGE("get color number of a group");
471   return 0;
472 }
473
474 //=============================================================================
475 /*!
476  *
477  */
478 //=============================================================================
479 void SMESH_GroupBase_i::SetColorNumber(CORBA::Long color)
480 {
481   SMESHDS_GroupBase* aGroupDS = GetGroupDS();
482   if (aGroupDS)
483     return aGroupDS->SetColorGroup(color);
484   MESSAGE("set color number of a group");
485   return ;
486 }