summaryrefslogtreecommitdiff
path: root/src/ShapeUpgrade/ShapeUpgrade_FaceDivide.cxx
blob: 1b41866f3d5f59989bc9b1de05b24ed474b91ffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// File:	ShapeUpgrade_FaceDivide.cxx
// Created:	Mon Apr 26 17:29:46 1999
// Author:	Andrey BETENEV
//		<abv@doomox.nnov.matra-dtv.fr>
//    gka  01.06.99 S4205: changing order of splitting surface/curves for converting to bezier

#include <ShapeUpgrade_FaceDivide.ixx>
#include <Precision.hxx>
#include <ShapeExtend.hxx>
#include <ShapeBuild_Edge.hxx>
#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Iterator.hxx>
#include <ShapeFix_ComposeShell.hxx>
#include <BRepTools.hxx>
#include <ShapeExtend_CompositeSurface.hxx>
#include <TopExp_Explorer.hxx>
#include <Bnd_Box2d.hxx>
#include <ShapeAnalysis.hxx>
#include <ShapeAnalysis_Edge.hxx>
#include <ShapeBuild_ReShape.hxx>

//=======================================================================
//function : ShapeUpgrade_FaceDivide
//purpose  : 
//=======================================================================

ShapeUpgrade_FaceDivide::ShapeUpgrade_FaceDivide():
       ShapeUpgrade_Tool(), myStatus(0)
{
  mySegmentMode = Standard_True;
  mySplitSurfaceTool = new ShapeUpgrade_SplitSurface;
  myWireDivideTool = new ShapeUpgrade_WireDivide;
}

//=======================================================================
//function : ShapeUpgrade_FaceDivide
//purpose  : 
//=======================================================================

ShapeUpgrade_FaceDivide::ShapeUpgrade_FaceDivide (const TopoDS_Face &F):
       ShapeUpgrade_Tool(), myStatus(0)
{
  mySegmentMode = Standard_True;
  mySplitSurfaceTool = new ShapeUpgrade_SplitSurface;
  myWireDivideTool = new ShapeUpgrade_WireDivide;
  Init ( F );
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void ShapeUpgrade_FaceDivide::Init (const TopoDS_Face &F)
{
  myResult = myFace = F;
}
  

//=======================================================================
//function : SetSurfaceSegmentMode
//purpose  : 
//=======================================================================

void ShapeUpgrade_FaceDivide::SetSurfaceSegmentMode(const Standard_Boolean Segment)
{
  mySegmentMode = Segment;
}


//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

Standard_Boolean ShapeUpgrade_FaceDivide::Perform ()
{
  myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
  if ( myFace.IsNull() ) return Standard_False;
  myResult = myFace;
  SplitSurface();
  SplitCurves();
  return Status ( ShapeExtend_DONE );
 } 

//=======================================================================
//function : SplitSurface
//purpose  : 
//=======================================================================

Standard_Boolean ShapeUpgrade_FaceDivide::SplitSurface ()  
{  
  Handle(ShapeUpgrade_SplitSurface) SplitSurf = GetSplitSurfaceTool();
  if ( SplitSurf.IsNull() ) return Standard_False;

  // myResult should be face; else return with FAIL
  if ( myResult.IsNull() || myResult.ShapeType() != TopAbs_FACE ) {
    myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
    return Standard_False;
  }
  TopoDS_Face face = TopoDS::Face ( myResult );
  
  TopLoc_Location L;
  Handle(Geom_Surface) surf;
  surf = BRep_Tool::Surface ( face, L );
  
  Standard_Real Uf,Ul,Vf,Vl;
//  BRepTools::UVBounds(myFace,Uf,Ul,Vf,Vl);
  ShapeAnalysis::GetFaceUVBounds ( face, Uf, Ul, Vf, Vl );
  if(Precision::IsInfinite(Uf) || Precision::IsInfinite(Ul) ||
     Precision::IsInfinite(Vf) || Precision::IsInfinite(Vl))
    return Standard_False;
       
  SplitSurf->Init ( surf, Uf, Ul, Vf, Vl );
  SplitSurf->Perform(mySegmentMode);

  // If surface was neither splitted nor modified, do nothing
  if ( ! SplitSurf->Status ( ShapeExtend_DONE ) ) return Standard_False;
    
  // if surface was modified, force copying all vertices (and edges as consequence)
  // to protect original shape from increasing tolerance after SameParameter
  if ( SplitSurf->Status ( ShapeExtend_DONE3 ) )
    for (TopExp_Explorer exp(face,TopAbs_VERTEX); exp.More(); exp.Next() ) {
      if ( Context()->IsRecorded ( exp.Current() ) ) continue;
//smh#8
      TopoDS_Shape emptyCopied = exp.Current().EmptyCopied();
      TopoDS_Vertex V = TopoDS::Vertex ( emptyCopied );
      Context()->Replace ( exp.Current(), V );
    }
    
  Handle(ShapeExtend_CompositeSurface) Grid = SplitSurf->ResSurfaces();
  
  ShapeFix_ComposeShell CompShell;
  CompShell.Init( Grid, L, face, Precision() );
  CompShell.SetContext(Context());
  CompShell.SetMaxTolerance(MaxTolerance());
  Handle(ShapeUpgrade_WireDivide) SplitWire = GetWireDivideTool();
  if ( ! SplitWire.IsNull() )
       CompShell.SetTransferParamTool(GetWireDivideTool()->GetTransferParamTool());
  CompShell.Perform();
  if ( CompShell.Status ( ShapeExtend_FAIL ) || 
       ! CompShell.Status ( ShapeExtend_DONE ) ) 
    myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );

  myResult = CompShell.Result();
  myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );

  return Standard_True;
}
 
