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