summaryrefslogtreecommitdiff
path: root/src/BRepOffsetAPI/BRepOffsetAPI_DraftAngle.cxx
blob: 5f63240ba936d7feb91629edb137adc5ad60f88a (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
// File:	BRepOffsetAPI_DraftAngle.cxx
// Created:	Wed Feb 22 11:32:25 1995
// Author:	Jacques GOUSSARD
//		<jag@topsn2>


#include <BRepOffsetAPI_DraftAngle.ixx>

#include <Draft_Modification.hxx>
#include <TopoDS.hxx>
#include <Geom_Surface.hxx>
#include <TopLoc_Location.hxx>

#include <TopTools_ListIteratorOfListOfShape.hxx>

#include <BRepLib.hxx>
#include <BRepFill_DataMapOfShapeSequenceOfReal.hxx>
#include <TopTools_DataMapOfShapeSequenceOfShape.hxx>
#include <TopTools_SequenceOfShape.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopExp_Explorer.hxx>
#include <BRepTools.hxx>
#include <TColgp_SequenceOfPnt.hxx>
#include <TColStd_SequenceOfReal.hxx>
#include <TopOpeBRep_EdgesIntersector.hxx>
#include <TopOpeBRep_Point2d.hxx>
#include <Precision.hxx>
#include <BRepFill_DataMapIteratorOfDataMapOfShapeSequenceOfReal.hxx>
#include <BRepOffsetAPI_SequenceOfSequenceOfReal.hxx>
#include <BRepOffsetAPI_SequenceOfSequenceOfShape.hxx>
#include <BRepTools_Substitution.hxx>
#include <BRep_Builder.hxx>
#include <TopExp.hxx>
#include <BRep_Tool.hxx>
#include <BRepAdaptor_Curve.hxx>
#include <BRepLib_MakeVertex.hxx>
#include <BRep_TEdge.hxx>
#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
#include <BRep_GCurve.hxx>
#include <TopoDS_Wire.hxx>
#include <BRepAdaptor_Curve2d.hxx>

//=======================================================================
//function : BRepOffsetAPI_DraftAngle
//purpose  : 
//=======================================================================

BRepOffsetAPI_DraftAngle::BRepOffsetAPI_DraftAngle () {}


//=======================================================================
//function : BRepOffsetAPI_DraftAngle
//purpose  : 
//=======================================================================

BRepOffsetAPI_DraftAngle::BRepOffsetAPI_DraftAngle (const TopoDS_Shape& S)
{
  myInitialShape = S;
  myModification = new Draft_Modification(S);
}


//=======================================================================
//function : Clear
//purpose  : 
//=======================================================================

void BRepOffsetAPI_DraftAngle::Clear ()
{
  if (!myModification.IsNull()) {
    (*((Handle(Draft_Modification)*)&myModification))->Clear();
  }
}


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

void BRepOffsetAPI_DraftAngle::Init (const TopoDS_Shape& S)
{
  myInitialShape = S;
  NotDone();
  if (!myModification.IsNull()) {
    (*((Handle(Draft_Modification)*)&myModification))->Init(S);   
  }
  else {
    myModification = new Draft_Modification(S);
  }
}


//=======================================================================
//function : Add
//purpose  : 
//=======================================================================

void BRepOffsetAPI_DraftAngle::Add(const TopoDS_Face& F,
			     const gp_Dir& D,
			     const Standard_Real Angle,
			     const gp_Pln& Plane,
                             const Standard_Boolean Flag)
{
// POP-DPF : protection
  if ( Abs(Angle) <= 1.e-04 ) 
    return;
  Standard_NullObject_Raise_if(myInitialShape.IsNull(),"");
  (*((Handle(Draft_Modification)*)&myModification))->Add(F,D,Angle,Plane, Flag);
}


//=======================================================================
//function : AddDone
//purpose  : 
//=======================================================================

Standard_Boolean BRepOffsetAPI_DraftAngle::AddDone () const
{
  Standard_NullObject_Raise_if(myInitialShape.IsNull(),"");
  return (*((Handle(Draft_Modification)*)&myModification))
    ->ProblematicShape().IsNull();
}


//=======================================================================
//function : Remove
//purpose  : 
//=======================================================================

void BRepOffsetAPI_DraftAngle::Remove(const TopoDS_Face& F)
{
  Standard_NullObject_Raise_if(myInitialShape.IsNull(),"");
  (*((Handle(Draft_Modification)*)&myModification))->Remove(F);
}


//=======================================================================
//function : ProblematicShape
//purpose  : 
//=======================================================================

const TopoDS_Shape& BRepOffsetAPI_DraftAngle::ProblematicShape () const
{
  Standard_NullObject_Raise_if(myInitialShape.IsNull(),"");
  return (*((Handle(Draft_Modification)*)&myModification))->ProblematicShape();
}


//=======================================================================
//function : ErrorStatus
//purpose  : 
//=======================================================================

Draft_ErrorStatus BRepOffsetAPI_DraftAngle::Status () const
{
  Standard_NullObject_Raise_if(myInitialShape.IsNull(),"");
  return (*((Handle(Draft_Modification)*)&myModification))->Error();
}


//=======================================================================
//function : ConnectedFaces
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& BRepOffsetAPI_DraftAngle::ConnectedFaces
   (const TopoDS_Face& F) const
{
  Standard_NullObject_Raise_if(myInitialShape.IsNull(),"");
  return (*((Handle(Draft_Modification)*)&myModification))->ConnectedFaces(F);
}


//=======================================================================
//function : ModifiedFaces
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& BRepOffsetAPI_DraftAngle::ModifiedFaces() const
{
  Standard_NullObject_Raise_if(myInitialShape.IsNull(),"");
  return (*((Handle(Draft_Modification)*)&myModification))->ModifiedFaces();
}

//=======================================================================
//function : ModifiedFaces
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& BRepOffsetAPI_DraftAngle::Generated(const TopoDS_Shape& S) 
{
  myGenerated.Clear();
  Standard_NullObject_Raise_if(myInitialShape.IsNull(),"");
  Handle(Draft_Modification) DMod = (*((Handle(Draft_Modification)*)&myModification));

  if (S.ShapeType() == TopAbs_FACE) {
    Handle(Geom_Surface) Surf;
    TopLoc_Location      L;
    Standard_Real        Tol;
    Standard_Boolean     RW,RF;
    if (DMod->NewSurface(TopoDS::Face(S), Surf, L, Tol, RW, RF)) {
      myGenerated.Append(ModifiedShape (S));
 
    }
  }
  return myGenerated;
}

//=======================================================================
//function : ModifiedFaces
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& BRepOffsetAPI_DraftAngle::Modified(const TopoDS_Shape& S) 
{
  myGenerated.Clear();
  Standard_NullObject_Raise_if(myInitialShape.IsNull(),"");
  Handle(Draft_Modification) DMod = (*((Handle(Draft_Modification)*)&myModification));

  if (S.ShapeType() == TopAbs_FACE) {
    Handle(Geom_Surface) Surf;
    TopLoc_Location      L;
    Standard_Real        Tol;
    Standard_Boolean     RW,RF;
    
    if (!DMod->NewSurface(TopoDS::Face(S), Surf, L, Tol, RW, RF)) {
      // Ce n est pas une generation => peut etre une  modif
      myGenerated.Append(ModifiedShape (S));
      if (myGenerated.Extent() == 1 && myGenerated.First().IsSame(S)) {
	myGenerated.Clear();
      }
    }
  }
  return myGenerated;
}


//=======================================================================
//function : Build
//purpose  : 
//=======================================================================

void BRepOffsetAPI_DraftAngle::Build()
{
  (*((Handle(Draft_Modification)*)&myModification))->Perform();
  if (!(*((Handle(Draft_Modification)*)&myModification))->IsDone()) {
    NotDone();
  }
  else {
    DoModif(myInitialShape);
    //BRepLib::SameParameter( myShape, 1.0e-7, Standard_True ); //patch
    CorrectWires();
    BRepLib::SameParameter( myShape, 1.0e-7, Standard_True ); //patch
  }
}

//=======================================================================
//function : CorrectWires
//purpose  : 
//=======================================================================

void BRepOffsetAPI_DraftAngle::CorrectWires()
{
  Standard_Real TolInter = 1.e-7;
  Standard_Integer i, j, k;

  TopTools_SequenceOfShape Eseq;
  TopTools_SequenceOfShape Wseq;
  TopTools_SequenceOfShape Fseq;
  TopoDS_Shape CurEdge, CurWire, CurFace;
  TopoDS_Iterator wit, eit;

  TopExp_Explorer fexp( myShape, TopAbs_FACE );
  for (; fexp.More(); fexp.Next())
    {
      CurFace = fexp.Current();
      wit.Initialize( CurFace );
      for (; wit.More(); wit.Next())
	{
	  CurWire = wit.Value();
	  TopTools_MapOfShape emap;
	  eit.Initialize( CurWire );
	  for (; eit.More(); eit.Next())
	    emap.Add( eit.Value() );
	  TopTools_MapIteratorOfMapOfShape mapit( emap );
	  for (; mapit.More(); mapit.Next())
	    {
	      CurEdge = mapit.Key();
	      if (BRepTools::IsReallyClosed( TopoDS::Edge(CurEdge), TopoDS::Face(CurFace) ))
		{
		  Eseq.Append( CurEdge );
		  Wseq.Append( CurWire );
		  Fseq.Append( CurFace );
		}
	    }
	}
    }

  BRepFill_DataMapOfShapeSequenceOfReal Emap;

  TopTools_SequenceOfShape NonSeam;
  TopTools_SequenceOfShape NonSeamWires;
  BRepOffsetAPI_SequenceOfSequenceOfReal ParsNonSeam;
  BRepOffsetAPI_SequenceOfSequenceOfShape Seam;
  BRepOffsetAPI_SequenceOfSequenceOfReal ParsSeam;

  TopTools_DataMapOfShapeShape WFmap;
  TopTools_DataMapOfShapeListOfShape WWmap;
  for (i = 1; i <= Eseq.Length(); i++)
    {
      CurEdge = Eseq(i);
      CurWire = Wseq(i);
      CurFace = Fseq(i);
      wit.Initialize( CurFace );
      for (; wit.More(); wit.Next())
	{
	  TopoDS_Shape aWire = wit.Value();
	  if (! aWire.IsSame( CurWire ))
	    {
	      TColgp_SequenceOfPnt pts;
	      TopTools_SequenceOfShape edges;
	      TColStd_SequenceOfReal pars;
	      Standard_Boolean Wadd = Standard_False;
	      eit.Initialize( aWire );
	      for (; eit.More(); eit.Next())
		{
		  TopoDS_Shape anEdge = eit.Value();
		  TopOpeBRep_EdgesIntersector EInter;
		  EInter.SetFaces( CurFace, CurFace );
		  EInter.ForceTolerances( TolInter, TolInter );
		  EInter.Perform( CurEdge, anEdge );
		  if (EInter.IsEmpty())
		    {
		      EInter.Perform( CurEdge.Reversed(), anEdge );
		      if (EInter.IsEmpty())
			continue;
		    }
		  Wadd = Standard_True;
		  if (! WFmap.IsBound( aWire ))
		    WFmap.Bind( aWire, CurFace );
		  Standard_Integer ind = 0;
		  for (j = 1; j <= NonSeam.Length(); j++)
		    if (anEdge.IsSame( NonSeam(j) ))
		      {
			ind = j;
			break;
		      }
		  if (ind == 0)
		    {
		      NonSeam.Append( anEdge );
		      NonSeamWires.Append( aWire );
		      ind = NonSeam.Length();
		      TColStd_SequenceOfReal emptyseq1, emptyseq2;
		      TopTools_SequenceOfShape emptyedgeseq;
		      ParsNonSeam.Append( emptyseq1 );
		      Seam.Append( emptyedgeseq );
		      ParsSeam.Append( emptyseq2 );
		    }
		  if (! Emap.IsBound( CurEdge ))
		    {
		      TColStd_SequenceOfReal emptyseq;
		      Emap.Bind( CurEdge, emptyseq );
		    }
		  EInter.InitPoint();
		  for (; EInter.MorePoint(); EInter.NextPoint())
		    {
		      const TopOpeBRep_Point2d& bp = EInter.Point();
		      if (bp.IsVertex(2))
			{
			  gp_Pnt Pnt = bp.Value();
			  Standard_Integer ied = 0;
			  for (j = 1; j <= pts.Length(); j++)
			    if (Pnt.IsEqual( pts(j), Precision::Confusion() ))
			      {
				ied = j;
				break;
			      }
			  if (ied == 0)
			    {
			      pts.Append( Pnt );
			      edges.Append( anEdge );
			      pars.Append( bp.Parameter(2) );
			      Emap(CurEdge).Append( bp.Parameter(1) );
			      ParsNonSeam(ind).Append( bp.Parameter(2) );
			      Seam(ind).Append( CurEdge );
			      ParsSeam(ind).Append( bp.Parameter(1) );
			    }
			  /*
			  else
			    {
			      Standard_Real ParOnSeam = bp.Parameter(1);
			      Standard_Real Par1 = pars(ied);
			      Standard_Real Par2 = bp.Parameter(2);
			      BRepAdaptor_Curve2d SeamCurve( CurEdge, CurFace );
			      BRepAdaptor_Curve2d Curve1( edges(ied), CurFace );
			      BRepAdaptor_Curve2d Curve2( anEdge. CurFace );
			      gp_Pnt2d P2d;
			      gp_Vec2d SeamDer, Der1, Der2;
			      //SeamCurve->D1( ParOnSeam, P2d, SeamDer );
			      //Curve1->D1( Par1, P2d, Der1 );
			      //Curve2->D1( Par2, P2d, Der2 );
			      Standard_Real Crossed1 = SeamDer ^ Der1;
			      Standard_Real Crossed2 = SeamDer ^ Der2;
			      //if (Crossed1 > 0
			    }
			  */
			}
		      else // ! bp.IsVertex(2)
			{
			  //Temporary the case of tangency is not implemented
			  Emap(CurEdge).Append( bp.Parameter(1) );
			  ParsNonSeam(ind).Append( bp.Parameter(2) );
			  Seam(ind).Append( CurEdge );
			  ParsSeam(ind).Append( bp.Parameter(1) );
			}
		    } //for (; EInter.MorePoint(); EInter.NextPoint())
		} //for (; eit.More(); eit.Next())
	      if (Wadd)
		{
		  if (! WWmap.IsBound( CurWire ))
		    {
		      TopTools_ListOfShape emptylist;
		      WWmap.Bind( CurWire, emptylist );
		    }
		  WWmap(CurWire).Append( aWire );
		}
	    } //if (! aWire.IsSame( CurWire ))
	} //for (; wit.More(); wit.Next())
    } //for (i = 1; i <= Eseq.Length(); i++)

  //Sorting
  for (i = 1; i <= NonSeam.Length(); i++)
    for (j = 1; j < ParsNonSeam(i).Length(); j++)
      for (k = j+1; k <= ParsNonSeam(i).Length(); k++)
	if (ParsNonSeam(i)(k) < ParsNonSeam(i)(j))
	  {
	    Standard_Real temp = ParsNonSeam(i)(j);
	    ParsNonSeam(i)(j) = ParsNonSeam(i)(k);
	    ParsNonSeam(i)(k) = temp;
	    TopoDS_Shape tmp = Seam(i)(j);
	    Seam(i)(j) = Seam(i)(k);
	    Seam(i)(k) = tmp;
	    temp = ParsSeam(i)(j);
	    ParsSeam(i)(j) = ParsSeam(i)(k);
	    ParsSeam(i)(k) = temp;
	  }
  BRepFill_DataMapIteratorOfDataMapOfShapeSequenceOfReal iter( Emap );
  for (; iter.More(); iter.Next())
    {
      TColStd_SequenceOfReal Seq;
      Seq = iter.Value();
      for (i = 1; i < Seq.Length(); i++)
      for (j = i+1; j <= Seq.Length(); j++)
	if (Seq(j) < Seq(i))
	  {
	    Standard_Real temp = Seq(i);
	    Seq(i) = Seq(j);
	    Seq(j) = temp;
	  }
      Emap( iter.Key() ) = Seq;
    }
  BRepFill_DataMapOfShapeSequenceOfReal EPmap;
  TopTools_DataMapOfShapeSequenceOfShape EVmap; //Seam
  TopTools_DataMapOfShapeSequenceOfShape EWmap; //Seam and wires intersecting it
  iter.Initialize( Emap );
  for (; iter.More(); iter.Next())
    {
      TColStd_SequenceOfReal parseq;
      EPmap.Bind( iter.Key(), parseq );
      TopTools_SequenceOfShape shapeseq;
      EVmap.Bind( iter.Key(), shapeseq );
      TopTools_SequenceOfShape shapeseq2;
      EWmap.Bind( iter.Key(), shapeseq2 );
    }

  //Reconstruction of non-seam edges
  BRepTools_Substitution aSub;
  BRep_Builder BB;
  for (i = 1; i <= NonSeam.Length(); i++)
    {
      TopoDS_Edge anEdge = TopoDS::Edge( NonSeam(i) );
      TopTools_ListOfShape NewEdges;
      TopoDS_Edge NewE;
      TopoDS_Vertex Vfirst, Vlast;
      TopExp::Vertices( anEdge, Vfirst, Vlast );
      Standard_Real par, FirstPar, LastPar;
      BRep_Tool::Range( anEdge, FirstPar, LastPar );
      Standard_Integer firstind = 1;
      par = ParsNonSeam(i)(1);
      TopoDS_Edge SeamEdge = TopoDS::Edge( Seam(i)(1) );
      //Find the face
      for (j = 1; j <= Eseq.Length(); j++)
	if (SeamEdge.IsSame( Eseq(j) ))
	  break;
      TopoDS_Face theFace = TopoDS::Face( Fseq(j) );
      TopLoc_Location L;
      Handle( Geom_Surface ) theSurf = BRep_Tool::Surface( theFace, L );
      if (Abs(par-FirstPar) <= Precision::Confusion())
	{
	  BB.UpdateVertex( Vfirst, ParsSeam(i)(1), SeamEdge, BRep_Tool::Tolerance(Vfirst) );
	  EPmap( SeamEdge ).Append( ParsSeam(i)(1) );
	  EVmap( SeamEdge ).Append( Vfirst );
	  EWmap( SeamEdge ).Append( NonSeamWires(i) );
	  firstind = 2;
	}
      Standard_Real prevpar = FirstPar;
      TopoDS_Vertex PrevV = Vfirst;
      for (j = firstind; j <= ParsNonSeam(i).Length(); j++)
	{
	  TopoDS_Shape aLocalShape = anEdge.EmptyCopied();
	  NewE = TopoDS::Edge( aLocalShape );
	  //NewE = TopoDS::Edge( anEdge.EmptyCopied() );
	  TopoDS_Vertex NewV;
	  par = ParsNonSeam(i)(j);
	  BB.Range( NewE, prevpar, par );
	  SeamEdge = TopoDS::Edge( Seam(i)(j) );
	  if (j == ParsNonSeam(i).Length() && Abs(par-LastPar) <= Precision::Confusion())
	    {
	      NewV = Vlast;
	      if (firstind == 2 && j == 2)
		{
		  BB.UpdateVertex( Vlast, ParsSeam(i)(j), SeamEdge, BRep_Tool::Tolerance(Vlast) );
		  EPmap( SeamEdge ).Append( ParsSeam(i)(j) );
		  EVmap( SeamEdge ).Append( Vlast );
		  EWmap( SeamEdge ).Append( NonSeamWires(i) );
		  break;
		}
	    }
	  else
	    {
	      BRepAdaptor_Curve bcur( NewE );
	      gp_Pnt Point = bcur.Value( par );
	      NewV = BRepLib_MakeVertex( Point );
	      BB.UpdateVertex( NewV, par, NewE, 10.*Precision::Confusion() );
	    }
	  BB.UpdateVertex( NewV, ParsSeam(i)(j), SeamEdge, 10.*Precision::Confusion() );
	  NewE.Orientation( TopAbs_FORWARD );
	  BB.Add( NewE, PrevV.Oriented(TopAbs_FORWARD) );
	  BB.Add( NewE, NewV.Oriented(TopAbs_REVERSED) );

	  NewEdges.Append( NewE );
	  EPmap( SeamEdge ).Append( ParsSeam(i)(j) );
	  EVmap( SeamEdge ).Append( NewV );
	  EWmap( SeamEdge ).Append( NonSeamWires(i) );

	  prevpar = par;
	  PrevV = NewV;
	}
      //The last edge
      TopoDS_Shape aLocalShape = anEdge.EmptyCopied();
      NewE = TopoDS::Edge( aLocalShape );
      //NewE = TopoDS::Edge( anEdge.EmptyCopied() );
      par = LastPar;
      if (Abs(prevpar-par) > Precision::Confusion())
	{
	  BB.Range( NewE, prevpar, par );
	  NewE.Orientation( TopAbs_FORWARD );
	  BB.Add( NewE, PrevV.Oriented(TopAbs_FORWARD) );
	  BB.Add( NewE, Vlast.Oriented(TopAbs_REVERSED) );
	  NewEdges.Append( NewE );
	}

      //Substitute anEdge by NewEdges
      aSub.Substitute( anEdge, NewEdges );
    }
  
  //Sorting of EPmap and EVmap and removing repeating points from them
  iter.Initialize( EPmap );
  for (; iter.More(); iter.Next())
    {
      TColStd_SequenceOfReal Seq;
      Seq = iter.Value();
      TopTools_SequenceOfShape SeqShape;
      SeqShape = EVmap( iter.Key() );
      TopTools_SequenceOfShape SeqShape2;
      SeqShape2 = EWmap( iter.Key() );
      for (i = 1; i < Seq.Length(); i++)
      for (j = i+1; j <= Seq.Length(); j++)
	if (Seq(j) < Seq(i))
	  {
	    Standard_Real temp = Seq(i);
	    Seq(i) = Seq(j);
	    Seq(j) = temp;
	    TopoDS_Shape tmp = SeqShape(i);
	    SeqShape(i) = SeqShape(j);
	    SeqShape(j) = tmp;
	    tmp = SeqShape2(i);
	    SeqShape2(i) = SeqShape2(j);
	    SeqShape2(j) = tmp;
	  }
      EPmap( iter.Key() ) = Seq;
      EVmap( iter.Key() ) = SeqShape;
      EWmap( iter.Key() ) = SeqShape2;
    }
  iter.Initialize( EPmap );
  for (; iter.More(); iter.Next())
    {
      TColStd_SequenceOfReal Seq;
      Seq = iter.Value();
      TopTools_SequenceOfShape SeqShape;
      SeqShape = EVmap( iter.Key() );
      TopTools_SequenceOfShape SeqShape2;
      SeqShape2 = EWmap( iter.Key() );
      Standard_Boolean remove = Standard_True;
      while (remove)
	{
	  remove = Standard_False;
	  for (i = 1; i < Seq.Length(); i++)
	    if (Abs(Seq(i)-Seq(i+1)) <= Precision::Confusion())
	      {
		Seq.Remove(i+1);
		SeqShape.Remove(i+1);
		SeqShape2.Remove(i+1);
		remove = Standard_True;
	      }
	}
      EPmap( iter.Key() ) = Seq;
      EVmap( iter.Key() ) = SeqShape;
      EWmap( iter.Key() ) = SeqShape2;
    }

  //Reconstruction of seam edges
  TopTools_DataMapOfShapeShape VEmap;
  iter.Initialize( Emap );
  for (; iter.More(); iter.Next())
    {
      TopoDS_Edge anEdge = TopoDS::Edge( iter.Key() );
      Standard_Boolean onepoint = Standard_False;
      TopTools_ListOfShape NewEdges;
      TColStd_SequenceOfReal Seq;
      Seq = iter.Value();
      TColStd_SequenceOfReal Seq2;
      Seq2 = EPmap( anEdge );
      TopTools_SequenceOfShape SeqVer;
      SeqVer = EVmap( anEdge );
      TopTools_SequenceOfShape SeqWire;
      SeqWire = EWmap( anEdge );
      TopoDS_Vertex Vfirst, Vlast;
      TopExp::Vertices( anEdge, Vfirst, Vlast );
      Standard_Real fpar, lpar, FirstPar, LastPar;
      BRep_Tool::Range( anEdge, FirstPar, LastPar );
      fpar = FirstPar;
      lpar = Seq(1);
      TopoDS_Edge NewE;
      Standard_Integer firstind = 1;
      if (Abs(fpar-lpar) <= Precision::Confusion())
	{
	  firstind = 2;
	  fpar = Seq(1);
	  lpar = Seq(2);
	}
      else
	{
	  if (Seq.Length()%2 != 0)
	    {
	      VEmap.Bind( Vfirst, anEdge );
	      firstind = 2;
	      fpar = Seq(1);
	      if (Seq.Length() > 2)
		lpar = Seq(2);
	      else
		onepoint = Standard_True;
	    }
	}
      if (!onepoint)
	{
	  TopoDS_Shape aLocalShape = anEdge.EmptyCopied();
	  NewE = TopoDS::Edge( aLocalShape );
	  //NewE = TopoDS::Edge( anEdge.EmptyCopied() );
	  BB.Range( NewE, fpar, lpar );
	  NewE.Orientation( TopAbs_FORWARD );
	  if (firstind == 1)
	    {
	      BB.Add( NewE, Vfirst.Oriented(TopAbs_FORWARD) );
	      aLocalShape = SeqVer(1).Oriented(TopAbs_REVERSED);
	      BB.Add( NewE, TopoDS::Vertex( aLocalShape ) );
	      //BB.Add( NewE, TopoDS::Vertex( SeqVer(1).Oriented(TopAbs_REVERSED) ) );
	    }
	  else
	    {
	      aLocalShape = SeqVer(1).Oriented(TopAbs_FORWARD);
	      BB.Add( NewE, TopoDS::Vertex( aLocalShape ) );
	      aLocalShape = SeqVer(2).Oriented(TopAbs_REVERSED);
	      BB.Add( NewE, TopoDS::Vertex( aLocalShape ) );
	      //BB.Add( NewE, TopoDS::Vertex( SeqVer(1).Oriented(TopAbs_FORWARD) ) );
	      //BB.Add( NewE, TopoDS::Vertex( SeqVer(2).Oriented(TopAbs_REVERSED) ) );
	    }
	  NewEdges.Append( NewE );
	  
	  firstind++;
	  for (i = firstind; i < Seq.Length(); i += 2)
	    {
	      aLocalShape = anEdge.EmptyCopied();
	      NewE = TopoDS::Edge( aLocalShape );
	      //NewE = TopoDS::Edge( anEdge.EmptyCopied() );
	      fpar = Seq(i);
	      lpar = Seq(i+1);
	      BB.Range( NewE, fpar, lpar );
	      //Find vertices
	      for (j = 1; j <= Seq2.Length(); j++)
		if (Abs(fpar-Seq2(j)) <= Precision::Confusion())
		  break;
	      NewE.Orientation( TopAbs_FORWARD );
	      TopoDS_Shape aLocalShape = SeqVer(j).Oriented(TopAbs_FORWARD);
	      BB.Add( NewE, TopoDS::Vertex( aLocalShape ) );
	      aLocalShape = SeqVer(j+1).Oriented(TopAbs_REVERSED);
	      BB.Add( NewE, TopoDS::Vertex( aLocalShape ) );
	      //BB.Add( NewE, TopoDS::Vertex( SeqVer(j).Oriented(TopAbs_FORWARD) ) );
	      //BB.Add( NewE, TopoDS::Vertex( SeqVer(j+1).Oriented(TopAbs_REVERSED) ) );
	      NewEdges.Append( NewE );
	    }
	}

      i = Seq.Length();
      fpar = Seq(i);
      lpar = LastPar;
      if (Abs(fpar-lpar) <= Precision::Confusion())
	continue;
      TopoDS_Shape aLocalShape = anEdge.EmptyCopied();
      NewE = TopoDS::Edge( aLocalShape );
      //NewE = TopoDS::Edge( anEdge.EmptyCopied() );
      BB.Range( NewE, fpar, lpar );
      NewE.Orientation( TopAbs_FORWARD );
      aLocalShape = SeqVer(SeqVer.Length()).Oriented(TopAbs_FORWARD);
      BB.Add( NewE, TopoDS::Vertex( aLocalShape ) );
      //BB.Add( NewE, TopoDS::Vertex( SeqVer(SeqVer.Length()).Oriented(TopAbs_FORWARD) ) );
      BB.Add( NewE, Vlast.Oriented(TopAbs_REVERSED) );
      NewEdges.Append( NewE );

      //Substitute anEdge by NewEdges
      aSub.Substitute( anEdge, NewEdges );
    }

  //Removing edges connected with missing extremities of seam edges
  TopTools_DataMapIteratorOfDataMapOfShapeShape itve( VEmap );
  for (; itve.More(); itve.Next())
    {
      TopoDS_Shape V = itve.Key();
      TopoDS_Shape E = itve.Value();
      TopoDS_Shape W;
      for (i = 1; i <= Eseq.Length(); i++)
	if (E.IsSame( Eseq(i) ))
	  {
	    W = Wseq(i);
	    break;
	  }
      TopoDS_Shape Etoremove;
      eit.Initialize( W );
      for (; eit.More(); eit.Next())
	{
	  TopoDS_Edge CurE = TopoDS::Edge( eit.Value() );
	  if (CurE.IsSame( E ))
	    continue;
	  TopoDS_Vertex Vfirst, Vlast;
	  TopExp::Vertices( CurE, Vfirst, Vlast );
	  if (Vfirst.IsSame( V ) || Vlast.IsSame( V ))
	    {
	      Etoremove = CurE;
	      break;
	    }
	}
      if (! Etoremove.IsNull())
	{
	  W.Free( Standard_True );
	  BB.Remove( W, Etoremove );
	}
    }

  aSub.Build( myShape );
  if (aSub.IsCopied( myShape ))
    {
      const TopTools_ListOfShape& listSh = aSub.Copy( myShape );
      if (! listSh.IsEmpty())
	myShape = listSh.First();
    }

  //Reconstruction of wires
  TopTools_ListOfShape theCopy;
  TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itww( WWmap );
  for (; itww.More(); itww.Next())
    {
      CurWire = itww.Key();
      theCopy = aSub.Copy( CurWire );
      CurWire = theCopy.First();
      CurWire.Free( Standard_True );
      TopTools_ListIteratorOfListOfShape itl( itww.Value() );
      for (; itl.More(); itl.Next())
	{
	  TopoDS_Shape aWire = itl.Value();
	  CurFace = WFmap( aWire );
	  theCopy = aSub.Copy( aWire );
	  aWire = theCopy.First();
	  //Adjusting period
	  TopLoc_Location L;
	  Handle( Geom_Surface ) theSurf = BRep_Tool::Surface( TopoDS::Face(CurFace), L );
	  eit.Initialize( aWire );
	  for (; eit.More(); eit.Next())
	    {
	      TopoDS_Edge anEdge = TopoDS::Edge( eit.Value() );
	      gp_Pnt2d Pfirst, Plast, Pmid;
	      BRep_Tool::UVPoints( anEdge, TopoDS::Face(CurFace), Pfirst, Plast );
	      BRepAdaptor_Curve2d bc2d( anEdge, TopoDS::Face(CurFace) );
	      Pmid = bc2d.Value( (bc2d.FirstParameter()+bc2d.LastParameter())/2. );
	      gp_Vec2d offset;
	      Standard_Boolean translate = Standard_False;
	      if (Pfirst.X()-2.*PI > Precision::Confusion() ||
		  Plast.X()-2.*PI > Precision::Confusion()  ||
		  Pmid.X()-2.*PI > Precision::Confusion())
		{
		  offset.SetCoord( -2.*PI, 0 );
		  translate = Standard_True;
		}
	      if (Pfirst.X() < -Precision::Confusion() ||
		  Plast.X() < -Precision::Confusion()  ||
		  Pmid.X() < -Precision::Confusion())
		{
		  offset.SetCoord( 2.*PI, 0 );
		  translate = Standard_True;
		}
	      if (translate)
		{
		  const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &anEdge.TShape());
		  BRep_ListIteratorOfListOfCurveRepresentation itcr( TE->ChangeCurves() );
		  Handle(BRep_GCurve) GC;
		  
		  for (; itcr.More(); itcr.Next())
		    {
		      GC = Handle(BRep_GCurve)::DownCast(itcr.Value());
		      if (!GC.IsNull() && GC->IsCurveOnSurface( theSurf, L ))
			{
			  Handle(Geom2d_Curve) PC = GC->PCurve();
			  PC = Handle(Geom2d_Curve)::DownCast( PC->Translated( offset ) );
			  GC->PCurve( PC );
			  TE->ChangeCurves().Remove( itcr );
			  TE->ChangeCurves().Append( GC );
			  break;
			}
		    }
		}
	    }
	  ///////////////////
	  eit.Initialize( aWire, Standard_False );
	  for (; eit.More(); eit.Next())
	    {
	      TopoDS_Shape anEdge = eit.Value();
	      BB.Add( CurWire, anEdge );
	    }
	  if (aSub.IsCopied( CurFace ))
	    {
	      theCopy = aSub.Copy( CurFace );
	      CurFace = theCopy.First();
	    }
	  CurFace.Free( Standard_True );
	  BB.Remove( CurFace, aWire );
	}
    }
}