//=======================================================================
//function : SplitCurves
//purpose  : 
//=======================================================================

Standard_Boolean ShapeUpgrade_FaceDivide::SplitCurves ()
{
  Handle(ShapeUpgrade_WireDivide) SplitWire = GetWireDivideTool();
  if ( SplitWire.IsNull() ) return Standard_False;
  
  SplitWire->SetMaxTolerance(MaxTolerance());
  for ( TopExp_Explorer explf(myResult,TopAbs_FACE); explf.More(); explf.Next()) {
    TopoDS_Shape S = Context()->Apply ( explf.Current(), TopAbs_SHAPE);
    
    // S should be face; else return with FAIL
    if ( S.IsNull() || S.ShapeType() != TopAbs_FACE ) {
      myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
      return Standard_False;
    }
    TopoDS_Face F = TopoDS::Face ( S );
    
    SplitWire->SetFace ( F );
    for ( TopoDS_Iterator wi(F,Standard_False); wi.More(); wi.Next() ) {
      //TopoDS_Wire wire = TopoDS::Wire ( wi.Value() );
      // modifications already defined in context are to be applied inside SplitWire
      if(wi.Value().ShapeType() !=TopAbs_WIRE)
	continue;
      TopoDS_Wire wire = TopoDS::Wire(wi.Value());
      SplitWire->Load ( wire );
      SplitWire->SetContext(Context());
      SplitWire->Perform ();
      if ( SplitWire->Status ( ShapeExtend_FAIL ) )
	myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
      if ( SplitWire->Status ( ShapeExtend_DONE ) ) {
	myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
	Context()->Replace ( wire, SplitWire->Wire() );
      }
    }
  }
  myResult = Context()->Apply ( myResult );
  return Status ( ShapeExtend_DONE );
}

//=======================================================================
//function : Shell
//purpose  : 
//=======================================================================

TopoDS_Shape ShapeUpgrade_FaceDivide::Result () const
{
  return myResult;
}

//=======================================================================
//function : Status
//purpose  : 
//=======================================================================

Standard_Boolean ShapeUpgrade_FaceDivide::Status (const ShapeExtend_Status status) const
{
  return ShapeExtend::DecodeStatus ( myStatus, status );
}
    
//=======================================================================
//function : SetSplitSurfaceTool
//purpose  : 
//=======================================================================

void ShapeUpgrade_FaceDivide::SetSplitSurfaceTool(const Handle(ShapeUpgrade_SplitSurface)& splitSurfaceTool)
{
  mySplitSurfaceTool = splitSurfaceTool;
}

//=======================================================================
//function : SetWireDivideTool
//purpose  : 
//=======================================================================

void ShapeUpgrade_FaceDivide::SetWireDivideTool(const Handle(ShapeUpgrade_WireDivide)& wireDivideTool)
{
  myWireDivideTool = wireDivideTool;
}

//=======================================================================
//function : GetSplitSurfaceTool
//purpose  : 
//=======================================================================

Handle(ShapeUpgrade_SplitSurface) ShapeUpgrade_FaceDivide::GetSplitSurfaceTool () const
{
  return mySplitSurfaceTool;
}
	
//=======================================================================
//function : GetWireDivideTool
//purpose  : 
//=======================================================================

Handle(ShapeUpgrade_WireDivide) ShapeUpgrade_FaceDivide::GetWireDivideTool () const
{
  return myWireDivideTool;
}