summaryrefslogtreecommitdiff
path: root/src/BRepCheck/BRepCheck_Analyzer.cxx
blob: 6934ffc23106c0b4bade1dda4493adcee0d21d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// File:	BRepCheck_Analyzer.cxx
// Created:	Fri Dec  8 16:15:28 1995
// Author:	Jacques GOUSSARD
//		<jag@bravox>


#include <BRepCheck_Analyzer.ixx>

#include <BRepCheck_Vertex.hxx>
#include <BRepCheck_Edge.hxx>
#include <BRepCheck_Wire.hxx>
#include <BRepCheck_Face.hxx>
#include <BRepCheck_Shell.hxx>
#include <BRepCheck_ListIteratorOfListOfStatus.hxx>

#include <TopoDS_Iterator.hxx>
#include <TopExp_Explorer.hxx>

#include <TopTools_MapOfShape.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>

#include <Standard_ErrorHandler.hxx>
#include <Standard_Failure.hxx>

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void BRepCheck_Analyzer::Init(const TopoDS_Shape& S,
			      const Standard_Boolean B)
{
  if (S.IsNull()) {
    Standard_NullObject::Raise();
  }
  myShape = S;
  myMap.Clear();
  Put(S,B);
  Perform(S);
}



//=======================================================================
//function : Put
//purpose  : 
//=======================================================================

void BRepCheck_Analyzer::Put(const TopoDS_Shape& S,
			     const Standard_Boolean B)
{
  if (!myMap.IsBound(S)) {
    Handle(BRepCheck_Result) HR;
    switch (S.ShapeType()) {
    case TopAbs_VERTEX:
      HR = new BRepCheck_Vertex(TopoDS::Vertex(S));
      break;
    case TopAbs_EDGE:
      HR = new BRepCheck_Edge(TopoDS::Edge(S));
      Handle(BRepCheck_Edge)::DownCast(HR)->GeometricControls(B);
      break;
    case TopAbs_WIRE:
      HR = new BRepCheck_Wire(TopoDS::Wire(S));
      Handle(BRepCheck_Wire)::DownCast(HR)->GeometricControls(B);
      break;
    case TopAbs_FACE:
      HR = new BRepCheck_Face(TopoDS::Face(S));
      Handle(BRepCheck_Face)::DownCast(HR)->GeometricControls(B);
      break;
    case TopAbs_SHELL:
      HR = new BRepCheck_Shell(TopoDS::Shell(S));
      break;
    case TopAbs_SOLID:
    case TopAbs_COMPSOLID:
    case TopAbs_COMPOUND:
      break;
#ifndef DEB
    default:
      break;
#endif
    }
    myMap.Bind(S,HR);
    for(TopoDS_Iterator theIterator(S);theIterator.More();theIterator.Next()) {
      Put(theIterator.Value(),B); // performs minimum on each shape
    }
  }
}


//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

