summaryrefslogtreecommitdiff
path: root/src/BRepLib/BRepLib_MakeShell.cxx
blob: f19f3459e26dff7d544335b35bba09cb27f90da7 (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
// File:	BRepLib_MakeShell.cxx
// Created:	Wed Jan  4 13:37:46 1995
// Author:	Bruno DUMORTIER
//		<dub@fuegox>


#include <BRepLib_MakeShell.ixx>

#include <BRepLib.hxx>
#include <Precision.hxx>
#include <TColStd_Array1OfReal.hxx>

#include <Geom_RectangularTrimmedSurface.hxx>
#include <Geom_BSplineSurface.hxx>
#include <Geom2d_Line.hxx>
#include <GeomAdaptor_Surface.hxx>
#include <TColGeom2d_Array1OfCurve.hxx>
#include <gp_Pnt.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopTools_Array1OfShape.hxx>
#include <BRep_Builder.hxx>


//=======================================================================
//function : BRepLib_MakeShell
//purpose  : 
//=======================================================================

BRepLib_MakeShell::BRepLib_MakeShell() :
       myError(BRepLib_EmptyShell)
{
}


//=======================================================================
//function : BRepLib_MakeShell
//purpose  : 
//=======================================================================

BRepLib_MakeShell::BRepLib_MakeShell(const Handle(Geom_Surface)& S,
				     const Standard_Boolean Segment)
{
  Standard_Real UMin,UMax,VMin,VMax;
  S->Bounds(UMin,UMax,VMin,VMax);
  Init(S,UMin,UMax,VMin,VMax,Segment);
}


//=======================================================================
//function : BRepLib_MakeShell
//purpose  : 
//=======================================================================

BRepLib_MakeShell::BRepLib_MakeShell(const Handle(Geom_Surface)& S, 
				     const Standard_Real UMin,
				     const Standard_Real UMax, 
				     const Standard_Real VMin, 
				     const Standard_Real VMax,
				     const Standard_Boolean Segment)
{
  Init(S,UMin,UMax,VMin,VMax,Segment);
}


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

void BRepLib_MakeShell::Init(const Handle(Geom_Surface)& S, 
			     const Standard_Real UMin, 
			     const Standard_Real UMax, 
			     const Standard_Real VMin, 
			     const Standard_Real VMax,
			     const Standard_Boolean Segment)
{
  Handle(Geom_Surface) BS = S;
  if ( S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
    Handle(Geom_RectangularTrimmedSurface) RTS = 
      Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
    BS = RTS->BasisSurface();
  }
  myError = BRepLib_EmptyShell;
  Standard_Real tol = Precision::Confusion();

  // Make a shell from a surface
  GeomAdaptor_Surface GS(BS,UMin,UMax,VMin,VMax);

  Standard_Integer nu = GS.NbUIntervals(GeomAbs_C2);
  Standard_Integer nv = GS.NbVIntervals(GeomAbs_C2);

  Standard_Boolean uperiodic = GS.IsUPeriodic();
  Standard_Boolean vperiodic = GS.IsVPeriodic();

  if (nu == 0 || nv == 0) return;

  // arrays of parameters and pcurves
  TColStd_Array1OfReal upars(1,nu+1);
  TColStd_Array1OfReal vpars(1,nv+1);
  TColGeom2d_Array1OfCurve uisos(1,nu+1);
  TColGeom2d_Array1OfCurve visos(1,nv+1);

  Standard_Integer iu,iv;

  GS.UIntervals(upars,GeomAbs_C2);
  gp_Dir2d dv(0,1);
  for (iu = 1; iu <= nu+1; iu++) {
    Standard_Real u = upars(iu);
    if (!Precision::IsInfinite(u))
      uisos(iu)  = new Geom2d_Line(gp_Pnt2d(u,0.),dv);
  }
  
  GS.VIntervals(vpars,GeomAbs_C2);
  gp_Dir2d du(1,0);
  for (iv = 1; iv <= nv+1 ; iv++) {
    Standard_Real v = vpars(iv);
    if (!Precision::IsInfinite(v))
      visos(iv)  = new Geom2d_Line(gp_Pnt2d(0.,v),du);
  }
    
  // create row by row

  // create the shell
  BRep_Builder B;
  B.MakeShell(TopoDS::Shell(myShape));

  // arrays of edges and vertices for each row
  TopTools_Array1OfShape botedges(1,nu);
  TopTools_Array1OfShape botvertices(1,nu+1);

  // copies of the first ones for periodic case
  TopTools_Array1OfShape fbotedges(1,nu);
  TopTools_Array1OfShape fbotvertices(1,nu+1);

  TopoDS_Face F;
  TopoDS_Wire W;
  TopoDS_Edge eleft,eright,etop,ebot,feleft;
  TopoDS_Vertex vlb,vlt,vrb,vrt,fvlt;


  // init the botedges and botvertices
  if (!Precision::IsInfinite(vpars(1))) {
    if (!Precision::IsInfinite(upars(1)))
      B.MakeVertex(vrt,S->Value(upars(1),vpars(1)),tol);
    fbotvertices(1) = botvertices(1) = vrt;

    for (iu = 1; iu <= nu; iu++) {
      vlt = vrt;

      if (uperiodic && iu == nu)
	vrt = TopoDS::Vertex(botvertices(1));
      else if (!Precision::IsInfinite(upars(iu+1)))
	B.MakeVertex(vrt,S->Value(upars(iu+1),vpars(1)),tol);

      fbotvertices(iu+1) = botvertices(iu+1) = vrt;
      B.MakeEdge(etop);
      if (!vlt.IsNull()) {
	vlt.Orientation(TopAbs_FORWARD);
	B.Add(etop,vlt);
      }
      if (!vrt.IsNull()) {
	vrt.Orientation(TopAbs_REVERSED);
	B.Add(etop,vrt);
      }
      fbotedges(iu) = botedges(iu) = etop;
    }
  }

  for (iv = 1; iv <= nv; iv++) {

    // compute the first edge and vertices of the line
    vrb = TopoDS::Vertex(botvertices(1));

    if (vperiodic && iv == nv) {
      vrt = TopoDS::Vertex(fbotvertices(1));
    }
    else {
      vrt.Nullify();
      if (!Precision::IsInfinite(vpars(iv+1))) {
	if (!Precision::IsInfinite(upars(1)))
	  B.MakeVertex(vrt,S->Value(upars(1),vpars(iv+1)),tol);
      }
    }

    eright.Nullify();
    if (!Precision::IsInfinite(upars(1))) {
      B.MakeEdge(eright);
      if (!vrb.IsNull()) {
	vrb.Orientation(TopAbs_FORWARD);
	B.Add(eright,vrb);
      }
      if (!vrt.IsNull()) {
	vrt.Orientation(TopAbs_REVERSED);
	B.Add(eright,vrt);
      }
    }

    fvlt  = vrt;
    feleft = eright;


    // make the row of faces
    
    for (iu = 1; iu <= nu; iu++) {
      
      // create the face at iu, iv

      // the surface
      Handle(Geom_Surface) SS = Handle(Geom_Surface)::DownCast(BS->Copy());
      if (GS.GetType() == GeomAbs_BSplineSurface && Segment) {
	Handle(Geom_BSplineSurface)::DownCast(SS)
	  ->Segment(upars(iu),upars(iu+1),
		    vpars(iv),vpars(iv+1) );
      }
      B.MakeFace(F,SS,tol);

      // the wire

      B.MakeWire(W);

      // the vertices

      vlb = vrb;
      vrb = TopoDS::Vertex(botvertices(iu+1));
      vlt = vrt;

      if (uperiodic && iu == nu)
	vrt = fvlt;
      else {
	vrt.Nullify();
	if (!Precision::IsInfinite(vpars(iv+1))) {
	  if (!Precision::IsInfinite(upars(iu+1)))
	    B.MakeVertex(vrt,S->Value(upars(iu+1),vpars(iv+1)),tol);
	}
      }

      botvertices(iu)   = vlt;
      botvertices(iu+1) = vrt;

      // the edges

      eleft = eright;

      if  (uperiodic && iu == nu)
	eright = feleft;
      else {
	eright.Nullify();
	if (!Precision::IsInfinite(upars(iu+1))) {
	  B.MakeEdge(eright);
	  if (!vrb.IsNull()) {
	    vrb.Orientation(TopAbs_FORWARD);
	    B.Add(eright,vrb);
	  }
	  if (!vrt.IsNull()) {
	    vrt.Orientation(TopAbs_REVERSED);
	    B.Add(eright,vrt);
	  }
	}
      }

      if ( uperiodic && nu == 1) {
	if (!eleft.IsNull() && !eright.IsNull()) {
	  B.UpdateEdge(eleft,uisos(2),uisos(1),F,tol);
	  B.Range(eleft,F,vpars(iv),vpars(iv+1));
	}
      }
      else {
	if (!eleft.IsNull()) {
	  B.UpdateEdge(eleft,uisos(iu),F,tol);
	  B.Range(eleft,F,vpars(iv),vpars(iv+1));
	}
	if (!eright.IsNull()) {
	  B.UpdateEdge(eright,uisos(iu+1),F,tol);
	  B.Range(eright,F,vpars(iv),vpars(iv+1));
	}
      }
      
      ebot = TopoDS::Edge(botedges(iu));

      if (vperiodic && iv == nv) 
	etop = TopoDS::Edge(fbotedges(iu));
      else {
	etop.Nullify();
	if (!Precision::IsInfinite(vpars(iv+1))) {
	  B.MakeEdge(etop);
	  if (!vlt.IsNull()) {
	    vlt.Orientation(TopAbs_FORWARD);
	    B.Add(etop,vlt);
	  }
	  if (!vrt.IsNull()) {
	    vrt.Orientation(TopAbs_REVERSED);
	    B.Add(etop,vrt);
	  }
	}
      }

      if ( vperiodic && nv == 1) {
	if (!ebot.IsNull() && !etop.IsNull()) {
	  B.UpdateEdge(ebot,visos(1),visos(2),F,tol);
	  B.Range(ebot,F,vpars(iv),vpars(iv+1));
	}
      }
      else {
	if (!ebot.IsNull()) {
	  B.UpdateEdge(ebot,visos(iv),F,tol);
	  B.Range(ebot,F,upars(iu),upars(iu+1));
	}
	if (!etop.IsNull()) {
	  B.UpdateEdge(etop,visos(iv+1),F,tol);
	  B.Range(etop,F,upars(iu),upars(iu+1));
	}
      }
      
      botedges(iu) = etop;

      if (!eleft.IsNull()) {
	eleft.Orientation(TopAbs_REVERSED);
	B.Add(W,eleft);
      }
      if (!ebot.IsNull()) {
	ebot.Orientation(TopAbs_FORWARD);
	B.Add(W,ebot);
      }
      if (!eright.IsNull()) {
	eright.Orientation(TopAbs_FORWARD);
	B.Add(W,eright);
      }
      if (!etop.IsNull()) {
	etop.Orientation(TopAbs_REVERSED);
	B.Add(W,etop);
      }
      
      B.Add(F,W);
      B.Add(myShape,F);
    }
  }
  
  // codage des courbes 3d et regularites.
  BRepLib::BuildCurves3d(myShape,tol);
  BRepLib::EncodeRegularity(myShape);
  
  myError = BRepLib_ShellDone;
  Done();
}


//=======================================================================
//function : Error
//purpose  : 
//=======================================================================

BRepLib_ShellError BRepLib_MakeShell::Error() const 
{
  return myError;
}


//=======================================================================
//function : TopoDS_Shell&
//purpose  : 
//=======================================================================

const TopoDS_Shell& BRepLib_MakeShell::Shell() const 
{
  return TopoDS::Shell(myShape);
}



//=======================================================================
//function : TopoDS_Shell
//purpose  : 
//=======================================================================

BRepLib_MakeShell::operator TopoDS_Shell() const
{
  return Shell();
}