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 // make little extension to ensure all pcurves fit inside new surface bounds
137 Standard_Real aSUf, aSUl, aSVf, aSVl;
138 surf->Bounds(aSUf, aSUl, aSVf, aSVl);
139 if (!surf->IsUPeriodic()) {
140 Standard_Real dU = (Ul - Uf) * 0.01;
141 if (Uf > aSUf) Uf -= Min(dU, Uf - aSUf);
142 if (Ul < aSUl) Ul += Min(dU, aSUl - Ul);
144 if (!surf->IsVPeriodic()) {
145 Standard_Real dV = (Vl - Vf) * 0.01;
146 if (Vf > aSVf) Vf -= Min(dV, Vf - aSVf);
147 if (Vl < aSVl) Vl += Min(dV, aSVl - Vl);
150 SplitSurf->Init ( surf, Uf, Ul, Vf, Vl );
151 SplitSurf->Perform(mySegmentMode);
153 // If surface was neither splitted nor modified, do nothing
154 if ( ! SplitSurf->Status ( ShapeExtend_DONE ) ) return Standard_False;
156 // if surface was modified, force copying all vertices (and edges as consequence)
157 // to protect original shape from increasing tolerance after SameParameter
158 if ( SplitSurf->Status ( ShapeExtend_DONE3 ) )
159 for (TopExp_Explorer exp(face,TopAbs_VERTEX); exp.More(); exp.Next() ) {
160 if ( Context()->IsRecorded ( exp.Current() ) ) continue;
162 TopoDS_Shape emptyCopied = exp.Current().EmptyCopied();
163 TopoDS_Vertex V = TopoDS::Vertex ( emptyCopied );
164 Context()->Replace ( exp.Current(), V );
167 Handle(ShapeExtend_CompositeSurface) Grid = SplitSurf->ResSurfaces();
169 ShapeFix_ComposeShell CompShell;
170 CompShell.Init( Grid, L, face, Precision() );
171 CompShell.SetContext(Context());
172 CompShell.SetMaxTolerance(MaxTolerance());
173 Handle(ShapeUpgrade_WireDivide) SplitWire = GetWireDivideTool();
174 if ( ! SplitWire.IsNull() )
175 CompShell.SetTransferParamTool(GetWireDivideTool()->GetTransferParamTool());
177 if ( CompShell.Status ( ShapeExtend_FAIL ) ||
178 ! CompShell.Status ( ShapeExtend_DONE ) )
179 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
181 myResult = CompShell.Result();
182 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
184 return Standard_True;
187 //=======================================================================
188 //function : SplitCurves
190 //=======================================================================
192 Standard_Boolean ShapeUpgrade_FaceDivide::SplitCurves ()
194 Handle(ShapeUpgrade_WireDivide) SplitWire = GetWireDivideTool();
195 if ( SplitWire.IsNull() ) return Standard_False;
197 SplitWire->SetMaxTolerance(MaxTolerance());
198 for ( TopExp_Explorer explf(myResult,TopAbs_FACE); explf.More(); explf.Next()) {
199 TopoDS_Shape S = Context()->Apply ( explf.Current(), TopAbs_SHAPE);
201 // S should be face; else return with FAIL
202 if ( S.IsNull() || S.ShapeType() != TopAbs_FACE ) {
203 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
204 return Standard_False;
206 TopoDS_Face F = TopoDS::Face ( S );
208 SplitWire->SetFace ( F );
209 for ( TopoDS_Iterator wi(F,Standard_False); wi.More(); wi.Next() ) {
210 //TopoDS_Wire wire = TopoDS::Wire ( wi.Value() );
211 // modifications already defined in context are to be applied inside SplitWire
212 if(wi.Value().ShapeType() !=TopAbs_WIRE)
214 TopoDS_Wire wire = TopoDS::Wire(wi.Value());
215 SplitWire->Load ( wire );
216 SplitWire->SetContext(Context());
217 SplitWire->Perform ();
218 if ( SplitWire->Status ( ShapeExtend_FAIL ) )
219 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
220 if ( SplitWire->Status ( ShapeExtend_DONE ) ) {
221 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
222 Context()->Replace ( wire, SplitWire->Wire() );
226 myResult = Context()->Apply ( myResult );
227 return Status ( ShapeExtend_DONE );
230 //=======================================================================
233 //=======================================================================
235 TopoDS_Shape ShapeUpgrade_FaceDivide::Result () const
240 //=======================================================================
243 //=======================================================================
245 Standard_Boolean ShapeUpgrade_FaceDivide::Status (const ShapeExtend_Status status) const
247 return ShapeExtend::DecodeStatus ( myStatus, status );
250 //=======================================================================
251 //function : SetSplitSurfaceTool
253 //=======================================================================
255 void ShapeUpgrade_FaceDivide::SetSplitSurfaceTool(const Handle(ShapeUpgrade_SplitSurface)& splitSurfaceTool)
257 mySplitSurfaceTool = splitSurfaceTool;
260 //=======================================================================
261 //function : SetWireDivideTool
263 //=======================================================================
265 void ShapeUpgrade_FaceDivide::SetWireDivideTool(const Handle(ShapeUpgrade_WireDivide)& wireDivideTool)
267 myWireDivideTool = wireDivideTool;
270 //=======================================================================
271 //function : GetSplitSurfaceTool
273 //=======================================================================
275 Handle(ShapeUpgrade_SplitSurface) ShapeUpgrade_FaceDivide::GetSplitSurfaceTool () const
277 return mySplitSurfaceTool;
280 //=======================================================================
281 //function : GetWireDivideTool
283 //=======================================================================
285 Handle(ShapeUpgrade_WireDivide) ShapeUpgrade_FaceDivide::GetWireDivideTool () const
287 return myWireDivideTool;