void BRepCheck_Analyzer::Perform(const TopoDS_Shape& S)
{
  for(TopoDS_Iterator theIterator(S);theIterator.More();theIterator.Next()) {
    Perform(theIterator.Value());
  }
  //
  TopAbs_ShapeEnum styp;
  TopExp_Explorer exp;
  //
  styp = S.ShapeType();
  switch (styp) {
  case TopAbs_VERTEX: 
    // modified by NIZHNY-MKK  Wed May 19 16:56:16 2004.BEGIN
    // There is no need to check anything.
    //       if (myShape.IsSame(S)) {
    // 	myMap(S)->Blind();
    //       }
    // modified by NIZHNY-MKK  Wed May 19 16:56:23 2004.END
  
    break;
  case TopAbs_EDGE:    {
    //  Modified by skv - Tue Apr 27 11:38:08 2004 Begin
    // There is no need to check anything except vertices on single edge.
    //       if (myShape.IsSame(S)) {
    // 	myMap(S)->Blind();
    //       }
    //  Modified by skv - Tue Apr 27 11:38:09 2004 End
    TopTools_MapOfShape MapS;
    
    for (exp.Init(S,TopAbs_VERTEX);exp.More(); exp.Next()) {
      const TopoDS_Shape& aVertex = exp.Current();
      try {
	OCC_CATCH_SIGNALS
	if (MapS.Add(aVertex)) {
	  myMap(aVertex)->InContext(S);
	}
      }
      catch(Standard_Failure) {
#ifdef DEB
	cout<<"BRepCheck_Analyzer : ";
	Standard_Failure::Caught()->Print(cout);  
	cout<<endl;
#endif
	if ( ! myMap(S).IsNull() ) {
	  myMap(S)->SetFailStatus(S);
	}
	Handle(BRepCheck_Result) aRes = myMap(aVertex);
	if ( ! aRes.IsNull() ) {
	  aRes->SetFailStatus(aVertex);
	  aRes->SetFailStatus(S);
	}
      }
    }
    }
    break;
  case TopAbs_WIRE:
    //  Modified by Sergey KHROMOV - Mon May  6 15:53:39 2002 Begin
    // There is no need to check the orientation of a single wire 
    // (not in context of face).
    //     {
    //       if (myShape.IsSame(S)) {
    // 	Handle(BRepCheck_Wire)::DownCast(myMap(S))->Orientation(TopoDS_Face(),
    // 								Standard_True);
    //       }
    //     }
    //  Modified by Sergey KHROMOV - Mon May  6 15:53:40 2002 End
    break;
  case TopAbs_FACE:
    {
      TopTools_MapOfShape MapS;
      for (exp.Init(S,TopAbs_VERTEX);exp.More(); exp.Next()) {
	try {
	  OCC_CATCH_SIGNALS
	  if (MapS.Add(exp.Current())) {
	    myMap(exp.Current())->InContext(S);
	  }
	}
	catch(Standard_Failure) {
#ifdef DEB
	  cout<<"BRepCheck_Analyzer : ";
	  Standard_Failure::Caught()->Print(cout);  
	  cout<<endl;
#endif
	  if ( ! myMap(S).IsNull() ) {
	    myMap(S)->SetFailStatus(S);
	  }
	  Handle(BRepCheck_Result) aRes = myMap(exp.Current());

	  if ( ! aRes.IsNull() ) {
	    aRes->SetFailStatus(exp.Current());
	    aRes->SetFailStatus(S);
	  }
	}
      }
      Standard_Boolean performwire = Standard_True;
      MapS.Clear();
      for (exp.Init(S,TopAbs_EDGE);exp.More(); exp.Next()) {
	try {
	  OCC_CATCH_SIGNALS
	  if (MapS.Add(exp.Current())) {
	    Handle(BRepCheck_Result)& res = myMap(exp.Current());
	    res->InContext(S);
	    if (performwire) {
	      for (res->InitContextIterator();
		   res->MoreShapeInContext();
		   res->NextShapeInContext()) {
		if(res->ContextualShape().IsSame(S)) {
		  break;
		}
	      }
	      BRepCheck_ListIteratorOfListOfStatus itl(res->StatusOnShape());
	      for (; itl.More(); itl.Next()) {
		BRepCheck_Status ste = itl.Value();
		if (ste == BRepCheck_NoCurveOnSurface  ||
		    ste == BRepCheck_InvalidCurveOnSurface ||
		    ste == BRepCheck_InvalidRange ||
		    ste == BRepCheck_InvalidCurveOnClosedSurface) {
		  performwire = Standard_False;
		  break;
		}
	      }
	    }
	  }
	}
	catch(Standard_Failure) {
#ifdef DEB
	  cout<<"BRepCheck_Analyzer : ";
	  Standard_Failure::Caught()->Print(cout);  
	  cout<<endl;
#endif

	  if ( ! myMap(S).IsNull() ) {
	    myMap(S)->SetFailStatus(S);
	  }
	  Handle(BRepCheck_Result) aRes = myMap(exp.Current());

	  if ( ! aRes.IsNull() ) {
	    aRes->SetFailStatus(exp.Current());
	    aRes->SetFailStatus(S);
	  }
	}
      }
      Standard_Boolean orientofwires = performwire;
      for (exp.Init(S,TopAbs_WIRE);exp.More(); exp.Next()) {
	try {
	  OCC_CATCH_SIGNALS
	  Handle(BRepCheck_Result)& res = myMap(exp.Current());
	  res->InContext(S);
	  if (orientofwires) {
	    for (res->InitContextIterator();
		 res->MoreShapeInContext();
		 res->NextShapeInContext()) {
	      if(res->ContextualShape().IsSame(S)) {
		break;
	      }
	    }
	    BRepCheck_ListIteratorOfListOfStatus itl(res->StatusOnShape());
	    for (; itl.More(); itl.Next()) {
	      BRepCheck_Status ste = itl.Value();
	      if (ste != BRepCheck_NoError) {
		orientofwires = Standard_False;
		break;
	      }
	    }
	  }	  
	}
	catch(Standard_Failure) {
#ifdef DEB
	  cout<<"BRepCheck_Analyzer : ";
	  Standard_Failure::Caught()->Print(cout);  
	  cout<<endl;
#endif
	  if ( ! myMap(S).IsNull() ) {
	    myMap(S)->SetFailStatus(S);
	  }
	  Handle(BRepCheck_Result) aRes = myMap(exp.Current());

	  if ( ! aRes.IsNull() ) {
	    aRes->SetFailStatus(exp.Current());
	    aRes->SetFailStatus(S);
	  }
	}
      }
      
      try {
        OCC_CATCH_SIGNALS
	if (performwire) {
	  if (orientofwires) {
	    Handle(BRepCheck_Face)::DownCast(myMap(S))->
	      OrientationOfWires(Standard_True);// on enregistre
	  }
	  //	else {
	  //	   Handle(BRepCheck_Face)::DownCast(myMap(S))->
	  //	      IntersectWires(Standard_True); // on enregistre
	  //      }
	  else {
	    Handle(BRepCheck_Face)::DownCast(myMap(S))->SetUnorientable();
	  }
	}
	else {
	  Handle(BRepCheck_Face)::DownCast(myMap(S))->SetUnorientable();
	}
      }
      catch(Standard_Failure) {
#ifdef DEB
	  cout<<"BRepCheck_Analyzer : ";
	  Standard_Failure::Caught()->Print(cout);  
	  cout<<endl;
#endif

	if ( ! myMap(S).IsNull() ) {
	  myMap(S)->SetFailStatus(S);
	}
	
	for (exp.Init(S,TopAbs_WIRE);exp.More(); exp.Next()) {
	  Handle(BRepCheck_Result) aRes = myMap(exp.Current());

	  if ( ! aRes.IsNull() ) {
	    aRes->SetFailStatus(exp.Current());
	    aRes->SetFailStatus(S);
	    myMap(S)->SetFailStatus(exp.Current());
	  }
	}
      }
    }
    break;
    
  case TopAbs_SHELL:   
    //modified by NIZNHY-PKV Mon Oct 13 14:23:53 2008f
    /* {
    Standard_Boolean VerifyOrientation, bFlag;
    //
    VerifyOrientation = Standard_True;
    //
    exp.Init(S,TopAbs_FACE);
    for (; exp.More(); exp.Next()) {
      const TopoDS_Shape& aF=exp.Current();
      try {
	OCC_CATCH_SIGNALS
	bFlag=  !(Handle(BRepCheck_Face)::DownCast(myMap(aF))->IsUnorientable());
	VerifyOrientation = (VerifyOrientation && bFlag);
      }
      catch(Standard_Failure) {
#ifdef DEB
	cout<<"BRepCheck_Analyzer : ";
	Standard_Failure::Caught()->Print(cout);  
	cout<<endl;
#endif
	
	if ( ! myMap(S).IsNull() ) {
	  myMap(S)->SetFailStatus(S);
	}
	Handle(BRepCheck_Result) aRes = myMap(exp.Current());
	
	if ( ! aRes.IsNull() ) {
	  aRes->SetFailStatus(exp.Current());
	  aRes->SetFailStatus(S);
	}
      }
    } // 
    try {
      OCC_CATCH_SIGNALS
      if (VerifyOrientation) {
	Handle(BRepCheck_Shell)::DownCast(myMap(S))->Orientation(Standard_True);
      }
      else {
	Handle(BRepCheck_Shell)::DownCast(myMap(S))->SetUnorientable();
      }
    }
    catch(Standard_Failure) {
#ifdef DEB
      cout<<"BRepCheck_Analyzer : ";
      Standard_Failure::Caught()->Print(cout);  
      cout<<endl;
#endif
      
      if ( ! myMap(S).IsNull() ) {
	myMap(S)->SetFailStatus(S);
      }
      exp.Init(S,TopAbs_FACE);
      for (; exp.More(); exp.Next()) {
	Handle(BRepCheck_Result) aRes = myMap(exp.Current());
	if ( ! aRes.IsNull() ) {
	  aRes->SetFailStatus(exp.Current());
	  aRes->SetFailStatus(S);
	  myMap(S)->SetFailStatus(exp.Current());
	}
      }
    }
  }
  */
    //modified by NIZNHY-PKV Mon Oct 13 14:24:04 2008t
    break;
    //
  case TopAbs_SOLID:    {
    exp.Init(S,TopAbs_SHELL);
    for (; exp.More(); exp.Next()) {
      const TopoDS_Shape& aShell=exp.Current();
      try {
	OCC_CATCH_SIGNALS
	myMap(aShell)->InContext(S);
      }
      catch(Standard_Failure) {
#ifdef DEB
	cout<<"BRepCheck_Analyzer : ";
	Standard_Failure::Caught()->Print(cout);  
	cout<<endl;
#endif
	if ( ! myMap(S).IsNull() ) {
	  myMap(S)->SetFailStatus(S);
	}
	//
	Handle(BRepCheck_Result) aRes = myMap(aShell);
	if (!aRes.IsNull() ) {
	  aRes->SetFailStatus(exp.Current());
	  aRes->SetFailStatus(S);
	}
      }
    }
  }
  break;//case TopAbs_SOLID
  default:
    break;
  }//switch (styp) {
}


