summaryrefslogtreecommitdiff
path: root/src/ShapeUpgrade/ShapeUpgrade_ShapeDivide.cxx
blob: 9c801e685088f23abdd50226f6a5ee6911ae62fd (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// File:	ShapeUpgrade_ShapeDivide.cxx
// Created:	Tue Apr 27 09:37:14 1999
// Author:	Pavel DURANDIN
//		<pdn@friendox.nnov.matra-dtv.fr>
//    abv 16.06.99 returning ReShape context; processing shared subshapes in compounds
//    sln 29.11.01 Bug21: in method Perform(..) nullify location of compound's faces only if mode myConsiderLocation is on

#include <ShapeUpgrade_ShapeDivide.ixx>
#include <Precision.hxx>
#include <ShapeExtend.hxx>
#include <ShapeBuild_ReShape.hxx>
#include <TopExp_Explorer.hxx>
#include <TopExp.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopoDS_Vertex.hxx>
#include <BRep_Builder.hxx>
#include <ShapeUpgrade_WireDivide.hxx>
#include <Standard_ErrorHandler.hxx>
#include <Standard_Failure.hxx>

//=======================================================================
//function : ShapeUpgrade_ShapeDivide
//purpose  : 
//=======================================================================

ShapeUpgrade_ShapeDivide::ShapeUpgrade_ShapeDivide() : myStatus(0)
{
  myPrecision = myMinTol = Precision::Confusion();
  myMaxTol = 1; //Precision::Infinite() ?? pdn
  mySplitFaceTool = new ShapeUpgrade_FaceDivide;
  myContext = new ShapeBuild_ReShape;
  mySegmentMode = Standard_True;
  myEdgeMode = 2;
}

//=======================================================================
//function : ShapeUpgrade_ShapeDivide
//purpose  : 
//=======================================================================

ShapeUpgrade_ShapeDivide::ShapeUpgrade_ShapeDivide(const TopoDS_Shape &S) : myStatus(0)
{
  myPrecision = myMinTol = Precision::Confusion();
  myMaxTol = 1; //Precision::Infinite() ?? pdn
  mySplitFaceTool = new ShapeUpgrade_FaceDivide;
  myContext = new ShapeBuild_ReShape;
  mySegmentMode = Standard_True;
  myEdgeMode = 2;
  Init(S);
}

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

void ShapeUpgrade_ShapeDivide::Init(const TopoDS_Shape &S)
{
  myShape = S;
}

//=======================================================================
//function : Delete
//purpose  : 
//=======================================================================

void ShapeUpgrade_ShapeDivide::Delete()
{}


//=======================================================================
//function : SetPrecision
//purpose  : 
//=======================================================================

void ShapeUpgrade_ShapeDivide::SetPrecision (const Standard_Real Prec)
{
  myPrecision = Prec;
}

//=======================================================================
//function : SetMaxTolerance
//purpose  : 
//=======================================================================

void ShapeUpgrade_ShapeDivide::SetMaxTolerance (const Standard_Real maxtol)
{
  myMaxTol = maxtol;
}


//=======================================================================
//function : SetMinTolerance
//purpose  : 
//=======================================================================

void ShapeUpgrade_ShapeDivide::SetMinTolerance (const Standard_Real mintol)
{
  myMinTol = mintol;
}


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

void ShapeUpgrade_ShapeDivide::SetSurfaceSegmentMode(const Standard_Boolean Segment)
{
  mySegmentMode = Segment;
}
//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

Standard_Boolean ShapeUpgrade_ShapeDivide::Perform(const Standard_Boolean newContext)
{
  myStatus = ShapeExtend::EncodeStatus ( ShapeExtend_OK );
  if(myShape.IsNull()) {
    myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL1 );
    return Standard_False;
  }
  
  if ( newContext || myContext.IsNull() ) 
    myContext = new ShapeBuild_ReShape;

  // Process COMPOUNDs separately in order to handle sharing in assemblies
  // NOTE: not optimized: subshape can be processed twice (second time - no modif)
  if ( myShape.ShapeType() == TopAbs_COMPOUND ) {
    Standard_Integer locStatus = myStatus;
    TopoDS_Compound C;
    BRep_Builder B;
    B.MakeCompound ( C );
    TopoDS_Shape savShape = myShape;
    for ( TopoDS_Iterator it(savShape,Standard_False); it.More(); it.Next() ) {
      TopoDS_Shape shape = it.Value();
      TopLoc_Location L = shape.Location();
      if(myContext->ModeConsiderLocation()) {
        TopLoc_Location nullLoc;
        shape.Location ( nullLoc );
      }
      myShape = myContext->Apply ( shape );
      Perform ( Standard_False );
     if(myContext->ModeConsiderLocation()) 
        myResult.Location ( L );
      myResult.Orientation(TopAbs::Compose(myResult.Orientation(),savShape.Orientation()));
      B.Add ( C, myResult );
      locStatus |= myStatus;
    }
    
    myShape = savShape;
    myStatus = locStatus;
    if ( Status ( ShapeExtend_DONE ) ) {
      myResult = myContext->Apply ( C, TopAbs_SHAPE );
      myContext->Replace ( myShape, myResult );
      return Standard_True;
    }
    myResult = myShape;
    return Standard_False;
  }

  // Process FACEs
  Handle(ShapeUpgrade_FaceDivide) SplitFace = GetSplitFaceTool();
  if ( ! SplitFace.IsNull() ) {
    SplitFace->SetPrecision( myPrecision );
    SplitFace->SetMaxTolerance(myMaxTol);
    SplitFace->SetSurfaceSegmentMode(mySegmentMode);
    Handle(ShapeUpgrade_WireDivide) SplitWire = SplitFace->GetWireDivideTool();
    if ( ! SplitWire.IsNull() ) {
      SplitWire->SetMinTolerance(myMinTol);
      SplitWire->SetEdgeMode(myEdgeMode);
    }
    for(TopExp_Explorer exp(myShape,TopAbs_FACE); exp.More(); exp.Next()) {
//smh#8
      TopoDS_Shape tmpF = exp.Current().Oriented ( TopAbs_FORWARD );
      TopoDS_Face F = TopoDS::Face (tmpF); // protection against INTERNAL shapes: cts20105a.rle
      TopoDS_Shape sh = myContext->Apply ( F, TopAbs_SHAPE );
      for (TopExp_Explorer exp2(sh,TopAbs_FACE); exp2.More(); exp2.Next()) {
//szv: try-catch added
	try {
	  OCC_CATCH_SIGNALS
	  for (; exp2.More(); exp2.Next()) {
	    TopoDS_Face face = TopoDS::Face ( exp2.Current() );
	    SplitFace->Init(face);
	    SplitFace->SetContext(myContext);
	    SplitFace->Perform();
	    if(SplitFace->Status(ShapeExtend_FAIL)) {
	      myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
	    }
	    if(SplitFace->Status(ShapeExtend_DONE)) {
	      myContext->Replace(face,SplitFace->Result());
	      if(SplitFace->Status(ShapeExtend_DONE1))
		myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
	      if(SplitFace->Status(ShapeExtend_DONE2))
		myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE2 );
	    }
	  }
	}
	catch (Standard_Failure) {
	  myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL2 );
#ifdef DEB
	  cout << "\nError: Exception in ShapeUpgrade_FaceDivide::Perform(): ";
	  Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
	}
      }
    }
  }
  
  // Process free WIREs
  Handle(ShapeUpgrade_WireDivide) SplitWire = SplitFace->GetWireDivideTool();
  if ( ! SplitWire.IsNull() ) {
    SplitWire->SetFace (TopoDS_Face());
    SplitWire->SetPrecision( myPrecision );
    SplitWire->SetMaxTolerance(myMaxTol);
    SplitWire->SetMinTolerance(myMinTol);
    SplitWire->SetEdgeMode(myEdgeMode);
    TopExp_Explorer exp;//svv Jan 10 2000 : porting on DEC
    for (exp.Init (myShape, TopAbs_WIRE, TopAbs_FACE); exp.More(); exp.Next()) {
//smh#8
      TopoDS_Shape tmpW = exp.Current().Oriented ( TopAbs_FORWARD );
      TopoDS_Wire W = TopoDS::Wire (tmpW );   // protection against INTERNAL shapes
      TopoDS_Shape sh = myContext->Apply ( W, TopAbs_SHAPE );
      for (TopExp_Explorer exp2 (sh,TopAbs_WIRE); exp2.More(); exp2.Next()) {
	TopoDS_Wire wire = TopoDS::Wire ( exp2.Current() );
	SplitWire->Load (wire);
	SplitWire->SetContext(myContext);
	SplitWire->Perform();
	if(SplitWire->Status(ShapeExtend_FAIL)) {
	  myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
	}
	if(SplitWire->Status(ShapeExtend_DONE)) {
	  myContext->Replace(wire,SplitWire->Wire());
	  myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
	}
      }
    }
  
    // Process free EDGEs
    for (exp.Init (myShape, TopAbs_EDGE, TopAbs_WIRE); exp.More(); exp.Next()) {
//smh#8
      TopoDS_Shape tmpE = exp.Current().Oriented ( TopAbs_FORWARD );
      TopoDS_Edge E = TopoDS::Edge (tmpE );                                       // protection against INTERNAL shapes
      TopoDS_Vertex V1,V2;
      TopExp::Vertices(E,V2,V1);
      if( V1.IsNull() && V2.IsNull() ) continue; // skl 27.10.2004 for OCC5624
      TopoDS_Shape sh = myContext->Apply ( E, TopAbs_SHAPE );
      for (TopExp_Explorer exp2 (sh,TopAbs_EDGE); exp2.More(); exp2.Next()) {
	TopoDS_Edge edge = TopoDS::Edge ( exp2.Current() );
	SplitWire->Load (edge);
	SplitWire->SetContext(myContext);
	SplitWire->Perform();
	if(SplitWire->Status(ShapeExtend_FAIL)) {
	  myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_FAIL3 );
	}
	if(SplitWire->Status(ShapeExtend_DONE)) {
	  myContext->Replace(edge,SplitWire->Wire());
	  myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE1 );
	}
      }
    }
  }
  myResult = myContext->Apply ( myShape, TopAbs_SHAPE );
  return ! myResult.IsSame ( myShape );
}

