summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepBuild/TopOpeBRepBuild_End.cxx
blob: 16911113b80dfb3f202eb5bb4264e252c591b331 (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
// File:	TopOpeBRepBuild_Builder.cxx
// Created:	Mon Jun 14 11:48:36 1993
// Author:	Jean Yves LEBEY
//		<jyl@zerox>

#include <TopOpeBRepBuild_Builder.jxx>
#include <TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeListOfShapeOn1State.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape.hxx>
#include <TopOpeBRepDS_ListOfShapeOn1State.hxx>
#include <Geom_Curve.hxx>
#include <Geom2d_Curve.hxx>
#include <TopOpeBRepTool_2d.hxx>
#include <BRep_Tool.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS.hxx>
#include <BRep_Builder.hxx>
#include <TopExp.hxx>
#include <BRepAdaptor_Curve.hxx>
//#include <BRepAdaptor_Curve2d.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRep_Tool.hxx>
#include <gp_Pnt.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <TopOpeBRepBuild_define.hxx>
#include <Bnd_Box.hxx>
#include <TopOpeBRepTool_FuseEdges.hxx>
#include <Standard_ProgramError.hxx>
#ifdef DEB
Standard_IMPORT Standard_Boolean TopOpeBRepBuild_GetcontextNOFE();
#endif

//=======================================================================
//function : End
//purpose  : 
//=======================================================================
void TopOpeBRepBuild_Builder::End()
{
  const TopOpeBRepDS_DataStructure& BDS = myDataStructure->DS();
  {
    // recodage de la continuite (edge,(f1,f2)) != C0 perdue lors
    // du changement de surface-support d'une arete non decoupee
    // d'une face tangente.  
    for (Standard_Integer i=1;i<=2;i++) {   
      TopoDS_Shape S; TopAbs_State sta=TopAbs_UNKNOWN;
      if      ( i==1 ) { S = myShape1; sta = myState1; }
      else if ( i==2 ) { S = myShape2; sta = myState2; }
      
      TopExp_Explorer exs;
      for (exs.Init(S,TopAbs_SHELL);exs.More();exs.Next()) {
//      for (TopExp_Explorer exs(S,TopAbs_SHELL);exs.More();exs.Next()) {
	const TopoDS_Shape& SH = exs.Current();
	Standard_Boolean SHhassha = BDS.HasShape(SH);
	if ( !SHhassha ) continue;
	
	Standard_Boolean Fhassam = Standard_False;
	TopExp_Explorer exf;
	for (exf.Init(SH,TopAbs_FACE);exf.More(); exf.Next()) {
//	for (TopExp_Explorer exf(SH,TopAbs_FACE);exf.More(); exf.Next()) {
	  Fhassam = myDataStructure->HasSameDomain(exf.Current());
	  if ( Fhassam ) break;
	}
	if ( !Fhassam ) continue;
	
	TopTools_IndexedDataMapOfShapeListOfShape M;
	TopExp::MapShapesAndAncestors(S,TopAbs_EDGE,TopAbs_FACE,M);
	Standard_Integer nE = M.Extent();
	for (Standard_Integer iE = 1; iE <= nE; iE++) {
	  const TopoDS_Edge& E = TopoDS::Edge(M.FindKey(iE));
	  if ( IsSplit(E,sta) ) continue;
	  const TopTools_ListOfShape& LF = M.FindFromIndex(iE);
	  if ( LF.Extent() < 2 ) continue;
	  
	  // NYI : > 2 faces connexes par E : iterer sur tous les couples
	  TopTools_ListIteratorOfListOfShape itLF(LF);
	  const TopoDS_Face& F1 = TopoDS::Face(itLF.Value()); itLF.Next();
	  const TopoDS_Face& F2 = TopoDS::Face(itLF.Value());
	  GeomAbs_Shape C = BRep_Tool::Continuity(E,F1,F2);
	  if ( C == GeomAbs_C0 ) continue;
	  
	  Standard_Boolean F1hassam = myDataStructure->HasSameDomain(F1);
	  Standard_Boolean F2hassam = myDataStructure->HasSameDomain(F2);
	  if ( !F1hassam  && !F2hassam ) continue;
	  
	  Standard_Boolean F1issplit = IsSplit(F1,sta);
	  Standard_Boolean F2issplit = IsSplit(F2,sta);
	  F1issplit &= (Splits(F1,sta).Extent() != 0);
	  F2issplit &= (Splits(F2,sta).Extent() != 0);
	  if ( !F1issplit && !F2issplit ) continue;
	  
	  TopoDS_Face FF1=F1,FF2=F2;
	  for (Standard_Integer ii=1; ii<=2; ii++) {
	    if ((ii==1 && !F1issplit) || (ii==2 && !F2issplit)) continue;
	    TopoDS_Face F;
	    if (ii==1) F = F1;
	    else       F = F2;
	    Standard_Boolean f = Standard_False;
	    TopTools_ListIteratorOfListOfShape it;
	    for (it.Initialize(Splits(F,sta));it.More();it.Next()) {
	      const TopoDS_Shape& SF = it.Value();
	      if (SF.ShapeType() != TopAbs_FACE) continue;
	      TopExp_Explorer ex;
	      for (ex.Init(SF,TopAbs_EDGE);ex.More();ex.Next()) {
		if (ex.Current().IsSame(E)) {
		  if (ii==1) FF1 = TopoDS::Face(it.Value());
		  else       FF2 = TopoDS::Face(it.Value());
		  f = Standard_True; break;
		}
	      }
	      if (f) break;
	    }
	  }
	  BRep_Builder B;
	  B.Continuity(E,FF1,FF2,C);
	}
      }
    }
  }
  
  // M.A.J de la tolerance des vertex
  {
// modified by NIZHNY-MKK  Fri Oct  6 16:13:33 2000.BEGIN
    TopTools_MapOfShape aMapOfNewEdges, aMapOfNewVertices;
    TopTools_ListIteratorOfListOfShape anIt;
    Standard_Integer iteratorofnewshape=0;
    for(iteratorofnewshape=1; iteratorofnewshape <= myDataStructure->NbCurves(); iteratorofnewshape++) {
      for(anIt.Initialize(NewEdges(iteratorofnewshape)); anIt.More(); anIt.Next()) {
	aMapOfNewEdges.Add(anIt.Value());
      }
    }
    for(iteratorofnewshape=1; iteratorofnewshape <= myDataStructure->NbPoints(); iteratorofnewshape++) {      
      aMapOfNewVertices.Add(NewVertex(iteratorofnewshape));
    }
// modified by NIZHNY-MKK  Fri Oct  6 16:13:36 2000.END

    TopoDS_Compound R;BRep_Builder B;B.MakeCompound(R);
    const TopTools_ListOfShape& lmergesha1 = Merged(myShape1,myState1);
    TopTools_ListIteratorOfListOfShape it(lmergesha1); for(;it.More();it.Next()) B.Add(R,it.Value());
    const TopTools_ListOfShape& LOES = Section();
#ifdef DEB
//    Standard_Integer nLOES = LOES.Extent();
#endif
        
    TopTools_IndexedDataMapOfShapeListOfShape idmoelof; TopExp::MapShapesAndAncestors(R,TopAbs_EDGE,TopAbs_FACE,idmoelof);
    TopTools_IndexedDataMapOfShapeListOfShape idmovloe; TopExp::MapShapesAndAncestors(R,TopAbs_VERTEX,TopAbs_EDGE,idmovloe);
    TopTools_IndexedDataMapOfShapeListOfShape idmovloes; for (TopTools_ListIteratorOfListOfShape I(LOES);I.More();I.Next())
      TopExp::MapShapesAndAncestors(I.Value(),TopAbs_VERTEX,TopAbs_EDGE,idmovloes);
    Standard_Integer iv,nv = idmovloe.Extent();
    for (iv=1;iv<=nv;iv++) {
      Standard_Integer nP1 = 0;
      const TopoDS_Vertex& V = TopoDS::Vertex(idmovloe.FindKey(iv));
      Standard_Boolean isbe = idmovloes.Contains(V);
      if ( !isbe ) continue;

      const TopTools_ListOfShape& loe1 = idmovloe.FindFromIndex(iv); 
#ifdef DEB
//      Standard_Integer nloe1 = loe1.Extent();
#endif
      TopTools_MapOfShape mose; //une seule fois chaque arete
      TopTools_ListOfShape loe; 
      TopTools_ListIteratorOfListOfShape iloe;
      for (iloe.Initialize(loe1);iloe.More();iloe.Next()) {
	const TopoDS_Edge& E = TopoDS::Edge(iloe.Value());
	Standard_Boolean isb = mose.Contains(E); if (isb) continue;
	mose.Add(E);loe.Append(E);
      }
#ifdef DEB
//      Standard_Integer nloe = loe.Extent();
#endif
      
      for (iloe.Initialize(loe);iloe.More();iloe.Next()) {
	const TopoDS_Edge& E = TopoDS::Edge(iloe.Value());
	const TopTools_ListOfShape& lof = idmoelof.FindFromKey(E);
	Standard_Integer nlof = lof.Extent();
	nP1 += nlof+1;
      }

      TColgp_Array1OfPnt TP(1,nP1);
      Standard_Integer nP2 = 0;
      for (iloe.Initialize(loe);iloe.More();iloe.Next()) {
	const TopoDS_Edge& E = TopoDS::Edge(iloe.Value());
        Standard_Real pv = BRep_Tool::Parameter(V,E);
        gp_Pnt Pv;
        Standard_Real f,l;Handle(Geom_Curve) C3D = BRep_Tool::Curve(E,f,l);
        if (!C3D.IsNull()) {
          Pv = C3D->Value(pv);
          TP(++nP2) = Pv;
        }
	const TopTools_ListOfShape& lof = idmoelof.FindFromKey(E);
#ifdef DEB
//	Standard_Integer nlof = lof.Extent();
#endif
	for (TopTools_ListIteratorOfListOfShape ilof(lof);ilof.More();ilof.Next()) {
	  const TopoDS_Face& F = TopoDS::Face(ilof.Value());
          Standard_Real tolpc;
          Standard_Boolean pcf = FC2D_HasCurveOnSurface(E,F);
          Handle(Geom2d_Curve) C2D;
          if (!pcf) {
            C2D = FC2D_CurveOnSurface(E,F,f,l,tolpc);
            if (C2D.IsNull()) Standard_ProgramError::Raise("TopOpeBRepBuild_Builder::End 1");
            Standard_Real tolE = BRep_Tool::Tolerance(E);
            Standard_Real tol = Max(tolE,tolpc);
            B.UpdateEdge(E,C2D,F,tol);
          }
          C2D = FC2D_CurveOnSurface(E,F,f,l,tolpc);
          gp_Pnt2d P2 = C2D->Value(pv);
          BRepAdaptor_Surface BAS(F,Standard_False);
          Pv = BAS.Value(P2.X(),P2.Y());
          TP(++nP2) = Pv;
// modified by NIZHNY-MKK  Fri Sep 29 16:08:28 2000.BEGIN
	  if(aMapOfNewEdges.Contains(E)) {
	    Standard_Real anEdgeTol = BRep_Tool::Tolerance(E);
	    Standard_Real aFaceTol = BRep_Tool::Tolerance(F);
	    if(anEdgeTol < aFaceTol)
	      B.UpdateEdge(E, aFaceTol);
	  }
// modified by NIZHNY-MKK  Fri Sep 29 16:08:32 2000.END
	}
// modified by NIZHNY-MKK  Fri Sep 29 16:54:08 2000.BEGIN
	if(aMapOfNewVertices.Contains(V)) {
	  Standard_Real aVertexTol = BRep_Tool::Tolerance(V);
	  Standard_Real anEdgeTol = BRep_Tool::Tolerance(E);
	  if (aVertexTol < anEdgeTol)
	    B.UpdateVertex(V, anEdgeTol);
	}
// modified by NIZHNY-MKK  Fri Sep 29 16:54:12 2000.END
      }
      
      Standard_Real newtol = BRep_Tool::Tolerance(V);
      Bnd_Box BOX;
      gp_Pnt Pv = BRep_Tool::Pnt(V);
      BOX.Set(Pv);
      for (Standard_Integer i=1;i<=nP2;i++) {
	const gp_Pnt& Pi = TP(i);
	BOX.Update(Pi.X(),Pi.Y(),Pi.Z());
      }
      Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;	
      BOX.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
      gp_Pnt P1(aXmin, aYmin, aZmin);
      gp_Pnt P2(aXmax, aYmax, aZmax);
      Standard_Real d = P1.Distance(P2);
      if (d > newtol) {
#ifdef DEB
        cout<<"\npoint P"<<iv<<" "<<Pv.X()<<" "<<Pv.Y()<<" "<<Pv.Z()<<endl;
        cout<<"TopOpeBRepBuild_Builder::End BOX newtol "<<newtol<<" -> "<<d<<endl;
#endif
        newtol = d;
        B.UpdateVertex(V,newtol);
      }
    }
  }

  Standard_Boolean makeFE = Standard_True;
#ifdef DEB
  makeFE = !TopOpeBRepBuild_GetcontextNOFE();
#endif

  if (makeFE) {
//    TopAbs_State state = myState1;
    TopTools_ListOfShape& ls = ChangeMerged(myShape1,myState1);
    for (TopTools_ListIteratorOfListOfShape itls(ls);itls.More();itls.Next()) {
      TopoDS_Shape& SFE = itls.Value();
      TopOpeBRepTool_FuseEdges FE(SFE);

      // avoid fusing old edges
      TopTools_IndexedMapOfShape mapOldEdges;
      TopExp::MapShapes (myShape1, TopAbs_EDGE, mapOldEdges);
      TopExp::MapShapes (myShape2, TopAbs_EDGE, mapOldEdges);
      FE.AvoidEdges (mapOldEdges);

      // Get List of edges that have been fused
      TopTools_DataMapOfIntegerListOfShape mle;
      FE.Edges(mle);

      Standard_Integer nle = mle.Extent();
      if ( nle != 0 ) {
	FE.Perform();
	SFE = FE.Shape();

	TopTools_DataMapOfIntegerShape mre;
	TopTools_DataMapOfShapeShape mlf;
	FE.ResultEdges(mre);
	FE.Faces(mlf);

	// edit the split to remove to edges to be fused and put them into the Merged
	//

	UpdateSplitAndMerged(mle, mre, mlf, TopAbs_IN);
	UpdateSplitAndMerged(mle, mre, mlf, TopAbs_OUT);
	UpdateSplitAndMerged(mle, mre, mlf, TopAbs_ON);

      }


    } // makeFE
  }
}

//=======================================================================
//function : UpdateSplitAndMerged
//purpose  :  edit the split to remove to edges to be fused and put them into the Merged
//=======================================================================

void TopOpeBRepBuild_Builder::UpdateSplitAndMerged(const  TopTools_DataMapOfIntegerListOfShape& mle,
				const  TopTools_DataMapOfIntegerShape& mre,
				const  TopTools_DataMapOfShapeShape& mlf,
				const  TopAbs_State state)
{
  const  TopOpeBRepDS_DataMapOfShapeListOfShapeOn1State& MapSplit = MSplit(state);
  TopOpeBRepDS_DataMapIteratorOfDataMapOfShapeListOfShapeOn1State it;
  for (it.Initialize(MapSplit); it.More(); it.Next()) {
    const TopoDS_Shape& e = it.Key();
 
    // For each edge of the MapSplit
    if ( e.ShapeType() == TopAbs_EDGE ) {
   
      // get the list of splitted edges.
      TopTools_ListOfShape& LstSplit = ChangeSplit(e,state);
   
      // for each edge of the list of splitted edges
      TopTools_ListIteratorOfListOfShape itSplitEdg;
      itSplitEdg.Initialize(LstSplit);
      while ( itSplitEdg.More()) {
	const TopoDS_Shape& edgecur = itSplitEdg.Value();
	
	// for each "packet" of edges to be fused
	TopTools_DataMapIteratorOfDataMapOfIntegerListOfShape itLstEdg;
	itLstEdg.Initialize(mle);
	Standard_Boolean Found = Standard_False;
	while ( itLstEdg.More() && !Found) {
	  const Standard_Integer& iLst = itLstEdg.Key();
	  const TopTools_ListOfShape& LmapEdg = mle.Find(iLst);
	  
	  // look for each edge of the list if it is in the map Split
	  TopTools_ListIteratorOfListOfShape itEdg; 
	  itEdg.Initialize(LmapEdg);
	  while ( itEdg.More() && !Found ) {
	    const TopoDS_Shape& edgefuse = itEdg.Value();
	    if (edgecur.IsSame(edgefuse)) {
	      Found = Standard_True;


	      LstSplit.Remove(itSplitEdg);

	      // edit the list of merged
	      TopAbs_State stateMerged;
	      if (ShapeRank(e) == 1)
		stateMerged = myState1;
	      else
		stateMerged = myState2;
	      
	      TopTools_ListOfShape LstMerged;
	      LstMerged.Append(mre(iLst));
	      ChangeMerged(e,stateMerged) = LstMerged;
	      
	    }
	    itEdg.Next();
	  }
	  
	  itLstEdg.Next();
	}
	
	if (!Found) {
	  itSplitEdg.Next();
	}

      }
   
    }
    // For each face of the MapSplit
    else if ( e.ShapeType() == TopAbs_FACE ) {
      // get the list of splitted faces.
      TopTools_ListOfShape& LstSplit = ChangeSplit(e,state);
   
      // for each face of the list of splitted faces
      TopTools_ListIteratorOfListOfShape itSplitFac;
      itSplitFac.Initialize(LstSplit);
      while ( itSplitFac.More()) {
	const TopoDS_Shape& facecur = itSplitFac.Value();

	if (mlf.IsBound(facecur)) {
	  LstSplit.InsertBefore(mlf(facecur),itSplitFac);
	  LstSplit.Remove(itSplitFac);

	}
	else {
	  itSplitFac.Next();
	}
      }      
    }
  }
}