Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/smesh.git] / src / SMDS / SMDS_MeshGroup.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 SMDS : implementaion of Salome mesh data structure
23 //  File   : SMDS_MeshGroup.cxx
24 //  Author : Jean-Michel BOULCOURT
25 //  Module : SMESH
26 //
27 #ifdef _MSC_VER
28 #pragma warning(disable:4786)
29 #endif
30
31 #include "SMDS_MeshGroup.hxx"
32 #include "utilities.h"
33
34 using namespace std;
35
36 //=======================================================================
37 //function : SMDS_MeshGroup
38 //purpose  : 
39 //=======================================================================
40
41 SMDS_MeshGroup::SMDS_MeshGroup(const SMDS_Mesh * theMesh,
42                                const SMDSAbs_ElementType theType)
43         :myMesh(theMesh),myType(theType), myParent(NULL)
44 {
45 }
46
47 //=======================================================================
48 //function : SMDS_MeshGroup
49 //purpose  : 
50 //=======================================================================
51
52 SMDS_MeshGroup::SMDS_MeshGroup(SMDS_MeshGroup * theParent,
53                                const SMDSAbs_ElementType theType)
54         :myMesh(theParent->myMesh),myType(theType), myParent(theParent)
55 {
56 }
57
58 //=======================================================================
59 //function : AddSubGroup
60 //purpose  : 
61 //=======================================================================
62
63 const SMDS_MeshGroup *SMDS_MeshGroup::AddSubGroup
64                 (const SMDSAbs_ElementType theType)
65 {
66         const SMDS_MeshGroup * subgroup = new SMDS_MeshGroup(this,theType);
67         myChildren.insert(myChildren.end(),subgroup);
68         return subgroup;
69 }
70
71 //=======================================================================
72 //function : RemoveSubGroup
73 //purpose  : 
74 //=======================================================================
75
76 bool SMDS_MeshGroup::RemoveSubGroup(const SMDS_MeshGroup * theGroup)
77 {
78         bool found = false;     
79         list<const SMDS_MeshGroup*>::iterator itgroup;
80         for(itgroup=myChildren.begin(); itgroup!=myChildren.end(); itgroup++)
81         {
82                 const SMDS_MeshGroup* subgroup=*itgroup;
83                 if (subgroup == theGroup)
84                 {
85                         found = true;
86                         myChildren.erase(itgroup);
87                 }
88         }
89
90         return found;
91 }
92
93 //=======================================================================
94 //function : RemoveFromParent
95 //purpose  : 
96 //=======================================================================
97
98 bool SMDS_MeshGroup::RemoveFromParent()
99 {
100         
101         if (myParent==NULL) return false;
102         else
103         {
104                 return (myParent->RemoveSubGroup(this));
105         }
106 }
107 //=======================================================================
108 //function : Clear
109 //purpose  : 
110 //=======================================================================
111
112 void SMDS_MeshGroup::Clear()
113 {
114         myElements.clear();
115         myType = SMDSAbs_All;
116 }
117
118 //=======================================================================
119 //function : Add
120 //purpose  : 
121 //=======================================================================
122
123 void SMDS_MeshGroup::Add(const SMDS_MeshElement * theElem)
124 {
125         // the type of the group is determined by the first element added
126         if (myElements.empty()) myType = theElem->GetType();
127         else if (theElem->GetType() != myType) {
128           MESSAGE("SMDS_MeshGroup::Add : Type Mismatch "<<theElem->GetType()<<"!="<<myType);
129           return;
130         }
131         
132         myElements.insert(theElem);
133 }
134
135 //=======================================================================
136 //function : Remove
137 //purpose  : 
138 //=======================================================================
139
140 bool SMDS_MeshGroup::Remove(const SMDS_MeshElement * theElem)
141 {
142   std::set<const SMDS_MeshElement *>::iterator found
143     = myElements.find(theElem);
144   if ( found != myElements.end() ) {
145     myElements.erase(found);
146     if (myElements.empty()) myType = SMDSAbs_All;
147     return true;
148   }
149   return false;
150 }
151
152 //=======================================================================
153 //function : Contains
154 //purpose  : 
155 //=======================================================================
156
157 bool SMDS_MeshGroup::Contains(const SMDS_MeshElement * theElem) const
158 {
159         return myElements.find(theElem)!=myElements.end();
160 }
161
162 //=======================================================================
163 //function : SetType
164 //purpose  : 
165 //=======================================================================
166
167 void SMDS_MeshGroup::SetType(const SMDSAbs_ElementType theType)
168 {
169   if (IsEmpty())
170     myType = theType;
171 }