1 // Created on: 1999-04-26
2 // Created by: Andrey BETENEV
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // gka 01.06.99 S4205: changing order of splitting surface/curves for converting to bezier
19 #include <Bnd_Box2d.hxx>
20 #include <BRep_Builder.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepTools.hxx>
23 #include <Precision.hxx>
24 #include <ShapeAnalysis.hxx>
25 #include <ShapeAnalysis_Edge.hxx>
26 #include <ShapeBuild_Edge.hxx>
27 #include <ShapeBuild_ReShape.hxx>
28 #include <ShapeExtend.hxx>
29 #include <ShapeExtend_CompositeSurface.hxx>
30 #include <ShapeFix_ComposeShell.hxx>
31 #include <ShapeUpgrade_FaceDivide.hxx>
32 #include <ShapeUpgrade_SplitSurface.hxx>
33 #include <ShapeUpgrade_WireDivide.hxx>
34 #include <Standard_Type.hxx>
35 #include <TopExp_Explorer.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <TopoDS_Iterator.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Vertex.hxx>
43 IMPLEMENT_STANDARD_RTTIEXT(ShapeUpgrade_FaceDivide,ShapeUpgrade_Tool)
45 //=======================================================================
46 //function : ShapeUpgrade_FaceDivide
48 //=======================================================================
49 ShapeUpgrade_FaceDivide::ShapeUpgrade_FaceDivide():
50 ShapeUpgrade_Tool(), myStatus(0)
52 mySegmentMode = Standard_True;
53 mySplitSurfaceTool = new ShapeUpgrade_SplitSurface;
54 myWireDivideTool = new ShapeUpgrade_WireDivide;
57 //=======================================================================
58 //function : ShapeUpgrade_FaceDivide
60 //=======================================================================
62 ShapeUpgrade_FaceDivide::ShapeUpgrade_FaceDivide (const TopoDS_Face &F):
63 ShapeUpgrade_Tool(), myStatus(0)
65 mySegmentMode = Standard_True;
66 mySplitSurfaceTool = new ShapeUpgrade_SplitSurface;
67 myWireDivideTool = new ShapeUpgrade_WireDivide;
71 //=======================================================================
74 //=======================================================================
76 void ShapeUpgrade_FaceDivide::Init (const TopoDS_Face &F)
78 myResult = myFace = F;
82 //=======================================================================
83 //function : SetSurfaceSegmentMode
85 //=======================================================================
87 void ShapeUpgrade_FaceDivide::SetSurfaceSegmentMode(const Standard_Boolean Segment)
89 mySegmentMode = Segment;
93 //=======================================================================
96 //=======================================================================
98 Standard_Boolean ShapeUpgrade_FaceDivide::Perform ()
100 myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
101 if ( myFace.IsNull() ) return Standard_False;
105 return Status ( ShapeExtend_DONE );
108 //=======================================================================
109 //function : SplitSurface
111 //=======================================================================
113 Standard_Boolean ShapeUpgrade_FaceDivide::SplitSurface ()
115 Handle(ShapeUpgrade_SplitSurface) SplitSurf = GetSplitSurfaceTool();
116 if ( SplitSurf.IsNull() ) return Standard_False;
118 // myResult should be face; else return with FAIL
119 if ( myResult.IsNull() || myResult.ShapeType() != TopAbs_FACE ) {
120 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
121 return Standard_False;
123 TopoDS_Face face = TopoDS::Face ( myResult );
126 Handle(Geom_Surface) surf;
127 surf = BRep_Tool::Surface ( face, L );
129 Standard_Real Uf,Ul,Vf,Vl;
130 // BRepTools::UVBounds(myFace,Uf,Ul,Vf,Vl);
131 ShapeAnalysis::GetFaceUVBounds ( face, Uf, Ul, Vf, Vl );
132 if(Precision::IsInfinite(Uf) || Precision::IsInfinite(Ul) ||
133 Precision::IsInfinite(Vf) || Precision::IsInfinite(Vl))
134 return Standard_False;
136 SplitSurf->Init ( surf, Uf, Ul, Vf, Vl );
137 SplitSurf->Perform(mySegmentMode);
139 // If surface was neither splitted nor modified, do nothing
140 if ( ! SplitSurf->Status ( ShapeExtend_DONE ) ) return Standard_False;
142 // if surface was modified, force copying all vertices (and edges as consequence)
143 // to protect original shape from increasing tolerance after SameParameter
144 if ( SplitSurf->Status ( ShapeExtend_DONE3 ) )
145 for (TopExp_Explorer exp(face,TopAbs_VERTEX); exp.More(); exp.Next() ) {
146 if ( Context()->IsRecorded ( exp.Current() ) ) continue;
148 TopoDS_Shape emptyCopied = exp.Current().EmptyCopied();
149 TopoDS_Vertex V = TopoDS::Vertex ( emptyCopied );
150 Context()->Replace ( exp.Current(), V );
153 Handle(ShapeExtend_CompositeSurface) Grid = SplitSurf->ResSurfaces();
155 ShapeFix_ComposeShell CompShell;
156 CompShell.Init( Grid, L, face, Precision() );
157 CompShell.SetContext(Context());
158 CompShell.SetMaxTolerance(MaxTolerance());
159 Handle(ShapeUpgrade_WireDivide) SplitWire = GetWireDivideTool();
160 if ( ! SplitWire.IsNull() )
161 CompShell.SetTransferParamTool(GetWireDivideTool()->GetTransferParamTool());
163 if ( CompShell.Status ( ShapeExtend_FAIL ) ||
164 ! CompShell.Status ( ShapeExtend_DONE ) )
165 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
167 myResult = CompShell.Result();
168 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
170 return Standard_True;
173 //=======================================================================
174 //function : SplitCurves
176 //=======================================================================
178 Standard_Boolean ShapeUpgrade_FaceDivide::SplitCurves ()
180 Handle(ShapeUpgrade_WireDivide) SplitWire = GetWireDivideTool();
181 if ( SplitWire.IsNull() ) return Standard_False;
183 SplitWire->SetMaxTolerance(MaxTolerance());
184 for ( TopExp_Explorer explf(myResult,TopAbs_FACE); explf.More(); explf.Next()) {
185 TopoDS_Shape S = Context()->Apply ( explf.Current(), TopAbs_SHAPE);
187 // S should be face; else return with FAIL
188 if ( S.IsNull() || S.ShapeType() != TopAbs_FACE ) {
189 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
190 return Standard_False;
192 TopoDS_Face F = TopoDS::Face ( S );
194 SplitWire->SetFace ( F );
195 for ( TopoDS_Iterator wi(F,Standard_False); wi.More(); wi.Next() ) {
196 //TopoDS_Wire wire = TopoDS::Wire ( wi.Value() );
197 // modifications already defined in context are to be applied inside SplitWire
198 if(wi.Value().ShapeType() !=TopAbs_WIRE)
200 TopoDS_Wire wire = TopoDS::Wire(wi.Value());
201 SplitWire->Load ( wire );
202 SplitWire->SetContext(Context());
203 SplitWire->Perform ();
204 if ( SplitWire->Status ( ShapeExtend_FAIL ) )
205 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
206 if ( SplitWire->Status ( ShapeExtend_DONE ) ) {
207 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
208 Context()->Replace ( wire, SplitWire->Wire() );
212 myResult = Context()->Apply ( myResult );
213 return Status ( ShapeExtend_DONE );
216 //=======================================================================
219 //=======================================================================
221 TopoDS_Shape ShapeUpgrade_FaceDivide::Result () const
226 //=======================================================================
229 //=======================================================================
231 Standard_Boolean ShapeUpgrade_FaceDivide::Status (const ShapeExtend_Status status) const
233 return ShapeExtend::DecodeStatus ( myStatus, status );
236 //=======================================================================
237 //function : SetSplitSurfaceTool
239 //=======================================================================
241 void ShapeUpgrade_FaceDivide::SetSplitSurfaceTool(const Handle(ShapeUpgrade_SplitSurface)& splitSurfaceTool)
243 mySplitSurfaceTool = splitSurfaceTool;
246 //=======================================================================
247 //function : SetWireDivideTool
249 //=======================================================================
251 void ShapeUpgrade_FaceDivide::SetWireDivideTool(const Handle(ShapeUpgrade_WireDivide)& wireDivideTool)
253 myWireDivideTool = wireDivideTool;
256 //=======================================================================
257 //function : GetSplitSurfaceTool
259 //=======================================================================
261 Handle(ShapeUpgrade_SplitSurface) ShapeUpgrade_FaceDivide::GetSplitSurfaceTool () const
263 return mySplitSurfaceTool;
266 //=======================================================================
267 //function : GetWireDivideTool
269 //=======================================================================
271 Handle(ShapeUpgrade_WireDivide) ShapeUpgrade_FaceDivide::GetWireDivideTool () const
273 return myWireDivideTool;