summaryrefslogtreecommitdiff
path: root/src/BOP/BOP_WireShape.cxx
blob: ae4bbff39e50514c70a43c541d744aae6fa3da7a (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
342
// File:	BOP_WireShape.cxx
// Created:	Mon Feb  4 12:02:05 2002
// Author:	Peter KURNEV
//		<pkv@irinox>

#include <BOP_WireShape.ixx>

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

#include <TopAbs_ShapeEnum.hxx>
#include <TopAbs_Orientation.hxx>

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

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

#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>

#include <BOPTools_DSFiller.hxx>
#include <BOPTools_PaveFiller.hxx>
#include <BOPTools_SplitShapesPool.hxx>
#include <BOPTools_PaveBlock.hxx>
#include <BOPTools_ListOfPaveBlock.hxx>
#include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
#include <BOPTools_CommonBlockPool.hxx>
#include <BOPTools_CommonBlock.hxx>
#include <BOPTools_ListOfCommonBlock.hxx>
#include <BOPTools_ListIteratorOfListOfCommonBlock.hxx>

#include <BOP_CorrectTolerances.hxx>
#include <BOP_BuilderTools.hxx>

#include <BooleanOperations_ShapesDataStructure.hxx>
#include <BooleanOperations_StateOfShape.hxx>

#include <BOP_BuilderTools.hxx>
#include <BOP_ConnexityBlock.hxx>
#include <BOP_ListOfConnexityBlock.hxx>
#include <BOP_ListIteratorOfListOfConnexityBlock.hxx>


static
  Standard_Integer InOrOut(const TopoDS_Vertex& ,
			   const TopoDS_Edge& );
static
  TopAbs_Orientation Orientation(const TopoDS_Vertex& ,
				 const TopoDS_Edge& );
static
  void OrientEdgesOnWire(const TopoDS_Wire& , 
			 TopoDS_Wire& );

//=======================================================================
// function: BOP_WireShape::BOP_WireShape
// purpose: 
//=======================================================================
  BOP_WireShape::BOP_WireShape()
{
}
//=======================================================================
// function: MakeResult
// purpose: 
//=======================================================================
  void BOP_WireShape::MakeResult()
{
  BRep_Builder aBB;
  TopoDS_Compound aCompound;

  aBB.MakeCompound(aCompound);
  
  TopoDS_Wire aWNew;
  BOP_ListOfConnexityBlock aLCB;
  BOP_BuilderTools::MakeConnexityBlocks(myLS, TopAbs_EDGE, aLCB);
  BOP_ListIteratorOfListOfConnexityBlock aLCBIt(aLCB);
  for (; aLCBIt.More(); aLCBIt.Next()) {
    const BOP_ConnexityBlock& aCB=aLCBIt.Value();
    const TopTools_ListOfShape& aLE=aCB.Shapes();
    TopoDS_Wire aW;
    aBB.MakeWire(aW);
    TopTools_ListIteratorOfListOfShape anIt(aLE);
    for (; anIt.More(); anIt.Next()) {
      const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
      aBB.Add(aW, aE);
    }
    OrientEdgesOnWire(aW, aWNew);
    aBB.Add(aCompound, aWNew);
  }
  myResult=aCompound;
}

//=======================================================================
// function: AddSplitPartsINOUT
// purpose: 
//=======================================================================
  void BOP_WireShape::AddSplitPartsINOUT()
{
  const BooleanOperations_ShapesDataStructure& aDS=myDSFiller->DS();
  const BOPTools_PaveFiller& aPaveFiller=myDSFiller->PaveFiller();
  const BOPTools_SplitShapesPool& aSplitShapesPool=aPaveFiller.SplitShapesPool();
  //
  Standard_Integer i, aNbPB, iRank, nSp, iBeg, iEnd;
  TopAbs_ShapeEnum aType, aTypeArg1, aTypeArg2;
  BooleanOperations_StateOfShape aState, aStateCmp;
  //
  aTypeArg1=aDS.Object().ShapeType();
  aTypeArg2=aDS.Tool().ShapeType();
  
  iBeg=1;
  iEnd=aDS.NumberOfShapesOfTheObject();

  if (aTypeArg1!=TopAbs_WIRE && aTypeArg2==TopAbs_WIRE) {
    iBeg=iEnd+1;
    iEnd=aDS.NumberOfSourceShapes();
  }
  else if (aTypeArg1==TopAbs_WIRE && aTypeArg2==TopAbs_WIRE){
    iBeg=1;
    iEnd=aDS.NumberOfSourceShapes();
  }
  //

  for (i=iBeg; i<=iEnd; ++i) {
    aType=aDS.GetShapeType(i);
    if (aType!=TopAbs_EDGE) {
      continue;
    }
    const TopoDS_Edge& aE=TopoDS::Edge(aDS.Shape(i));
    
    iRank=aDS.Rank(i);
    aStateCmp=BOP_BuilderTools::StateToCompare(iRank, myOperation);

    const BOPTools_ListOfPaveBlock& aLPB=aSplitShapesPool(aDS.RefEdge(i));
    aNbPB=aLPB.Extent();
    //
    if (!aNbPB) {
      aState=aDS.GetState(i);
      if (aState==aStateCmp) {
	myLS.Append(aE);
      }
    }
    //
    else {
      BOPTools_ListIteratorOfListOfPaveBlock aPBIt(aLPB);
      for (; aPBIt.More(); aPBIt.Next()) {
	const BOPTools_PaveBlock& aPB=aPBIt.Value();
	nSp=aPB.Edge();
	const TopoDS_Edge& aSS=TopoDS::Edge(aDS.Shape(nSp));
	aState=aDS.GetState(nSp);
	if (aState==aStateCmp) {
	  myLS.Append(aSS);
	}
      }
    }
  }
}

//=======================================================================
// function: AddSplitPartsON
// purpose: 
//=======================================================================
  void BOP_WireShape::AddSplitPartsON()
{
  if (myOperation==BOP_CUT || myOperation==BOP_CUT21) {
    return;
  }

  const BooleanOperations_ShapesDataStructure& aDS=myDSFiller->DS();
  const BOPTools_PaveFiller& aPaveFiller=myDSFiller->PaveFiller();
  const BOPTools_CommonBlockPool& aCommonBlockPool=aPaveFiller.CommonBlockPool();

  Standard_Integer i, nSpTaken, iBeg, iEnd;
  TopAbs_ShapeEnum aType, aTypeArg1, aTypeArg2;
  BOPTools_ListIteratorOfListOfCommonBlock anItCB;
  //
  aTypeArg1=aDS.Object().ShapeType();
  aTypeArg2=aDS.Tool().ShapeType();
  
  iBeg=1;
  iEnd=aDS.NumberOfShapesOfTheObject();
  if (aTypeArg1!=TopAbs_WIRE && aTypeArg2==TopAbs_WIRE) {
    iBeg=iEnd+1;
    iEnd=aDS.NumberOfSourceShapes();
  }
  else if (aTypeArg1==TopAbs_WIRE && aTypeArg2==TopAbs_WIRE){
    iEnd=aDS.NumberOfSourceShapes();
  }
  //
  for (i=iBeg; i<=iEnd; ++i) {
    aType=aDS.GetShapeType(i);
    if (aType!=TopAbs_EDGE) {
      continue;
    }
    //
    const BOPTools_ListOfCommonBlock& aLCB=aCommonBlockPool(aDS.RefEdge(i));

    anItCB.Initialize(aLCB);
    for (; anItCB.More(); anItCB.Next()) {
      BOPTools_CommonBlock& aCB=anItCB.Value();
      const BOPTools_PaveBlock& aPB=aCB.PaveBlock1();
      nSpTaken=aPB.Edge();
      const TopoDS_Edge& aSS=TopoDS::Edge(aDS.Shape(nSpTaken));
      myLS.Append(aSS);
    }
  }
}

//=======================================================================
// function: OrientEdgesOnWire
// purpose: 
//=======================================================================
void OrientEdgesOnWire(const TopoDS_Wire& aWire, 
		       TopoDS_Wire& aWireNew)
{
  Standard_Integer i, aNbV, aNbE, j, iCnt, iInOrOut, aNbRest;

  TopTools_IndexedDataMapOfShapeListOfShape aVEMap;
  TopTools_IndexedMapOfShape aProcessedEdges, aRestEdges, aEMap;
  TopTools_ListIteratorOfListOfShape anIt;
  BRep_Builder aBB;

  aBB.MakeWire(aWireNew);
  
  TopExp::MapShapesAndAncestors(aWire, TopAbs_VERTEX, TopAbs_EDGE, aVEMap);
  
  aNbV=aVEMap.Extent();
  //
  // Do
  for (i=1; i<=aNbV; i++) {
    const TopoDS_Vertex& aV=TopoDS::Vertex(aVEMap.FindKey(i));
    
    const TopTools_ListOfShape& aLE=aVEMap.FindFromIndex(i);
    aNbE=aLE.Extent();
    
    if (aNbE>=2) {
      iCnt=0;
      anIt.Initialize(aLE);
      for(; anIt.More(); anIt.Next()) {
	const TopoDS_Edge& aE=TopoDS::Edge(anIt.Value());
	if (aProcessedEdges.Contains(aE)) {
	  iInOrOut=InOrOut(aV, aE);
	  iCnt+=iInOrOut;
	}
	else {
	  aRestEdges.Add(aE);
	}
      }

      TopoDS_Edge* pE;
      aNbRest=aRestEdges.Extent();
      for (j=1; j<=aNbRest; j++) {
	const TopoDS_Edge& aE=TopoDS::Edge(aRestEdges(j));
	pE=(TopoDS_Edge*)&aE;
	
	iInOrOut=InOrOut(aV, aE);
	if (iCnt>0) {
	  if (iInOrOut>0) {
	    pE->Reverse();
	  }
	  --iCnt;
	}
	else if (iCnt<=0){
	  if (iInOrOut<0) {
	    pE->Reverse();
	  }
	  ++iCnt;
	}
	aProcessedEdges.Add(*pE);
      }
    }//if (aNbE>=2) 
  }
  //
  //
  aNbE=aProcessedEdges.Extent();
  for (i=1; i<=aNbE; i++) {
    const TopoDS_Edge& aE=TopoDS::Edge(aProcessedEdges(i));
    aBB.Add(aWireNew, aE);
  }

  TopExp::MapShapes(aWire, TopAbs_EDGE, aEMap);
  
  aNbE=aEMap.Extent();
  for (i=1; i<=aNbE; i++) {
    const TopoDS_Edge& aE=TopoDS::Edge(aEMap(i));
    if (!aProcessedEdges.Contains(aE)) {
      aProcessedEdges.Add(aE);
      aBB.Add(aWireNew, aE);
    }
  }
}

//=======================================================================
//function : Orientation
//purpose  :
//=======================================================================
  TopAbs_Orientation Orientation(const TopoDS_Vertex& aV,
				 const TopoDS_Edge& aE)
{
  TopAbs_Orientation anOr=TopAbs_INTERNAL;

  TopExp_Explorer anExp;
  anExp.Init(aE, TopAbs_VERTEX);
  for (; anExp.More(); anExp.Next()) {
    const TopoDS_Vertex& aVE1=TopoDS::Vertex(anExp.Current());
    if (aVE1.IsSame(aV)) {
      anOr=aVE1.Orientation();
      break;
    }
  }
  return anOr;
}

///=======================================================================
//function : InOrOut
//purpose  :
//=======================================================================
  Standard_Integer InOrOut(const TopoDS_Vertex& aV,
			   const TopoDS_Edge& aE)
{
  TopAbs_Orientation anOrV, anOrE;
  anOrV=aV.Orientation();
  anOrE=aE.Orientation();
  if (anOrV==TopAbs_INTERNAL){
    return 0;
  }
  
  anOrV=Orientation(aV, aE);
  
  if (anOrV==anOrE) {
    return -1; // escape
  }
  else {
    return 1; // entry
  }
}