//=======================================================================
//function : Result
//purpose  : 
//=======================================================================

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

//=======================================================================
//function : GetContext
//purpose  : 
//=======================================================================

Handle(ShapeBuild_ReShape) ShapeUpgrade_ShapeDivide::GetContext() const
{
  //if ( myContext.IsNull() ) myContext = new ShapeBuild_ReShape;
  return myContext;
}

//=======================================================================
//function : SetContext
//purpose  : 
//=======================================================================

void ShapeUpgrade_ShapeDivide::SetContext (const Handle(ShapeBuild_ReShape) &context)
{
  myContext = context;
}

//=======================================================================
//function : SetSplitFaceTool
//purpose  : 
//=======================================================================

void ShapeUpgrade_ShapeDivide::SetSplitFaceTool(const Handle(ShapeUpgrade_FaceDivide)& splitFaceTool)
{
  mySplitFaceTool = splitFaceTool;
}

//=======================================================================
//function : GetSplitFaceTool
//purpose  : 
//=======================================================================

Handle(ShapeUpgrade_FaceDivide) ShapeUpgrade_ShapeDivide::GetSplitFaceTool () const
{
  return mySplitFaceTool;
}

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

Standard_Boolean ShapeUpgrade_ShapeDivide::Status (const ShapeExtend_Status status) const
{
  return ShapeExtend::DecodeStatus ( myStatus, status );
}

//=======================================================================
//function : SetEdgeMode
//purpose  : 
//=======================================================================

 void ShapeUpgrade_ShapeDivide::SetEdgeMode(const Standard_Integer aEdgeMode) 
{
  myEdgeMode = aEdgeMode;
}