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