summaryrefslogtreecommitdiff
path: root/src/ShapeCustom/ShapeCustom_SweptToElementary.cxx
blob: 74158d369a13feae5260c18ea68c806e03d40ac4 (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
#include <ShapeCustom_SweptToElementary.ixx>

#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
#include <BRep_TEdge.hxx>
#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
#include <BRep_GCurve.hxx>

#include <Geom_SweptSurface.hxx>
#include <Geom_SurfaceOfRevolution.hxx>
#include <Geom_SurfaceOfLinearExtrusion.hxx>
#include <Geom_RectangularTrimmedSurface.hxx>
#include <Geom_OffsetSurface.hxx>
#include <Geom_ToroidalSurface.hxx>
#include <Geom_SphericalSurface.hxx>
#include <Geom_CylindricalSurface.hxx>
#include <Geom_ConicalSurface.hxx>
#include <Geom_Plane.hxx>

#include <Adaptor3d_SurfaceOfRevolution.hxx>
#include <Adaptor3d_SurfaceOfLinearExtrusion.hxx>
#include <GeomAdaptor_HCurve.hxx>

#include <gp_Pln.hxx>
#include <gp_Cylinder.hxx>
#include <gp_Sphere.hxx>
#include <gp_Cone.hxx>
#include <gp_Torus.hxx>

#include <ShapeAnalysis_Surface.hxx>

#include <BRepTools.hxx>


//=======================================================================
//function : ShapeCustom_SweptToElementary
//purpose  : 
//=======================================================================

ShapeCustom_SweptToElementary::ShapeCustom_SweptToElementary()
{
}


//=======================================================================
//function : IsToConvert
//purpose  : auxilary (Analyze surface: is it to be converted?)
//=======================================================================
static Standard_Boolean IsToConvert (const Handle(Geom_Surface) &S,
				     Handle(Geom_SweptSurface) &SS)
{
  Handle(Geom_Surface) Stmp;

  if(S->IsKind(STANDARD_TYPE(Geom_SweptSurface))) {
    SS = Handle(Geom_SweptSurface)::DownCast(S);
    return Standard_True;
  }
  if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
    Handle(Geom_RectangularTrimmedSurface) RTS = 
      Handle(Geom_RectangularTrimmedSurface)::DownCast(S);
    Stmp = RTS->BasisSurface();
  }
  else if(S->IsKind(STANDARD_TYPE(Geom_OffsetSurface))) {
    Handle(Geom_OffsetSurface) OS = Handle(Geom_OffsetSurface)::DownCast(S);
    Stmp = OS->BasisSurface();
  }
  if(Stmp.IsNull() ) return Standard_False;
  if(S->IsKind(STANDARD_TYPE(Geom_SweptSurface))) {
    SS = Handle(Geom_SweptSurface)::DownCast(Stmp);
    return Standard_True;
  }
  return Standard_False;
}


//=======================================================================
//function : NewSurface
//purpose  : 
//=======================================================================

Standard_Boolean ShapeCustom_SweptToElementary::NewSurface(const TopoDS_Face& F,
                                                           Handle(Geom_Surface)& S,
                                                           TopLoc_Location& L,
                                                           Standard_Real& Tol,
                                                           Standard_Boolean& RevWires,
                                                           Standard_Boolean& RevFace) 
{
  S = BRep_Tool::Surface(F,L);
  Handle(Geom_SweptSurface) SS;
  if(!IsToConvert(S,SS)) return Standard_False;

  // case SurfaceOfRevolution
  if(SS->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
    Handle(Geom_SurfaceOfRevolution) SR = Handle(Geom_SurfaceOfRevolution)::DownCast(SS);
    Handle(Geom_Curve) bc = SR->BasisCurve();
    gp_Ax1 ax1 = SR->Axis();
    Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
    HC->ChangeCurve().Load(bc,bc->FirstParameter(),bc->LastParameter());
    Adaptor3d_SurfaceOfRevolution AS(HC,ax1);
    switch(AS.GetType()){
    // skl 18.12.2003 - plane not used, problems in PRO14665.igs
    //case GeomAbs_Plane : {
    //  Handle(Geom_Plane) Pl = new Geom_Plane(AS.Plane());
    //  S = Pl;
    //} break;
    case GeomAbs_Cylinder : {
      Handle(Geom_CylindricalSurface) Cy = 
        new Geom_CylindricalSurface(AS.Cylinder());
      S = Cy;
    } break;
    case GeomAbs_Sphere : {
      Handle(Geom_SphericalSurface) Sp = 
        new Geom_SphericalSurface(AS.Sphere());
      S = Sp;
    } break;
    case GeomAbs_Cone : {
      Handle(Geom_ConicalSurface) Co = 
        new Geom_ConicalSurface(AS.Cone());
      S = Co;
    } break;
    case GeomAbs_Torus : {
      Handle(Geom_ToroidalSurface) To = 
        new Geom_ToroidalSurface(AS.Torus());
      S = To;
    } break;
    default : return Standard_False; break;
    }
  }
  // case SurfaceOfLinearExtrusion
  else if(SS->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
    Handle(Geom_SurfaceOfLinearExtrusion) SLE = 
      Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(SS);
    Handle(Geom_Curve) bc = SLE->BasisCurve();
    gp_Dir dir = SLE->Direction();
    Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
    HC->ChangeCurve().Load(bc,bc->FirstParameter(),bc->LastParameter());
    Adaptor3d_SurfaceOfLinearExtrusion AS(HC,dir);
    switch(AS.GetType()){
    // skl 18.12.2003 - plane not used, problems in ims013.igs
    //case GeomAbs_Plane : {
    //  Handle(Geom_Plane) Pl = new Geom_Plane(AS.Plane());
    //  S = Pl;
    //} break;
    case GeomAbs_Cylinder : {
      Handle(Geom_CylindricalSurface) Cy = 
        new Geom_CylindricalSurface(AS.Cylinder());
      S = Cy;
    } break;
    default : return Standard_False; break;
    }
  }

  Tol = BRep_Tool::Tolerance(F);
  RevWires = Standard_False;
  RevFace = Standard_False;
  return Standard_True;
}


