Salome HOME
783547bb103973f060c842abbec89e45f68a0c11
[modules/smesh.git] / src / SMESH / SMESH_HypoFilter.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_HypoFilter.cxx
25 //  Module : SMESH
26 //  $Header$
27
28 #include "SMESH_HypoFilter.hxx"
29
30 #include "SMESH_Hypothesis.hxx"
31 #include "SMESH_subMesh.hxx"
32
33 using namespace std;
34
35
36 //=======================================================================
37 //function : NamePredicate::Value
38 //purpose  : 
39 //=======================================================================
40
41 bool SMESH_HypoFilter::NamePredicate::IsOk (const SMESH_Hypothesis* aHyp,
42                                             const TopoDS_Shape&     /*aShape*/ ) const
43 {
44   return ( _name == aHyp->GetName() );
45 }
46
47 //=======================================================================
48 //function : TypePredicate::Value
49 //purpose  : 
50 //=======================================================================
51
52 int SMESH_HypoFilter::TypePredicate::Value( const SMESH_Hypothesis* aHyp ) const
53 {
54   return aHyp->GetType();
55 };
56
57 //=======================================================================
58 //function : DimPredicate::Value
59 //purpose  : 
60 //=======================================================================
61
62 int SMESH_HypoFilter::DimPredicate::Value( const SMESH_Hypothesis* aHyp ) const
63 {
64   return aHyp->GetDim();
65 }
66
67 //=======================================================================
68 //function : ApplicablePredicate::IsOk
69 //purpose  : 
70 //=======================================================================
71
72 bool SMESH_HypoFilter::ApplicablePredicate::IsOk(const SMESH_Hypothesis* aHyp,
73                                                  const TopoDS_Shape&     /*aShape*/) const
74 {
75   return SMESH_subMesh::IsApplicableHypotesis( aHyp, (TopAbs_ShapeEnum)_shapeType );
76 };
77
78 //=======================================================================
79 //function : ApplicablePredicate::ApplicablePredicate
80 //purpose  : 
81 //=======================================================================
82
83 SMESH_HypoFilter::ApplicablePredicate::ApplicablePredicate( const TopoDS_Shape& theShape )
84 {
85   _shapeType = ( theShape.IsNull() ? TopAbs_SHAPE : theShape.ShapeType());
86 }
87
88 //=======================================================================
89 //function : InstancePredicate::IsOk
90 //purpose  : 
91 //=======================================================================
92
93 bool SMESH_HypoFilter::InstancePredicate::IsOk(const SMESH_Hypothesis* aHyp,
94                                                const TopoDS_Shape&     /*aShape*/) const
95 {
96   return _hypo == aHyp;
97 }
98
99 //=======================================================================
100 //function : IsGlobalPredicate::IsOk
101 //purpose  : 
102 //=======================================================================
103
104 bool SMESH_HypoFilter::IsGlobalPredicate::IsOk(const SMESH_Hypothesis* aHyp,
105                                                const TopoDS_Shape&     aShape) const
106 {
107   return ( !_mainShape.IsNull() && !aShape.IsNull() && _mainShape.IsSame( aShape ));
108 }
109
110 //=======================================================================
111 //function : SMESH_HypoFilter
112 //purpose  : 
113 //=======================================================================
114
115 SMESH_HypoFilter::SMESH_HypoFilter()
116 {
117 }
118
119 //=======================================================================
120 //function : SMESH_HypoFilter
121 //purpose  : 
122 //=======================================================================
123
124 SMESH_HypoFilter::SMESH_HypoFilter( SMESH_HypoPredicate* aPredicate, bool notNagate )
125 {
126   add( notNagate ? AND : AND_NOT, aPredicate );
127 }
128
129 //=======================================================================
130 //function : And
131 //purpose  : 
132 //=======================================================================
133
134 void SMESH_HypoFilter::And( SMESH_HypoPredicate* aPredicate )
135 {
136   add( AND, aPredicate );
137 }
138
139 //=======================================================================
140 //function : AndNot
141 //purpose  : 
142 //=======================================================================
143
144 void SMESH_HypoFilter::AndNot( SMESH_HypoPredicate* aPredicate )
145 {
146   add( AND_NOT, aPredicate );
147 }
148
149 //=======================================================================
150 //function : Or
151 //purpose  : 
152 //=======================================================================
153
154 void SMESH_HypoFilter::Or( SMESH_HypoPredicate* aPredicate )
155 {
156   add( OR, aPredicate );
157 }
158
159 //=======================================================================
160 //function : OrNot
161 //purpose  : Return predicates
162 //=======================================================================
163
164 void SMESH_HypoFilter::OrNot( SMESH_HypoPredicate* aPredicate )
165 {
166   add( OR_NOT, aPredicate );
167 }
168
169 //=======================================================================
170 //function : Is
171 //purpose  : 
172 //=======================================================================
173
174 SMESH_HypoPredicate* SMESH_HypoFilter::Is(const SMESH_Hypothesis* theHypo)
175 {
176   return new InstancePredicate( theHypo );
177 }
178
179 //=======================================================================
180 //function : IsAlgo
181 //purpose  : 
182 //=======================================================================
183
184 SMESH_HypoPredicate* SMESH_HypoFilter::IsAlgo()
185 {
186   return new TypePredicate( MORE, SMESHDS_Hypothesis::PARAM_ALGO );
187 }
188
189 //=======================================================================
190 //function : IsGlobal
191 //purpose  : 
192 //=======================================================================
193
194  SMESH_HypoPredicate* SMESH_HypoFilter::IsGlobal(const TopoDS_Shape& theMainShape)
195 {
196   return new IsGlobalPredicate( theMainShape );
197 }
198
199 //=======================================================================
200 //function : HasName
201 //purpose  : 
202 //=======================================================================
203
204 SMESH_HypoPredicate* SMESH_HypoFilter::HasName(const string & theName)
205 {
206   return new NamePredicate( theName );
207 }
208
209 //=======================================================================
210 //function : HasDim
211 //purpose  : 
212 //=======================================================================
213
214 SMESH_HypoPredicate* SMESH_HypoFilter::HasDim(const int theDim)
215 {
216   return new DimPredicate( EQUAL, theDim );
217 }
218
219 //=======================================================================
220 //function : IsApplicableTo
221 //purpose  : 
222 //=======================================================================
223
224 SMESH_HypoPredicate* SMESH_HypoFilter::IsApplicableTo(const TopoDS_Shape& theShape)
225 {
226   return new ApplicablePredicate( theShape );
227 }
228
229 //=======================================================================
230 //function : HasType
231 //purpose  : 
232 //=======================================================================
233
234 SMESH_HypoPredicate* SMESH_HypoFilter::HasType(const int theHypType)
235 {
236   return new TypePredicate( EQUAL, theHypType );
237 }
238
239 //=======================================================================
240 //function : IsOk
241 //purpose  : 
242 //=======================================================================
243
244 bool SMESH_HypoFilter::IsOk (const SMESH_Hypothesis* aHyp,
245                              const TopoDS_Shape&     aShape) const
246 {
247   if ( myPredicates.empty() )
248     return true;
249
250   bool ok = ( myPredicates.front()->_logical_op <= AND_NOT );
251   list<SMESH_HypoPredicate*>::const_iterator pred = myPredicates.begin();
252   for ( ; pred != myPredicates.end(); ++pred )
253   {
254     bool ok2 = (*pred)->IsOk( aHyp, aShape );
255     switch ( (*pred)->_logical_op ) {
256     case AND:     ok = ok && ok2; break;
257     case AND_NOT: ok = ok && !ok2; break;
258     case OR:      ok = ok || ok2; break;
259     case OR_NOT:  ok = ok || !ok2; break;
260     default:;
261     }
262   }
263   return ok;
264 }
265
266 //=======================================================================
267 //function : Init
268 //purpose  : 
269 //=======================================================================
270
271 void SMESH_HypoFilter::Init  ( SMESH_HypoPredicate* aPredicate, bool notNagate )
272 {
273   list<SMESH_HypoPredicate*>::const_iterator pred = myPredicates.begin();
274   for ( ; pred != myPredicates.end(); ++pred )
275     delete *pred;
276
277   add( notNagate ? AND : AND_NOT, aPredicate );
278 }
279
280
281 //=======================================================================
282 //function : IsOk
283 //purpose  : 
284 //=======================================================================
285
286 SMESH_HypoFilter::~SMESH_HypoFilter()
287 {
288   Init(0);
289 }
290