0032717: Modeling Algorithms - BRepBuilderAPI_Copy should not remove triangulation...
[occt.git] / src / BRepBuilderAPI / BRepBuilderAPI_Copy.cxx
1 // Created on: 1994-12-12
2 // Created by: Jacques GOUSSARD
3 // Copyright (c) 1994-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_Tool.hxx>
19 #include <BRepBuilderAPI_Copy.hxx>
20 #include <BRepTools_Modification.hxx>
21 #include <Geom2d_Curve.hxx>
22 #include <Geom_Curve.hxx>
23 #include <Geom_Surface.hxx>
24 #include <gp_Pnt.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopoDS_Vertex.hxx>
27 #include <Poly_Triangulation.hxx>
28
29 namespace {
30
31 //! Tool class implementing necessary functionality for copying geometry
32 class BRepBuilderAPI_Copy_Modification : public BRepTools_Modification 
33 {
34 public:
35   BRepBuilderAPI_Copy_Modification (const Standard_Boolean copyGeom,
36                                     const Standard_Boolean copyMesh)
37     : myCopyGeom(copyGeom),
38       myCopyMesh(copyMesh)
39   {
40   }
41
42   //! Returns true to indicate the need to copy face;
43   //! copies surface if requested
44   Standard_Boolean NewSurface (const TopoDS_Face& F, Handle(Geom_Surface)& S,
45                                TopLoc_Location& L, Standard_Real& Tol, 
46                                Standard_Boolean& RevWires, Standard_Boolean& RevFace) Standard_OVERRIDE
47   {
48     S = BRep_Tool::Surface(F,L);
49     Tol = BRep_Tool::Tolerance(F);
50     RevWires = RevFace = Standard_False;
51
52     if ( ! S.IsNull() && myCopyGeom )
53       S = Handle(Geom_Surface)::DownCast(S->Copy());
54
55     return Standard_True;
56   }
57
58   //! Returns true to indicate the need to copy triangulation;
59   //! copies it if required
60   Standard_Boolean NewTriangulation(const TopoDS_Face& F, Handle(Poly_Triangulation)& T) Standard_OVERRIDE
61   {
62     if (!myCopyMesh
63       && BRep_Tool::IsGeometric (F))
64     {
65       return Standard_False;
66     }
67
68     TopLoc_Location L;
69     T = BRep_Tool::Triangulation(F, L);
70
71     if (T.IsNull())
72       return Standard_False;
73
74     // mesh is copied if and only if the geometry need to be copied too
75     if (myCopyGeom)
76       T = T->Copy();
77     return Standard_True;
78   }
79
80   //! Returns true to indicate the need to copy edge;
81   //! copies curves if requested
82   Standard_Boolean NewCurve (const TopoDS_Edge& E, Handle(Geom_Curve)& C,
83                              TopLoc_Location& L, Standard_Real& Tol) Standard_OVERRIDE
84   {
85     Standard_Real f,l;
86     C = BRep_Tool::Curve (E, L, f, l);
87     Tol = BRep_Tool::Tolerance(E);
88
89     if ( ! C.IsNull() && myCopyGeom )
90       C = Handle(Geom_Curve)::DownCast(C->Copy());
91
92     return Standard_True;
93   }
94
95   //! Returns true to indicate the need to copy polygon;
96   //! copies it if required
97   Standard_Boolean NewPolygon(const TopoDS_Edge& E, Handle(Poly_Polygon3D)& P) Standard_OVERRIDE
98   {
99     if (!myCopyMesh
100       && BRep_Tool::IsGeometric (E))
101     {
102       return Standard_False;
103     }
104
105     TopLoc_Location aLoc;
106     P = BRep_Tool::Polygon3D(E, aLoc);
107
108     if (P.IsNull())
109       return Standard_False;
110
111     // polygon is copied if and only if the geometry need to be copied too
112     if (myCopyGeom)
113       P = P->Copy();
114     return Standard_True;
115   }
116
117   //! Returns true to indicate the need to copy polygon;
118   //! copies it if required
119   Standard_Boolean NewPolygonOnTriangulation(const TopoDS_Edge& E, const TopoDS_Face& F,
120                                              Handle(Poly_PolygonOnTriangulation)& P) Standard_OVERRIDE
121   {
122     if (!myCopyMesh
123       && BRep_Tool::IsGeometric (E))
124     {
125       return Standard_False;
126     }
127
128     TopLoc_Location aLoc;
129     Handle(Poly_Triangulation) aTria = BRep_Tool::Triangulation(F, aLoc);
130     P = BRep_Tool::PolygonOnTriangulation(E, aTria, aLoc);
131
132     if (P.IsNull())
133       return Standard_False;
134
135     // polygon is copied if and only if the geometry need to be copied too
136     if (myCopyGeom)
137       P = P->Copy();
138     return Standard_True;
139   }
140
141   //! Returns true to indicate the need to copy vertex
142   Standard_Boolean NewPoint (const TopoDS_Vertex& V, gp_Pnt& P,
143                              Standard_Real& Tol) Standard_OVERRIDE
144   {
145     P = BRep_Tool::Pnt(V);
146     Tol = BRep_Tool::Tolerance(V);
147     return Standard_True;
148   }
149
150   //! Returns true to indicate the need to copy edge;
151   //! copies pcurve if requested
152   Standard_Boolean NewCurve2d (const TopoDS_Edge& E, 
153                                const TopoDS_Face& F,
154                                const TopoDS_Edge& /*NewE*/,
155                                const TopoDS_Face& /*NewF*/,
156                                Handle(Geom2d_Curve)& C, 
157                                Standard_Real& Tol) Standard_OVERRIDE
158   {
159     Tol = BRep_Tool::Tolerance(E);
160     Standard_Real f, l;
161     C = BRep_Tool::CurveOnSurface (E, F, f, l);
162
163     if ( ! C.IsNull() && myCopyGeom )
164       C = Handle(Geom2d_Curve)::DownCast (C->Copy());
165
166     return Standard_True;
167   }
168
169   //! Returns true to indicate the need to copy vertex
170   Standard_Boolean NewParameter (const TopoDS_Vertex& V, const TopoDS_Edge& E,
171                                  Standard_Real& P, Standard_Real& Tol) Standard_OVERRIDE
172   {
173     if (V.IsNull()) return Standard_False; // infinite edge may have Null vertex
174
175     Tol = BRep_Tool::Tolerance(V);
176     P = BRep_Tool::Parameter (V, E);
177
178     return Standard_True;
179   }
180
181   //! Returns the  continuity of E between F1 and F2
182   GeomAbs_Shape Continuity (const TopoDS_Edge& E, const TopoDS_Face& F1,
183                             const TopoDS_Face& F2, const TopoDS_Edge&,
184                             const TopoDS_Face&, const TopoDS_Face&) Standard_OVERRIDE
185   {
186     return BRep_Tool::Continuity (E, F1, F2);
187   }
188
189 public:
190   DEFINE_STANDARD_RTTI_INLINE(BRepBuilderAPI_Copy_Modification,BRepTools_Modification)
191
192 private: 
193   Standard_Boolean myCopyGeom;
194   Standard_Boolean myCopyMesh;
195 };
196
197 } // anonymous namespace
198
199 //=======================================================================
200 //function : BRepBuilderAPI_Copy
201 //purpose  : 
202 //=======================================================================
203
204 BRepBuilderAPI_Copy::BRepBuilderAPI_Copy ()
205 {
206   myModification = new BRepBuilderAPI_Copy_Modification(Standard_True, Standard_False);
207 }
208
209
210 //=======================================================================
211 //function : BRepBuilderAPI_Copy
212 //purpose  : 
213 //=======================================================================
214
215 BRepBuilderAPI_Copy::BRepBuilderAPI_Copy(const TopoDS_Shape& S, const Standard_Boolean copyGeom, const Standard_Boolean copyMesh)
216 {
217   myModification = new BRepBuilderAPI_Copy_Modification(copyGeom, copyMesh);
218   DoModif(S);
219 }
220
221
222 //=======================================================================
223 //function : Perform
224 //purpose  : 
225 //=======================================================================
226
227 void BRepBuilderAPI_Copy::Perform(const TopoDS_Shape& S, const Standard_Boolean copyGeom, const Standard_Boolean copyMesh)
228 {
229   myModification = new BRepBuilderAPI_Copy_Modification(copyGeom, copyMesh);
230   NotDone(); // on force la copie si on vient deja d`en faire une
231   DoModif(S);
232 }
233