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