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