1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 // abv 28.04.99 S4137: ading method Apply for work on all types of shapes
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>
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>
30 IMPLEMENT_STANDARD_RTTIEXT(ShapeBuild_ReShape,BRepTools_ReShape)
32 //=======================================================================
33 //function : ShapeBuild_ReShape
35 //=======================================================================
36 ShapeBuild_ReShape::ShapeBuild_ReShape()
40 //=======================================================================
43 //=======================================================================
45 TopoDS_Shape ShapeBuild_ReShape::Apply (const TopoDS_Shape& shape,
46 const TopAbs_ShapeEnum until,
47 const Standard_Integer buildmode)
49 if (shape.IsNull()) return shape;
51 if (Status (shape,newsh,Standard_False) != 0) return newsh;
53 TopAbs_ShapeEnum st = shape.ShapeType();
54 if (st == until) return newsh; // critere d arret
56 Standard_Integer modif = 0;
57 if (st == TopAbs_COMPOUND || st == TopAbs_COMPSOLID) {
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);
67 if (modif == 0) return shape;
71 if (st == TopAbs_SOLID) {
77 for (TopoDS_Iterator it (shape); it.More(); it.Next()) {
78 TopoDS_Shape sh = it.Value();
79 newsh = Apply (sh,until,buildmode);
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 ();
90 if (nbsub == 0) modif = -1;
91 B.Add (C,newsh); // c est tout
94 if (modif == 0 && !sh.IsEqual(newsh)) modif = 1;
100 if ( (modif < 0 && buildmode < 2) || (modif == 0 && buildmode < 1) )
106 if (st == TopAbs_SHELL) {
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()) {
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 ();
125 if (nbsub == 0) modif = -1;
126 B.Add (C,newsh); // c est tout
129 if (modif == 0 && !sh.IsEqual(newsh)) modif = 1;
134 if ( (modif < 0 && buildmode < 2) || (modif == 0 && buildmode < 1) )
138 S.Closed (BRep_Tool::IsClosed (S));
142 std::cout<<"BRepTools_ReShape::Apply NOT YET IMPLEMENTED"<<std::endl;
146 //=======================================================================
149 //=======================================================================
151 TopoDS_Shape ShapeBuild_ReShape::Apply (const TopoDS_Shape& shape,
152 const TopAbs_ShapeEnum until)
154 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
155 if ( shape.IsNull() ) return shape;
157 // apply direct replacement
158 TopoDS_Shape newsh = Value ( shape );
160 // if shape removed, return NULL
161 if ( newsh.IsNull() ) {
162 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
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 )) )
171 TopoDS_Shape res = Apply ( newsh, until );
172 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
176 TopAbs_ShapeEnum st = shape.ShapeType();
177 if ( st >= until ) return newsh; // critere d arret
178 if(st == TopAbs_VERTEX || st == TopAbs_SHAPE)
180 // define allowed types of components
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;
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 );
195 if ( ShapeExtend::DecodeStatus ( myStatus, ShapeExtend_DONE4 ) )
196 locStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
199 if ( newsh.IsNull() ) {
200 locStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE4 );
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 );
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 );
214 if ( ! nitems ) locStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
216 if ( ! modif ) return shape;
218 // restore Range on edge broken by EmptyCopied()
219 if ( st == TopAbs_EDGE ) {
221 sbe.CopyRanges ( TopoDS::Edge ( result ), TopoDS::Edge ( shape ));
223 else if (st == TopAbs_WIRE || st == TopAbs_SHELL)
224 result.Closed (BRep_Tool::IsClosed (result));
225 result.Orientation(orient);
226 myStatus = locStatus;
228 replace(shape, result,
229 result.IsNull() ? TReplacementKind_Remove : TReplacementKind_Modify);
234 //=======================================================================
237 //=======================================================================
239 Standard_Integer ShapeBuild_ReShape::Status(const TopoDS_Shape& ashape,
241 const Standard_Boolean last)
243 return BRepTools_ReShape::Status(ashape,newsh,last);
246 //=======================================================================
249 //=======================================================================
251 Standard_Boolean ShapeBuild_ReShape::Status (const ShapeExtend_Status status) const
253 return ShapeExtend::DecodeStatus ( myStatus, status );