//=======================================================================
//function : NewCurve
//purpose  : 
//=======================================================================

Standard_Boolean ShapeCustom_SweptToElementary::NewCurve(const TopoDS_Edge& E,
                                                         Handle(Geom_Curve)& C,
                                                         TopLoc_Location& L,
                                                         Standard_Real& Tol) 
{
  //:p5 abv 26 Feb 99: force copying of edge if any its pcurve will be replaced
  Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());

  // iterate on pcurves
  BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
  for ( ; itcr.More(); itcr.Next() ) {
    Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itcr.Value());
    if ( GC.IsNull() || ! GC->IsCurveOnSurface() ) continue;
    Handle(Geom_Surface) S = GC->Surface();
    Handle(Geom_SweptSurface) SS;
    if(!IsToConvert(S,SS)) continue;
    Standard_Real f, l;
    C = BRep_Tool::Curve ( E, L, f, l );
    if ( ! C.IsNull() ) C = Handle(Geom_Curve)::DownCast ( C->Copy() );
    Tol = BRep_Tool::Tolerance ( E );
    return Standard_True;
  }
  return Standard_False;
}


//=======================================================================
//function : NewPoint
//purpose  : 
//=======================================================================

Standard_Boolean ShapeCustom_SweptToElementary::NewPoint(const TopoDS_Vertex& /*V*/,
                                                         gp_Pnt& /*P*/,Standard_Real& /*Tol*/) 
{
  // 3d points are never modified
  return Standard_False;
}


//=======================================================================
//function : NewCurve2d
//purpose  : 
//=======================================================================

Standard_Boolean ShapeCustom_SweptToElementary::NewCurve2d(const TopoDS_Edge& E,
                                                           const TopoDS_Face& F,
                                                           const TopoDS_Edge& NewE,
                                                           const TopoDS_Face& NewF,
                                                           Handle(Geom2d_Curve)& C,
                                                           Standard_Real& Tol) 
{
  TopLoc_Location L;
  Handle(Geom_Surface) S = BRep_Tool::Surface(F,L);
  Handle(Geom_SweptSurface) SS;

  // just copy pcurve if either its surface is changing or edge was copied
  if ( !IsToConvert(S,SS) && E.IsSame(NewE) ) return Standard_False;
  
  Standard_Real f, l;
  C = BRep_Tool::CurveOnSurface(E,F,f,l);
  if ( ! C.IsNull() ) {
    C = Handle(Geom2d_Curve)::DownCast ( C->Copy() );
    Handle(Geom_Surface) NS = BRep_Tool::Surface(NewF,L);
    if ( !NS.IsNull() && NS->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ) {
      if(SS->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
        Handle(Geom_SurfaceOfRevolution) SR = Handle(Geom_SurfaceOfRevolution)::DownCast(SS);
        Standard_Real U1,U2,V1,V2;
        SR->Bounds(U1,U2,V1,V2);
        gp_Pnt P0;
        SR->D0(U1,V1,P0);
        Handle(ShapeAnalysis_Surface) sas = new ShapeAnalysis_Surface(NS);
        gp_Pnt2d p2d = sas->ValueOfUV(P0,Precision::Confusion());
        gp_Vec2d shift(p2d.X()-U1,p2d.Y()-V1);
        C->Translate(shift);
      }
    }
    if ( !NS.IsNull() && NS->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ) {
      if(SS->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
       Handle(Geom_SurfaceOfRevolution) SR = Handle(Geom_SurfaceOfRevolution)::DownCast(SS);
        gp_Pnt PR,PS;
        Handle(Geom_SphericalSurface) SPH = Handle(Geom_SphericalSurface)::DownCast(NS);
        Standard_Real US1,US2,VS1,VS2;
        SPH->Bounds(US1,US2,VS1,VS2);
        SPH->D0(US1,VS1,PS);
        Standard_Real UR1,UR2,VR1,VR2;
        SR->Bounds(UR1,UR2,VR1,VR2);
        SR->D0(UR1,VR1,PR);
        gp_Pnt P0 = SPH->Location();
        gp_Vec VS(P0,PS);
        gp_Vec VR(P0,PR);
        Standard_Real angle = VS.Angle(VR);
        gp_Vec2d shift(0,VS1-VR1+angle);
        C->Translate(shift);
      }
    }
  }
  
  Tol = BRep_Tool::Tolerance ( E );
  return Standard_True;
}


//=======================================================================
//function : NewParameter
//purpose  : 
//=======================================================================

Standard_Boolean ShapeCustom_SweptToElementary::NewParameter(const TopoDS_Vertex& /*V*/,
                                                             const TopoDS_Edge& /*E*/,
                                                             Standard_Real& /*P*/,
                                                             Standard_Real& /*Tol*/) 
{
  return Standard_False;
}


//=======================================================================
//function : Continuity
//purpose  : 
//=======================================================================

GeomAbs_Shape ShapeCustom_SweptToElementary::Continuity(const TopoDS_Edge& E,
                                                        const TopoDS_Face& F1,
                                                        const TopoDS_Face& F2,
                                                        const TopoDS_Edge& /*NewE*/,
                                                        const TopoDS_Face& /*NewF1*/,
                                                        const TopoDS_Face& /*NewF2*/) 
{
  return BRep_Tool::Continuity(E,F1,F2);
}