0033022: Coding - get rid of unused headers [ShapeBuild to STEPControl]
[occt.git] / src / ShapeCustom / ShapeCustom_DirectModification.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 //:l8 abv 15.01.99: CTS22022: writing correct pcurves for indirect tori
15 //:p5 abv 26.02.99: PRO18207: force copying of edge if any its pcurve is to be replaced
16 //szv#4 S4163
17 //S4181 pdn 20.04.99 Modification of indirect rectangular trimming surfaces and taking
18 // locations into account
19 //szv 03.01.01 PositiveCones merged in
20
21 #include <BRep_Builder.hxx>
22 #include <BRep_GCurve.hxx>
23 #include <BRep_TEdge.hxx>
24 #include <BRep_Tool.hxx>
25 #include <BRepTools.hxx>
26 #include <Geom2d_Curve.hxx>
27 #include <Geom_ConicalSurface.hxx>
28 #include <Geom_Curve.hxx>
29 #include <Geom_ElementarySurface.hxx>
30 #include <Geom_RectangularTrimmedSurface.hxx>
31 #include <Geom_Surface.hxx>
32 #include <gp_Mat.hxx>
33 #include <gp_Pnt.hxx>
34 #include <Message_Msg.hxx>
35 #include <ShapeCustom_DirectModification.hxx>
36 #include <Standard_Type.hxx>
37 #include <TopLoc_Location.hxx>
38 #include <TopoDS.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS_Vertex.hxx>
42
43 IMPLEMENT_STANDARD_RTTIEXT(ShapeCustom_DirectModification,ShapeCustom_Modification)
44
45 //=======================================================================
46 //function : ShapeCustom_DirectModification
47 //purpose  : 
48 //=======================================================================
49 ShapeCustom_DirectModification::ShapeCustom_DirectModification()
50 {
51 }
52
53 //S4181 returns 0 - none, 1 - indirect, 2 - negative cone, 3 - indirect negative cone
54 static Standard_Integer IsIndirectSurface (Handle(Geom_Surface) &S,
55                                            TopLoc_Location &L)
56 {
57   Standard_Integer result = 0;
58
59   Handle(Geom_Surface) TS = S;
60   while (TS->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface)))
61     TS = Handle(Geom_RectangularTrimmedSurface)::DownCast(TS)->BasisSurface();
62
63   Handle(Geom_ElementarySurface) ES = Handle(Geom_ElementarySurface)::DownCast(TS);
64   if (!ES.IsNull()) {
65     // is the surface indirect ?
66     gp_Trsf t = L.Transformation();
67     Standard_Boolean neg = t.IsNegative();
68     Standard_Boolean det = ( t.VectorialPart().Determinant() < 0.0 );
69     Standard_Boolean dir = ES->Position().Direct();
70     if ((neg != det) == dir) result = 1;
71     Handle(Geom_ConicalSurface) CS = Handle(Geom_ConicalSurface)::DownCast(ES);
72     if (!CS.IsNull()) {
73       // does the cone have negative semiangle ?
74       if ( CS->SemiAngle() < 0.0 ) result += 2;
75     }
76     if (result) S = TS;
77   }
78
79   return result;
80 }
81
82 //=======================================================================
83 //function : NewSurface
84 //purpose  : 
85 //=======================================================================
86
87 Standard_Boolean ShapeCustom_DirectModification::NewSurface (const TopoDS_Face& F,
88                                                              Handle(Geom_Surface)& S,
89                                                              TopLoc_Location& L,
90                                                              Standard_Real& Tol,
91                                                              Standard_Boolean& RevWires,
92                                                              Standard_Boolean& RevFace)
93 {
94   S = BRep_Tool::Surface(F,L);
95   
96   switch (IsIndirectSurface(S,L)) {
97     case 1: { // Indirect surface
98       // UReverse a copy of S
99       S = S->UReversed();
100       RevWires = Standard_True;
101       RevFace = Standard_True;
102       break;
103     }
104     case 2: { // Negative cone
105       // U- and VReverse a copy of S
106       S = S->VReversed();
107       S->UReverse();
108       RevWires = Standard_False;
109       RevFace = Standard_False;
110       break;
111     }
112     case 3: { // Indirect negative cone
113       // VReverse a copy of S
114       S = S->VReversed();
115       RevWires = Standard_True;
116       RevFace = Standard_True;
117       break;
118     }
119     default: return Standard_False;
120   }
121
122   SendMsg( F, Message_Msg("DirectModification.NewSurface.MSG0"));
123
124   Tol = BRep_Tool::Tolerance(F);
125
126   return Standard_True;
127 }
128
129 //=======================================================================
130 //function : NewCurve
131 //purpose  : 
132 //=======================================================================
133
134 Standard_Boolean ShapeCustom_DirectModification::NewCurve (const TopoDS_Edge& E,
135                                                            Handle(Geom_Curve)& C,
136                                                            TopLoc_Location& L,
137                                                            Standard_Real& Tol) 
138 {
139   //:p5 abv 26 Feb 99: force copying of edge if any its pcurve will be replaced
140   Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
141
142   // iterate on pcurves
143   BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
144   for ( ; itcr.More(); itcr.Next() ) {
145     Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itcr.Value());
146     if ( GC.IsNull() || ! GC->IsCurveOnSurface() ) continue;
147     Handle(Geom_Surface) S = GC->Surface();
148     TopLoc_Location Loc = GC->Location();
149     if ( ! IsIndirectSurface ( S, Loc ) ) continue;
150     Standard_Real f, l;
151     C = BRep_Tool::Curve ( E, L, f, l );
152     if ( ! C.IsNull() ) C = Handle(Geom_Curve)::DownCast ( C->Copy() );
153     Tol = BRep_Tool::Tolerance(E);
154     return Standard_True;
155   }
156   return Standard_False;
157 }
158
159 //=======================================================================
160 //function : NewPoint
161 //purpose  : 
162 //=======================================================================
163
164 Standard_Boolean ShapeCustom_DirectModification::NewPoint (const TopoDS_Vertex& /*V*/,
165                                                            gp_Pnt& /*P*/, 
166                                                            Standard_Real& /*Tol*/) 
167 {
168   // 3d points are never modified
169   return Standard_False;
170 }
171
172 //=======================================================================
173 //function : NewCurve2d
174 //purpose  : 
175 //=======================================================================
176
177 Standard_Boolean ShapeCustom_DirectModification::NewCurve2d (const TopoDS_Edge& E,
178                                                              const TopoDS_Face& F,
179                                                              const TopoDS_Edge& NewE,
180                                                              const TopoDS_Face& NewF,
181                                                              Handle(Geom2d_Curve)& C,
182                                                              Standard_Real& Tol) 
183 {
184   TopLoc_Location L;
185   Handle(Geom_Surface) S = BRep_Tool::Surface(F,L);
186
187   Standard_Integer result = IsIndirectSurface ( S, L );
188   if ( !result && E.IsSame(NewE) ) return Standard_False;
189
190   Standard_Real f, l;
191   C = BRep_Tool::CurveOnSurface( E, F, f, l );
192   Tol = BRep_Tool::Tolerance(E);
193
194   if ( result ) {
195     
196     gp_Trsf2d T;
197
198     switch (result) {
199       case 1: { // Indirect surface
200         // mirror the PCurve about the V axis
201         T.SetMirror(gp::OY2d());
202         C = Handle(Geom2d_Curve)::DownCast(C->Transformed(T));
203         break;
204       }
205       case 2: { // Negative cone
206         // mirror the PCurve about the U and V axis
207         T.SetMirror(gp::OX2d());
208         C = Handle(Geom2d_Curve)::DownCast(C->Transformed(T));
209         T.SetMirror(gp::OY2d());
210         C->Transform(T);
211         break;
212       }
213       case 3: { // Indirect negative cone
214         // mirror the PCurve about the U axis
215         T.SetMirror(gp::OX2d());
216         C = Handle(Geom2d_Curve)::DownCast(C->Transformed(T));
217         break;
218       }
219     }
220
221     //#26 rln When seam edge contains triangulations trimming is lost by BRep_Builder::UpdateEdge
222     if (BRepTools::IsReallyClosed (E, F)) {
223       //szv#4:S4163:12Mar99 SGI warning
224       TopoDS_Shape sh = NewE.Reversed();
225       Handle(Geom2d_Curve) tmp = BRep_Tool::CurveOnSurface( TopoDS::Edge(sh), NewF, f, l );
226       if (tmp.IsNull()) {
227         tmp = BRep_Tool::CurveOnSurface (E, F, f, l);
228         BRep_Builder B;
229         B.UpdateEdge (NewE, tmp, C, NewF, Tol);
230         B.Range (NewE, NewF, f, l);
231         //anyway, tmp will be removed later by BRepTools_Modifier
232       }
233     }
234   }
235   else {
236     //:p5 abv 26 Feb 99: force copying of pcurves if edge was copied
237     if ( ! C.IsNull() ) C = Handle(Geom2d_Curve)::DownCast ( C->Copy() );
238   }
239
240   return Standard_True;
241 }
242
243 //=======================================================================
244 //function : NewParameter
245 //purpose  : 
246 //=======================================================================
247
248 Standard_Boolean ShapeCustom_DirectModification::NewParameter (const TopoDS_Vertex& /*V*/,
249                                                                const TopoDS_Edge& /*E*/,
250                                                                Standard_Real& /*P*/,
251                                                                Standard_Real& /*Tol*/) 
252 {
253   return Standard_False;
254 }
255
256 //=======================================================================
257 //function : Continuity
258 //purpose  : 
259 //=======================================================================
260
261 GeomAbs_Shape ShapeCustom_DirectModification::Continuity (const TopoDS_Edge& E,
262                                                           const TopoDS_Face& F1,
263                                                           const TopoDS_Face& F2,
264                                                           const TopoDS_Edge& /*NewE*/,
265                                                           const TopoDS_Face& /*NewF1*/,
266                                                           const TopoDS_Face& /*NewF2*/) 
267 {
268   return BRep_Tool::Continuity(E,F1,F2);
269 }