summaryrefslogtreecommitdiff
path: root/src/BOP/BOP_Refiner.cxx
blob: b44380785276c2137ee2fc19e4c87a8daa757ba1 (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
// File:	BOP_Refiner.cxx
// Created:	Mon Dec 24 12:21:48 2001
// Author:	Peter KURNEV
//		<pkv@irinox>


#include <BOP_Refiner.ixx>

#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_IndexedMapOfShape.hxx>

#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>

#include <TopoDS_Vertex.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Wire.hxx>

#include <BRep_Builder.hxx>


//=======================================================================
// function: BOP_Refiner::BOP_Refiner
// purpose: 
//=======================================================================
  BOP_Refiner::BOP_Refiner()
:
  myIsDone(Standard_False),
  myErrorStatus(1),
  myNbRemovedVertices(0),
  myNbRemovedEdges(0)
{}
//=======================================================================
// function: BOP_Refiner::BOP_Refiner
// purpose: 
//=======================================================================
  BOP_Refiner::BOP_Refiner(const TopoDS_Shape& aS)
:
  myIsDone(Standard_False),
  myErrorStatus(1),
  myNbRemovedVertices(0),
  myNbRemovedEdges(0)
{
  myShape=aS;
}

//=======================================================================
// function: SetShape
// purpose: 
//=======================================================================
  void BOP_Refiner::SetShape (const TopoDS_Shape& aS)
{
  myShape=aS;
}
//=======================================================================
// function: SetInternals
// purpose: 
//=======================================================================
  void BOP_Refiner::SetInternals (const TopTools_ListOfShape& aLS)
{
  myInternals.Clear();
  TopTools_ListIteratorOfListOfShape anIt(aLS);
  for(;  anIt.More(); anIt.Next()) {
    const TopoDS_Shape& aS=anIt.Value();
    myInternals.Append(aS);
  }
}
//=======================================================================
// function: IsDone
// purpose: 
//=======================================================================
  Standard_Boolean BOP_Refiner::IsDone() const
{
  return myIsDone;
}
//=======================================================================
// function: ErrorStatus
// purpose: 
//=======================================================================
  Standard_Integer BOP_Refiner::ErrorStatus() const
{
  return myErrorStatus;
}
//=======================================================================
// function: Shape
// purpose: 
//=======================================================================
  const TopoDS_Shape& BOP_Refiner::Shape() const
{
  return myShape;
}
//=======================================================================
// function: NbRemovedVertices
// purpose: 
//=======================================================================
  Standard_Integer BOP_Refiner::NbRemovedVertices() const
{
  return myNbRemovedVertices;
}
//=======================================================================
// function: NbRemovedEdges
// purpose: 
//=======================================================================
  Standard_Integer BOP_Refiner::NbRemovedEdges() const
{
  return myNbRemovedEdges;
}
//=======================================================================
// function: Do
// purpose: 
//=======================================================================
  void BOP_Refiner::Do()
{
  DoInternals();
}
//=======================================================================
// function: DoInternals
// purpose: 
//=======================================================================
  void BOP_Refiner::DoInternals()
{
  Standard_Integer i, aNb, aNbF, aNbE;
  BRep_Builder aBB;
  TopTools_IndexedDataMapOfShapeListOfShape aMVF, aMFV, aMVE;
  TopTools_IndexedMapOfShape aMInternals;
  //
  // 0. Source Internals
  TopTools_ListIteratorOfListOfShape anItx(myInternals);
  for(;  anItx.More(); anItx.Next()) {
    const TopoDS_Shape& aS=anItx.Value();
    aMInternals.Add(aS);
  }
  //
  // 1. Vertices
  TopExp::MapShapesAndAncestors(myShape, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
  TopExp::MapShapesAndAncestors(myShape, TopAbs_VERTEX, TopAbs_FACE, aMVF);
  
  aNb=aMVF.Extent();
  for (i=1; i<=aNb; ++i) {
    const TopoDS_Vertex& aV=TopoDS::Vertex(aMVF.FindKey(i));
    
    if (aV.Orientation()!=TopAbs_INTERNAL) {
      continue;
    }
    // how many edges attached to the vertex ?
    aNbE=aMVE.FindFromKey(aV).Extent();
    
    if (aNbE) {
      continue;
    }

    const TopTools_ListOfShape& aLF=aMVF(i);
    // how many edges attached to the vertex ?
    aNbF=aLF.Extent();
    if (aNbF==1) {
      const TopoDS_Face& aF=TopoDS::Face(aLF.First());
      
      if (aMFV.Contains(aF)) {
	TopTools_ListOfShape& aLFWithVertex=aMFV.ChangeFromKey(aF);
	aLFWithVertex.Append(aV);
      }
      else {
	TopTools_ListOfShape aLFWithVertex;
	aLFWithVertex.Append(aV);
	aMFV.Add(aF, aLFWithVertex);
      }
    }//  if (aNbF==1)
  }// for (i=1; i<=aNb; ++i)
  
  aNbF=aMFV.Extent();
  for (i=1; i<=aNbF; ++i) {
    const TopoDS_Face& aF=TopoDS::Face(aMFV.FindKey(i));
    TopoDS_Face* pF=(TopoDS_Face*)&aF;

    const TopTools_ListOfShape& aLV=aMFV(i);
    TopTools_ListIteratorOfListOfShape anIt(aLV);
    for (; anIt.More(); anIt.Next()) {
      const TopoDS_Vertex& aV=TopoDS::Vertex(anIt.Value());
      //
      if (aMInternals.Contains(aV)) {
	continue;
      }
      //
      pF->Free(Standard_True);
      aBB.Remove(*pF, aV);
      myNbRemovedVertices++;
    }
  }
  //
  // 2. Edges
  TopTools_IndexedDataMapOfShapeListOfShape aMEF, aMFE;
  TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aMEF);
  
  aNb=aMEF.Extent();
  for (i=1; i<=aNb; ++i) {
    const TopoDS_Edge& aE=TopoDS::Edge(aMEF.FindKey(i));
    if (aE.Orientation()!=TopAbs_INTERNAL) {
      continue;
    }
    const TopTools_ListOfShape& aLF=aMEF(i);
    aNbF=aLF.Extent();
    if (aNbF==1) {
      const TopoDS_Face& aF=TopoDS::Face(aLF.First());
      
      if (aMFE.Contains(aF)) {
	TopTools_ListOfShape& aLFWithEdge=aMFE.ChangeFromKey(aF);
	aLFWithEdge.Append(aE);
      }
      else {
	TopTools_ListOfShape aLFWithEdge;
	aLFWithEdge.Append(aE);
	aMFE.Add(aF, aLFWithEdge);
      }
    }//  if (aNbF==1)
  }// for (i=1; i<=aNb; ++i)
  //
  //modified by NIZNHY-PKV Wed Nov 03 14:27:22 2010f
  Standard_Boolean bFound;
  TopoDS_Iterator aItS;
  TopAbs_Orientation aOr;
  //modified by NIZNHY-PKV Wed Nov 03 14:27:24 2010t
  //
  aNbF=aMFE.Extent();
  for (i=1; i<=aNbF; ++i) {
    //modified by NIZNHY-PKV Wed Nov 03 14:29:03 2010f
    TopoDS_Face aF=TopoDS::Face(aMFE.FindKey(i));
    aOr=aF.Orientation();
    if (aOr==TopAbs_INTERNAL) {
      aF.Orientation(TopAbs_FORWARD);
    }
    //const TopoDS_Face& aF=TopoDS::Face(aMFE.FindKey(i));
    //modified by NIZNHY-PKV Wed Nov 03 14:29:06 2010t
    TopoDS_Face* pF=(TopoDS_Face*)&aF;
    
    const TopTools_ListOfShape& aLE=aMFE(i);
    TopTools_ListIteratorOfListOfShape anIt(aLE);
    for (; anIt.More(); anIt.Next()) {
      const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
      //
      if (aMInternals.Contains(aE)) {
	continue;
      }
      //
      TopTools_IndexedDataMapOfShapeListOfShape aMEW;
      TopExp::MapShapesAndAncestors(aF, TopAbs_EDGE, TopAbs_WIRE, aMEW);
      //
      if (aMEW.Contains(aE)) {
	const TopTools_ListOfShape& aLW=aMEW.FindFromKey(aE);
	TopTools_ListIteratorOfListOfShape aWIt(aLW);
	for (; aWIt.More(); aWIt.Next()) {
	  const TopoDS_Wire& aW=TopoDS::Wire(aWIt.Value());
	  //
	  //modified by NIZNHY-PKV Wed Nov 03 14:12:48 2010f
	  bFound=Standard_False;
	  aItS.Initialize(aW);
	  for(; aItS.More(); aItS.Next()) {
	    const TopoDS_Shape& aEW=aItS.Value();
	    if (aEW==aE) {
	      bFound=Standard_True;
	      break;
	    }
	  }
	  //
	  if (!bFound) {
	    continue;
	  }
	  //modified by NIZNHY-PKV Wed Nov 03 14:14:22 2010t
	  //
	  TopoDS_Wire* pW=(TopoDS_Wire*)&aW;
	  pW->Free(Standard_True);
	  //
	  TopExp_Explorer anExp(*pW, TopAbs_EDGE);
	  for (; anExp.More(); anExp.Next()) {
	    const TopoDS_Edge& anEdge=TopoDS::Edge(anExp.Current());
	    aBB.Remove(*pW, anEdge);
	    anExp.Init(*pW, TopAbs_EDGE);
	  }
	  //
	  pF->Free(Standard_True);
	  aBB.Remove(*pF, aW);
	  //modified by NIZNHY-PKV Wed Nov 03 14:29:56 2010f
	  pF->Orientation(aOr);
	  //modified by NIZNHY-PKV Wed Nov 03 14:29:59 2010t
	  myNbRemovedEdges++;
	}
      }
    }// next inernal edge 
  }// for (i=1; i<=aNbF; ++i)
  myIsDone=!myIsDone;
}
// myErrorStatus:
// 0 - OK
// 1 - Nothing has been done after constructor
//