1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's classes
24 // File : SMESH_Group_i.cxx
25 // Author : Sergey ANIKIN, OCC
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"
36 #include "SMESH_Filter_i.hxx"
37 #include "SMESH_PythonDump.hxx"
39 #include CORBA_SERVER_HEADER(SMESH_Filter)
41 #include "utilities.h"
43 using namespace SMESH;
45 //=============================================================================
49 //=============================================================================
51 SMESH_GroupBase_i::SMESH_GroupBase_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
52 : SALOME::GenericObj_i( thePOA ),
53 myMeshServant( theMeshServant ),
54 myLocalID( theLocalID )
56 // PAL7962: san -- To ensure correct mapping of servant and correct reference counting in GenericObj_i,
57 // servant activation is performed by SMESH_Mesh_i::createGroup()
58 // thePOA->activate_object( this );
61 SMESH_Group_i::SMESH_Group_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
62 : SALOME::GenericObj_i( thePOA ),
63 SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
65 //MESSAGE("SMESH_Group_i; this = "<<this );
68 SMESH_GroupOnGeom_i::SMESH_GroupOnGeom_i( PortableServer::POA_ptr thePOA, SMESH_Mesh_i* theMeshServant, const int theLocalID )
69 : SALOME::GenericObj_i( thePOA ),
70 SMESH_GroupBase_i( thePOA, theMeshServant, theLocalID )
72 //MESSAGE("SMESH_GroupOnGeom_i; this = "<<this );
75 //=============================================================================
79 //=============================================================================
81 SMESH_GroupBase_i::~SMESH_GroupBase_i()
83 MESSAGE("~SMESH_GroupBase_i; this = "<<this );
85 myMeshServant->removeGroup(myLocalID);
88 //=======================================================================
89 //function : GetSmeshGroup
91 //=======================================================================
93 ::SMESH_Group* SMESH_GroupBase_i::GetSmeshGroup() const
95 if ( myMeshServant ) {
96 ::SMESH_Mesh& aMesh = myMeshServant->GetImpl();
97 return aMesh.GetGroup(myLocalID);
102 //=======================================================================
103 //function : GetGroupDS
105 //=======================================================================
107 SMESHDS_GroupBase* SMESH_GroupBase_i::GetGroupDS() const
109 ::SMESH_Group* aGroup = GetSmeshGroup();
111 return aGroup->GetGroupDS();
115 //=============================================================================
119 //=============================================================================
121 void SMESH_GroupBase_i::SetName( const char* theName )
124 ::SMESH_Group* aGroup = GetSmeshGroup();
126 MESSAGE("can't set name of a vague group");
130 if ( aGroup->GetName() && !strcmp( aGroup->GetName(), theName ) )
131 return; // nothing to rename
133 aGroup->SetName(theName);
135 // Update group name in a study
136 SMESH_Gen_i* aGen = myMeshServant->GetGen();
137 aGen->SetName( aGen->ObjectToSObject( aGen->GetCurrentStudy(), _this() ), theName );
139 // Update Python script
140 TPythonDump() << _this() << ".SetName( '" << theName << "' )";
143 //=============================================================================
147 //=============================================================================
149 char* SMESH_GroupBase_i::GetName()
151 ::SMESH_Group* aGroup = GetSmeshGroup();
153 return CORBA::string_dup (aGroup->GetName());
154 MESSAGE("get name of a vague group");
155 return CORBA::string_dup( "NO_NAME" );
158 //=============================================================================
162 //=============================================================================
164 SMESH::ElementType SMESH_GroupBase_i::GetType()
166 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
168 SMDSAbs_ElementType aSMDSType = aGroupDS->GetType();
169 SMESH::ElementType aType;
171 case SMDSAbs_Node: aType = SMESH::NODE; break;
172 case SMDSAbs_Edge: aType = SMESH::EDGE; break;
173 case SMDSAbs_Face: aType = SMESH::FACE; break;
174 case SMDSAbs_Volume: aType = SMESH::VOLUME; break;
175 case SMDSAbs_0DElement: aType = SMESH::ELEM0D; break;
176 default: aType = SMESH::ALL; break;
180 MESSAGE("get type of a vague group");
185 //=============================================================================
189 //=============================================================================
191 CORBA::Long SMESH_GroupBase_i::Size()
193 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
195 return aGroupDS->Extent();
196 MESSAGE("get size of a vague group");
200 //=============================================================================
204 //=============================================================================
206 CORBA::Boolean SMESH_GroupBase_i::IsEmpty()
208 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
210 return aGroupDS->IsEmpty();
211 MESSAGE("checking IsEmpty of a vague group");
215 //=============================================================================
219 //=============================================================================
221 void SMESH_Group_i::Clear()
223 // Update Python script
224 TPythonDump() << _this() << ".Clear()";
227 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
232 MESSAGE("attempt to clear a vague group");
235 //=============================================================================
239 //=============================================================================
241 CORBA::Boolean SMESH_GroupBase_i::Contains( CORBA::Long theID )
243 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
245 return aGroupDS->Contains(theID);
246 MESSAGE("attempt to check contents of a vague group");
250 //=============================================================================
254 //=============================================================================
256 CORBA::Long SMESH_Group_i::Add( const SMESH::long_array& theIDs )
258 // Update Python script
259 TPythonDump() << "nbAdd = " << _this() << ".Add( " << theIDs << " )";
261 // Add elements to the group
262 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
265 for (int i = 0; i < theIDs.length(); i++) {
266 int anID = (int) theIDs[i];
267 if (aGroupDS->Add(anID))
272 MESSAGE("attempt to add elements to a vague group");
276 //=============================================================================
280 //=============================================================================
282 CORBA::Long SMESH_Group_i::Remove( const SMESH::long_array& theIDs )
284 // Update Python script
285 TPythonDump() << "nbDel = " << _this() << ".Remove( " << theIDs << " )";
287 // Remove elements from the group
288 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
291 for (int i = 0; i < theIDs.length(); i++) {
292 int anID = (int) theIDs[i];
293 if (aGroupDS->Remove(anID))
298 MESSAGE("attempt to remove elements from a vague group");
302 //=============================================================================
306 //=============================================================================
308 typedef bool (SMESHDS_Group::*TFunChangeGroup)(const int);
311 ChangeByPredicate( SMESH::Predicate_i* thePredicate,
312 SMESHDS_GroupBase* theGroupBase,
313 TFunChangeGroup theFun)
316 if(SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>(theGroupBase)){
317 SMESH::Controls::Filter::TIdSequence aSequence;
318 const SMDS_Mesh* aMesh = theGroupBase->GetMesh();
319 SMESH::Filter_i::GetElementsId(thePredicate,aMesh,aSequence);
321 CORBA::Long i = 0, iEnd = aSequence.size();
323 if((aGroupDS->*theFun)(aSequence[i]))
332 AddByPredicate( SMESH::Predicate_ptr thePredicate )
334 if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
335 TPythonDump()<<_this()<<".AddByPredicate("<<aPredicate<<")";
336 return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Add);
343 RemoveByPredicate( SMESH::Predicate_ptr thePredicate )
345 if(SMESH::Predicate_i* aPredicate = SMESH::GetPredicate(thePredicate)){
346 TPythonDump()<<_this()<<".RemoveByPredicate("<<aPredicate<<")";
347 return ChangeByPredicate(aPredicate,GetGroupDS(),&SMESHDS_Group::Remove);
352 CORBA::Long SMESH_Group_i::AddFrom( SMESH::SMESH_IDSource_ptr theSource )
355 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( GetGroupDS() );
357 SMESH::long_array_var anIds;
358 SMESH::SMESH_GroupBase_var group = SMESH::SMESH_GroupBase::_narrow(theSource);
359 SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow(theSource);
360 SMESH::SMESH_subMesh_var submesh = SMESH::SMESH_subMesh::_narrow(theSource);
361 SMESH::Filter_var filter = SMESH::Filter::_narrow(theSource);
362 if ( !group->_is_nil())
363 anIds = group->GetType()==GetType() ? theSource->GetIDs() : new SMESH::long_array();
364 else if ( !mesh->_is_nil() )
365 anIds = mesh->GetElementsByType( GetType() );
366 else if ( !submesh->_is_nil())
367 anIds = submesh->GetElementsByType( GetType() );
368 else if ( !filter->_is_nil() )
369 anIds = filter->GetElementType()==GetType() ? theSource->GetIDs() : new SMESH::long_array();
371 anIds = theSource->GetIDs();
372 for ( int i = 0, total = anIds->length(); i < total; i++ ) {
373 if ( aGroupDS->Add((int)anIds[i]) ) nbAdd++;
377 // Update Python script
378 TPythonDump() << "nbAdd = " << _this() << ".AddFrom( " << theSource << " )";
383 //=============================================================================
387 //=============================================================================
389 CORBA::Long SMESH_GroupBase_i::GetID( CORBA::Long theIndex )
391 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
393 return aGroupDS->GetID(theIndex);
394 MESSAGE("attempt to iterate on a vague group");
398 //=============================================================================
402 //=============================================================================
404 SMESH::long_array* SMESH_GroupBase_i::GetListOfID()
406 SMESH::long_array_var aRes = new SMESH::long_array();
407 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
409 int aSize = aGroupDS->Extent();
411 for (int i = 0; i < aSize; i++)
412 aRes[i] = aGroupDS->GetID(i+1);
415 MESSAGE("get list of IDs of a vague group");
419 //=============================================================================
423 //=============================================================================
424 SMESH::SMESH_Mesh_ptr SMESH_GroupBase_i::GetMesh()
426 SMESH::SMESH_Mesh_var aMesh;
428 aMesh = SMESH::SMESH_Mesh::_narrow( myMeshServant->_this() );
429 return aMesh._retn();
432 //=======================================================================
433 //function : GetShape
435 //=======================================================================
437 GEOM::GEOM_Object_ptr SMESH_GroupOnGeom_i::GetShape()
439 GEOM::GEOM_Object_var aGeomObj;
440 SMESHDS_GroupOnGeom* aGroupDS = dynamic_cast<SMESHDS_GroupOnGeom*>( GetGroupDS() );
442 SMESH_Gen_i* aGen = GetMeshServant()->GetGen();
443 aGeomObj = aGen->ShapeToGeomObject( aGroupDS->GetShape() );
445 return aGeomObj._retn();
448 //=============================================================================
452 //=============================================================================
453 SALOMEDS::Color SMESH_GroupBase_i::GetColor()
455 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
458 Quantity_Color aQColor = aGroupDS->GetColor();
459 SALOMEDS::Color aColor;
460 aColor.R = aQColor.Red();
461 aColor.G = aQColor.Green();
462 aColor.B = aQColor.Blue();
466 MESSAGE("get color of a group");
467 return SALOMEDS::Color();
470 //=============================================================================
474 //=============================================================================
475 void SMESH_GroupBase_i::SetColor(const SALOMEDS::Color& color)
477 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
480 Quantity_Color aQColor( color.R, color.G, color.B, Quantity_TOC_RGB );
481 return aGroupDS->SetColor(aQColor);
483 MESSAGE("set color of a group");
487 //=============================================================================
491 //=============================================================================
492 CORBA::Long SMESH_GroupBase_i::GetColorNumber()
494 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
496 return aGroupDS->GetColorGroup();
497 MESSAGE("get color number of a group");
501 //=============================================================================
505 //=============================================================================
506 void SMESH_GroupBase_i::SetColorNumber(CORBA::Long color)
508 SMESHDS_GroupBase* aGroupDS = GetGroupDS();
510 return aGroupDS->SetColorGroup(color);
511 MESSAGE("set color number of a group");
515 //=============================================================================
517 * Returns statistic of mesh elements
518 * Result array of number enityties
519 * Inherited from SMESH_IDSource
521 //=============================================================================
522 SMESH::long_array* SMESH_GroupBase_i::GetMeshInfo()
524 SMESH::long_array_var aRes = new SMESH::long_array();
525 aRes->length(SMESH::Entity_Last);
526 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
529 SMESHDS_GroupBase* aGrpDS = GetGroupDS();
532 if ( GetType() == NODE )
533 aRes[ SMESH::Entity_Node ] = aGrpDS->Extent();
535 SMESH_Mesh_i::CollectMeshInfo( aGrpDS->GetElements(), aRes);
539 //=======================================================================
541 //purpose : Returns ids of members
542 //=======================================================================
544 SMESH::long_array* SMESH_GroupBase_i::GetIDs()
546 SMESH::long_array_var aResult = GetListOfID();
547 return aResult._retn();
550 //=======================================================================
551 //function : GetTypes
552 //purpose : Returns types of elements it contains
553 //=======================================================================
555 SMESH::array_of_ElementType* SMESH_GroupBase_i::GetTypes()
557 SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
559 types[0] = GetType();
560 return types._retn();