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