Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/smesh.git] / src / SMDS / SMDS_MeshGroup.cxx
1 // Copyright (C) 2007-2012  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), myTic(0)
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   ++myTic;
118 }
119
120 //=======================================================================
121 //function : Add
122 //purpose  : 
123 //=======================================================================
124
125 void SMDS_MeshGroup::Add(const SMDS_MeshElement * theElem)
126 {
127   // the type of the group is determined by the first element added
128   if (myElements.empty()) myType = theElem->GetType();
129   else if (theElem->GetType() != myType) {
130     MESSAGE("SMDS_MeshGroup::Add : Type Mismatch "<<theElem->GetType()<<"!="<<myType);
131     return;
132   }
133         
134   myElements.insert(myElements.end(), theElem);
135   ++myTic;
136 }
137
138 //=======================================================================
139 //function : Remove
140 //purpose  : 
141 //=======================================================================
142
143 bool SMDS_MeshGroup::Remove(const SMDS_MeshElement * theElem)
144 {
145   set<const SMDS_MeshElement *>::iterator found = myElements.find(theElem);
146   if ( found != myElements.end() ) {
147     myElements.erase(found);
148     if (myElements.empty()) myType = SMDSAbs_All;
149     ++myTic;
150     return true;
151   }
152   return false;
153 }
154
155 //=======================================================================
156 //function : Contains
157 //purpose  : 
158 //=======================================================================
159
160 bool SMDS_MeshGroup::Contains(const SMDS_MeshElement * theElem) const
161 {
162         return myElements.find(theElem)!=myElements.end();
163 }
164
165 //=======================================================================
166 //function : SetType
167 //purpose  : 
168 //=======================================================================
169
170 void SMDS_MeshGroup::SetType(const SMDSAbs_ElementType theType)
171 {
172   if (IsEmpty())
173     myType = theType;
174 }