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 <BRepCheck_Analyzer.hxx>
18 #include <TopTools_ListOfShape.hxx>
19 #include <TopTools_ListIteratorOfListOfShape.hxx>
21 #include <BRepGProp.hxx>
22 #include <GProp_GProps.hxx>
23 #include <Geom_Plane.hxx>
24 #include <BRepBuilderAPI_FindPlane.hxx>
26 //#define DEB_SPLIT_TO_ZONES 1
27 //#define DEB_SPLIT_TO_ZONES_CHECK_PARTITION 1
28 #if (defined (DEB_SPLIT_TO_ZONES) || defined(DEB_SPLIT_TO_ZONES_CHECK_PARTITION))
29 #include <BRepTools.hxx>
30 static TCollection_AsciiString fileNameBefore("BeforeTranslation");
33 TopoDS_Face HYDROData_SplitToZonesTool::SplitData::Face() const
39 if ( Shape.ShapeType() == TopAbs_FACE )
41 aResFace = TopoDS::Face( Shape );
43 else if ( Shape.ShapeType() == TopAbs_WIRE )
45 BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( Shape ), Standard_True );
47 if( aMakeFace.IsDone() )
48 aResFace = aMakeFace.Face();
55 Standard_Boolean buildLimFace(const TopoDS_Wire& theBndWire, TopoDS_Face& outFace)
58 BRepGProp::LinearProperties(theBndWire,G);
59 const gp_Pnt& aCPnt = G.CentreOfMass();
61 BRepBuilderAPI_FindPlane fndPlane (theBndWire, Precision::Confusion());
63 aPln = fndPlane.Plane()->Pln();
65 aPln = gp_Pln(aCPnt, gp::OZ().Direction());
66 BRepBuilderAPI_MakeFace aMkFace(aPln, theBndWire);
67 if(aMkFace.IsDone()) {
68 outFace = aMkFace.Face();
69 if(!outFace.IsNull()) {
70 #ifdef DEB_SPLIT_TO_ZONES
71 // BRepTools::Write(limFace,"FL.brep");
76 return Standard_False;
78 //======================================================================================
80 void HYDROData_SplitToZonesTool::SetFileNames(const QString& theNameBefore, const QString& theNameAfter)
82 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
83 if(!theNameBefore.isNull() && !theNameBefore.isEmpty())
85 TCollection_AsciiString aName (theNameBefore.toStdString().data());
86 fileNameBefore = aName;
88 if(!theNameAfter.isNull() && !theNameAfter.isEmpty())
90 TCollection_AsciiString aName (theNameAfter.toStdString().data());
91 HYDROData_Transform::SetFileName (aName);
95 //======================================================================================
96 Standard_Integer HYDROData_SplitToZonesTool::SplitFaces(const TopoDS_Compound& theComp,
97 HYDROData_Transform& theTool)
100 theTool.SetArgument(theComp);
102 const Standard_Boolean bToTransform = theTool.ToTransform();
103 theTool.SetToTransform(bToTransform);
105 Standard_Integer anErr = theTool.ErrorStatus();
109 //======================================================================================
110 HYDROData_SplitToZonesTool::SplitDataList
111 HYDROData_SplitToZonesTool::Split( const HYDROData_SequenceOfObjects& theObjectList,
112 const HYDROData_SequenceOfObjects& theGroupsList,
113 const Handle(HYDROData_PolylineXY)& thePolyline )
115 SplitDataList anOutputSplitDataList;
116 if(theObjectList.IsEmpty()) return anOutputSplitDataList;
118 // Collect the object shapes to split. InputDataList will contain elements which will hold shape & name_of_shape.
119 SplitDataList anInputSplitDataList;
120 //TCollection_AsciiString aNam("Faces_");
121 for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
123 Handle(HYDROData_Object) aGeomObj =
124 Handle(HYDROData_Object)::DownCast( theObjectList.Value( anIndex ) );
125 if( aGeomObj.IsNull() )
128 TopoDS_Shape aShape = aGeomObj->GetTopShape();
129 if ( aShape.IsNull() )
131 //TCollection_AsciiString aName = aNam + anIndex + ".brep";
132 //BRepTools::Write(aShape, aName.ToCString());
134 if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
135 // Create split data for each face contained in the compound
136 TopExp_Explorer anExp( aShape, TopAbs_FACE );
137 for ( ; anExp.More(); anExp.Next() ) {
138 const TopoDS_Face& aFace = TopoDS::Face( anExp.Current() );
139 if ( !aFace.IsNull() ) {
140 SplitData aSplitData( SplitData::Data_Zone, aFace, aGeomObj->GetName() );
141 anInputSplitDataList.append( aSplitData );
145 SplitData aSplitData( SplitData::Data_Zone, aShape, aGeomObj->GetName() );
146 anInputSplitDataList.append( aSplitData );
150 SplitDataList anInputGroupList;
151 for( int anIndex = 1, aLength = theGroupsList.Length(); anIndex <=aLength; anIndex++ )
153 Handle(HYDROData_ShapesGroup) aGeomGroup =
154 Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
155 if( aGeomGroup.IsNull() )
158 TopTools_SequenceOfShape aGroupShapes;
159 aGeomGroup->GetShapes( aGroupShapes );
160 for( int i = 1, aNbShapes = aGroupShapes.Length(); i <= aNbShapes; i++ ) {
161 const TopoDS_Shape& aGroupShape = aGroupShapes.Value( i );
162 if ( aGroupShape.IsNull() )
165 if ( aGroupShape.ShapeType() == TopAbs_COMPOUND ) {
166 TopExp_Explorer anExp( aGroupShape, TopAbs_EDGE );
167 for ( ; anExp.More(); anExp.Next() ) {
168 const TopoDS_Edge& anEdge = TopoDS::Edge( anExp.Current() );
169 if ( !anEdge.IsNull() ) {
170 SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
171 anInputGroupList.append( aSplitData );
175 SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
176 anInputGroupList.append( aSplitData );
177 #ifdef DEB_SPLIT_TO_ZONES
178 QString aStr = aSplitData.ObjectNames.join(" ");
179 cout << "Group# = " <<anIndex <<" Nmb in grp = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
184 // If only one shape is given we don't split it
185 // algorithm just returns the unpacked input data
186 bool limplus1Object(false);
187 if(theObjectList.Size() == 1 ) {
188 if(thePolyline.IsNull()) {
189 anOutputSplitDataList.append(anInputSplitDataList);
190 if(!theGroupsList.IsEmpty() )
191 anOutputSplitDataList.append(anInputGroupList);
192 return anOutputSplitDataList;
194 limplus1Object = true;// size =1 && hasLimits
196 HYDROData_DataMapOfShapeListOfString aDM3;
197 if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
198 QStringList aListOfNames;
199 for (int i=0;i < anInputGroupList.size() ;i++) {
200 const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
201 aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);
204 // Step 1. Prepare Partition structures.
205 TopoDS_Shape aResult;
206 BOPCol_ListOfShape aLS;
207 QStringList aListOfNames;
208 TopoDS_Compound aCmp;
210 aBB.MakeCompound(aCmp);
211 for (int i=0;i < anInputSplitDataList.size() ;i++) {
212 const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
213 aDM3.Bind(aSh, anInputSplitDataList.at(i).ObjectNames);
216 #ifdef DEB_SPLIT_TO_ZONES
217 //TCollection_AsciiString aName = aNam + i + ".brep";
218 //BRepTools::Write(aSh, aName.ToCString());
221 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
222 TCollection_AsciiString aNameBefore = fileNameBefore + ".brep";
223 BRepTools::Write(aCmp, aNameBefore.ToCString());
226 HYDROData_DataMapOfShapeListOfShape aDM1;
227 if(anInputSplitDataList.size() > 1) {
228 HYDROData_Transform splitTool;
229 const Standard_Integer anErr = SplitFaces(aCmp, splitTool);
231 return anOutputSplitDataList;
232 aResult = splitTool.Shape();
233 if (aResult.IsNull())
234 return anOutputSplitDataList;
235 BRepCheck_Analyzer aCheck (aResult);
236 if(!aCheck.IsValid()) {
237 #ifdef DEB_SPLIT_TO_ZONES
238 cout << "result is not valid" <<endl;
239 BRepTools::Write(aResult, "SplitFacesNV.brep");
241 return anOutputSplitDataList;
243 #ifdef DEB_SPLIT_TO_ZONES
244 BRepTools::Write(aResult, "SplitFacesV.brep");
247 // Step 3. Collect history
248 //HYDROData_DataMapOfShapeListOfShape aDM1;
249 BOPCol_ListIteratorOfListOfShape anIt(aLS);
250 //TCollection_AsciiString aNamM ("EdgM_");
251 //TCollection_AsciiString aNamG ("EdgG_");
252 for (int i =1;anIt.More();anIt.Next(),i++) {
253 Standard_Boolean foundF(Standard_False);
254 const TopTools_ListOfShape& aListOfNew = splitTool.Modified(anIt.Value());
255 if(!aListOfNew.IsEmpty())
256 foundF = Standard_True;
257 TopTools_ListOfShape aList;
258 TopTools_ListIteratorOfListOfShape it(aListOfNew);
259 for(;it.More();it.Next())
260 aList.Append(it.Value());
262 // Bug in History: partition should give only modified entities! => temporary solution is used
263 const TopTools_ListOfShape& aListOfGen = splitTool.Generated(anIt.Value());
264 if(!aListOfGen.IsEmpty()) {
265 foundF = Standard_True;
266 it.Initialize(aListOfGen);
267 for(;it.More();it.Next())
268 aList.Append(it.Value());
270 if(!foundF) // face is not modified
271 aList.Append (anIt.Value());
272 aDM1.Bind(anIt.Value(), aList);
273 #ifdef DEB_SPLIT_TO_ZONES
274 TCollection_AsciiString aName;
276 if(!anInputGroupList.isEmpty() ) { // 1
277 TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
278 for (int j =1;exp.More();exp.Next(),j++) {
280 Standard_Boolean foundE(Standard_False);
281 const TopTools_ListOfShape& aListM = splitTool.Modified(exp.Current());
282 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
283 if(aListM.Extent()) foundE = Standard_True;
284 it.Initialize(aListM);
285 for(int k=1;it.More();it.Next(),k++) {
286 aList.Append(it.Value());
287 //aName = aNamM + i + j +k +".brep";
288 //BRepTools::Write(it.Value(),aName.ToCString());
290 /* const TopTools_ListOfShape& aListG = splitTool.Generated(exp.Current());
291 if(!aListG.IsEmpty()) {
292 foundE = Standard_True;
293 it.Initialize(aListG);
294 for(int k=1;it.More();it.Next(),k++) {
295 aList.Append(it.Value());
296 //aName = aNamG + i + j +k +".brep";
297 //BRepTools::Write(it.Value(),aName.ToCString());
300 //cout << "NB_EDGE = " << aList.Extent() <<endl;
302 aList.Append (exp.Current());
303 #ifdef DEB_SPLIT_TO_ZONES
304 aName = aNamG + i + j +".brep";
305 BRepTools::Write(exp.Current(),aName.ToCString());
306 cout << aName.ToCString()<< " = " << exp.Current().TShape() <<endl;
309 aDM1.Bind(exp.Current(), aList);
314 aResult = anInputSplitDataList.at(0).Shape; // get single input shape
315 // aDM2: NewShape ==> ListOfOldShapes
316 HYDROData_DataMapOfShapeListOfShape aDM2;
317 // make limiting face
318 HYDROData_DataMapOfShapeListOfShape aDM4;
319 Standard_Boolean hasLimits(Standard_False);
321 HYDROData_MapOfShape aBndView;
322 if (! thePolyline.IsNull()) {
323 const TopoDS_Wire aBndWire = TopoDS::Wire(thePolyline->GetShape());
324 if(!aBndWire.IsNull()) {
326 if(buildLimFace(aBndWire, limFace)) {
327 TopoDS_Shape aComResult;
328 BRepAlgoAPI_Common mkCom(aResult, limFace);
330 aComResult = mkCom.Shape();
331 BRepCheck_Analyzer aCheck (aComResult);
332 if(aCheck.IsValid()) {
333 #ifdef DEB_SPLIT_TO_ZONES
334 BRepTools::Write(aComResult,"CommonV.brep");
336 aBndName = thePolyline->GetName();
337 hasLimits = Standard_True; // DM2 should be filled here
338 TopExp_Explorer exp (limFace, TopAbs_EDGE);
339 for (int i =1;exp.More();exp.Next(),i++) {
340 const TopoDS_Shape& anEdge = exp.Current();
341 if(anEdge.IsNull()) continue;
342 aBndView.Add(anEdge);
344 HYDROData_MapOfShape aView;
345 exp.Init (aResult, TopAbs_FACE);
346 for (int i =1;exp.More();exp.Next(),i++) {
347 const TopoDS_Shape& aFace = exp.Current();
348 if(!aFace.IsNull()) {
349 const TopTools_ListOfShape& aListOfNew = mkCom.Modified(aFace);
350 //cout << "Modified: " << aListOfNew.Extent() <<endl;
351 if(!aListOfNew.IsEmpty()) {
352 aDM4.Bind(aFace, aListOfNew);
353 //TCollection_AsciiString aName = aNam + i + ".brep";
354 //BRepTools::Write(aListOfNew.Last(), aName.ToCString());
357 if(!mkCom.IsDeleted(aFace)) {
358 const TopTools_ListOfShape& aListOfGen = mkCom.Generated(aFace);
359 if(!aListOfGen.IsEmpty()) {
360 aDM4.Bind(aFace, aListOfGen);
361 //TCollection_AsciiString aName = aNam + i + "g.brep";
362 //BRepTools::Write(aListOfGen.Last(), aName.ToCString());
365 TopTools_ListOfShape aList;
367 aDM4.Bind(aFace, aList); //the same face - not modified
371 TopExp_Explorer exp2 (aFace, TopAbs_EDGE);
372 for (int j =1;exp2.More();exp2.Next(),j++) {
373 const TopoDS_Shape& anEdge = exp2.Current();
374 if(!anEdge.IsNull()) {
375 if(aView.Contains(anEdge)) continue;
377 const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);
378 if(!aListOfNewEd.IsEmpty())
379 aDM4.Bind(anEdge, aListOfNewEd);
381 if(!mkCom.IsDeleted(anEdge)) {
382 const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
383 if(!aListOfGenEd.IsEmpty()) {
384 aDM4.Bind(anEdge, aListOfGenEd);
386 TopTools_ListOfShape aList;
387 aList.Append(anEdge);
388 aDM4.Bind(anEdge, aList);//the same edge - not modified
395 } //end DM4 filling (phase 1)
396 //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
397 // phase 2 (from tool)
398 TopExp_Explorer expt (limFace, TopAbs_EDGE);
399 for(;expt.More();expt.Next()) {
400 const TopoDS_Shape& anEdge = expt.Current();
401 if(!anEdge.IsNull()) {
402 const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);
403 if(!aListOfNewEd.IsEmpty())
404 aDM4.Bind(anEdge, aListOfNewEd);
406 if(!mkCom.IsDeleted(anEdge)) {
407 const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
408 if(!aListOfGenEd.IsEmpty()) {
409 aDM4.Bind(anEdge, aListOfGenEd);
411 TopTools_ListOfShape aList;
412 aList.Append(anEdge);
413 aDM4.Bind(anEdge, aList);//the same edge - not modified
419 //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
421 // fill DM1 (old - new) and DM2 (new - old)
422 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape mIt(aDM4);
423 for(;mIt.More();mIt.Next()) {
424 const TopoDS_Shape& aKey = mIt.Key();//old
425 TopTools_ListOfShape aList;
427 const TopTools_ListOfShape& aListOfNew = mIt.Value();
428 aDM1.Bind(aKey, aListOfNew);
429 TopTools_ListIteratorOfListOfShape it(aListOfNew);
430 for(;it.More();it.Next()) {
431 if(!aDM2.IsBound(it.Value()))
432 aDM2.Bind(it.Value(), aList);
434 TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
440 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
441 //DM4 contains Old - New after common op. DM1: old - new after Split op.
442 for(;aMIt.More();aMIt.Next()) {
443 const TopoDS_Shape& aKey = aMIt.Key();
444 TopTools_ListOfShape aList;
446 const TopTools_ListOfShape& aListOfNew = aMIt.Value();
447 TopTools_ListIteratorOfListOfShape it(aListOfNew);
448 for(;it.More();it.Next()) {// iterate new: b1, b2, b3...
449 if(!aDM4.IsBound(it.Value())) // bi - is deleted
450 continue; // go to the next bi
452 const TopTools_ListOfShape& aListOfNew4 = aDM4.Find(it.Value());
453 TopTools_ListIteratorOfListOfShape it4(aListOfNew4); // {c1, c2, c3,...}
454 for(;it4.More();it4.Next()) {
455 if(!aDM2.IsBound(it4.Value()))
456 aDM2.Bind(it4.Value(), aList);
458 TopTools_ListOfShape& aList = aDM2.ChangeFind(it4.Value());
467 hasLimits = Standard_False;
468 #ifdef DEB_SPLIT_TO_ZONES
469 BRepTools::Write(aComResult,"CommonNV.brep");
475 }// end limits processing
477 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
478 for(;aMIt.More();aMIt.Next()) {
479 const TopoDS_Shape& aKey = aMIt.Key();
480 TopTools_ListOfShape aList;
482 const TopTools_ListOfShape& aListOfNew = aMIt.Value();
483 TopTools_ListIteratorOfListOfShape it(aListOfNew);
484 for(;it.More();it.Next()) {
485 if(!aDM2.IsBound(it.Value()))
486 aDM2.Bind(it.Value(), aList);
488 TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
494 // Step 4. Fill output structure.
495 #ifdef DEB_SPLIT_TO_ZONES
496 TCollection_AsciiString aNam4 ("SC_");
498 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
499 for(int i =1;aMIt.More();aMIt.Next(),i++) {
500 SplitData aDestSplitData;
501 const TopoDS_Shape& aKey = aMIt.Key(); //new
502 aDestSplitData.Shape = aKey;
503 if(aKey.ShapeType() == TopAbs_FACE)
504 aDestSplitData.Type = SplitData::Data_Zone;
506 aDestSplitData.Type = SplitData::Data_Edge;
507 #ifdef DEB_SPLIT_TO_ZONES
508 TCollection_AsciiString aName = aNam4 + i + ".brep";
509 BRepTools::Write(aKey,aName.ToCString());
513 QStringList aListOfNames; // names processing
514 const TopTools_ListOfShape& aListOfOld = aMIt.Value();
515 TopTools_ListIteratorOfListOfShape it(aListOfOld);
516 for(int j =1;it.More();it.Next(),j++) {
517 const TopoDS_Shape& aSh = it.Value(); //old
518 if(aDM3.IsBound(aSh)) {
519 const QStringList& ObjectNames = aDM3.Find(aSh);
520 aListOfNames.append(ObjectNames);
521 #ifdef DEB_SPLIT_TO_ZONES
522 TCollection_AsciiString aName = aNam4 + i +"_" + j + ".brep";
523 BRepTools::Write(aSh ,aName.ToCString());
526 if(aBndView.Contains(aSh) && hasLimits) {
527 aListOfNames.append(aBndName);
528 #ifdef DEB_SPLIT_TO_ZONES
529 cout << " BndName = "<<aBndName.toStdString() <<endl;
535 aDestSplitData.ObjectNames = aListOfNames;
536 anOutputSplitDataList.append(aDestSplitData);
537 #ifdef DEB_SPLIT_TO_ZONES
538 QString aStr = aDestSplitData.ObjectNames.join(" ");
539 cout << "New# = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aDestSplitData.ObjectNames.size() <<endl;
543 return anOutputSplitDataList;
546 bool HYDROData_SplitToZonesTool::SplitTwoData( const SplitData& theData1,
547 const SplitData& theData2,
548 SplitData& theData1Subtracted,
549 SplitData& theData2Subtracted,
550 SplitData& theDataIntersected )
552 const TopoDS_Shape& aShape1 = theData1.Shape;
553 const TopoDS_Shape& aShape2 = theData2.Shape;
555 const QStringList& anObjectNames1 = theData1.ObjectNames;
556 const QStringList& anObjectNames2 = theData2.ObjectNames;
558 BRepAlgoAPI_Common aCommon( aShape1, aShape2 );
559 TopoDS_Shape aCommonShape = aCommon.Shape();
560 if( aCommonShape.IsNull() )
562 theData1Subtracted = theData1;
563 theData2Subtracted = theData2;
567 BRepAlgoAPI_Cut aCut1( aShape1, aShape2 );
568 TopoDS_Shape aCut1Shape = aCut1.Shape();
570 BRepAlgoAPI_Cut aCut2( aShape2, aShape1 );
571 TopoDS_Shape aCut2Shape = aCut2.Shape();
573 theData1Subtracted = SplitData( SplitData::Data_Zone, aCut1Shape, anObjectNames1 );
574 theData2Subtracted = SplitData( SplitData::Data_Zone, aCut2Shape, anObjectNames2 );
575 theDataIntersected = SplitData( SplitData::Data_Zone, aCommonShape, anObjectNames1 + anObjectNames2 );
580 HYDROData_SplitToZonesTool::SplitDataList
581 HYDROData_SplitToZonesTool::ExtractSeparateData( const SplitData& theData )
583 SplitDataList aSplitDataList;
584 TopExp_Explorer anExp( theData.Shape, TopAbs_FACE );
585 for( ; anExp.More(); anExp.Next() )
587 TopoDS_Shape aShape = anExp.Current();
588 if( aShape.ShapeType() == TopAbs_FACE )
590 TopoDS_Face aFace = TopoDS::Face( aShape );
591 if( !aFace.IsNull() )
593 SplitData aSplitData( SplitData::Data_Zone, aFace, theData.ObjectNames );
594 aSplitDataList.append( aSplitData );
598 return aSplitDataList;