0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / ShapeCustom / ShapeCustom_ConvertToBSpline.cxx
1 // Created on: 1999-06-17
2 // Created by: data exchange team
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <BRep_GCurve.hxx>
19 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
20 #include <BRep_TEdge.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepTools.hxx>
23 #include <Geom2d_Curve.hxx>
24 #include <Geom_BSplineSurface.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom_OffsetSurface.hxx>
27 #include <Geom_Plane.hxx>
28 #include <Geom_RectangularTrimmedSurface.hxx>
29 #include <Geom_Surface.hxx>
30 #include <Geom_SurfaceOfLinearExtrusion.hxx>
31 #include <Geom_SurfaceOfRevolution.hxx>
32 #include <gp_Pnt.hxx>
33 #include <Message_Msg.hxx>
34 #include <Precision.hxx>
35 #include <ShapeConstruct.hxx>
36 #include <ShapeCustom_ConvertToBSpline.hxx>
37 #include <Standard_Type.hxx>
38 #include <TopLoc_Location.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS_Vertex.hxx>
42
43 IMPLEMENT_STANDARD_RTTIEXT(ShapeCustom_ConvertToBSpline,ShapeCustom_Modification)
44
45 //=======================================================================
46 //function : ShapeCustom_ConvertToBSpline
47 //purpose  : 
48 //=======================================================================
49 ShapeCustom_ConvertToBSpline::ShapeCustom_ConvertToBSpline():
50        myExtrMode(Standard_True), myRevolMode(Standard_True),
51        myOffsetMode(Standard_True), myPlaneMode(Standard_False)
52 {
53 }
54
55 //=======================================================================
56 //function : IsToConvert
57 //purpose  : 
58 //=======================================================================
59
60 Standard_Boolean ShapeCustom_ConvertToBSpline::IsToConvert(const Handle(Geom_Surface) &S,
61                                                            Handle(Geom_Surface) &SS) const
62 {
63   SS = S;
64   if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
65     Handle(Geom_RectangularTrimmedSurface) RTS = 
66       Handle(Geom_RectangularTrimmedSurface)::DownCast ( S );
67     SS = RTS->BasisSurface();
68   }
69   if(SS->IsKind(STANDARD_TYPE(Geom_OffsetSurface))) {
70     if(myOffsetMode)
71       return Standard_True;
72     else {
73       Handle(Geom_OffsetSurface) OS = Handle(Geom_OffsetSurface)::DownCast ( SS );
74       Handle(Geom_Surface) basis = OS->BasisSurface();
75       Handle(Geom_Surface) tmp;
76       return IsToConvert(basis,tmp);
77     }
78   }
79   if ( SS->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) )
80     return myExtrMode;
81   if ( SS->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution)) )
82     return myRevolMode;
83   if ( SS->IsKind(STANDARD_TYPE(Geom_Plane)) )
84     return myPlaneMode;  
85   return Standard_False;
86 }
87     
88 //=======================================================================
89 //function : NewSurface
90 //purpose  : 
91 //=======================================================================
92
93 Standard_Boolean ShapeCustom_ConvertToBSpline::NewSurface (const TopoDS_Face& F,
94                                                            Handle(Geom_Surface)& S,
95                                                            TopLoc_Location& L,
96                                                            Standard_Real& Tol,
97                                                            Standard_Boolean& RevWires,
98                                                            Standard_Boolean& RevFace) 
99 {
100   S = BRep_Tool::Surface(F,L);
101   Standard_Real U1,U2,V1,V2;
102   S->Bounds(U1,U2,V1,V2);
103   Standard_Real Umin, Umax,Vmin,Vmax;
104   BRepTools::UVBounds(F,Umin, Umax, Vmin, Vmax);
105   if(Precision::IsInfinite(U1) || Precision::IsInfinite(U2)) {
106     U1 = Umin;
107     U2 = Umax;
108   }
109   if(Precision::IsInfinite(V1) || Precision::IsInfinite(V2)) {
110     V1 = Vmin;
111     V2 = Vmax;
112   }
113    
114   Handle(Geom_Surface) surf;
115   if ( ! IsToConvert ( S, surf) ) return Standard_False;
116   
117   Handle(Geom_Surface) res;
118   if(surf->IsKind(STANDARD_TYPE(Geom_OffsetSurface))&&!myOffsetMode) {
119     Handle(Geom_OffsetSurface) OS = Handle(Geom_OffsetSurface)::DownCast ( surf );
120     Handle(Geom_Surface) basis = OS->BasisSurface();
121     Standard_Real offset = OS->Offset();
122     Handle(Geom_BSplineSurface) bspl = ShapeConstruct::
123       ConvertSurfaceToBSpline(basis,U1,U2,V1,V2,Precision::Approximation(),
124                                 surf->Continuity(),10000,15);
125     Handle(Geom_OffsetSurface) nOff = new Geom_OffsetSurface(bspl,offset);
126     res = nOff;
127   }
128   else {
129     GeomAbs_Shape cnt = surf->Continuity();
130     if(surf->IsKind(STANDARD_TYPE(Geom_OffsetSurface)))
131       cnt = GeomAbs_C0; //pdn 30.06.99 because of hang-up in GeomConvert_ApproxSurface
132     res = ShapeConstruct::ConvertSurfaceToBSpline(surf,U1,U2,V1,V2,Precision::Approximation(),
133                                                   cnt,10000,15);
134   }
135   if(S->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)) ) {
136     Handle(Geom_RectangularTrimmedSurface) RTS = 
137       Handle(Geom_RectangularTrimmedSurface)::DownCast ( S );
138     Standard_Real UF, UL, VF, VL;
139     RTS->Bounds ( UF, UL, VF, VL );
140     S = new Geom_RectangularTrimmedSurface ( res, UF, UL, VF, VL );
141   }
142   else 
143     S = res;
144   
145   SendMsg( F, Message_Msg("ConvertToBSpline.NewSurface.MSG0"));
146
147   Tol = BRep_Tool::Tolerance(F);
148   RevWires = Standard_False;
149   RevFace = Standard_False;
150   return Standard_True;
151 }
152   
153 //=======================================================================
154 //function : NewCurve
155 //purpose  : 
156 //=======================================================================
157
158 Standard_Boolean ShapeCustom_ConvertToBSpline::NewCurve (const TopoDS_Edge& E,
159                                                          Handle(Geom_Curve)& C,
160                                                          TopLoc_Location& L,
161                                                          Standard_Real& Tol) 
162 {
163   //:p5 abv 26 Feb 99: force copying of edge if any its pcurve will be replaced
164   Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
165
166   // iterate on pcurves
167   BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
168   for ( ; itcr.More(); itcr.Next() ) {
169     Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itcr.Value());
170     if ( GC.IsNull() || ! GC->IsCurveOnSurface() ) continue;
171     Handle(Geom_Surface) S = GC->Surface();
172     Handle(Geom_Surface) ES;
173     if ( ! IsToConvert ( S, ES ) ) continue;
174     Standard_Real f, l;
175     C = BRep_Tool::Curve ( E, L, f, l );
176     if ( ! C.IsNull() ) C = Handle(Geom_Curve)::DownCast ( C->Copy() );
177     Tol = BRep_Tool::Tolerance ( E );
178     SendMsg( E, Message_Msg("ConvertToBSpline.NewCurve.MSG0"));
179     return Standard_True;
180   } 
181   return Standard_False;
182 }
183
184 //=======================================================================
185 //function : NewPoint
186 //purpose  : 
187 //=======================================================================
188
189 Standard_Boolean ShapeCustom_ConvertToBSpline::NewPoint (const TopoDS_Vertex& /*V*/,
190                                                          gp_Pnt& /*P*/, Standard_Real& /*Tol*/)
191 {
192   return Standard_False;
193 }
194
195 //=======================================================================
196 //function : NewCurve2d
197 //purpose  : 
198 //=======================================================================
199
200 Standard_Boolean ShapeCustom_ConvertToBSpline::NewCurve2d (const TopoDS_Edge& E,
201                                                            const TopoDS_Face& F,
202                                                            const TopoDS_Edge& NewE,
203                                                            const TopoDS_Face& /*NewF*/,
204                                                            Handle(Geom2d_Curve)& C,
205                                                            Standard_Real& Tol) 
206 {
207   TopLoc_Location L;
208   Handle(Geom_Surface) S = BRep_Tool::Surface(F,L);
209   Handle(Geom_Surface) ES;
210   
211   // just copy pcurve if either its surface is changing or edge was copied
212   if ( ! IsToConvert ( S, ES ) && E.IsSame ( NewE ) ) return Standard_False;
213   
214   Standard_Real f, l;
215   C = BRep_Tool::CurveOnSurface(E,F,f,l);
216   if ( ! C.IsNull() ) 
217     C = Handle(Geom2d_Curve)::DownCast ( C->Copy() );
218   
219   Tol = BRep_Tool::Tolerance ( E );
220   return Standard_True;
221 }
222
223 //=======================================================================
224 //function : NewParameter
225 //purpose  : 
226 //=======================================================================
227
228 Standard_Boolean ShapeCustom_ConvertToBSpline::NewParameter (const TopoDS_Vertex& /*V*/,
229                                                              const TopoDS_Edge& /*E*/,
230                                                              Standard_Real& /*P*/,
231                                                              Standard_Real& /*Tol*/)
232 {
233   return Standard_False;
234 }
235
236 //=======================================================================
237 //function : Continuity
238 //purpose  : 
239 //=======================================================================
240
241 GeomAbs_Shape ShapeCustom_ConvertToBSpline::Continuity (const TopoDS_Edge& E,
242                                                         const TopoDS_Face& F1,
243                                                         const TopoDS_Face& F2,
244                                                         const TopoDS_Edge& /*NewE*/,
245                                                         const TopoDS_Face& /*NewF1*/,
246                                                         const TopoDS_Face& /*NewF2*/)
247 {
248   return BRep_Tool::Continuity(E,F1,F2);
249 }
250
251 void ShapeCustom_ConvertToBSpline::SetExtrusionMode(const Standard_Boolean extrMode)
252 {
253   myExtrMode = extrMode;
254 }
255
256 void ShapeCustom_ConvertToBSpline::SetRevolutionMode(const Standard_Boolean revolMode)
257 {
258   myRevolMode = revolMode;
259 }
260
261 void ShapeCustom_ConvertToBSpline::SetOffsetMode(const Standard_Boolean offsetMode)
262 {
263   myOffsetMode = offsetMode;
264 }
265
266 void ShapeCustom_ConvertToBSpline::SetPlaneMode(const Standard_Boolean planeMode)
267 {
268   myPlaneMode = planeMode;
269 }