2 #include "HYDROData_SplitToZonesTool.h"
4 #include "HYDROData_PolylineXY.h"
5 #include "HYDROData_ShapesGroup.h"
6 #include <HYDROData_Transform.h>
7 #include <BRepAlgoAPI_Cut.hxx>
8 #include <BRepAlgoAPI_Common.hxx>
9 #include <BRepBuilderAPI_MakeFace.hxx>
10 #include <BRep_Builder.hxx>
11 #include <TopExp_Explorer.hxx>
13 #include <TopoDS_Shape.hxx>
14 #include <TopoDS_Compound.hxx>
15 #include <TopoDS_Wire.hxx>
16 #include <TopoDS_Edge.hxx>
17 #include <TopoDS_Iterator.hxx>
18 #include <BRepCheck_Analyzer.hxx>
19 #include <NCollection_IncAllocator.hxx>
20 #include <BOPAlgo_Builder.hxx>
21 #include <BOPAlgo_PaveFiller.hxx>
22 #include <NCollection_Map.hxx>
23 #include <NCollection_DataMap.hxx>
24 #include <TopTools_ListOfShape.hxx>
25 #include <TopTools_ListIteratorOfListOfShape.hxx>
26 #include <TopTools_ShapeMapHasher.hxx>
28 #include <BRepGProp.hxx>
29 #include <GProp_GProps.hxx>
30 #include <Geom_Plane.hxx>
31 #include <BRepBuilderAPI_FindPlane.hxx>
34 typedef NCollection_Map<TopoDS_Shape, TopTools_ShapeMapHasher> HYDROData_MapOfShape;
35 typedef HYDROData_MapOfShape::Iterator HYDROData_MapIteratorOfMapOfShape;
36 typedef NCollection_DataMap<TopoDS_Shape, TopTools_ListOfShape, TopTools_ShapeMapHasher> HYDROData_DataMapOfShapeListOfShape;
37 typedef HYDROData_DataMapOfShapeListOfShape::Iterator HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape;
38 typedef NCollection_DataMap<TopoDS_Shape, QStringList, TopTools_ShapeMapHasher> HYDROData_DataMapOfShapeListOfString;
39 typedef HYDROData_DataMapOfShapeListOfString::Iterator HYDROData_DataMapIteratorOfDataMapOfShapeListOfString;
40 #undef _NCollection_MapHasher
42 //#define DEB_SPLIT_TO_ZONES 1
43 #ifdef DEB_SPLIT_TO_ZONES
44 #include <BRepTools.hxx>
46 TopoDS_Face HYDROData_SplitToZonesTool::SplitData::Face() const
52 if ( Shape.ShapeType() == TopAbs_FACE )
54 aResFace = TopoDS::Face( Shape );
56 else if ( Shape.ShapeType() == TopAbs_WIRE )
58 BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( Shape ), Standard_True );
60 if( aMakeFace.IsDone() )
61 aResFace = aMakeFace.Face();
68 Standard_Boolean buildLimFace(const TopoDS_Wire& theBndWire, TopoDS_Face& outFace)
71 BRepGProp::LinearProperties(theBndWire,G);
72 const gp_Pnt& aCPnt = G.CentreOfMass();
74 BRepBuilderAPI_FindPlane fndPlane (theBndWire, Precision::Confusion());
76 aPln = fndPlane.Plane()->Pln();
78 aPln = gp_Pln(aCPnt, gp::OZ().Direction());
79 BRepBuilderAPI_MakeFace aMkFace(aPln, theBndWire);
80 if(aMkFace.IsDone()) {
81 outFace = aMkFace.Face();
82 if(!outFace.IsNull()) {
83 #ifdef DEB_SPLIT_TO_ZONES
84 // BRepTools::Write(limFace,"FL.brep");
89 return Standard_False;
91 HYDROData_SplitToZonesTool::SplitDataList
92 HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects& theObjectList,
93 const HYDROData_SequenceOfObjects& theGroupsList,
94 const Handle(HYDROData_PolylineXY)& thePolyline )
96 SplitDataList anOutputSplitDataList;
97 if(theObjectList.IsEmpty()) return anOutputSplitDataList;
99 // Collect the object shapes to split. InputDataList will contain elements which will hold shape & name_of_shape.
100 SplitDataList anInputSplitDataList;
101 TCollection_AsciiString aNam("Faces_");
102 for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
104 Handle(HYDROData_Object) aGeomObj =
105 Handle(HYDROData_Object)::DownCast( theObjectList.Value( anIndex ) );
106 if( aGeomObj.IsNull() )
109 TopoDS_Shape aShape = aGeomObj->GetTopShape();
110 if ( aShape.IsNull() )
112 //TCollection_AsciiString aName = aNam + anIndex + ".brep";
113 //BRepTools::Write(aShape, aName.ToCString());
115 if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
116 // Create split data for each face contained in the compound
117 TopExp_Explorer anExp( aShape, TopAbs_FACE );
118 for ( ; anExp.More(); anExp.Next() ) {
119 const TopoDS_Face& aFace = TopoDS::Face( anExp.Current() );
120 if ( !aFace.IsNull() ) {
121 SplitData aSplitData( SplitData::Data_Zone, aFace, aGeomObj->GetName() );
122 anInputSplitDataList.append( aSplitData );
126 SplitData aSplitData( SplitData::Data_Zone, aShape, aGeomObj->GetName() );
127 anInputSplitDataList.append( aSplitData );
131 SplitDataList anInputGroupList;
132 for( int anIndex = 1; anIndex <= theGroupsList.Length(); anIndex++ )
134 Handle(HYDROData_ShapesGroup) aGeomGroup =
135 Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
136 if( aGeomGroup.IsNull() )
139 TopTools_SequenceOfShape aGroupShapes;
140 aGeomGroup->GetShapes( aGroupShapes );
141 for( int i = 1, aNbShapes = aGroupShapes.Length(); i <= aNbShapes; i++ ) {
142 const TopoDS_Shape& aGroupShape = aGroupShapes.Value( i );
143 if ( aGroupShape.IsNull() )
146 if ( aGroupShape.ShapeType() == TopAbs_COMPOUND ) {
147 TopExp_Explorer anExp( aGroupShape, TopAbs_EDGE );
148 for ( ; anExp.More(); anExp.Next() ) {
149 const TopoDS_Edge& anEdge = TopoDS::Edge( anExp.Current() );
150 if ( !anEdge.IsNull() ) {
151 SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
152 anInputGroupList.append( aSplitData );
156 SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
157 anInputGroupList.append( aSplitData );
161 // If only one shape is given we don't split it
162 // algorithm just returns the unpacked input data
163 if(theObjectList.Size() == 1) {
164 anOutputSplitDataList.append(anInputSplitDataList);
165 if(!theGroupsList.IsEmpty() )
166 anOutputSplitDataList.append(anInputGroupList);
167 return anOutputSplitDataList;
169 HYDROData_DataMapOfShapeListOfString aDM3;
170 if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
171 QStringList aListOfNames;
172 for (int i=0;i < anInputGroupList.size() ;i++) {
173 const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
174 aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);
177 // Step 1. Prepare Partition structures.
178 Handle(NCollection_BaseAllocator) pA1 = new NCollection_IncAllocator, pA2 = new NCollection_IncAllocator;
179 BOPAlgo_PaveFiller* aPaveFiller = new BOPAlgo_PaveFiller(pA1);
180 BOPAlgo_Builder* aBuilder = new BOPAlgo_Builder(pA2);
181 BOPCol_ListOfShape aLS;
183 QStringList aListOfNames;
184 TopoDS_Compound aCmp;
186 aBB.MakeCompound(aCmp);
187 for (int i=0;i < anInputSplitDataList.size() ;i++) {
188 const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
189 aDM3.Bind(aSh, anInputSplitDataList.at(i).ObjectNames);
192 //TCollection_AsciiString aName = aNam + i + ".brep";
193 //BRepTools::Write(aSh, aName.ToCString());
195 HYDROData_Transform sTool;
197 sTool.SetArgument(aCmp);
199 const Standard_Boolean bToTransform = sTool.ToTransform();
200 Standard_Integer anErr(0);
201 sTool.SetToTransform(bToTransform);
203 anErr = sTool.ErrorStatus();
205 return anOutputSplitDataList;
206 const TopoDS_Shape& aResult = sTool.Shape();
211 aPaveFiller->SetArguments(aLS);
212 aPaveFiller->Perform();
213 anErr = aPaveFiller->ErrorStatus();
215 return anOutputSplitDataList;
216 BOPDS_PDS pDS = aPaveFiller->PDS();
218 return anOutputSplitDataList;
221 // Step 2. Split faces
222 BOPCol_ListIteratorOfListOfShape anIt(aLS);
223 for (; anIt.More(); anIt.Next()) {
224 const TopoDS_Shape& aS = anIt.Value();
225 aBuilder->AddArgument(aS);
227 aBuilder->PerformWithFiller(*aPaveFiller);
228 anErr = aBuilder->ErrorStatus();
232 return anOutputSplitDataList;
233 aResult = aBuilder->Shape();
236 if (aResult.IsNull())
237 return anOutputSplitDataList;
238 BRepCheck_Analyzer aCheck (aResult);
239 if(!aCheck.IsValid()) {
240 #ifdef DEB_SPLIT_TO_ZONES
241 cout << "result is not valid" <<endl;
242 BRepTools::Write(aResult, "SplitFacesNV.brep");
244 return anOutputSplitDataList;
246 #ifdef DEB_SPLIT_TO_ZONES
247 BRepTools::Write(aResult, "SplitFacesV.brep");
250 // Step 3. Collect history
251 HYDROData_DataMapOfShapeListOfShape aDM1;
252 BOPCol_ListIteratorOfListOfShape anIt(aLS);
253 //TCollection_AsciiString aNamM ("EdgM_");
254 //TCollection_AsciiString aNamG ("EdgG_");
255 for (int i =1;anIt.More();anIt.Next(),i++) {
256 Standard_Boolean foundF(Standard_False);
257 const TopTools_ListOfShape& aListOfNew = /*aBuilder->*/sTool.Modified(anIt.Value());
258 if(!aListOfNew.IsEmpty())
259 foundF = Standard_True;
260 TopTools_ListOfShape aList;
261 TopTools_ListIteratorOfListOfShape it(aListOfNew);
262 for(;it.More();it.Next())
263 aList.Append(it.Value());
264 // Bug in History: partition should give only modified entities! => temporary solution is used
265 const TopTools_ListOfShape& aListOfGen = /*aBuilder->*/sTool.Generated(anIt.Value());
266 if(!aListOfGen.IsEmpty())
267 foundF = Standard_True;
268 it.Initialize(aListOfGen);
269 for(;it.More();it.Next())
270 aList.Append(it.Value());
271 if(!foundF) // face is not modified
272 aList.Append (anIt.Value());
273 aDM1.Bind(anIt.Value(), aList);
274 //TCollection_AsciiString aName;
275 if(!anInputGroupList.isEmpty() ) { // 1
277 TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
278 for (int j =1;exp.More();exp.Next(),j++) {
279 Standard_Boolean foundE(Standard_False);
280 const TopTools_ListOfShape& aListM = /*aBuilder->*/sTool.Modified(exp.Current());
281 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
282 if(aListM.Extent()) foundE = Standard_True;
283 it.Initialize(aListM);
284 for(int k=1;it.More();it.Next(),k++) {
285 aList.Append(it.Value());
286 //aName = aNamM + i + j +k +".brep";
287 //BRepTools::Write(it.Value(),aName.ToCString());
289 const TopTools_ListOfShape& aListG = /*aBuilder->*/sTool.Generated(exp.Current());
290 if(aListG.Extent()) foundE = Standard_True;
291 it.Initialize(aListG);
292 for(int k=1;it.More();it.Next(),k++) {
293 aList.Append(it.Value());
294 //aName = aNamG + i + j +k +".brep";
295 //BRepTools::Write(it.Value(),aName.ToCString());
297 //cout << "NB_EDGE = " << aList.Extent() <<endl;
299 aList.Append (exp.Current());
300 //aName = aNamG + i + j +".brep";
301 //BRepTools::Write(exp.Current(),aName.ToCString());
303 aDM1.Bind(exp.Current(), aList);
308 // aDM2: NewShape ==> ListOfOldShapes
309 HYDROData_DataMapOfShapeListOfShape aDM2;
310 // make limiting face
311 HYDROData_DataMapOfShapeListOfShape aDM4;
312 Standard_Boolean hasLimits(Standard_False);
313 if (! thePolyline.IsNull()) {
314 //const TopoDS_Shape aShape = thePolyline->GetShape();
315 const TopoDS_Wire aBndWire = TopoDS::Wire(thePolyline->GetShape());
316 if(!aBndWire.IsNull()) {
318 if(buildLimFace(aBndWire, limFace)) {
319 TopoDS_Shape aComResult;
320 BRepAlgoAPI_Common mkCom(aResult, limFace);
322 aComResult = mkCom.Shape();
323 BRepCheck_Analyzer aCheck (aComResult);
324 if(aCheck.IsValid()) {
325 #ifdef DEB_SPLIT_TO_ZONES
326 BRepTools::Write(aComResult,"CommonV.brep");
329 hasLimits = Standard_True; // DM2 should filled here
330 HYDROData_MapOfShape aView;
331 TopExp_Explorer exp (aResult, TopAbs_FACE);
332 for (int i =1;exp.More();exp.Next(),i++) {
333 const TopoDS_Shape& aFace = exp.Current();
334 if(!aFace.IsNull()) {
335 const TopTools_ListOfShape& aListOfNew = mkCom.Modified(aFace);
336 //cout << "Modified: " << aListOfNew.Extent() <<endl;
337 const TopTools_ListOfShape& aListOfGen = mkCom.Generated(aFace);
338 if(!aListOfNew.IsEmpty()) {
339 aDM4.Bind(aFace, aListOfNew);
340 //TCollection_AsciiString aName = aNam + i + ".brep";
341 //BRepTools::Write(aListOfNew.Last(), aName.ToCString());
344 if(!mkCom.IsDeleted(aFace)) {
345 if(!aListOfGen.IsEmpty()) {
346 aDM4.Bind(aFace, aListOfGen);
347 //TCollection_AsciiString aName = aNam + i + "g.brep";
348 //BRepTools::Write(aListOfGen.Last(), aName.ToCString());
351 TopTools_ListOfShape aList;
353 aDM4.Bind(aFace, aList); //the same face - not modified
357 TopExp_Explorer exp2 (aFace, TopAbs_EDGE);
358 for (int j =1;exp2.More();exp2.Next(),j++) {
359 const TopoDS_Shape& anEdge = exp2.Current();
360 if(!anEdge.IsNull()) {
361 if(aView.Contains(anEdge)) continue;
363 const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);
364 const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
365 if(!aListOfNewEd.IsEmpty())
366 aDM4.Bind(anEdge, aListOfNewEd);
368 if(!mkCom.IsDeleted(anEdge)) {
369 if(!aListOfGenEd.IsEmpty()) {
370 aDM4.Bind(anEdge, aListOfGenEd);
372 TopTools_ListOfShape aList;
373 aList.Append(anEdge);
374 aDM4.Bind(anEdge, aList);//the same edge - not modified
381 } //end DM4 filling (phase 1)
382 //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
383 // phase 2 (from tool)
384 TopExp_Explorer expt (limFace, TopAbs_EDGE);
385 for(;expt.More();expt.Next()) {
386 const TopoDS_Shape& anEdge = expt.Current();
387 if(!anEdge.IsNull()) {
388 const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);
389 const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
390 if(!aListOfNewEd.IsEmpty())
391 aDM4.Bind(anEdge, aListOfNewEd);
393 if(!mkCom.IsDeleted(anEdge)) {
394 if(!aListOfGenEd.IsEmpty()) {
395 aDM4.Bind(anEdge, aListOfGenEd);
397 TopTools_ListOfShape aList;
398 aList.Append(anEdge);
399 aDM4.Bind(anEdge, aList);//the same edge - not modified
405 //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
406 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
407 //DM4 contains Old - New after common op. DM1: old - new after Split op.
408 for(;aMIt.More();aMIt.Next()) {
409 const TopoDS_Shape& aKey = aMIt.Key();
410 TopTools_ListOfShape aList;
412 const TopTools_ListOfShape& aListOfNew = aMIt.Value();
413 TopTools_ListIteratorOfListOfShape it(aListOfNew);
414 for(;it.More();it.Next()) {// iterate new: b1, b2, b3...
415 if(!aDM4.IsBound(it.Value())) // bi - is deleted
416 continue; // go to the next bi
418 const TopTools_ListOfShape& aListOfNew4 = aDM4.Find(it.Value());
419 TopTools_ListIteratorOfListOfShape it4(aListOfNew4); // {c1, c2, c3,...}
420 for(;it4.More();it4.Next()) {
421 if(!aDM2.IsBound(it4.Value()))
422 aDM2.Bind(it4.Value(), aList);
424 TopTools_ListOfShape& aList = aDM2.ChangeFind(it4.Value());
432 hasLimits = Standard_False;
433 #ifdef DEB_SPLIT_TO_ZONES
434 BRepTools::Write(aComResult,"CommonNV.brep");
442 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
443 for(;aMIt.More();aMIt.Next()) {
444 const TopoDS_Shape& aKey = aMIt.Key();
445 TopTools_ListOfShape aList;
447 const TopTools_ListOfShape& aListOfNew = aMIt.Value();
448 TopTools_ListIteratorOfListOfShape it(aListOfNew);
449 for(;it.More();it.Next()) {
450 if(!aDM2.IsBound(it.Value()))
451 aDM2.Bind(it.Value(), aList);
453 TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
459 // Step 4. Fill output structure.
460 //TCollection_AsciiString aNam4 ("SC_");
462 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
463 for(int i =1;aMIt.More();aMIt.Next(),i++) {
464 SplitData aDestSplitData;
465 const TopoDS_Shape& aKey = aMIt.Key(); //new
466 aDestSplitData.Shape = aKey;
467 if(aKey.ShapeType() == TopAbs_FACE)
468 aDestSplitData.Type = SplitData::Data_Zone;
470 aDestSplitData.Type = SplitData::Data_Edge;
471 #ifdef DEB_SPLIT_TO_ZONES
472 //TCollection_AsciiString aName = aNam4 + i + ".brep";
473 //BRepTools::Write(aKey,aName.ToCString());
477 QStringList aListOfNames; // names processing
478 const TopTools_ListOfShape& aListOfOld = aMIt.Value();
479 TopTools_ListIteratorOfListOfShape it(aListOfOld);
480 for(;it.More();it.Next()) {
481 const TopoDS_Shape& aSh = it.Value(); //old
482 if(aDM3.IsBound(aSh)) {
483 const QStringList& ObjectNames = aDM3.Find(aSh);
484 aListOfNames.append(ObjectNames);
488 aDestSplitData.ObjectNames = aListOfNames;
489 anOutputSplitDataList.append(aDestSplitData);
492 return anOutputSplitDataList;
495 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
496 const SplitData& theData2,
497 SplitData& theData1Subtracted,
498 SplitData& theData2Subtracted,
499 SplitData& theDataIntersected )
501 const TopoDS_Shape& aShape1 = theData1.Shape;
502 const TopoDS_Shape& aShape2 = theData2.Shape;
504 const QStringList& anObjectNames1 = theData1.ObjectNames;
505 const QStringList& anObjectNames2 = theData2.ObjectNames;
507 BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
508 TopoDS_Shape aCommonShape = aCommon.Shape();
509 if( aCommonShape.IsNull() )
511 theData1Subtracted = theData1;
512 theData2Subtracted = theData2;
516 BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
517 TopoDS_Shape aCut1Shape = aCut1.Shape();
519 BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
520 TopoDS_Shape aCut2Shape = aCut2.Shape();
522 theData1Subtracted = SplitData( SplitData::Data_Zone, aCut1Shape, anObjectNames1 );
523 theData2Subtracted = SplitData( SplitData::Data_Zone, aCut2Shape, anObjectNames2 );
524 theDataIntersected = SplitData( SplitData::Data_Zone, aCommonShape, anObjectNames1 + anObjectNames2 );
529 HYDROData_SplitToZonesTool::SplitDataList
530 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
532 SplitDataList aSplitDataList;
533 TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
534 for( ; anExp.More(); anExp.Next() )
536 TopoDS_Shape aShape = anExp.Current();
537 if( aShape.ShapeType() == TopAbs_FACE )
539 TopoDS_Face aFace = TopoDS::Face( aShape );
540 if( !aFace.IsNull() )
542 SplitData aSplitData( SplitData::Data_Zone, aFace, theData.ObjectNames );
543 aSplitDataList.append( aSplitData );
547 return aSplitDataList;