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 #ifdef DEB_SPLIT_TO_ZONES
121 TCollection_AsciiString aNam("Faces_");
123 for( int anIndex = 1, aLength = theObjectList.Length(); anIndex <= aLength; anIndex++ )
125 Handle(HYDROData_Object) aGeomObj =
126 Handle(HYDROData_Object)::DownCast( theObjectList.Value( anIndex ) );
127 if( aGeomObj.IsNull() )
130 TopoDS_Shape aShape = aGeomObj->GetTopShape();
131 if ( aShape.IsNull() )
133 #ifdef DEB_SPLIT_TO_ZONES
134 TCollection_AsciiString aName = aNam + anIndex + ".brep";
135 BRepTools::Write(aShape, aName.ToCString());
137 if ( aShape.ShapeType() == TopAbs_COMPOUND ) {
138 // Create split data for each face contained in the compound
139 TopExp_Explorer anExp( aShape, TopAbs_FACE );
140 for ( ; anExp.More(); anExp.Next() ) {
141 const TopoDS_Face& aFace = TopoDS::Face( anExp.Current() );
142 if ( !aFace.IsNull() ) {
143 SplitData aSplitData( SplitData::Data_Zone, aFace, aGeomObj->GetName() );
144 anInputSplitDataList.append( aSplitData );
148 SplitData aSplitData( SplitData::Data_Zone, aShape, aGeomObj->GetName() );
149 anInputSplitDataList.append( aSplitData );
153 SplitDataList anInputGroupList;
154 for( int anIndex = 1, aLength = theGroupsList.Length(); anIndex <=aLength; anIndex++ )
156 Handle(HYDROData_ShapesGroup) aGeomGroup =
157 Handle(HYDROData_ShapesGroup)::DownCast( theGroupsList.Value( anIndex ) );
158 if( aGeomGroup.IsNull() )
161 TopTools_SequenceOfShape aGroupShapes;
162 aGeomGroup->GetShapes( aGroupShapes );
163 for( int i = 1, aNbShapes = aGroupShapes.Length(); i <= aNbShapes; i++ ) {
164 const TopoDS_Shape& aGroupShape = aGroupShapes.Value( i );
165 if ( aGroupShape.IsNull() )
168 if ( aGroupShape.ShapeType() == TopAbs_COMPOUND ) {
169 TopExp_Explorer anExp( aGroupShape, TopAbs_EDGE );
170 for ( ; anExp.More(); anExp.Next() ) {
171 const TopoDS_Edge& anEdge = TopoDS::Edge( anExp.Current() );
172 if ( !anEdge.IsNull() ) {
173 SplitData aSplitData( SplitData::Data_Edge, anEdge, aGeomGroup->GetName() );
174 anInputGroupList.append( aSplitData );
178 SplitData aSplitData( SplitData::Data_Edge, aGroupShape, aGeomGroup->GetName() );
179 anInputGroupList.append( aSplitData );
180 #ifdef DEB_SPLIT_TO_ZONES
181 QString aStr = aSplitData.ObjectNames.join(" ");
182 cout << "Group# = " <<anIndex <<" Nmb in grp = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
187 // If only one shape is given we don't split it
188 // algorithm just returns the unpacked input data
189 bool limplus1Object(false);
190 if(theObjectList.Size() == 1 ) {
191 if(thePolyline.IsNull()) {
192 anOutputSplitDataList.append(anInputSplitDataList);
193 if(!theGroupsList.IsEmpty() )
194 anOutputSplitDataList.append(anInputGroupList);
195 return anOutputSplitDataList;
197 limplus1Object = true;// size =1 && hasLimits
199 HYDROData_DataMapOfShapeListOfString aDM3;
200 if(!anInputGroupList.isEmpty()) {// Old edge ==> List_Of_Names
201 QStringList aListOfNames;
202 for (int i=0;i < anInputGroupList.size() ;i++) {
203 const TopoDS_Shape& aSh = anInputGroupList.at(i).Shape;
204 aDM3.Bind(aSh, anInputGroupList.at(i).ObjectNames);
208 // Step 1. Prepare Partition structures.
209 TopoDS_Shape aResult;
210 BOPCol_ListOfShape aLS;
211 QStringList aListOfNames;
212 TopoDS_Compound aCmp;
214 aBB.MakeCompound(aCmp);
215 for (int i=0;i < anInputSplitDataList.size() ;i++) {
216 const TopoDS_Shape& aSh = anInputSplitDataList.at(i).Shape;
217 aDM3.Bind(aSh, anInputSplitDataList.at(i).ObjectNames);
220 #ifdef DEB_SPLIT_TO_ZONES
221 //TCollection_AsciiString aName = aNam + i + ".brep";
222 //BRepTools::Write(aSh, aName.ToCString());
225 #ifdef DEB_SPLIT_TO_ZONES_CHECK_PARTITION
226 TCollection_AsciiString aNameBefore = fileNameBefore + ".brep";
227 BRepTools::Write(aCmp, aNameBefore.ToCString());
230 HYDROData_DataMapOfShapeListOfShape aDM1;
231 if(anInputSplitDataList.size() > 1) {
232 HYDROData_Transform splitTool;
233 const Standard_Integer anErr = SplitFaces(aCmp, splitTool);
235 return anOutputSplitDataList;
236 aResult = splitTool.Shape();
237 if (aResult.IsNull())
238 return anOutputSplitDataList;
239 BRepCheck_Analyzer aCheck (aResult);
240 if(!aCheck.IsValid()) {
241 #ifdef DEB_SPLIT_TO_ZONES
242 cout << "result is not valid" <<endl;
243 BRepTools::Write(aResult, "SplitFacesNV.brep");
245 return anOutputSplitDataList;
247 #ifdef DEB_SPLIT_TO_ZONES
248 BRepTools::Write(aResult, "SplitFacesV.brep");
251 // Step 3. Collect history
252 //HYDROData_DataMapOfShapeListOfShape aDM1;
253 BOPCol_ListIteratorOfListOfShape anIt(aLS);
254 #ifdef DEB_SPLIT_TO_ZONES
255 TCollection_AsciiString aNamM ("EdgM_");
256 TCollection_AsciiString aNamG ("EdgG_");
258 for (int i =1;anIt.More();anIt.Next(),i++) {
259 Standard_Boolean foundF(Standard_False);
260 const TopTools_ListOfShape& aListOfNew = splitTool.Modified(anIt.Value());
261 if(!aListOfNew.IsEmpty())
262 foundF = Standard_True;
264 TopTools_ListOfShape aList;
266 TopTools_ListIteratorOfListOfShape it(aListOfNew);
267 for(;it.More();it.Next())
268 aList.Append(it.Value());
269 /* *********************************************************************
270 // Bug in History: partition should give only modified entities! => temporary solution is used
271 //const TopTools_ListOfShape& aListOfGen = splitTool.Generated(anIt.Value());
272 //if(!aListOfGen.IsEmpty())
273 //foundF = Standard_True;
274 //it.Initialize(aListOfGen);
275 //for(;it.More();it.Next())
276 // aList.Append(it.Value());
277 ********************************************************************* */
278 if(!foundF) // face is not modified
279 aList.Append (anIt.Value());
280 aDM1.Bind(anIt.Value(), aList);
281 #ifdef DEB_SPLIT_TO_ZONES
282 TCollection_AsciiString aName;
284 if(!anInputGroupList.isEmpty() ) { // 1
285 TopExp_Explorer exp (anIt.Value(), TopAbs_EDGE);
286 for (int j =1;exp.More();exp.Next(),j++) {
288 Standard_Boolean foundE(Standard_False);
289 const TopTools_ListOfShape& aListM = splitTool.Modified(exp.Current());
290 //cout << "NB_EDGE_M = " << aListM.Extent() <<endl;
291 if(aListM.Extent()) foundE = Standard_True;
292 it.Initialize(aListM);
293 for(int k=1;it.More();it.Next(),k++) {
294 aList.Append(it.Value());
295 #ifdef DEB_SPLIT_TO_ZONES
296 //aName = aNamM + i + j +k +".brep";
297 //BRepTools::Write(it.Value(),aName.ToCString());
300 /* *********************************************************************
301 //const TopTools_ListOfShape& aListG = splitTool.Generated(exp.Current());
302 //if(aListG.Extent()) foundE = Standard_True;
303 //it.Initialize(aListG);
304 //for(int k=1;it.More();it.Next(),k++)
305 //aList.Append(it.Value());
306 //cout << "NB_EDGE = " << aList.Extent() <<endl;
307 ************************************************************************** */
309 aList.Append (exp.Current());
310 #ifdef DEB_SPLIT_TO_ZONES
311 aName = aNamG + i + j +".brep";
312 BRepTools::Write(exp.Current(),aName.ToCString());
313 cout << aName.ToCString()<< " = " << exp.Current().TShape() <<endl;
316 aDM1.Bind(exp.Current(), aList);
321 aResult = anInputSplitDataList.at(0).Shape; // get single input shape
324 // aDM2: NewShape ==> ListOfOldShapes
325 HYDROData_DataMapOfShapeListOfShape aDM2;
326 // make limiting face
327 HYDROData_DataMapOfShapeListOfShape aDM4;
328 Standard_Boolean hasLimits(Standard_False);
330 HYDROData_MapOfShape aBndView;
331 if (! thePolyline.IsNull()) {
332 const TopoDS_Wire aBndWire = TopoDS::Wire(thePolyline->GetShape());
333 if(!aBndWire.IsNull()) {
335 if(buildLimFace(aBndWire, limFace)) {
336 TopoDS_Shape aComResult;
337 BRepAlgoAPI_Common mkCom(aResult, limFace);
339 aComResult = mkCom.Shape();
340 BRepCheck_Analyzer aCheck (aComResult);
341 if(aCheck.IsValid()) {
342 #ifdef DEB_SPLIT_TO_ZONES
343 BRepTools::Write(aComResult,"CommonV.brep");
344 BRepTools::Write(limFace,"limFace.brep");
346 aBndName = thePolyline->GetName();
347 hasLimits = Standard_True; // DM2 should be filled here
348 TopExp_Explorer exp (limFace, TopAbs_EDGE);
349 for (int i =1;exp.More();exp.Next(),i++) {
350 const TopoDS_Shape& anEdge = exp.Current();
351 if(anEdge.IsNull()) continue;
352 aBndView.Add(anEdge);
353 QStringList aListOfNames;
354 aListOfNames.append(aBndName);
355 aDM3.Bind(anEdge, aListOfNames);
356 TopTools_ListOfShape aList;
357 aList.Append(anEdge);
358 aDM1.Bind(anEdge,aList);
360 HYDROData_MapOfShape aView;
361 exp.Init (aResult, TopAbs_FACE);
362 for (int i =1;exp.More();exp.Next(),i++) {
363 const TopoDS_Shape& aFace = exp.Current();
364 if(!aFace.IsNull()) {
365 const TopTools_ListOfShape& aListOfNew = mkCom.Modified(aFace);
366 //cout << "Modified: " << aListOfNew.Extent() <<endl;
367 if(!aListOfNew.IsEmpty()) {
368 aDM4.Bind(aFace, aListOfNew);
369 #ifdef DEB_SPLIT_TO_ZONES
370 //TCollection_AsciiString aName = aNam + i + ".brep";
371 //BRepTools::Write(aListOfNew.Last(), aName.ToCString());
375 if(!mkCom.IsDeleted(aFace)) {
376 const TopTools_ListOfShape& aListOfGen = mkCom.Generated(aFace);
377 if(!aListOfGen.IsEmpty()) {
378 /* aDM4.Bind(aFace, aListOfGen); ??? */
379 #ifdef DEB_SPLIT_TO_ZONES
380 //TCollection_AsciiString aName = aNam + i + "g.brep";
381 //BRepTools::Write(aListOfGen.Last(), aName.ToCString());
385 TopTools_ListOfShape aList;
387 aDM4.Bind(aFace, aList); //the same face - not modified
391 TopExp_Explorer exp2 (aFace, TopAbs_EDGE);
392 for (int j =1;exp2.More();exp2.Next(),j++) {
393 const TopoDS_Shape& anEdge = exp2.Current();
394 if(!anEdge.IsNull()) {
395 if(aView.Contains(anEdge)) continue;
397 const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);
398 if(!aListOfNewEd.IsEmpty())
399 aDM4.Bind(anEdge, aListOfNewEd);
401 if(!mkCom.IsDeleted(anEdge)) {
402 const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
403 if(!aListOfGenEd.IsEmpty()) {
404 /* aDM4.Bind(anEdge, aListOfGenEd); ???*/
406 TopTools_ListOfShape aList;
407 aList.Append(anEdge);
408 aDM4.Bind(anEdge, aList);//the same edge - not modified
415 } //end DM4 filling (phase 1)
416 //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
417 // phase 2 (from tool)
418 #ifdef DEB_SPLIT_TO_ZONES
419 TCollection_AsciiString aNam("BndEd_");
421 TopExp_Explorer expt (limFace, TopAbs_EDGE);
422 for(int i =1;expt.More();expt.Next(),i++) {
423 const TopoDS_Shape& anEdge = expt.Current();
424 if(!anEdge.IsNull()) {
425 const TopTools_ListOfShape& aListOfNewEd = mkCom.Modified(anEdge);
426 #ifdef DEB_SPLIT_TO_ZONES
427 TopTools_ListIteratorOfListOfShape itl(aListOfNewEd);
428 for(int j=1;itl.More();itl.Next(),j++) {
429 TCollection_AsciiString aName = aNam + i + "_" + j + ".brep";
430 BRepTools::Write(itl.Value(), aName.ToCString());
431 cout <<aName.ToCString()<<" = "<< itl.Value().TShape() <<endl;
434 if(!aListOfNewEd.IsEmpty())
435 aDM4.Bind(anEdge, aListOfNewEd);
437 if(!mkCom.IsDeleted(anEdge)) {
438 const TopTools_ListOfShape& aListOfGenEd = mkCom.Generated(anEdge);
439 if(!aListOfGenEd.IsEmpty()) {
440 /* aDM4.Bind(anEdge, aListOfGenEd); ??? */
442 TopTools_ListOfShape aList;
443 aList.Append(anEdge);
444 aDM4.Bind(anEdge, aList);//the same edge - not modified
450 //cout << "DM4 Ext = " <<aDM4.Extent() <<endl;
452 // fill DM1 (old - new) and DM2 (new - old)
453 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape mIt(aDM4);
454 for(;mIt.More();mIt.Next()) {
455 const TopoDS_Shape& aKey = mIt.Key();//old
456 TopTools_ListOfShape aList;
458 const TopTools_ListOfShape& aListOfNew = mIt.Value();
459 aDM1.Bind(aKey, aListOfNew);
460 TopTools_ListIteratorOfListOfShape it(aListOfNew);
461 for(;it.More();it.Next()) {
462 if(!aDM2.IsBound(it.Value()))
463 aDM2.Bind(it.Value(), aList);
465 TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
471 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
472 //DM4 contains Old - New after common op. DM1: old - new after Split op.
473 for(;aMIt.More();aMIt.Next()) {
474 const TopoDS_Shape& aKey = aMIt.Key();
475 TopTools_ListOfShape aList;
477 const TopTools_ListOfShape& aListOfNew = aMIt.Value();
478 TopTools_ListIteratorOfListOfShape it(aListOfNew);
479 for(;it.More();it.Next()) {// iterate new: b1, b2, b3...
480 if(!aDM4.IsBound(it.Value())) // bi - is deleted
481 continue; // go to the next bi
483 const TopTools_ListOfShape& aListOfNew4 = aDM4.Find(it.Value());
484 TopTools_ListIteratorOfListOfShape it4(aListOfNew4); // {c1, c2, c3,...}
485 for(;it4.More();it4.Next()) {
486 if(!aDM2.IsBound(it4.Value()))
487 aDM2.Bind(it4.Value(), aList);
489 TopTools_ListOfShape& aList = aDM2.ChangeFind(it4.Value());
498 hasLimits = Standard_False;
499 #ifdef DEB_SPLIT_TO_ZONES
500 BRepTools::Write(aComResult,"CommonNV.brep");
506 }// end limits processing
508 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM1);
509 for(;aMIt.More();aMIt.Next()) {
510 const TopoDS_Shape& aKey = aMIt.Key();
511 TopTools_ListOfShape aList;
513 const TopTools_ListOfShape& aListOfNew = aMIt.Value();
514 TopTools_ListIteratorOfListOfShape it(aListOfNew);
515 for(;it.More();it.Next()) {
516 if(!aDM2.IsBound(it.Value()))
517 aDM2.Bind(it.Value(), aList);
519 TopTools_ListOfShape& aList = aDM2.ChangeFind(it.Value());
525 // Step 4. Fill output structure.
526 #ifdef DEB_SPLIT_TO_ZONES
527 TCollection_AsciiString aNam4 ("SC_");
529 HYDROData_DataMapIteratorOfDataMapOfShapeListOfShape aMIt(aDM2);
530 for(int i =1;aMIt.More();aMIt.Next(),i++) {
531 SplitData aDestSplitData;
532 const TopoDS_Shape& aKey = aMIt.Key(); //new
533 aDestSplitData.Shape = aKey;
534 if(aKey.ShapeType() == TopAbs_FACE)
535 aDestSplitData.Type = SplitData::Data_Zone;
537 aDestSplitData.Type = SplitData::Data_Edge;
538 #ifdef DEB_SPLIT_TO_ZONES
539 TCollection_AsciiString aName = aNam4 + i + ".brep";
540 BRepTools::Write(aKey,aName.ToCString());
544 QStringList aListOfNames; // names processing
545 const TopTools_ListOfShape& aListOfOld = aMIt.Value();
546 TopTools_ListIteratorOfListOfShape it(aListOfOld);
547 for(int j =1;it.More();it.Next(),j++) {
548 const TopoDS_Shape& aSh = it.Value(); //old
549 if(aDM3.IsBound(aSh)) {
550 const QStringList& ObjectNames = aDM3.Find(aSh);
551 aListOfNames.append(ObjectNames);
552 #ifdef DEB_SPLIT_TO_ZONES
553 TCollection_AsciiString aName = aNam4 + i +"_" + j + ".brep";
554 BRepTools::Write(aSh ,aName.ToCString());
557 #ifdef DEB_SPLIT_TO_ZONES
558 TCollection_AsciiString aName = aNam4 +"__" + i +"_" + j + ".brep";
559 BRepTools::Write(aSh ,aName.ToCString());
560 cout <<aName.ToCString()<<" = "<< aSh.TShape() <<endl;
562 if(aBndView.Contains(aSh) && hasLimits) {
563 aListOfNames.append(aBndName);
564 #ifdef DEB_SPLIT_TO_ZONES
565 cout << " BndName = "<<aBndName.toStdString() <<endl;
571 aDestSplitData.ObjectNames = aListOfNames;
572 anOutputSplitDataList.append(aDestSplitData);
573 #ifdef DEB_SPLIT_TO_ZONES
574 QString aStr = aDestSplitData.ObjectNames.join(" ");
575 cout << "New# = " << i<< " Names = "<<aStr.toStdString() << " size = " <<aDestSplitData.ObjectNames.size() <<endl;
579 return anOutputSplitDataList;