summaryrefslogtreecommitdiff
path: root/src/BRepOffsetAPI/BRepOffsetAPI_MakeOffset.cxx
blob: 815f486d83d5315bde9ee4bf50ee505dc28f6d88 (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
// File:	BRepOffsetAPI_MakeOffset.cxx
// Created:	Mon Sep 18 14:16:15 1995
// Author:	Bruno DUMORTIER
//		<dub@fuegox>


#include <BRepOffsetAPI_MakeOffset.ixx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
#include <BRepAlgo_FaceRestrictor.hxx>
#include <Extrema_ExtPS.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRepFill_OffsetWire.hxx>
#include <BRepFill_ListIteratorOfListOfOffsetWire.hxx>
#include <BRepTopAdaptor_FClass2d.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Pnt.hxx>
#include <Precision.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx> 
#include <TopExp_Explorer.hxx>
#include <TopExp.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Wire.hxx>

#include <StdFail_NotDone.hxx>

//=======================================================================
//function : BRepOffsetAPI_MakeOffset
//purpose  : 
//=======================================================================

BRepOffsetAPI_MakeOffset::BRepOffsetAPI_MakeOffset()
:myIsInitialized( Standard_False)
{
}


//=======================================================================
//function : BRepOffsetAPI_MakeOffset
//purpose  : 
//=======================================================================

BRepOffsetAPI_MakeOffset::BRepOffsetAPI_MakeOffset(const TopoDS_Face& Spine, 
				       const GeomAbs_JoinType Join)
{
  Init(Spine, Join);
}


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

void BRepOffsetAPI_MakeOffset::Init(const TopoDS_Face&     Spine,
			      const GeomAbs_JoinType Join)
{
  myFace          = Spine;
  myIsInitialized = Standard_True;
  myJoin          = Join;
  TopExp_Explorer exp;
  for (exp.Init(myFace,TopAbs_WIRE); exp.More();exp.Next()) {
    myWires.Append(exp.Current());
  }
}

//=======================================================================
//function : BRepOffsetAPI_MakeOffset
//purpose  : 
//=======================================================================

BRepOffsetAPI_MakeOffset::BRepOffsetAPI_MakeOffset(const TopoDS_Wire& Spine, 
					const GeomAbs_JoinType Join)
{
  myWires.Append(Spine);
  myIsInitialized = Standard_True;
  myJoin = Join;
}

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

void BRepOffsetAPI_MakeOffset::Init(const GeomAbs_JoinType Join)
{
  myJoin = Join;
}
 
//=======================================================================
//function : BRepOffsetAPI_MakeOffset
//purpose  : 
//=======================================================================

void BRepOffsetAPI_MakeOffset::AddWire(const TopoDS_Wire& Spine)
					    
{
  myIsInitialized = Standard_True;
  myWires.Append(Spine);
}

//=======================================================================
//function : BuildDomain
//purpose  : 
//=======================================================================

static void BuildDomains(TopoDS_Face&               myFace,
			 TopTools_ListOfShape&      WorkWires,
			 BRepFill_ListOfOffsetWire& myAlgos,
			 GeomAbs_JoinType           myJoin,
			 Standard_Boolean           isPositive)
{
  BRepAlgo_FaceRestrictor  FR;
  TopoDS_Vertex            VF,VL;
  TopTools_ListOfShape     LOW;
  BRep_Builder             B;

  if (myFace.IsNull()) {
    myFace   = BRepBuilderAPI_MakeFace(TopoDS::Wire(WorkWires.First()),Standard_True);
    if (myFace.IsNull())
      StdFail_NotDone::Raise ("BRepOffsetAPI_MakeOffset : the wire is not planar");
  }
//  Modified by Sergey KHROMOV - Thu Apr 26 16:04:43 2001 Begin
  TopExp_Explorer anExp(myFace, TopAbs_WIRE);
  TopoDS_Shape    aWire1 = WorkWires.First();
  TopoDS_Shape    aWire2;
  if (anExp.More()) {
    aWire2 = anExp.Current();
    if ((aWire1.Orientation() == aWire2.Orientation() && isPositive) ||
	(aWire1.Orientation() == TopAbs::Complement(aWire2.Orientation()) && !isPositive)) {
      TopTools_ListOfShape LWires;
      TopTools_ListIteratorOfListOfShape itl;
      for (itl.Initialize(WorkWires); itl.More(); itl.Next()) {
	const TopoDS_Shape& W = itl.Value();
	LWires.Append(W.Reversed());
      }
      WorkWires = LWires;
    }
  }
//  Modified by Sergey KHROMOV - Thu Apr 26 16:04:44 2001 End
  FR.Init(myFace,Standard_True);
  //====================================================
  // Construction des faces limites par les wires fermes.
  //====================================================
  TopTools_ListIteratorOfListOfShape itl(WorkWires);
  for (; itl.More(); itl.Next()) {
    TopoDS_Wire& W = TopoDS::Wire(itl.Value());
    if (W.Closed()){
      FR.Add(W);
      continue;
    }
    TopExp::Vertices (W,VF,VL);
    if (VF.IsSame(VL)) {
      FR.Add(W);
    }
    else {
      LOW.Append(W);
    }
  }
  FR.Perform();
  if (!FR.IsDone()) {
    StdFail_NotDone::Raise ("BRepOffsetAPI_MakeOffset : Build Domains");
  }
  TopTools_ListOfShape Faces;
  for (; FR.More(); FR.Next()) {
    Faces.Append(FR.Current());
  }

  //===========================================
  // Pas de wire ferme => un seul domaine
  //===========================================
  if (Faces.IsEmpty()) {
    TopoDS_Shape aLocalShape = myFace.EmptyCopied();
    TopoDS_Face F = TopoDS::Face(aLocalShape);
//    TopoDS_Face F = TopoDS::Face(myFace.EmptyCopied());
    TopTools_ListIteratorOfListOfShape itW(LOW);
    for ( ; itW.More(); itW.Next()) {
      B.Add(F,itW.Value());
    }
    BRepFill_OffsetWire Algo(F, myJoin);
    myAlgos.Append(Algo);
    return;
  }
  
  //====================================================
  // Classification des wires ouverts.
  //====================================================  
//  for (TopTools_ListIteratorOfListOfShape itF(Faces); itF.More(); itF.Next()) {
  TopTools_ListIteratorOfListOfShape itF;
  for (itF.Initialize(Faces) ; itF.More(); itF.Next()) {
    TopoDS_Face&          F = TopoDS::Face(itF.Value());
    BRepAdaptor_Surface   S(F,0);
    Standard_Real         Tol = BRep_Tool::Tolerance(F); 

    BRepTopAdaptor_FClass2d CL(F,Precision::Confusion());

    TopTools_ListIteratorOfListOfShape itW(LOW);
    while (itW.More()) {
      TopoDS_Wire& W = TopoDS::Wire(itW.Value());
      //=======================================================
      // Choix d un point sur le wire. + projection sur la face.
      //=======================================================
      TopExp_Explorer exp(W,TopAbs_VERTEX);
      TopoDS_Vertex   V  = TopoDS::Vertex(exp.Current());
      gp_Pnt2d        PV;
      gp_Pnt          P3d = BRep_Tool::Pnt(V);
      Extrema_ExtPS ExtPS (P3d,S,Tol,Tol);
      Standard_Real     Dist2Min = Precision::Infinite();
      Standard_Real     Found   = Standard_False;
      for (Standard_Integer ie = 1; ie <= ExtPS.NbExt(); ie++) {
	Standard_Real X,Y;
	if (ExtPS.SquareDistance(ie) < Dist2Min) {
	  Dist2Min = ExtPS.SquareDistance(ie);
	  Found   = Standard_True;
	  ExtPS.Point(ie).Parameter(X,Y);
	  PV.SetCoord(X,Y);
	}
      }
      if ( Found && (CL.Perform(PV) == TopAbs_IN)) {
	// On a trouve la face qui contient le wire  on l enleve de la liste
	B.Add(F,W);
	LOW.Remove(itW);
      }
      else {
	itW.Next();
      }
    }
  }
  //========================================
  // Creation des algos sur chaque domaine.
  //========================================
  for (itF.Initialize(Faces); itF.More(); itF.Next()) {
    BRepFill_OffsetWire Algo(TopoDS::Face(itF.Value()), myJoin);
    myAlgos.Append(Algo);
  }
}

//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

void BRepOffsetAPI_MakeOffset::Perform(const Standard_Real Offset, 
				 const Standard_Real Alt)
{
  StdFail_NotDone_Raise_if ( !myIsInitialized,
			    "BRepOffsetAPI_MakeOffset : Perform without Init");

  Standard_Integer i = 1;
  BRepFill_ListIteratorOfListOfOffsetWire itOW;  
  TopoDS_Compound Res;
  BRep_Builder    B;
  B.MakeCompound (Res);

  myLastIsLeft = (Offset <= 0);
  
  if (Offset <= 0.) {
    if (myLeft.IsEmpty()) {
//  Modified by Sergey KHROMOV - Fri Apr 27 14:35:26 2001 Begin
      BuildDomains(myFace,myWires,myLeft,myJoin, Standard_False);
//  Modified by Sergey KHROMOV - Fri Apr 27 14:35:26 2001 End
    }
    for (itOW.Initialize(myLeft); itOW.More(); itOW.Next()) {
      BRepFill_OffsetWire& Algo = itOW.Value();
      Algo.Perform(Abs(Offset),Alt);
      if (Algo.IsDone() && !Algo.Shape().IsNull()) {
	B.Add(Res,Algo.Shape());
	if (i == 1) myShape = Algo.Shape();
	i++;
      }
    }
  }
  else {
    if (myRight.IsEmpty()) {
//  Modified by Sergey KHROMOV - Fri Apr 27 14:35:28 2001 Begin
      BuildDomains(myFace,myWires,myRight,myJoin, Standard_True);
//  Modified by Sergey KHROMOV - Fri Apr 27 14:35:35 2001 End
    }
    for (itOW.Initialize(myRight); itOW.More(); itOW.Next()) {
      BRepFill_OffsetWire& Algo = itOW.Value();
      Algo.Perform(Offset,Alt);
      if (Algo.IsDone() && !Algo.Shape().IsNull()) {
	B.Add(Res,Algo.Shape());
	if (i == 1) myShape = Algo.Shape();
	i++;
      }
    }
  }
  if (i > 2) myShape = Res;
  Done();
}

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

void BRepOffsetAPI_MakeOffset::Build()
{
  Done();
}


//=======================================================================
//function : ShapesFromShape
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& BRepOffsetAPI_MakeOffset::Generated
(const TopoDS_Shape& S)
{
  myGenerated.Clear();
  BRepFill_ListIteratorOfListOfOffsetWire itOW;
  BRepFill_ListOfOffsetWire* Algos;
  Algos= &myLeft;
  if (!myLastIsLeft) {
    Algos = &myRight;
  }
  for (itOW.Initialize(*Algos); itOW.More(); itOW.Next()) {
    BRepFill_OffsetWire& OW = itOW.Value();
    TopTools_ListOfShape L;
    L =  OW.GeneratedShapes(S.Oriented(TopAbs_FORWARD));
    myGenerated.Append(L);
    L =  OW.GeneratedShapes(S.Oriented(TopAbs_REVERSED));
    myGenerated.Append(L);
  }
  return myGenerated;
}