summaryrefslogtreecommitdiff
path: root/src/BOPTest/BOPTest_TolerCommands.cxx
blob: 80d11b98ea6a51876f20a815d142bbff6373e873 (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
// File:	BOPTest_TolerCommands.cxx
// Created:	10:45:01 2000
// Author:	Peter KURNEV
//		<pkv@irinox>


#include <BOPTest.ixx>
//
#include <stdio.h>
//
#include <TCollection_AsciiString.hxx>
#include <TColStd_IndexedMapOfTransient.hxx>
//
#include <TopoDS_Shape.hxx>
#include <TopoDS_Vertex.hxx>
//
#include <DBRep.hxx>
//
#include <gp_Pnt2d.hxx>

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

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

#include <TopAbs_Orientation.hxx>

#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_MapOfShape.hxx>
#include <TopTools_IndexedMapOfShape.hxx>

#include <BRep_TVertex.hxx>
#include <BRep_TEdge.hxx>
#include <BRep_ListOfCurveRepresentation.hxx>
#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
#include <BRep_CurveRepresentation.hxx>
#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>
#include <BRep_TFace.hxx>

#include <TopLoc_Location.hxx>

#include <BRepLib.hxx>

#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>

static 
  Standard_Integer breducetolerance (Draw_Interpretor&, Standard_Integer, const char** );
static 
  Standard_Integer  btolx           (Draw_Interpretor&, Standard_Integer, const char** );
static
  Standard_Integer  bopaddpcs       (Draw_Interpretor&, Standard_Integer, const char** );
//
static 
  void ProcessVertex(const TopoDS_Vertex&,
		     const TopTools_ListOfShape&,
		     const TopTools_ListOfShape&);
static
  void ProcessEdge(const TopoDS_Edge&, const Standard_Real);

static
  void ReduceVertexTolerance (const TopoDS_Shape&);

static 
  void ReduceFaceTolerance (const TopoDS_Shape&);

static
  void ReduceEdgeTolerance (const TopoDS_Shape&, const Standard_Real);


//=======================================================================
//function : TolerCommands
//purpose  : 
//=======================================================================
  void  BOPTest::TolerCommands(Draw_Interpretor& theCommands)
{
  static Standard_Boolean done = Standard_False;
  if (done) 
    return;

  done = Standard_True;
  // Chapter's name
  const char* g = "CCR commands";
  //
  theCommands.Add("breducetolerance" ,  "Use >breducetolerance Shape [maxTol=0.01]",  __FILE__, breducetolerance, g);
  theCommands.Add("btolx"     ,  "Use >btolx Shape [minTol=1.e-7]",  __FILE__, btolx, g);
  theCommands.Add("bopaddpcs" ,  "Use >bopaddpcs Shape"           ,  __FILE__, bopaddpcs, g);
}

//=======================================================================
//function : btolx
//purpose  : 
//=======================================================================
Standard_Integer  btolx(Draw_Interpretor& di, Standard_Integer n,  const char** a)
{
  if (n<2) {
    di << " Use >btolx Shape [minTol=1.e-7]\n";
    return 1;
  }

  TopoDS_Shape aS = DBRep::Get(a[1]);
  
  if (aS.IsNull()) {
    di << " Null shape is not allowed \n";
    return 1;
  }
  //
  Standard_Real aTolEMin=1.e-7;
  if (n==3) {
    aTolEMin=atof(a[2]);
  }
  //
  // Edge Tolerances
  ReduceEdgeTolerance(aS, aTolEMin);
  //
  // Face Tolerances
  ReduceFaceTolerance(aS);
  //
  // Vertex Tolerances
  ReduceVertexTolerance(aS);
  //
  BRepLib::SameParameter(aS, 1.e-7, Standard_True);
  //
  DBRep::Set (a[1], aS);
  return 0;
}
//=======================================================================
//function : ReduceEdgeTolerance
//purpose  : 
//=======================================================================
void ReduceEdgeTolerance (const TopoDS_Shape& aS, const Standard_Real aTolTreshold)
{
  Standard_Integer i, aNbE;
  TopTools_IndexedMapOfShape aEMap;
  //
  TopExp::MapShapes(aS, TopAbs_EDGE, aEMap);
  //
  aNbE=aEMap.Extent();
  for (i=1; i<=aNbE; i++) {
    const TopoDS_Edge& aE= TopoDS::Edge(aEMap(i));

    ProcessEdge(aE, aTolTreshold);
  } 
}
//=======================================================================
//function : ReduceFaceTolerance
//purpose  : 
//=======================================================================
void ReduceFaceTolerance (const TopoDS_Shape& aS)
{
  Standard_Integer i, j, aNbF, aNbE;
  Standard_Real aTolE, aTolx, aTolEMin;
  TopTools_IndexedMapOfShape aFMap, aEMap;
  //
  aTolEMin=1.e-7;
  //
  TopExp::MapShapes(aS, TopAbs_FACE, aFMap);
  aNbF=aFMap.Extent();
  for (i=1; i<=aNbF; i++) {
    aTolx=1.e6;
    const TopoDS_Face& aF= TopoDS::Face(aFMap(i));
    Handle(BRep_TFace)& aTF = *((Handle(BRep_TFace)*)&aF.TShape());
    //
    TopExp::MapShapes(aF, TopAbs_EDGE, aEMap);
    aNbE=aEMap.Extent();
    for (j=1; j<=aNbE; ++j) {
      const TopoDS_Edge& aE= TopoDS::Edge(aEMap(j));
      aTolE =BRep_Tool::Tolerance(aE);
      if (aTolE<aTolx) {
	aTolx=aTolE;
      }
    }
    aTolE=(aTolx>aTolEMin) ? aTolx : aTolEMin;
    aTF->Tolerance(aTolE);
  }
}
//=======================================================================
//function : ReduceVertexTolerance
//purpose  : 
//=======================================================================
void ReduceVertexTolerance (const TopoDS_Shape& aS)
{
  Standard_Integer i, aNbV;
  TopTools_IndexedDataMapOfShapeListOfShape aVEMap, aVFMap;

  TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_EDGE, aVEMap);
  TopExp::MapShapesAndAncestors(aS, TopAbs_VERTEX, TopAbs_FACE, aVFMap);

  aNbV=aVEMap.Extent();
  for (i=1; i<=aNbV; i++) {
    const TopoDS_Vertex& aV= TopoDS::Vertex(aVEMap.FindKey(i));
    const TopTools_ListOfShape& aLE=aVEMap(i);
    const TopTools_ListOfShape& aLF=aVFMap.FindFromKey(aV);
    
    ProcessVertex(aV, aLE, aLF);
  }
}
//=======================================================================
//function : ProcessEdge
//purpose  : 
//=======================================================================
void ProcessEdge(const TopoDS_Edge& aE, const Standard_Real aTolTreshold)
{
  Standard_Integer i, aNb=23;
  Standard_Real aD2, aTolMax2, aT1, aT2, aT, dT;
  gp_Pnt aPC3D, aP3D;
  gp_Pnt2d aPC2D;

  //TopTools_ListIteratorOfListOfShape anIt;// Wng in Gcc 3.0
  BRep_ListIteratorOfListOfCurveRepresentation itcr;
  //
  Handle(Geom_Curve) aC3D=BRep_Tool::Curve(aE, aT1, aT2);
  if (aC3D.IsNull()) {
    return;
  }
  //
  dT=(aT2-aT1)/aNb;
  //
  Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
  const TopLoc_Location& Eloc = aE.Location();
  //
  aTolMax2=-1.e6;
  const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
  //
  itcr.Initialize(aLCR);
  for (; itcr.More(); itcr.Next()) {
    const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
    const TopLoc_Location& loc = cr->Location();
    TopLoc_Location L = (Eloc * loc);//.Predivided(aV.Location());
    //
    // 3D-Curve
    if (cr->IsCurve3D()) {
      continue;
    }
    //
    // 2D-Curve
    else if (cr->IsCurveOnSurface()) {
      const Handle(Geom2d_Curve)& aC2D = cr->PCurve();
      if (aC2D.IsNull()) {
	continue;
      }
      // Surface
      const Handle(Geom_Surface)& aS=cr->Surface();
      //
      // 2D-point treatment
      for (i=0; i<=aNb; ++i) {
	aT=aT1+i*dT;
	if (i==aNb) {
	  aT=aT2;
	}
	aPC3D=aC3D->Value(aT);
	aPC2D=aC2D->Value(aT);
	aS->D0(aPC2D.X(), aPC2D.Y(), aP3D);
	aP3D.Transform(L.Transformation());
	aD2=aPC3D.SquareDistance(aP3D);
	if (aD2 > aTolMax2) {
	  aTolMax2=aD2;
	}
      }
    } //if (cr->IsCurveOnSurface())
  }//for (; itcr.More(); itcr.Next())

  //#########################################################
  //
  if (aTolMax2<0.){
    return;
  }
  //
  //
  aTolMax2=sqrt(aTolMax2); 
  
  //printf(" aTolMax=%15.10lf, aTolWas=%15.10lf\n", aTolMax2, aTolE);

  Standard_Real aTolSet;
  aTolSet=(aTolMax2>aTolTreshold) ? aTolMax2 : aTolTreshold;

  TE->Tolerance(aTolSet);
}
//=======================================================================
//function : ProcessVertex
//purpose  : 
//=======================================================================
void ProcessVertex(const TopoDS_Vertex& aV,
		   const TopTools_ListOfShape& aLE,
		   const TopTools_ListOfShape& aLF)
{
  Standard_Real aTol, aD2, aTolMax2, aTolE, aParam;
  gp_Pnt aPC3D;
  gp_Pnt2d aPC2D;
  TopAbs_Orientation anOrV;

  TopTools_ListIteratorOfListOfShape anIt;
  TopTools_MapOfShape aProcessedEdges;
  TopExp_Explorer aVExp;
  
  BRep_ListIteratorOfListOfCurveRepresentation itcr;
  //
  aTolMax2=-1.e6;
  //
  Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*) &aV.TShape());
  const gp_Pnt& aPV3D = TV->Pnt();
  aTol =BRep_Tool::Tolerance(aV);
  //
  anIt.Initialize(aLE);
  for (; anIt.More(); anIt.Next()) {
    const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
    //
    if (aProcessedEdges.Contains(aE)) {
      continue;
    }
    aProcessedEdges.Add(aE);			       
    //
    Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
    const TopLoc_Location& Eloc = aE.Location();
    //
    aVExp.Init(aE, TopAbs_VERTEX);
    for (; aVExp.More(); aVExp.Next()) {
      const TopoDS_Vertex& aVx=TopoDS::Vertex(aVExp.Current());
      //
      if (!aVx.IsSame(aV)) {
	continue;
      }
      //
      anOrV=aVx.Orientation();
      if (!(anOrV==TopAbs_FORWARD || anOrV==TopAbs_REVERSED)) {
	continue;
      }
      //
      const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
      itcr.Initialize(aLCR);
      for (; itcr.More(); itcr.Next()) {
	const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
	const TopLoc_Location& loc = cr->Location();
	TopLoc_Location L = (Eloc * loc).Predivided(aV.Location());
	//
	// 3D-Curve
	if (cr->IsCurve3D()) {
	  const Handle(Geom_Curve)& aC3D = cr->Curve3D();
	  //
	  if (aC3D.IsNull()) {
	    continue;
	  }
	  // 3D-point treatment
	  aParam=BRep_Tool::Parameter(aVx, aE);
	  aPC3D= aC3D->Value(aParam);
	  aPC3D.Transform(L.Transformation());
	  aD2=aPV3D.SquareDistance(aPC3D);
	  if (aD2 > aTolMax2) {
	    aTolMax2=aD2;
	  }
	  //
	}//if (cr->IsCurve3D())
	//
	// 2D-Curve
	else if (cr->IsCurveOnSurface()) {
	  const Handle(Geom2d_Curve)& aC2D = cr->PCurve();
	  if (aC2D.IsNull()) {
	    continue;
	  }
	  // Surface
	  const Handle(Geom_Surface)& aS=cr->Surface();
	  //
	  // 2D-point treatment
	  aParam=BRep_Tool::Parameter(aVx, aE, aS, L);
	  aPC2D=aC2D->Value(aParam);
	  aS->D0(aPC2D.X(), aPC2D.Y(), aPC3D);
	  aPC3D.Transform(L.Transformation());
	  aD2=aPV3D.SquareDistance(aPC3D);
	  if (aD2 > aTolMax2) {
	    aTolMax2=aD2;
	  }
	} //if (cr->IsCurveOnSurface())

      }//for (; itcr.More(); itcr.Next())
    }//for (; aVExp.More(); aVExp.Next()) 
  }//for (; anIt.More(); anIt.Next()) 
  //#########################################################
  //
  // Reducing
  if (aTolMax2<0.){
    return;
  }
  //
  aTolMax2=sqrt(aTolMax2); 
  if (aTolMax2>aTol) {
    return;
  }
  //
  aProcessedEdges.Clear();
  anIt.Initialize(aLE);
  for (; anIt.More(); anIt.Next()) {
    const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());

    if (aProcessedEdges.Contains(aE)) {
      continue;
    }
    aProcessedEdges.Add(aE);

    aTolE =BRep_Tool::Tolerance(aE);
    if (aTolMax2 < aTolE) {
      aTolMax2=aTolE;
    }
  }
  //
  aProcessedEdges.Clear();
  anIt.Initialize(aLF);
  for (; anIt.More(); anIt.Next()) {
    const TopoDS_Face& aF=TopoDS::Face(anIt.Value());
    
    if (aProcessedEdges.Contains(aF)) {
      continue;
    }
    aProcessedEdges.Add(aF);
    
    aTolE =BRep_Tool::Tolerance(aF);
    if (aTolMax2 < aTolE) {
      aTolMax2=aTolE;
    }
  }
  //
  if (aTolMax2>aTol) {
    return;
  }
  //
  // Update Tolerance
  TV->Tolerance(aTolMax2);
}
//=======================================================================
//function : breducetolerance
//purpose  : 
//=======================================================================
Standard_Integer  breducetolerance(Draw_Interpretor& di, 
				   Standard_Integer n, 
				   const char** a)
{
  if (n<2) {
    di << " Use >bupdatetolerance Shape\n";
    return 1;
  }

  TopoDS_Shape aS = DBRep::Get(a[1]);
  
  if (aS.IsNull()) {
    di << " Null shape is not allowed \n";
    return 1;
  }
  ReduceVertexTolerance(aS);
  DBRep::Set (a[1], aS);
  
  return 0;
}
//
//
static
  void PreparePCurves(const TopoDS_Shape& aShape, Draw_Interpretor& di);

