summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepBuild/TopOpeBRepBuild_Tools.cxx
blob: ccda68351d3b53c2efe317fc3ce2896043110531 (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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
// File:	TopOpeBRepBuild_Tools.cxx
// Created:	Tue Nov  2 15:50:35 1999
// Author:	Peter KURNEV
//		<pkv@irinox.nnov.matra-dtv.fr>
#include <TopOpeBRepBuild_Tools.ixx>

#include <gp_Pnt2d.hxx>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <gp_Dir.hxx>
#include <Adaptor3d_CurveOnSurface.hxx>
#include <Adaptor2d_HCurve2d.hxx>

#include <BRepTools.hxx>
#include <BRep_Tool.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRepAdaptor_Curve.hxx>

#include <GeomAdaptor_Surface.hxx>
#include <GeomAdaptor_Curve.hxx>

#include <Geom_Curve.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_Surface.hxx>
#include <Geom2d_Curve.hxx>
#include <GeomProjLib.hxx>

#include <Geom2dAPI_ProjectPointOnCurve.hxx>
#include <GeomAPI_ProjectPointOnCurve.hxx>

#include <TopOpeBRepTool_2d.hxx>

#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_MapIteratorOfMapOfShape.hxx>
#include <TopTools_MapOfShape.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <TopTools_SequenceOfShape.hxx>

#include <TopoDS_Shape.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS.hxx>

#include <TopOpeBRepDS_DataStructure.hxx>
#include <TopOpeBRepDS_IndexedDataMapOfShapeWithState.hxx>
#include <TopOpeBRepDS_ShapeWithState.hxx>
#include <TopOpeBRepDS_DataMapOfShapeState.hxx>
#include <TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeState.hxx>

#include <TopOpeBRepTool_CurveTool.hxx>
#include <TopOpeBRepTool_TOOL.hxx>
#include <TopLoc_Location.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>

#include <TCollection_AsciiString.hxx>
#include <BRep_Tool.hxx>
#include <BRepAdaptor_HSurface.hxx>
#include <GeomAdaptor_HCurve.hxx>
#include <ProjLib_ProjectedCurve.hxx>
#include <GeomAbs_CurveType.hxx>
#include <Geom2d_Line.hxx>
#include <Geom2d_Circle.hxx>
#include <Geom2d_Ellipse.hxx>
#include <Geom2d_Parabola.hxx>
#include <Geom2d_Hyperbola.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <BRepTopAdaptor_FClass2d.hxx>
#include <TopOpeBRepBuild_CorrectFace2d.hxx>
#include <Precision.hxx>
#include <stdio.h>

//define parameter division number as 10*e^(-PI) = 0.43213918
const Standard_Real PAR_T = 0.43213918;

//=======================================================================
//function TopOpeBRepBuild_Tools::DumpMapOfShapeWithState
//purpose  : 
//=======================================================================
  void TopOpeBRepBuild_Tools::DumpMapOfShapeWithState(const Standard_Integer iP,
						      const TopOpeBRepDS_IndexedDataMapOfShapeWithState& 
						      aMapOfShapeWithState)
{
  static Standard_Integer cnt=0;
  TCollection_AsciiString aFName1 ("/DEBUG/TOPOPE/"), postfix;

  Standard_CString ShapeType [9] = {"COMPO", "COMPS", "SOLID", "SHELL", "FACE ", "WIRE ", "EDGE ", "VERTX"};
  Standard_CString ShapeState[4] = {"IN ", "OUT", "ON ", "UNKNOWN"};
  
  printf("\n\n********************************\n");
  printf("*                              *\n");
  Standard_Integer i, n=aMapOfShapeWithState.Extent();
  if (!iP) {
    printf("*  Object comparing with TOOL  *\n");
    postfix=TCollection_AsciiString("Obj");
  }
    
  else {
    printf("*  Tool comparing with Object  *\n");
    postfix=TCollection_AsciiString("Tool");
  }
  
  printf("*                              *\n");
  printf("********************************\n");
  printf("***       aMapOfShapeWithState.Extent()=%d\n", n);
  printf("                 C O N T E N T S\n"); 

  TCollection_AsciiString aFName;
  aFName+=aFName1; 
  aFName+=postfix;

  for (i=1; i<=n; i++) {
    TCollection_AsciiString aI(i), aName;;
    aName+=aFName; aName+=aI;

    const TopoDS_Shape& aShape=aMapOfShapeWithState.FindKey(i);
    const TopOpeBRepDS_ShapeWithState& aShapeWithState=
      aMapOfShapeWithState.FindFromIndex(i);
    
    BRepTools::Write (aShape, aName.ToCString());

    TCollection_AsciiString ann;
    ann+=postfix; ann+=aI;

    printf("Key: %-8s , " , ann.ToCString());
    printf("%s, ", ShapeType[aShape.ShapeType()]);
    if (!iP) 
      printf("State comp.with Tool=%s\n",  ShapeState[aShapeWithState.State()]);

    else 
      printf("State comp.with Obj =%s\n",  ShapeState[aShapeWithState.State()]);
    
    if (aShapeWithState.IsSplitted()) {
      
      const TopTools_ListOfShape& aListOfShape=aShapeWithState.Part(TopAbs_IN);
      TopTools_ListIteratorOfListOfShape anIt(aListOfShape);
      for (;anIt.More(); anIt.Next()) {
	const TopoDS_Shape& aS=anIt.Value();
	
	TCollection_AsciiString cn(cnt), prefix("_S_"), sn;
	sn+=aFName; sn+=prefix; sn+=cn;
	BRepTools::Write (aS, sn.ToCString());
	
	TCollection_AsciiString an;//=postfix+prefix+cn;
	an+=postfix; an+=prefix; an+=cn;
	printf("  -> Splitted Part IN : %s\n",  an.ToCString());
	cnt++;
      }

      const TopTools_ListOfShape& aListOfShapeOut=aShapeWithState.Part(TopAbs_OUT);
      anIt.Initialize (aListOfShapeOut);
      for (;anIt.More(); anIt.Next()) {
	const TopoDS_Shape& aS=anIt.Value();

	TCollection_AsciiString cn(cnt), prefix("_S_"), sn;//=aFName+prefix+cn;
	sn+=aFName; sn+=prefix; sn+=cn;
	BRepTools::Write (aS, sn.ToCString());
	
	TCollection_AsciiString an;//=postfix+prefix+cn;
	an+=postfix; an+=prefix; an+=cn;
	printf("  -> Splitted Part OUT: %-s\n",  an.ToCString());
	cnt++;
      }

      const TopTools_ListOfShape& aListOfShapeOn=aShapeWithState.Part(TopAbs_ON);
      anIt.Initialize (aListOfShapeOn);
      for (;anIt.More(); anIt.Next()) {
	const TopoDS_Shape& aS=anIt.Value();

	TCollection_AsciiString cn(cnt), prefix("_S_"), sn;//=aFName+prefix+cn;
	sn+=aFName; sn+=prefix; sn+=cn;
	BRepTools::Write (aS, sn.ToCString());

	TCollection_AsciiString an;//=postfix+prefix+cn;
	an+=postfix; an+=prefix; an+=cn;
	printf("  -> Splitted Part ON : %s\n",  an.ToCString());
	cnt++;
      } 
    }
  
  }
  cnt=0;
}


//=======================================================================
//function TopOpeBRepBuild_Tools::FindState
//purpose  : 
//=======================================================================
  void TopOpeBRepBuild_Tools::FindState (const TopoDS_Shape& aSubsh, 
					 const TopAbs_State aState,
					 const TopAbs_ShapeEnum aSubshEnum,
					 const TopTools_IndexedDataMapOfShapeListOfShape& aMapSubshAnc,
					 TopTools_MapOfShape& aMapProcessedSubsh,
					 TopOpeBRepDS_DataMapOfShapeState& aMapSS)
{
  Standard_Integer i, nSub;
  const TopTools_ListOfShape& aListOfShapes=aMapSubshAnc.FindFromKey(aSubsh);
  TopTools_ListIteratorOfListOfShape anIt(aListOfShapes);
  for (; anIt.More(); anIt.Next()) {
    const TopoDS_Shape& aS=anIt.Value();
    TopTools_IndexedMapOfShape aSubshMap;
    TopExp::MapShapes (aS, aSubshEnum, aSubshMap);
    nSub=aSubshMap.Extent();
    for (i=1; i<=nSub; i++) {
      const TopoDS_Shape& aSS=aSubshMap(i);
      if (! aMapProcessedSubsh.Contains(aSS)) {
	aMapProcessedSubsh.Add(aSS);
	aMapSS.Bind (aSS, aState);
	FindState (aSS, aState, aSubshEnum, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
      }
    }
  }
}

//=======================================================================
//function TopOpeBRepBuild_Tools::PropagateState
//purpose  : 
//=======================================================================
  void TopOpeBRepBuild_Tools::PropagateState (const TopOpeBRepDS_DataMapOfShapeState& aSplShapesState,
					      const TopTools_IndexedMapOfShape& aShapesToRestMap,
					      const TopAbs_ShapeEnum aSubshEnum,// Vertex 
					      const TopAbs_ShapeEnum aShapeEnum,//Edge
					      TopOpeBRepTool_ShapeClassifier& aShapeClassifier,
					      TopOpeBRepDS_IndexedDataMapOfShapeWithState& aMapOfShapeWithState,
                                              const TopTools_MapOfShape& anAvoidSubshMap)
{
  Standard_Integer j, nSub, nRest;
  TopOpeBRepDS_DataMapOfShapeState aMapSS, aMapSS1;
  
  
  TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeState anItSS (aSplShapesState);
  for (; anItSS.More(); anItSS.Next()) {
    const TopoDS_Shape& aShape= anItSS.Key();
    TopAbs_State aState       = anItSS.Value();
    TopTools_IndexedMapOfShape aSubshapes;
    TopExp::MapShapes (aShape, aSubshEnum, aSubshapes);
    nSub=aSubshapes.Extent();
    for (j=1; j<=nSub; j++) 
      if (!anAvoidSubshMap.Contains(aSubshapes(j))) // MSV: enforce subshapes avoidance
        aMapSS.Bind (aSubshapes(j), aState);
  }

  aMapSS1=aMapSS;

  // 1. Build the Map of ShapesAndAncestors for ShapesToRest
  TopTools_IndexedDataMapOfShapeListOfShape aMapSubshAnc;
  nRest=aShapesToRestMap.Extent();
  for (j=1; j<=nRest; j++) 
    TopExp::MapShapesAndAncestors(aShapesToRestMap(j), aSubshEnum, aShapeEnum, aMapSubshAnc);
  
  // 2. Make Map Of all subshapes  aMapSS
  TopTools_MapOfShape aProcessedSubshapes;
  anItSS.Initialize (aMapSS1);
  for (; anItSS.More(); anItSS.Next()) {
    const TopoDS_Shape& aSubsh = anItSS.Key();
    TopAbs_State aState        = anItSS.Value();
    if (aMapSubshAnc.Contains (aSubsh)) {
      aProcessedSubshapes.Add (aSubsh);
      FindState (aSubsh, aState, aSubshEnum, aMapSubshAnc, aProcessedSubshapes, aMapSS);
    }
  }

  // 3. Propagate the states on ShapesToRestMap
  TopoDS_Shape aNullShape;
  TopTools_MapOfShape aNonPassedShapes;
  nRest=aShapesToRestMap.Extent();
  for (j=1; j<=nRest; j++) {
    const TopoDS_Shape& aS=aShapesToRestMap.FindKey(j);
    TopTools_IndexedMapOfShape aSubshMap;
    TopExp::MapShapes (aS, aSubshEnum, aSubshMap);
    const TopoDS_Shape& aSubsh=aSubshMap(1);
    if (aMapSS.IsBound(aSubsh)) {
      TopAbs_State aState=aMapSS.Find(aSubsh);

      if (aState==TopAbs_ON) {
	aState=aShapeClassifier.StateShapeReference(aS, aNullShape);
      }
      // Add the Rest Shape to aMapOfShapeWithState
      TopOpeBRepDS_ShapeWithState aShapeWithState;
      aShapeWithState.SetState (aState);
      aShapeWithState.SetIsSplitted (Standard_False);
      aMapOfShapeWithState.Add (aS, aShapeWithState);
    }
    
    else {
      aNonPassedShapes.Add(aS);
    }
  }

  // 4. Define the states for aNonPassedShapes 
  //   (for faces themselves and for theirs Wires, Edges):   
  if (aNonPassedShapes.Extent()) {
    // Build the Map of ShapesAndAncestors for aNonPassedShapes
    aMapSubshAnc.Clear();
    TopTools_MapIteratorOfMapOfShape aMapIt;
    aMapIt.Initialize (aNonPassedShapes);
    for (; aMapIt.More(); aMapIt.Next()) 
      TopExp::MapShapesAndAncestors (aMapIt.Key(), aSubshEnum, aShapeEnum, aMapSubshAnc);

    aMapSS.Clear();
    aMapIt.Initialize (aNonPassedShapes);
    for (; aMapIt.More(); aMapIt.Next()) {
      // Face
      const TopoDS_Shape& aNonPassedShape=aMapIt.Key();

      if (!aMapSS.IsBound(aNonPassedShape)) {
	TopAbs_State aState = FindStateThroughVertex (aNonPassedShape, aShapeClassifier,
                                                      aMapOfShapeWithState,anAvoidSubshMap);
	aMapSS.Bind (aNonPassedShape, aState);

	// First Subshape
	TopTools_IndexedMapOfShape aTmpMap;
	TopExp::MapShapes (aNonPassedShape, aSubshEnum, aTmpMap);
	TopoDS_Shape aFirstSubsh;
        for (j=1; j <= aTmpMap.Extent() && aFirstSubsh.IsNull(); j++)
          if (!anAvoidSubshMap.Contains(aTmpMap(j)))
            aFirstSubsh = aTmpMap(j);
        if (aFirstSubsh.IsNull()) continue;
	aMapSS.Bind (aFirstSubsh, aState);

	// Propagation of aState for subshapes
	TopTools_MapOfShape aMapProcessedSubsh;
	if (aSubshEnum==TopAbs_EDGE)
	  FindState1 (aFirstSubsh, aState, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
	else // if (aSubshEnum==TopAbs_VERTEX)
	  FindState2 (aFirstSubsh, aState, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
      }
    }

    // Fill aShapeWithState
    TopOpeBRepDS_ShapeWithState aShapeWithState;
    aShapeWithState.SetIsSplitted (Standard_False);
    TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeState anII(aMapSS);
    for (; anII.More(); anII.Next()) {
      aShapeWithState.SetState (anII.Value());
      if (anII.Key().ShapeType() != TopAbs_VERTEX)
	aMapOfShapeWithState.Add (anII.Key(), aShapeWithState);
    }
  }
}

//=======================================================================
//function :  TopOpeBRepBuild_Tools::FindState2
//purpose  : 
//=======================================================================
  void TopOpeBRepBuild_Tools::FindState2 (const TopoDS_Shape& aSubsh,
					  const TopAbs_State aState,
					  const TopTools_IndexedDataMapOfShapeListOfShape& aMapSubshAnc,
					  TopTools_MapOfShape& aMapProcessedSubsh,
					  TopOpeBRepDS_DataMapOfShapeState& aMapSS)
{
  Standard_Integer i, nSub;
  const TopTools_ListOfShape& aListOfShapes=aMapSubshAnc.FindFromKey(aSubsh);
  TopTools_ListIteratorOfListOfShape anIt(aListOfShapes);
  for (; anIt.More(); anIt.Next()) {
    //Shape
    const TopoDS_Shape& aShape=anIt.Value();
    aMapSS.Bind (aShape, aState);

    //Subshape
    TopTools_IndexedMapOfShape aSubshMap;
    TopExp::MapShapes (aShape, TopAbs_VERTEX, aSubshMap);
    nSub=aSubshMap.Extent();
    for (i=1; i<=nSub; i++) {
      const TopoDS_Shape& aSS=aSubshMap(i);
      if (! aMapProcessedSubsh.Contains(aSS)) {
	aMapProcessedSubsh.Add(aSS);
	aMapSS.Bind (aSS, aState);
	FindState2 (aSS, aState, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
      }
    }
  }
}

//=======================================================================
//function :TopOpeBRepBuild_Tools::FindState1
//purpose  : 
//=======================================================================
  void TopOpeBRepBuild_Tools::FindState1 (const TopoDS_Shape& aSubsh, 
					  const TopAbs_State aState,
					  const TopTools_IndexedDataMapOfShapeListOfShape& aMapSubshAnc,
					  TopTools_MapOfShape& aMapProcessedSubsh,
					  TopOpeBRepDS_DataMapOfShapeState& aMapSS)
{
  Standard_Integer i, nSub, j, nW;
  const TopTools_ListOfShape& aListOfShapes=aMapSubshAnc.FindFromKey(aSubsh);
  TopTools_ListIteratorOfListOfShape anIt(aListOfShapes);
  for (; anIt.More(); anIt.Next()) {
    //Face
    const TopoDS_Shape& aShape=anIt.Value();
    aMapSS.Bind (aShape, aState);
    //Wire
    TopTools_IndexedMapOfShape aWireMap;
    TopExp::MapShapes (aShape, TopAbs_WIRE, aWireMap);
    nW=aWireMap.Extent();
    for (j=1; j<=nW; j++) aMapSS.Bind (aWireMap(j), aState);
    //Edge
    TopTools_IndexedMapOfShape aSubshMap;
    TopExp::MapShapes (aShape, TopAbs_EDGE, aSubshMap);
    nSub=aSubshMap.Extent();
    for (i=1; i<=nSub; i++) {
      const TopoDS_Shape& aSS=aSubshMap(i);
      if (! aMapProcessedSubsh.Contains(aSS)) {
	aMapProcessedSubsh.Add(aSS);
	aMapSS.Bind (aSS, aState);
	FindState1 (aSS, aState, aMapSubshAnc, aMapProcessedSubsh, aMapSS);
      }
    }
  }
}

//=======================================================================
//function :TopOpeBRepBuild_Tools::FindStateThroughVertex
//purpose  :
//=======================================================================
  TopAbs_State TopOpeBRepBuild_Tools::FindStateThroughVertex (const TopoDS_Shape& aShape,
		      TopOpeBRepTool_ShapeClassifier& aShapeClassifier,
		      TopOpeBRepDS_IndexedDataMapOfShapeWithState& aMapOfShapeWithState,
                      const TopTools_MapOfShape& anAvoidSubshMap)
{
  TopTools_IndexedMapOfShape aSubshMap;
  TopExp::MapShapes (aShape, TopAbs_VERTEX, aSubshMap);

  TopoDS_Shape aSubsh;
  Standard_Integer i;
  for (i=1; i <= aSubshMap.Extent() && aSubsh.IsNull(); i++)
    if (!anAvoidSubshMap.Contains (aSubshMap(i)) )
      aSubsh = aSubshMap(i);
  if (aSubsh.IsNull()) {
    // try an edge
    aSubshMap.Clear();
    TopExp::MapShapes (aShape, TopAbs_EDGE, aSubshMap);
    for (i=1; i <= aSubshMap.Extent() && aSubsh.IsNull(); i++)
      if (!anAvoidSubshMap.Contains (aSubshMap(i)) )
        aSubsh = aSubshMap(i);
    if (aSubsh.IsNull()) {
#ifdef DEB
      cout<<"FindStateThroughVertex: warning: all vertices are avoided"<<endl;
#endif
      return TopAbs_UNKNOWN;    // failure
    }
  }

  TopoDS_Shape aNullShape;
  TopAbs_State aState=aShapeClassifier.StateShapeReference(aSubsh, aNullShape);
  TopOpeBRepDS_ShapeWithState aShapeWithState;
  aShapeWithState.SetState (aState);
  aShapeWithState.SetIsSplitted (Standard_False);
  aMapOfShapeWithState.Add(aShape, aShapeWithState);
  SpreadStateToChild (aShape, aState, aMapOfShapeWithState);
  return aState;
 
}


//=======================================================================
//function :TopOpeBRepBuild_Tools::SpreadStateToChild
//purpose  :
//=======================================================================
  void  TopOpeBRepBuild_Tools::SpreadStateToChild (const TopoDS_Shape& aShape,
						   const TopAbs_State aState,
						   TopOpeBRepDS_IndexedDataMapOfShapeWithState& aMapOfShapeWithState)
{
  TopTools_IndexedMapOfShape aChildMap;
  TopExp::MapShapes (aShape, TopAbs_FACE, aChildMap);
  TopExp::MapShapes (aShape, TopAbs_WIRE, aChildMap);
  TopExp::MapShapes (aShape, TopAbs_EDGE, aChildMap);

  TopOpeBRepDS_ShapeWithState aShapeWithState;
  aShapeWithState.SetState (aState);
  aShapeWithState.SetIsSplitted (Standard_False);
 
  Standard_Integer i, n=aChildMap.Extent();
  for (i=1; i<=n; i++) {
    aMapOfShapeWithState.Add(aChildMap(i), aShapeWithState);
  }
}

//=======================================================================
//function :TopOpeBRepBuild_Tools::PropagateStateForWires
//purpose  :
//=======================================================================
  void TopOpeBRepBuild_Tools::PropagateStateForWires(const TopTools_IndexedMapOfShape& aFacesToRestMap,
						     TopOpeBRepDS_IndexedDataMapOfShapeWithState& 
						     aMapOfShapeWithState)
{
  Standard_Integer i, j, nF, nW, k, nE;

  nF=aFacesToRestMap.Extent();
  for (i=1; i<=nF; i++) {
    const TopoDS_Shape& aF=aFacesToRestMap(i);
    if (aMapOfShapeWithState.Contains (aF)) {
      const TopOpeBRepDS_ShapeWithState& aSWS=aMapOfShapeWithState.FindFromKey(aF);
      TopAbs_State aSt=aSWS.State();
      
      TopTools_IndexedMapOfShape aWireMap;
      TopExp::MapShapes (aF, TopAbs_WIRE, aWireMap);
      nW=aWireMap.Extent();
      for (j=1; j<=nW; j++) {
	const TopoDS_Shape& aW=aWireMap(j);
	TopOpeBRepDS_ShapeWithState aWireSWS;
	aWireSWS.SetState(aSt);
	aWireSWS.SetIsSplitted (Standard_False);
	aMapOfShapeWithState.Add(aW, aWireSWS);

	TopTools_IndexedMapOfShape aEdgeMap;
	TopExp::MapShapes (aW, TopAbs_EDGE, aEdgeMap);
	nE=aEdgeMap.Extent();
	for (k=1; k<=nE; k++) {
	  const TopoDS_Shape& aE=aEdgeMap(k);
	  if (!aMapOfShapeWithState.Contains (aE)) {
	    TopOpeBRepDS_ShapeWithState anEdgeSWS;
	    anEdgeSWS.SetState(aSt);
	    anEdgeSWS.SetIsSplitted (Standard_False);
	    aMapOfShapeWithState.Add(aE, anEdgeSWS);
	  }
	}
      }
    }
  }
}

//=======================================================================
//function :TopOpeBRepBuild_Tools:: GetNormalToFaceOnEdge
//purpose  : 
//=======================================================================
  void TopOpeBRepBuild_Tools::GetNormalToFaceOnEdge (const TopoDS_Face& aFObj,
						     const TopoDS_Edge& anEdgeObj,
						     gp_Vec& aNormal)
{
  TopoDS_Edge aEd=anEdgeObj;
  TopoDS_Face aFS=aFObj;
  Standard_Real f2 = 0., l2 = 0., tolpc = 0., f = 0., l = 0., par = 0.;
  Handle(Geom2d_Curve) C2D=FC2D_CurveOnSurface(aEd,aFS,f2,l2,tolpc, Standard_True);

  BRepAdaptor_Curve  aCA(aEd);
  f=aCA.FirstParameter();
  l=aCA.LastParameter();
  par= f*PAR_T + (1 - PAR_T)*l;

  gp_Pnt2d aUV1 ; 
  C2D -> D0(par, aUV1);

  gp_Pnt aP;
  gp_Vec  aTg1, aTg2;
  BRepAdaptor_Surface aSA1(aFS);
  aSA1.D1(aUV1.X(), aUV1.Y(), aP, aTg1, aTg2);
  aNormal = aTg1^aTg2;
}

//=======================================================================
//function : TopOpeBRepBuild_Tools::GetNormalInNearestPoint
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_Tools::GetNormalInNearestPoint(const TopoDS_Face& F, 
					       const TopoDS_Edge& E,
					       gp_Vec& aNormal) 
{
  Standard_Real f2 = 0., l2 = 0., tolpc = 0., par = 0.;
  
  gp_Vec2d aTangent;

  Handle(Geom2d_Curve) C2D = FC2D_CurveOnSurface(E, F, f2, l2, tolpc, Standard_True);
  

  par = f2*PAR_T + (1 - PAR_T)*l2;

  gp_Pnt2d aP;
  C2D -> D1(par, aP, aTangent);

  Standard_Real Xnorm = -aTangent.Y();
  Standard_Real Ynorm = aTangent.X();
  
  Standard_Real step = TopOpeBRepTool_TOOL::minDUV(F); step *= 1e-2;

  gp_Vec2d aPV(aP.X(), aP.Y());
  gp_Dir2d aStepV(Xnorm, Ynorm);
  gp_Vec2d aNorm2d = aPV + gp_Vec2d(step*aStepV);

  Standard_Real newU = aNorm2d.X();
  Standard_Real newV = aNorm2d.Y();
  gp_Vec  aTg1, aTg2;
  gp_Pnt aP1;

  BRepAdaptor_Surface BS(F);
  BS.D1(newU, newV, aP1, aTg1, aTg2);

  
  gp_Pnt2d aP2d(newU, newV);
  BRepTopAdaptor_FClass2d FC(F, Precision::PConfusion());
  TopAbs_State aState = FC.Perform(aP2d);

  //point out of face: try to go at another direction
  if(aState == TopAbs_OUT) {
    aStepV.Reverse();
    aNorm2d = aPV + gp_Vec2d(step*aStepV);

    newU = aNorm2d.X();
    newV = aNorm2d.Y();

    BS.D1(newU, newV, aP1, aTg1, aTg2);

//in principle, we must check again
//    aP2d.SetX(newU); aP2d.SetY(newV);
//    BRepClass_FaceClassifier FC(Fex, aP2d, 1e-7);
//    TopAbs_State aState = FC.State();  
  }

  aNormal = aTg1^aTg2;
}


//=======================================================================
//function : TopOpeBRepBuild_Tools::GetTangentToEdgeEdge
//purpose  : 
//=======================================================================
Standard_Boolean TopOpeBRepBuild_Tools::GetTangentToEdgeEdge (const TopoDS_Face& ,//aFObj,
							      const TopoDS_Edge& anEdgeObj,
							      const TopoDS_Edge& aOriEObj,
							      gp_Vec& aTangent)
{

  if (BRep_Tool::Degenerated(aOriEObj) ||
      BRep_Tool::Degenerated(anEdgeObj)) {
    return TopOpeBRepBuild_Tools::GetTangentToEdge (anEdgeObj, aTangent) ;
  }

  TopoDS_Edge aEd=anEdgeObj, aEOri = aOriEObj;

  Standard_Real f = 0., l = 0., par = 0., parOri = 0.;

  BRepAdaptor_Curve  aCA(aEd);
  BRepAdaptor_Curve  aCAOri(aEOri);
  
  f=aCA.FirstParameter();
  l=aCA.LastParameter();

  par= f*PAR_T + (1 - PAR_T)*l;

  gp_Pnt aP;
  gp_Vec aTgPiece;
  aCA.D1(par, aP, aTgPiece);
  aTangent = aTgPiece;

  gp_Pnt aPOri;
  gp_Vec aTgOri;
  /////
  Handle (Geom_Curve) GCOri=aCAOri.Curve().Curve();
  Handle (Geom_Curve) aCopyCurve = Handle(Geom_Curve)::DownCast(GCOri -> Copy());

  const TopLoc_Location& aLoc = aEOri.Location();
  gp_Trsf aTrsf = aLoc.Transformation();
  aCopyCurve -> Transform(aTrsf);

  GeomAPI_ProjectPointOnCurve aPP(aP, aCopyCurve, aCopyCurve->FirstParameter(), aCopyCurve->LastParameter());
#ifdef DEB
//  gp_Pnt aNP = aPP.NearestPoint();
#endif
  parOri = aPP.LowerDistanceParameter();
 
  aCopyCurve -> D1(parOri, aPOri, aTgOri);// aPOri must be equal aNP !
  //printf(" aNP  ={%lf, %lf, %lf}\n", aNP.X(), aNP.Y(), aNP.Z());
  //printf(" aPOri={%lf, %lf, %lf}\n", aPOri.X(), aPOri.Y(), aPOri.Z());
  if (aEd.Orientation() == TopAbs_REVERSED) 
    aTangent.Reverse();

  if(aTgOri*aTgPiece < 0.) {
    aTangent.Reverse();
    return Standard_True;
  }
  return Standard_False;
}


//=======================================================================
//function : TopOpeBRepBuild_Tools::GetTangentToEdge
//purpose  : 
//=======================================================================
Standard_Boolean TopOpeBRepBuild_Tools::GetTangentToEdge (const TopoDS_Edge& anEdgeObj,
							  gp_Vec& aTangent)
{
  TopoDS_Edge aEd=anEdgeObj;

  Standard_Real f = 0., l = 0., par = 0.;

  BRepAdaptor_Curve  aCA(aEd);
  
  f=aCA.FirstParameter();
  l=aCA.LastParameter();

  par= f*PAR_T + (1 - PAR_T)*l;
  gp_Pnt aP;
  aCA.D1(par, aP, aTangent);

  return Standard_True;

}

//=======================================================================
//function : TopOpeBRepBuild_Tools::GetAdjacentFace
//purpose  : 
//=======================================================================
Standard_Boolean TopOpeBRepBuild_Tools::GetAdjacentFace (const TopoDS_Shape& aFaceObj,
							 const TopoDS_Shape& anEObj,
							 const TopTools_IndexedDataMapOfShapeListOfShape& anEdgeFaceMap,
							 TopoDS_Shape& anAdjFaceObj)
{
  const TopTools_ListOfShape& aListOfAdjFaces=anEdgeFaceMap.FindFromKey(anEObj);
  TopTools_ListIteratorOfListOfShape anIt(aListOfAdjFaces);
  TopoDS_Shape anAdjShape;
  for (; anIt.More(); anIt.Next()) {
    if (anIt.Value()!=aFaceObj) {
      anAdjShape=anIt.Value();
      break;
    }
  }

  if (!anAdjShape.IsNull()) {
    anAdjFaceObj=TopoDS::Face(anAdjShape);
    return Standard_True;
  }
  else {
    return Standard_False;
  }
}

//=======================================================================
//function : UpdatePCurves
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_Tools::UpdatePCurves(const TopoDS_Wire& aWire,
					  const TopoDS_Face& fromFace,
					  const TopoDS_Face& toFace)
{
  TopExp_Explorer aExp(aWire, TopAbs_EDGE);

  for(; aExp.More(); aExp.Next()) {
    TopoDS_Shape aEdge = aExp.Current();
    UpdateEdgeOnFace(TopoDS::Edge(aEdge), fromFace, toFace);
  }
}

//=======================================================================
//function : UpdateEdgeOnFace
//purpose  : 
//======================================================================= 
void TopOpeBRepBuild_Tools::UpdateEdgeOnFace(const TopoDS_Edge& aEdgeToUpdate,
					     const TopoDS_Face& fromFace,
					     const TopoDS_Face& toFace)
{
  BRep_Builder BB;

  Standard_Real tolE = BRep_Tool::Tolerance(TopoDS::Edge(aEdgeToUpdate));
  Standard_Real f2 = 0.,l2 = 0.,tolpc = 0.; 
  Handle(Geom2d_Curve) C2D; 
    
  if(BRep_Tool::Degenerated(aEdgeToUpdate)) {
    //we can not compute PCurve for Degenerated Edge
    //so we take as it was on old face and after (in CorrectFace2D) 
    // we will adjust this PCurve
    C2D = FC2D_CurveOnSurface(aEdgeToUpdate, fromFace,
			      f2, l2, tolpc, Standard_True);
    Standard_Real tol = Max(tolE, tolpc);
    Handle(Geom2d_Curve) C2Dn = Handle(Geom2d_Curve)::DownCast(C2D -> Copy());
    Handle(Geom2d_TrimmedCurve) newC2D = new Geom2d_TrimmedCurve(C2Dn, f2, l2);
    BB.UpdateEdge(aEdgeToUpdate ,newC2D, toFace , tol);
    
  }
  else { //not degenerated edge

    if(BRep_Tool::IsClosed(aEdgeToUpdate, fromFace)) {
      UpdateEdgeOnPeriodicalFace(aEdgeToUpdate, fromFace, toFace);
    }
    else {
      C2D = FC2D_CurveOnSurface(aEdgeToUpdate, toFace , f2, l2, tolpc, Standard_True);
      Standard_Real tol = Max(tolE,tolpc);
      BB.UpdateEdge(aEdgeToUpdate ,C2D, toFace , tol);
    }
  }
}

//=======================================================================
//function : UpdateEdgeOnPeriodicalFace
//purpose  : 
//======================================================================= 
void TopOpeBRepBuild_Tools::UpdateEdgeOnPeriodicalFace(const TopoDS_Edge& aEdgeToUpdate,
						       const TopoDS_Face& fromFace,
						       const TopoDS_Face& toFace)
{
  Standard_Boolean DiffOriented = Standard_False;
  BRep_Builder BB; 
  TopoDS_Edge newE = aEdgeToUpdate; //newE.Orientation(TopAbs_FORWARD);
  TopoDS_Face fFace = fromFace;   //fFace.Orientation(TopAbs_FORWARD);
  TopoDS_Face tFace = toFace; //tFace.Orientation(TopAbs_FORWARD);
  Standard_Real fc = 0., lc = 0.;

  Handle(Geom2d_Curve) cc = BRep_Tool::CurveOnSurface(newE, tFace, fc, lc);

  if(!cc.IsNull()) {
    //cout << "Pcurves exist" << endl;
    return;
  }

  gp_Vec aN1, aN2;
  TopOpeBRepBuild_Tools::GetNormalToFaceOnEdge(TopoDS::Face(fromFace), 
					       TopoDS::Edge(aEdgeToUpdate), aN1);
  

  TopOpeBRepBuild_Tools::GetNormalToFaceOnEdge(TopoDS::Face(toFace), 
					       TopoDS::Edge(aEdgeToUpdate), aN2);
  
  if(aN1*aN2 < 0)
    DiffOriented = Standard_True;
  
  Standard_Real tolE = BRep_Tool::Tolerance(newE);
  Standard_Real f2 = 0., l2 = 0., tolpc = 0., tol = 0.;

  //first  PCurve
  Handle(Geom2d_Curve) C2D = FC2D_CurveOnSurface(newE,
						 tFace, f2, 
						 l2, tolpc, Standard_True);

  tol = Max(tolpc, tolE);

  BRepAdaptor_Surface aBAS(fFace);
  gp_Vec2d aTrV;

  Standard_Real ff = 0., lf = 0., fr = 0., lr = 0.;
  gp_Pnt2d aUVf, aUVr;
  
  Handle(Geom2d_Curve) oldC2DFor = BRep_Tool::CurveOnSurface(newE, //FC2D_CurveOnSurface(newE,
						       fFace, ff, 
						       lf);//, tolpc, Standard_True);
  newE.Reverse();
  Handle(Geom2d_Curve) oldC2DRev = BRep_Tool::CurveOnSurface(newE, //FC2D_CurveOnSurface(newE,
						       fFace, fr, 
						       lr);//, tolpc, Standard_True);

  oldC2DFor -> D0(ff, aUVf);
  oldC2DRev -> D0(fr, aUVr);

  if(!DiffOriented)
    aTrV = gp_Vec2d(aUVf, aUVr);
  else
    aTrV = gp_Vec2d(aUVr, aUVf);

  gp_Vec2d aux(gp_Pnt2d(0., 0.), gp_Pnt2d(1., 1.));
  Standard_Real scalar = aux*aTrV;
  Standard_Boolean dir = (scalar >= 0.) ? Standard_True : Standard_False;

  //compute right order of pcurves
  gp_Vec2d aYVec(gp_Pnt2d(0., 0.), gp_Pnt2d(0., 1.));
  gp_Pnt2d aUVfv, aUVlv;
  C2D -> D0(f2, aUVfv);
  C2D -> D0(l2, aUVlv);
  gp_Vec2d C2DVec(aUVfv, aUVlv);
  
  Standard_Boolean firstOrder = Standard_True;
  scalar = aYVec*C2DVec;
  if(fabs(scalar) <= 1e-10) {// compute along X axe
    gp_Vec2d aXVec(gp_Pnt2d(0., 0.), gp_Pnt2d(1., 0.));
    scalar = aXVec*C2DVec; 
    firstOrder = (scalar >= 0.) ? Standard_True : Standard_False;
  }
  else 
    firstOrder = (scalar > 0.) ? Standard_False : Standard_True;

  Handle(Geom2d_Curve) aTrC = Handle(Geom2d_Curve)::DownCast(C2D->Copy());
  aTrC -> Translate(aTrV);

  if(dir) {
    if(firstOrder) 
      BB.UpdateEdge(aEdgeToUpdate, C2D, aTrC, toFace, tol);
    else
      BB.UpdateEdge(aEdgeToUpdate, aTrC, C2D, toFace, tol);
  }
  else {
    if(!firstOrder) 
      BB.UpdateEdge(aEdgeToUpdate, C2D, aTrC, toFace, tol);
    else
      BB.UpdateEdge(aEdgeToUpdate, aTrC, C2D, toFace, tol);
  }
}

//=======================================================================
//function : TopOpeBRepBuild_Tools::IsDegEdgesTheSame
//purpose  : 
//=======================================================================
Standard_Boolean TopOpeBRepBuild_Tools::IsDegEdgesTheSame (const TopoDS_Shape& anE1,
							   const TopoDS_Shape& anE2)
{
  TopTools_IndexedMapOfShape aVMap1, aVMap2;
  TopExp::MapShapes(anE1, TopAbs_VERTEX, aVMap1);
  TopExp::MapShapes(anE2, TopAbs_VERTEX, aVMap2);
  
  if (!aVMap1.Extent() || !aVMap2.Extent())
    return Standard_False;

  if (aVMap1(1).IsSame(aVMap2(1)))
    return Standard_True;
  else 
    return Standard_False;
}

//=======================================================================
//function : NormalizeFace
//purpose  : remove all INTERNAL and EXTERNAL edges from the face
//=======================================================================
void TopOpeBRepBuild_Tools::NormalizeFace(const TopoDS_Shape& oldFace,
					  TopoDS_Shape& corrFace)
{
  Standard_Real tolF1;
  
  TopLoc_Location Loc;
  TopoDS_Face aF1 = TopoDS::Face(oldFace), 
              aNewFace;

  aF1.Orientation(TopAbs_FORWARD);

  Handle(Geom_Surface) Surf = BRep_Tool::Surface(aF1, Loc);
  tolF1 = BRep_Tool::Tolerance(aF1);
  BRep_Builder BB;
  BB.MakeFace(aNewFace, Surf, Loc, tolF1);
  
  TopExp_Explorer aFExp(aF1, TopAbs_WIRE);
  for (; aFExp.More(); aFExp.Next()) {
    Standard_Integer NbGoodEdges = 0;
    TopoDS_Shape aWire=aFExp.Current();
    aWire.Orientation(TopAbs_FORWARD);
    TopoDS_Wire aNewWire;

    BB.MakeWire(aNewWire);

    TopExp_Explorer aWExp(aWire, TopAbs_EDGE);
    for (; aWExp.More(); aWExp.Next()) {
      TopoDS_Shape anEdge=aWExp.Current();

      if (anEdge.Orientation()==TopAbs_EXTERNAL || 
	  anEdge.Orientation()==TopAbs_INTERNAL) 
	continue;

      BB.Add (aNewWire, TopoDS::Edge(anEdge));
      NbGoodEdges++;
    }
    //keep wire  orientation
    aNewWire.Orientation(aFExp.Current().Orientation());//aWire.Orientation()); 

    if(NbGoodEdges) //we add new wire only if it contains at least one edge
      BB.Add (aNewFace, aNewWire); 
  }
  //keep face  orientation
  aNewFace.Orientation(oldFace.Orientation());

  corrFace = aNewFace;
}


//=======================================================================
//function : CorrectFace2d
//purpose  : adjust PCurves of periodical face in 2d
//=======================================================================
void TopOpeBRepBuild_Tools::CorrectFace2d (const TopoDS_Shape& oldFace,
					   TopoDS_Shape& corrFace,
					   const TopTools_IndexedMapOfOrientedShape& aSourceShapes,
					   TopTools_IndexedDataMapOfShapeShape& aMapOfCorrect2dEdges)
{
  TopOpeBRepBuild_CorrectFace2d aCorrectFace2d(TopoDS::Face(oldFace),  
					       aSourceShapes, 
					       aMapOfCorrect2dEdges);


  aCorrectFace2d.Perform();
  corrFace=oldFace;
}