summaryrefslogtreecommitdiff
path: root/src/BOP/BOP_ArgumentAnalyzer.cxx
blob: 7dfb8c2c981008ec14c7d4dc126c66bff662755c (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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
// File:      BOP_ArgumentAnalyzer.cxx
// Created:   02.09.04 16:26:31
// Copyright: Open Cascade 2004

#include <BOP_ArgumentAnalyzer.ixx>
#include <BOP_CheckResult.hxx>
#include <BOP_ShellSolid.hxx>
#include <BOP_WireSolid.hxx>
#include <BOP_WireShell.hxx>
#include <BOP_WireEdgeSet.hxx>
#include <BOP_FaceBuilder.hxx>

#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
#include <BRepExtrema_DistShapeShape.hxx>
#include <gp_Pnt.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Solid.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_SequenceOfShape.hxx>
#include <TopTools_MapOfShape.hxx>

#include <TColStd_Array2OfBoolean.hxx>

#include <IntTools_Context.hxx>
#include <IntTools_Range.hxx>
#include <IntTools_ShrunkRange.hxx>
#include <IntTools_EdgeEdge.hxx>
#include <IntTools_CommonPrt.hxx>

#include <BOPTools_DSFiller.hxx>
#include <BOPTools_Tools3D.hxx>
#include <BOPTools_Checker.hxx>
#include <BOPTools_CheckResult.hxx>
#include <BOPTools_ListOfCheckResults.hxx>
#include <BOPTools_ListIteratorOfListOfCheckResults.hxx>

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

static Standard_Boolean TestShapeType(const TopoDS_Shape & TheShape);

static Standard_Boolean CheckEdge(const TopoDS_Edge& theEdge);

static Standard_Boolean TestSubShapeType(const TopAbs_ShapeEnum theT1,
                                         const TopAbs_ShapeEnum theT2,
                                         const BOP_Operation    theOP);

// ================================================================================
// function: Constructor
// purpose:
// ================================================================================
BOP_ArgumentAnalyzer::BOP_ArgumentAnalyzer() : 
myStopOnFirst(Standard_False),
myOperation(BOP_UNKNOWN),
myArgumentTypeMode(Standard_False),
mySelfInterMode(Standard_False),
mySmallEdgeMode(Standard_False),
myRebuildFaceMode(Standard_False),
myTangentMode(Standard_False),
myMergeVertexMode(Standard_False),
myMergeEdgeMode(Standard_False)
// myMergeFaceMode(Standard_False)
{
}

// ================================================================================
// function: SetShape1
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::SetShape1(const TopoDS_Shape & TheShape)
{
  myShape1 = TheShape;
}

// ================================================================================
// function: SetShape2
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::SetShape2(const TopoDS_Shape & TheShape)
{
  myShape2 = TheShape;
}

// ================================================================================
// function: GetShape1
// purpose:
// ================================================================================
const TopoDS_Shape & BOP_ArgumentAnalyzer::GetShape1() const
{
  return myShape1;
}

// ================================================================================
// function: GetShape2
// purpose:
// ================================================================================
const TopoDS_Shape & BOP_ArgumentAnalyzer::GetShape2() const
{
  return myShape2;
}

// ================================================================================
// function: OperationType
// purpose:
// ================================================================================
BOP_Operation& BOP_ArgumentAnalyzer::OperationType() 
{
  return myOperation;
}

// ================================================================================
// function: StopOnFirstFaulty
// purpose:
// ================================================================================
Standard_Boolean & BOP_ArgumentAnalyzer::StopOnFirstFaulty()
{
  return myStopOnFirst;
}

// ================================================================================
// function: Perform
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::Perform()
{
  try {
    OCC_CATCH_SIGNALS
    myResult.Clear();

    if(myArgumentTypeMode) {
      TestTypes();
    }

    if(mySelfInterMode) {
      TestSelfInterferences();
    }

    if(mySmallEdgeMode) {
      if(!(!myResult.IsEmpty() && myStopOnFirst))
        TestSmallEdge();
    }

    if(myRebuildFaceMode) {
      if(!(!myResult.IsEmpty() && myStopOnFirst))
        TestRebuildFace();
    }

    if(myTangentMode) {
      if(!(!myResult.IsEmpty() && myStopOnFirst))
        TestTangent();
    }

    if(myMergeVertexMode) {
      if(!(!myResult.IsEmpty() && myStopOnFirst))
        TestMergeVertex();
    }
    
    if(myMergeEdgeMode) {
      if(!(!myResult.IsEmpty() && myStopOnFirst))
        TestMergeEdge();
    }
    
//     if(myMergeFaceMode) {
//       TestMergeFace();
//     }
  }
  catch(Standard_Failure) {
    BOP_CheckResult aResult;
    aResult.SetCheckStatus(BOP_CheckUnknown);
    myResult.Append(aResult);
  }
}

// ================================================================================
// function: HasFaulty
// purpose:
// ================================================================================
Standard_Boolean BOP_ArgumentAnalyzer::HasFaulty() const
{
  return ( !myResult.IsEmpty());
}

// ================================================================================
// function: GetCheckResult
// purpose:
// ================================================================================
const BOP_ListOfCheckResult& BOP_ArgumentAnalyzer::GetCheckResult() const
{
  return myResult;
}

// ================================================================================
// function: TestTypes
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::TestTypes()
{
  Standard_Boolean isS1 = myShape1.IsNull(), isS2 = myShape2.IsNull();

  if(isS1 && isS2) {
    BOP_CheckResult aResult;
    aResult.SetCheckStatus(BOP_BadType);
    myResult.Append(aResult);
    return;
  }

  Standard_Boolean testS1 = TestShapeType(myShape1);
  Standard_Boolean testS2 = TestShapeType(myShape2);

  // single shape check (begin)
  if((isS1 && !isS2) || (!isS1 && isS2)) {
//     Standard_Boolean testS = (isS1) ? testS1 : testS2;
    Standard_Boolean testS = (isS1) ? testS2 : testS1;

    if(!testS) {
      const TopoDS_Shape & aS = (isS1) ? myShape1 : myShape2;
      BOP_CheckResult aResult;
      aResult.SetShape1(aS);
      aResult.SetCheckStatus(BOP_BadType);
      myResult.Append(aResult);
      return;
    }
    
  } // single shape is set (end)
  // two shapes check (begin)
  else {
    // test compounds and compsolids
    if(!testS1 || !testS2) {
      BOP_CheckResult aResult;
      if(!testS1 && !testS2) {
	aResult.SetShape1(myShape1);
	aResult.SetShape2(myShape2);
      }
      else {
	const TopoDS_Shape & aS = (!testS1) ? myShape1 : myShape2;
        if(!testS1)
          aResult.SetShape1(aS);
        else
          aResult.SetShape2(aS);
      }
      aResult.SetCheckStatus(BOP_BadType);
      myResult.Append(aResult);
      return;
    }
    // test faces, wires, edges
    TopAbs_ShapeEnum aT1 = myShape1.ShapeType(), aT2 = myShape2.ShapeType();

    if(aT1 != TopAbs_COMPOUND && aT2 != TopAbs_COMPOUND) {
      Standard_Boolean aTestRes = TestSubShapeType(aT1,aT2,myOperation);
      if(!aTestRes) {
        BOP_CheckResult aResult;
	aResult.SetShape1(myShape1);
	aResult.SetShape2(myShape2);
	aResult.SetCheckStatus(BOP_BadType);
	myResult.Append(aResult);
	return;
      }
    }
    else {
      Standard_Boolean aTestRes = Standard_True;
      if(aT1 == TopAbs_COMPOUND && aT2 != TopAbs_COMPOUND) {
        TopoDS_Iterator itS1(myShape1);
        while(itS1.More()) {
          aT1 = itS1.Value().ShapeType();
          aTestRes = TestSubShapeType(aT1,aT2,myOperation);
          if(!aTestRes)
            break;
          itS1.Next();
        }
      }
      else if(aT1 != TopAbs_COMPOUND && aT2 == TopAbs_COMPOUND) {
        TopoDS_Iterator itS2(myShape2);
        while(itS2.More()) {
          aT2 = itS2.Value().ShapeType();
          aTestRes = TestSubShapeType(aT1,aT2,myOperation);
          if(!aTestRes)
            break;
          itS2.Next();
        }
      }
      else {
        TopoDS_Iterator itS1(myShape1);
        while(itS1.More()) {
          aT1 = itS1.Value().ShapeType();
          TopoDS_Iterator itS2(myShape2);
          while(itS2.More()) {
            aT2 = itS2.Value().ShapeType();
            aTestRes = TestSubShapeType(aT1,aT2,myOperation);
            if(!aTestRes)
              break;
            itS2.Next();
          }
          if(!aTestRes)
            break;
          itS1.Next();
        }
      }

      if(!aTestRes) {
        BOP_CheckResult aResult;
	aResult.SetShape1(myShape1);
	aResult.SetShape2(myShape2);
	aResult.SetCheckStatus(BOP_BadType);
	myResult.Append(aResult);
	return;
      }
    }
  } // both shapes are set (end)
}

// ================================================================================
// function: TestSelfInterferences
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::TestSelfInterferences()
{
  Standard_Integer i = 0;

  for(i = 0; i < 2; i++) {
    TopoDS_Shape aS = (i == 0) ? myShape1 : myShape2;

    if(aS.IsNull())
      continue;
    BOPTools_Checker aChecker(aS);
    aChecker.Perform();

    if (aChecker.HasFaulty()) {
      const BOPTools_ListOfCheckResults& aResultList = aChecker.GetCheckResult();
      BOPTools_ListIteratorOfListOfCheckResults anIt(aResultList);

      for(; anIt.More(); anIt.Next()) {
	const BOPTools_CheckResult& aCheckResult = anIt.Value();

	if((aCheckResult.GetCheckStatus() == BOPTools_CHKUNKNOWN) ||
	   (aCheckResult.GetCheckStatus() == BOPTools_BADSHRANKRANGE) ||
	   (aCheckResult.GetCheckStatus() == BOPTools_NULLSRANKRANGE)) {
	  continue;
	}
	BOP_CheckResult aResult;
	if(i == 0)
	  aResult.SetShape1(myShape1);
	else
	  aResult.SetShape2(myShape2);
	TopTools_ListIteratorOfListOfShape anIt2(aCheckResult.GetShapes());

	for(; anIt2.More(); anIt2.Next()) {
	  if(i == 0)
	    aResult.AddFaultyShape1(anIt2.Value());
	  else
	    aResult.AddFaultyShape2(anIt2.Value());
	}
	aResult.SetCheckStatus(BOP_SelfIntersect);
	myResult.Append(aResult);

	if(myStopOnFirst) {
	  return;
	}
      }
    }
  }
}

// ================================================================================
// function: TestSmallEdge
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::TestSmallEdge() 
{
  Standard_Integer i = 0;
  BRepExtrema_DistShapeShape aDist;

  for(i = 0; i < 2; i++) {
    TopoDS_Shape aS = (i == 0) ? myShape1 : myShape2;

    if(aS.IsNull())
      continue;

    TopExp_Explorer anExp(aS, TopAbs_EDGE);

    for(; anExp.More(); anExp.Next()) {
      TopoDS_Edge anEdge = TopoDS::Edge(anExp.Current());

      if(!CheckEdge(anEdge)) {
	Standard_Boolean bKeepResult = Standard_True;

	if(myOperation == BOP_SECTION) {
	  TopoDS_Shape anOtherS = (i == 0) ? myShape2 : myShape1;
	  
	  if(!anOtherS.IsNull()) {
//	    BRepExtrema_DistShapeShape aDist;
	    aDist.LoadS2(anOtherS);
	    
	    Standard_Boolean bVertexIsOnShape = Standard_False;
	    Standard_Integer ii = 0;
	    TopExp_Explorer anExpV(anEdge, TopAbs_VERTEX);
	    
	    for(; anExpV.More(); anExpV.Next()) {
	      TopoDS_Shape aV = anExpV.Current();

	      aDist.LoadS1(aV);
	      aDist.Perform();

	      if(aDist.IsDone()) {

		for(ii = 1; ii <= aDist.NbSolution(); ii++) {
		  Standard_Real aTolerance = BRep_Tool::Tolerance(TopoDS::Vertex(aV));
		  TopoDS_Shape aSupportShape = aDist.SupportOnShape2(ii);

		  switch(aSupportShape.ShapeType()) {
		  case TopAbs_VERTEX: {
		    aTolerance += BRep_Tool::Tolerance(TopoDS::Vertex(aSupportShape));
		    break;
		  }
		  case TopAbs_EDGE: {
		    aTolerance += BRep_Tool::Tolerance(TopoDS::Edge(aSupportShape));
		    break;
		  }
		  case TopAbs_FACE: {
		    aTolerance += BRep_Tool::Tolerance(TopoDS::Face(aSupportShape));
		    break;
		  }
		  default:
		    break;
		  }
		  
		  if(aDist.Value() < aTolerance) {
		    bVertexIsOnShape = Standard_True;
		    break;
		  }
		}
	      }
	    }

	    if(!bVertexIsOnShape) {
	      bKeepResult = Standard_False;
	    }
	  }
	}

	if(bKeepResult) {
	  BOP_CheckResult aResult;

	  if(i == 0) {
	    aResult.SetShape1(myShape1);
	    aResult.AddFaultyShape1(anEdge);
	  }
	  else {
	    aResult.SetShape2(myShape2);
	    aResult.AddFaultyShape2(anEdge);
	  }
	  
	  aResult.SetCheckStatus(BOP_TooSmallEdge);
	  myResult.Append(aResult);

	  if(myStopOnFirst) {
	    return;
	  }
	}
      }
    }
  }
}

// ================================================================================
// function: TestRebuildFace
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::TestRebuildFace() 
{
  if((myOperation == BOP_SECTION) ||
     (myOperation == BOP_UNKNOWN))
    return;
  Standard_Integer i = 0;

  for(i = 0; i < 2; i++) {
    TopoDS_Shape aS = (i == 0) ? myShape1 : myShape2;

    if(aS.IsNull())
      continue;

    TopExp_Explorer anExp(aS, TopAbs_FACE);

    for(; anExp.More(); anExp.Next()) {
      TopoDS_Face aFace = TopoDS::Face(anExp.Current());

      BOP_WireEdgeSet aWES (aFace);
      TopExp_Explorer anExpE(aFace, TopAbs_EDGE);
      Standard_Integer nbstartedges = 0;

      for(; anExpE.More(); anExpE.Next()) {
	aWES.AddStartElement(anExpE.Current());
	nbstartedges++;
      }
      BOP_FaceBuilder aFB;
      aFB.Do(aWES);
      const TopTools_ListOfShape& aLF = aFB.NewFaces();
      Standard_Boolean bBadFace = Standard_False;

      if(aLF.Extent() != 1) {
	bBadFace = Standard_True;
      }
      else {
	Standard_Integer nbedgeused = 0;
	anExpE.Init(aLF.First(), TopAbs_EDGE);

	for(; anExpE.More(); anExpE.Next(), nbedgeused++) {}

	if(nbstartedges != nbedgeused) {
	  bBadFace = Standard_True;
	}
      }

      if(bBadFace) {
	BOP_CheckResult aResult;

	if(i == 0) {
	  aResult.SetShape1(myShape1);
	  aResult.AddFaultyShape1(aFace);
	}
	else {
	  aResult.SetShape2(myShape2);
	  aResult.AddFaultyShape2(aFace);
	}
	  
	aResult.SetCheckStatus(BOP_NonRecoverableFace);
	myResult.Append(aResult);

	if(myStopOnFirst) {
	  return;
	}
      }
    }
  }
}

// ================================================================================
// function: TestTangent
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::TestTangent() 
{
 // not implemented
}

// ================================================================================
// function: TestMergeSubShapes
// purpose:
// ================================================================================
 void BOP_ArgumentAnalyzer::TestMergeSubShapes(const TopAbs_ShapeEnum theType) 
{
  if(myShape1.IsNull() || myShape2.IsNull())
    return;

  BOP_CheckStatus aStatus = BOP_CheckUnknown;

  switch(theType) {
  case TopAbs_VERTEX: {
    aStatus = BOP_IncompatibilityOfVertex;
    break;
  }
  case TopAbs_EDGE: {
    aStatus = BOP_IncompatibilityOfEdge;
    break;
  }
  case TopAbs_FACE: {
    aStatus = BOP_IncompatibilityOfFace;
    break;
  }
  default: 
    return;
  }
  TopExp_Explorer anExp1(myShape1, theType);
  TopExp_Explorer anExp2(myShape2, theType);
  TopTools_SequenceOfShape aSeq1, aSeq2;
  TopTools_MapOfShape aMap1, aMap2;

  for(; anExp1.More(); anExp1.Next()) {
    TopoDS_Shape aS1 = anExp1.Current();

    if(aMap1.Contains(aS1))
      continue;
    aSeq1.Append(aS1);
    aMap1.Add(aS1);
  }

  for(; anExp2.More(); anExp2.Next()) {
    TopoDS_Shape aS2 = anExp2.Current();
    if(aMap2.Contains(aS2))
      continue;
    aSeq2.Append(aS2);
    aMap2.Add(aS2);
  }

  TColStd_Array2OfBoolean anArrayOfFlag(1, aSeq1.Length(), 1, aSeq2.Length());
  Standard_Integer i = 0, j = 0;
  for(i = 1; i <= aSeq1.Length(); i++)
    for(j = 1; j <= aSeq2.Length(); j++)
      anArrayOfFlag.SetValue(i, j, Standard_False);

  for(i = 1; i <= aSeq1.Length(); i++) {
    TopoDS_Shape aS1 = aSeq1.Value(i);
    TopTools_ListOfShape aListOfS2;
    Standard_Integer nbs = 0;

    for(j = 1; j <= aSeq2.Length(); j++) {
      TopoDS_Shape aS2 = aSeq2.Value(j);
      Standard_Boolean bIsEqual = Standard_False;

      if(theType == TopAbs_VERTEX) {

        TopoDS_Vertex aV1 = TopoDS::Vertex(aS1);
	TopoDS_Vertex aV2 = TopoDS::Vertex(aS2);
        gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
        gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
        Standard_Real aDist = aP1.Distance(aP2);

        if(aDist <= (BRep_Tool::Tolerance(aV1) + BRep_Tool::Tolerance(aV2))) {
          bIsEqual = Standard_True;
        }
      }
      else if(theType == TopAbs_EDGE) {
	Standard_Integer aDiscretize = 30;
	Standard_Real    aDeflection = 0.01;
	TopoDS_Edge aE1 = TopoDS::Edge(aS1);
	TopoDS_Edge aE2 = TopoDS::Edge(aS2);

	IntTools_EdgeEdge aEE;
	aEE.SetEdge1 (aE1);
	aEE.SetEdge2 (aE2);
	aEE.SetTolerance1 (BRep_Tool::Tolerance(aE1));
	aEE.SetTolerance2 (BRep_Tool::Tolerance(aE2));
	aEE.SetDiscretize (aDiscretize);
	aEE.SetDeflection (aDeflection);

	Standard_Real f = 0., l = 0.;
	BRep_Tool::Range(aE1, f, l);
	aEE.SetRange1(f, l);

	BRep_Tool::Range(aE2, f, l);
	aEE.SetRange2(f, l);

	aEE.Perform();

	if (aEE.IsDone()) {
	  const IntTools_SequenceOfCommonPrts& aCPrts = aEE.CommonParts();
	  Standard_Integer ii = 0;

	  for (ii = 1; ii <= aCPrts.Length(); ii++) {
	    const IntTools_CommonPrt& aCPart = aCPrts(ii);

	    if (aCPart.Type() == TopAbs_EDGE) {
	      bIsEqual = Standard_True;
	    }
	  }
	}
      }
      else if(theType == TopAbs_FACE) {
        // not yet implemented!
      }

      if(bIsEqual) {
	anArrayOfFlag.SetValue(i, j, Standard_True );
	aListOfS2.Append(aS2);
	nbs++;
      }
    }

    if(nbs > 1) {
      BOP_CheckResult aResult;

      aResult.SetShape1(myShape1);
      aResult.SetShape2(myShape2);
      aResult.AddFaultyShape1(aS1);
      TopTools_ListIteratorOfListOfShape anIt(aListOfS2);

      for(; anIt.More(); anIt.Next()) {
	aResult.AddFaultyShape2(anIt.Value());
      }

      aResult.SetCheckStatus(aStatus);
      myResult.Append(aResult);

      if(myStopOnFirst) {
	return;
      }
    }
  }

  for(i = 1; i <= aSeq2.Length(); i++) {
    TopoDS_Shape aS2 = aSeq2.Value(i);
    TopTools_ListOfShape aListOfS1;
    Standard_Integer nbs = 0;

    for(j = 1; j <= aSeq1.Length(); j++) {
      TopoDS_Shape aS1 = aSeq1.Value(j);

      if(anArrayOfFlag.Value(j, i)) {
	aListOfS1.Append(aS1);
	nbs++;
      }
    }

    if(nbs > 1) {
      BOP_CheckResult aResult;

      aResult.SetShape1(myShape1);
      aResult.SetShape2(myShape2);
      TopTools_ListIteratorOfListOfShape anIt(aListOfS1);

      for(; anIt.More(); anIt.Next()) {
	aResult.AddFaultyShape1(anIt.Value());
      }
      aResult.AddFaultyShape2(aS2);

      aResult.SetCheckStatus(aStatus);
      myResult.Append(aResult);

      if(myStopOnFirst) {
	return;
      }
    }
  }
}

// ================================================================================
// function: TestMergeVertex
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::TestMergeVertex() 
{
  TestMergeSubShapes(TopAbs_VERTEX); 
}

// ================================================================================
// function: TestMergeEdge
// purpose:
// ================================================================================
void BOP_ArgumentAnalyzer::TestMergeEdge() 
{
  TestMergeSubShapes(TopAbs_EDGE); 
}

// ================================================================================
// function: TestMergeFace
// purpose:
// ================================================================================
// void BOP_ArgumentAnalyzer::TestMergeFace() 
// {
  // not implemented
// }

// ----------------------------------------------------------------------
// static function: TestShapeType
// purpose:
// ----------------------------------------------------------------------
Standard_Boolean TestShapeType(const TopoDS_Shape & TheShape)
{
  if(TheShape.IsNull())
    return Standard_False;

  TopAbs_ShapeEnum aT = TheShape.ShapeType();

  if(aT == TopAbs_COMPOUND && BOPTools_Tools3D::IsEmptyShape(TheShape))
    return Standard_False;

  TopoDS_Iterator anIt;
  TopoDS_Shape aSTmp, aShape;
  Standard_Integer aNbShapes, TreatRes = 0;

  if(aT==TopAbs_COMPOUND || aT==TopAbs_COMPSOLID) {
    aNbShapes=0;
    anIt.Initialize(TheShape);
    for (; anIt.More(); anIt.Next()) {
      if(!aNbShapes) {
	aSTmp=anIt.Value();
      }
      aNbShapes++;
      if(aNbShapes>1) {
	break;
      }
    }
    if(aT == TopAbs_COMPOUND) {
      if (aNbShapes==1) {
	TreatRes = BOPTools_DSFiller::TreatCompound(TheShape, aSTmp);
	if(TreatRes != 0)
	  return Standard_False;
	aShape=aSTmp;
	aT = aShape.ShapeType();
      }
      else if (aNbShapes>1) {
	TreatRes = BOPTools_DSFiller::TreatCompound(TheShape, aSTmp);
	if(TreatRes != 0)
	  return Standard_False;
	aShape=aSTmp;
	aT=aShape.ShapeType();
      }
    }
  }

  if(aT==TopAbs_COMPOUND || aT==TopAbs_COMPSOLID) {
    return Standard_False;
  }

  return Standard_True;
}

// ----------------------------------------------------------------------
// static function: CheckEdge
// purpose:
// ----------------------------------------------------------------------
Standard_Boolean CheckEdge(const TopoDS_Edge& theEdge) {
  IntTools_Context aContext;
  TopoDS_Vertex aV1, aV2;
  TopExp::Vertices(theEdge, aV1, aV2);

  if(aV1.IsNull() || aV2.IsNull() || BRep_Tool::Degenerated(theEdge))
    return Standard_True;
  Standard_Real aFirst = 0., aLast = 0.;
  BRep_Tool::Range(theEdge, aFirst, aLast);
  IntTools_Range aRange(aFirst, aLast);
  IntTools_ShrunkRange aSR(theEdge, aV1, aV2, aRange, aContext);

  if (!aSR.IsDone() || aSR.ErrorStatus() == 6) {
    return Standard_False;
  }
  return Standard_True;
}

// ----------------------------------------------------------------------
// static function: TestSubShapeType
// purpose:
// ----------------------------------------------------------------------
Standard_Boolean TestSubShapeType(const TopAbs_ShapeEnum theT1,
                                  const TopAbs_ShapeEnum theT2,
                                  const BOP_Operation    theOP)
{
  TopAbs_ShapeEnum aT1 = theT1, aT2 = theT2;

  if(aT1==TopAbs_FACE) {
    if(aT2==TopAbs_SOLID || aT2==TopAbs_SHELL || aT2==TopAbs_FACE ||
       aT2==TopAbs_WIRE || aT2==TopAbs_EDGE) {
      aT1=TopAbs_SHELL;
    }
  }
  if(aT2==TopAbs_FACE) {
    if(aT1==TopAbs_SOLID || aT1==TopAbs_SHELL ||
       aT1==TopAbs_WIRE || aT1==TopAbs_EDGE) {
      aT2=TopAbs_SHELL;
    }
  }
  if(aT1==TopAbs_EDGE) {
    if(aT2==TopAbs_SOLID || aT2==TopAbs_SHELL ||
       aT2==TopAbs_WIRE || aT2==TopAbs_EDGE) {
      aT1=TopAbs_WIRE;
    }
  }
  if(aT2==TopAbs_EDGE) {
    if(aT1==TopAbs_SOLID || aT1==TopAbs_SHELL || aT1==TopAbs_WIRE) {
      aT2=TopAbs_WIRE;
    }
  }

  // test operations
  if(theOP!=BOP_UNKNOWN) {
    Standard_Boolean opOk;
    if(aT1==TopAbs_SHELL && aT2==TopAbs_SHELL)
      opOk = Standard_True;
    else if(aT1==TopAbs_SOLID && aT2==TopAbs_SOLID)
      opOk = Standard_True;
    else if((aT1==TopAbs_SOLID && aT2==TopAbs_SHELL) ||
            (aT2==TopAbs_SOLID && aT1==TopAbs_SHELL))
      opOk = Standard_True;
    else if(aT1==TopAbs_WIRE && aT2==TopAbs_WIRE)
      opOk = Standard_True;
    else if((aT1==TopAbs_WIRE  && aT2==TopAbs_SHELL) ||
            (aT2==TopAbs_WIRE  && aT1==TopAbs_SHELL))
      opOk = Standard_True;
    else if((aT1==TopAbs_WIRE  && aT2==TopAbs_SOLID) ||
            (aT2==TopAbs_WIRE  && aT1==TopAbs_SOLID))
      opOk = Standard_True;
    else 
      opOk = Standard_False;

    if(!opOk) {
      return Standard_False;
    }

    if((aT1==TopAbs_SHELL && aT2==TopAbs_SOLID) ||
       (aT2==TopAbs_SHELL && aT1==TopAbs_SOLID)) {
      if(!BOP_ShellSolid::CheckArgTypes(aT1,aT2,theOP)) {
        return Standard_False;
      }
    }
    if((aT1==TopAbs_WIRE && aT2==TopAbs_SOLID) ||
       (aT2==TopAbs_WIRE && aT1==TopAbs_SOLID)) {
      if(!BOP_WireSolid::CheckArgTypes(aT1,aT2,theOP)) {
        return Standard_False;
      }
    }
    if((aT1==TopAbs_WIRE && aT2==TopAbs_SHELL) ||
       (aT1==TopAbs_WIRE && aT2==TopAbs_SHELL)) {
      if(!BOP_WireShell::CheckArgTypes(aT1,aT2,theOP)) {
        return Standard_False;
      }
    }
  }
  
  return Standard_True;
}