//=======================================================================
//function : bopaddpcs
//purpose  : Some Edgesdo not contain P-Curveson Faces to which they belong to. 
//           These faces usually based on Geom_Plane surface.
//           To prevent sophisticated treatment the Command "bopaddpcs:
//           adds P-Curves for the edges . 
//=======================================================================
Standard_Integer  bopaddpcs(Draw_Interpretor& di, Standard_Integer n,  const char** a)
{
  if (n<2) {
    di << " Use >bopaddpcs Shape\n";
    return 1;
  }

  TopoDS_Shape aS = DBRep::Get(a[1]);
  
  if (aS.IsNull()) {
    di << " Null shape is not allowed \n";
    return 1;
  }
  //
  PreparePCurves(aS, di);
  //
  DBRep::Set (a[1], aS);
  return 0;
}

//=======================================================================
//function : PreparePCurves
//purpose  : 
//=======================================================================
void PreparePCurves(const TopoDS_Shape& aShape, Draw_Interpretor& di)
{
  Standard_Integer i, aNbE;
  Standard_Real aTolE, aT1, aT2;
  TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
  TopLoc_Location aLoc;
  Handle(Geom_Curve)   aC3D;
  Handle(Geom2d_Curve) aC2D;
  BRep_Builder aBB;
  //
  TopExp::MapShapesAndAncestors (aShape, TopAbs_EDGE, TopAbs_FACE, aEFMap);
  //
  aNbE=aEFMap.Extent();
  for (i=1; i<=aNbE; ++i) {
    const TopoDS_Edge& aE=TopoDS::Edge(aEFMap.FindKey(i));
    //
    if (BRep_Tool::Degenerated(aE)) {
      continue;
    }
    //
    aC3D=BRep_Tool::Curve(aE, aT1, aT2);
    if (aC3D.IsNull()) {
      continue;
    }
    aTolE=BRep_Tool::Tolerance(aE);
    //
    const TopTools_ListOfShape& aLF=aEFMap(i);
    TopTools_ListIteratorOfListOfShape aFIt(aLF);
    for (; aFIt.More(); aFIt.Next()) {
      const TopoDS_Face& aF=TopoDS::Face(aFIt.Value());
      //
      // Map of surfaces on which the edge lays .
      TColStd_IndexedMapOfTransient aSCRMap;
      Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
      const BRep_ListOfCurveRepresentation& aLCR=TE->Curves();
      BRep_ListIteratorOfListOfCurveRepresentation itcr;
      itcr.Initialize(aLCR);
      for (; itcr.More(); itcr.Next()) {
	const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
	//
	if (cr->IsCurveOnSurface()) {
	  const Handle(Geom_Surface)& aSCR=cr->Surface();
	  aSCRMap.Add(aSCR);
	}
	//
      }
      //
      const Handle(Geom_Surface)& aS=BRep_Tool::Surface(aF, aLoc);
      if (!aSCRMap.Contains(aS)) {
	// try to obtain 2D curve
	aC2D=BRep_Tool::CurveOnSurface(aE, aS, aLoc, aT1, aT2);
	if (aC2D.IsNull()) {
	  di << " Warning: Can not obtain P-Curve\n";
	  continue;
	}
	else {
	  aBB.UpdateEdge(aE, aC2D, aF, aTolE);
	}
      }
    }
  }
}