//=======================================================================
//function : IsValid
//purpose  : 
//=======================================================================

Standard_Boolean BRepCheck_Analyzer::IsValid(const TopoDS_Shape& S) const
{
  if (!myMap(S).IsNull()) {
    BRepCheck_ListIteratorOfListOfStatus itl;
    itl.Initialize(myMap(S)->Status());
    if (itl.Value() != BRepCheck_NoError) { // a voir
      return Standard_False;
    }
  }

  for(TopoDS_Iterator theIterator(S);theIterator.More();theIterator.Next()) {
    if (!IsValid(theIterator.Value())) {
      return Standard_False;
    }
  }

  switch (S.ShapeType()) {
  case TopAbs_EDGE:
    {
      return ValidSub(S,TopAbs_VERTEX);
    }
//    break;
  case TopAbs_FACE:
    {
      Standard_Boolean valid = ValidSub(S,TopAbs_WIRE);
      valid = valid && ValidSub(S,TopAbs_EDGE);
      valid = valid && ValidSub(S,TopAbs_VERTEX);
      return valid;
    }

//    break;
  case TopAbs_SHELL:
//    return ValidSub(S,TopAbs_FACE);
    break;
  case TopAbs_SOLID:
//    return ValidSub(S,TopAbs_EDGE);
//    break;
    return ValidSub(S,TopAbs_SHELL);
    break;
  default:
    break;
  }

  return Standard_True;
}

//=======================================================================
//function : ValidSub
//purpose  : 
//=======================================================================

Standard_Boolean BRepCheck_Analyzer::ValidSub
   (const TopoDS_Shape& S,
    const TopAbs_ShapeEnum SubType) const
{
  BRepCheck_ListIteratorOfListOfStatus itl;
  TopExp_Explorer exp;
  for (exp.Init(S,SubType);exp.More(); exp.Next()) {
//  for (TopExp_Explorer exp(S,SubType);exp.More(); exp.Next()) {
    const Handle(BRepCheck_Result)& RV = myMap(exp.Current());
    for (RV->InitContextIterator();
	 RV->MoreShapeInContext(); 
	 RV->NextShapeInContext()) {
      if (RV->ContextualShape().IsSame(S)) {
	break;
      }
    }

    if(!RV->MoreShapeInContext()) break;

    for (itl.Initialize(RV->StatusOnShape()); itl.More(); itl.Next()) {
      if (itl.Value() != BRepCheck_NoError) {
	return Standard_False;
      }
    }
  }
  return Standard_True ;
}