0030895: Coding Rules - specify std namespace explicitly for std::cout and streams
[occt.git] / src / ShapeBuild / ShapeBuild_ReShape.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 //    abv 28.04.99 S4137: ading method Apply for work on all types of shapes
15
16 #include <BRep_Builder.hxx>
17 #include <BRep_Tool.hxx>
18 #include <ShapeBuild_Edge.hxx>
19 #include <ShapeBuild_ReShape.hxx>
20 #include <ShapeExtend.hxx>
21 #include <Standard_Type.hxx>
22 #include <TopExp_Explorer.hxx>
23 #include <TopoDS.hxx>
24 #include <TopoDS_Compound.hxx>
25 #include <TopoDS_Iterator.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <TopoDS_Shell.hxx>
28 #include <TopoDS_Solid.hxx>
29
30 IMPLEMENT_STANDARD_RTTIEXT(ShapeBuild_ReShape,BRepTools_ReShape)
31
32 //=======================================================================
33 //function : ShapeBuild_ReShape
34 //purpose  : 
35 //=======================================================================
36 ShapeBuild_ReShape::ShapeBuild_ReShape()
37 {
38 }
39
40 //=======================================================================
41 //function : Apply
42 //purpose  : 
43 //=======================================================================
44
45 TopoDS_Shape ShapeBuild_ReShape::Apply (const TopoDS_Shape& shape,
46                                         const TopAbs_ShapeEnum until,
47                                         const Standard_Integer buildmode) 
48 {
49   if (shape.IsNull()) return shape;
50   TopoDS_Shape newsh;
51   if (Status (shape,newsh,Standard_False) != 0) return newsh;
52
53   TopAbs_ShapeEnum st = shape.ShapeType();
54   if (st == until) return newsh;    // critere d arret
55
56   Standard_Integer modif = 0;
57   if (st == TopAbs_COMPOUND || st == TopAbs_COMPSOLID) {
58     BRep_Builder B;
59     TopoDS_Compound C;
60     B.MakeCompound (C);
61     for (TopoDS_Iterator it (shape); it.More(); it.Next()) {
62       TopoDS_Shape sh = it.Value();
63       Standard_Integer stat = Status (sh,newsh,Standard_False);
64       if (stat != 0) modif = 1;
65       if (stat >= 0) B.Add (C,newsh);
66     }
67     if (modif == 0) return shape;
68     return C;
69   }
70
71   if (st == TopAbs_SOLID) {
72     BRep_Builder B;
73     TopoDS_Compound C;
74     B.MakeCompound (C);
75     TopoDS_Solid S;
76     B.MakeSolid (S);
77     for (TopoDS_Iterator it (shape); it.More(); it.Next()) {
78       TopoDS_Shape sh = it.Value();
79       newsh = Apply (sh,until,buildmode);
80       if (newsh.IsNull()) {
81         modif = -1;
82       } 
83       else if (newsh.ShapeType() != TopAbs_SHELL) {
84         Standard_Integer nbsub = 0;
85         for (TopExp_Explorer exh(newsh,TopAbs_SHELL); exh.More(); exh.Next()) {
86           TopoDS_Shape onesh = exh.Current ();
87           B.Add (S,onesh);
88           nbsub ++;
89         }
90         if (nbsub == 0) modif = -1;
91         B.Add (C,newsh);  // c est tout
92       } 
93       else {
94         if (modif == 0 && !sh.IsEqual(newsh)) modif = 1;
95         B.Add (C,newsh);
96         B.Add (S,newsh);
97       }
98     }
99
100     if ( (modif < 0 && buildmode < 2) || (modif == 0 && buildmode < 1) )
101       return C;
102     else
103       return S;
104   }
105
106   if (st == TopAbs_SHELL) {
107     BRep_Builder B;
108     TopoDS_Compound C;
109     B.MakeCompound (C);
110     TopoDS_Shell S;
111     B.MakeShell (S);
112     for (TopoDS_Iterator it (shape); it.More(); it.Next()) {
113       TopoDS_Shape sh = it.Value();
114       newsh = Apply (sh,until,buildmode);
115       if (newsh.IsNull()) {
116         modif = -1;
117       } 
118       else if (newsh.ShapeType() != TopAbs_FACE) {
119         Standard_Integer nbsub = 0;
120         for (TopExp_Explorer exf(newsh,TopAbs_FACE); exf.More(); exf.Next()) {
121           TopoDS_Shape onesh = exf.Current ();
122           B.Add (S,onesh);
123           nbsub ++;
124         }
125         if (nbsub == 0) modif = -1;
126         B.Add (C,newsh);  // c est tout
127       } 
128       else {
129         if (modif == 0 && !sh.IsEqual(newsh)) modif = 1;
130         B.Add (C,newsh);
131         B.Add (S,newsh);
132       }
133     }
134     if ( (modif < 0 && buildmode < 2) || (modif == 0 && buildmode < 1) )
135       return C;
136     else
137     {
138       S.Closed (BRep_Tool::IsClosed (S));
139       return S;
140     }
141   }
142   std::cout<<"BRepTools_ReShape::Apply NOT YET IMPLEMENTED"<<std::endl;
143   return shape;
144 }
145
146 //=======================================================================
147 //function : Apply
148 //purpose  : 
149 //=======================================================================
150
151 TopoDS_Shape ShapeBuild_ReShape::Apply (const TopoDS_Shape& shape,
152                                         const TopAbs_ShapeEnum until) 
153 {
154   myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
155   if ( shape.IsNull() ) return shape;
156
157   // apply direct replacement
158   TopoDS_Shape newsh = Value ( shape );
159   
160   // if shape removed, return NULL
161   if ( newsh.IsNull() ) {
162     myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
163     return newsh;
164   }
165   
166   // if shape replaced, apply modifications to the result recursively 
167   Standard_Boolean aConsLoc = ModeConsiderLocation();
168   if ( (aConsLoc && ! newsh.IsPartner (shape)) || 
169       (!aConsLoc &&! newsh.IsSame ( shape )) )
170   {
171     TopoDS_Shape res = Apply ( newsh, until );
172     myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
173     return res;
174   }
175
176   TopAbs_ShapeEnum st = shape.ShapeType();
177   if ( st >= until ) return newsh;    // critere d arret
178   if(st == TopAbs_VERTEX || st == TopAbs_SHAPE)
179     return shape;
180   // define allowed types of components
181
182   BRep_Builder B;
183   
184   TopoDS_Shape result = shape.EmptyCopied();
185   TopAbs_Orientation orient = shape.Orientation(); //JR/Hp: or -> orient
186   result.Orientation(TopAbs_FORWARD); // protect against INTERNAL or EXTERNAL shapes
187   Standard_Boolean modif = Standard_False;
188   Standard_Integer locStatus = myStatus;
189   
190   // apply recorded modifications to subshapes
191   for ( TopoDS_Iterator it(shape,Standard_False); it.More(); it.Next() ) {
192     TopoDS_Shape sh = it.Value();
193     newsh = Apply ( sh, until );
194     if ( newsh != sh ) {
195       if ( ShapeExtend::DecodeStatus ( myStatus, ShapeExtend_DONE4 ) )
196         locStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
197       modif = 1;
198     }
199     if ( newsh.IsNull() ) {
200       locStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
201       continue;
202     }
203     locStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
204     if ( st == TopAbs_COMPOUND || newsh.ShapeType() == sh.ShapeType()) { //fix for SAMTECH bug OCC322 about abcense internal vertices after sewing.
205       B.Add ( result, newsh );
206       continue;
207     }
208     Standard_Integer nitems = 0;
209     for ( TopoDS_Iterator subit(newsh); subit.More(); subit.Next(), nitems++ ) {
210       TopoDS_Shape subsh = subit.Value();
211       if ( subsh.ShapeType() == sh.ShapeType() ) B.Add ( result, subsh );
212       else locStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
213     }
214     if ( ! nitems ) locStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
215   }
216   if ( ! modif ) return shape;
217
218   // restore Range on edge broken by EmptyCopied()
219   if ( st == TopAbs_EDGE ) {
220     ShapeBuild_Edge sbe;
221     sbe.CopyRanges ( TopoDS::Edge ( result ), TopoDS::Edge ( shape ));
222   }
223   else if (st == TopAbs_WIRE || st == TopAbs_SHELL)
224     result.Closed (BRep_Tool::IsClosed (result));
225   result.Orientation(orient);
226   myStatus = locStatus;
227
228   replace(shape, result,
229     result.IsNull() ? TReplacementKind_Remove : TReplacementKind_Modify);
230
231   return result;
232 }
233
234 //=======================================================================
235 //function : Status
236 //purpose  : 
237 //=======================================================================
238
239 Standard_Integer ShapeBuild_ReShape::Status(const TopoDS_Shape& ashape,
240                                             TopoDS_Shape& newsh,
241                                             const Standard_Boolean last) 
242 {
243   return BRepTools_ReShape::Status(ashape,newsh,last);
244 }
245
246 //=======================================================================
247 //function : Status
248 //purpose  : 
249 //=======================================================================
250
251 Standard_Boolean ShapeBuild_ReShape::Status (const ShapeExtend_Status status) const
252 {
253   return ShapeExtend::DecodeStatus ( myStatus, status );
254 }