1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_Stream.h"
21 #include "HYDROData_Document.h"
22 #include "HYDROData_PolylineXY.h"
23 #include "HYDROData_Polyline3D.h"
24 #include "HYDROData_Profile.h"
25 #include "HYDROData_ShapesGroup.h"
26 #include "HYDROData_ShapesTool.h"
27 #include "HYDROData_IAltitudeObject.h"
28 #include "HYDROData_IProfilesInterpolator.h"
29 #include "HYDROData_Tool.h"
31 #include <TDataStd_RealArray.hxx>
33 #include <Precision.hxx>
35 #include <NCollection_DataMap.hxx>
37 #include <TColStd_Array1OfReal.hxx>
38 #include <TColStd_ListOfReal.hxx>
39 #include <TColStd_ListIteratorOfListOfReal.hxx>
40 #include <TCollection_CompareOfReal.hxx>
41 #include <TColgp_Array1OfPnt.hxx>
42 #include <TColgp_HArray1OfPnt.hxx>
45 #include <TopoDS_Wire.hxx>
46 #include <TopoDS_Shell.hxx>
47 #include <TopoDS_Face.hxx>
48 #include <TopoDS_Edge.hxx>
49 #include <TopoDS_Vertex.hxx>
51 #include <TopExp_Explorer.hxx>
53 #include <Bnd_Box.hxx>
55 #include <BRep_Builder.hxx>
56 #include <BRepBuilderAPI_MakeEdge.hxx>
57 #include <BRepBuilderAPI_MakeWire.hxx>
58 #include <BRepBuilderAPI_MakeFace.hxx>
60 #include <BRepBndLib.hxx>
61 #include <BRepProj_Projection.hxx>
62 #include <BRepExtrema_ExtCC.hxx>
63 #include <BRepCheck_Analyzer.hxx>
73 #include <GeomAPI_Interpolate.hxx>
74 #include <Geom_BSplineCurve.hxx>
76 #include <TopTools_Array1OfShape.hxx>
78 #include <SortTools_QuickSortOfReal.hxx>
81 #include <QStringList>
83 //#define DEB_STREAM 1
85 //#define DEB_HASINT 1
86 //#define DEB_UPDATE 1
87 #include <BRepTools.hxx>
88 #include <TCollection_AsciiString.hxx>
91 typedef NCollection_DataMap<Standard_Real, Handle(HYDROData_Profile)> HYDROData_DataMapOfRealOfHDProfile;
93 IMPLEMENT_STANDARD_HANDLE(HYDROData_Stream,HYDROData_NaturalObject)
94 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Stream,HYDROData_NaturalObject)
97 HYDROData_Stream::HYDROData_Stream()
98 : HYDROData_NaturalObject()
102 HYDROData_Stream::~HYDROData_Stream()
106 QStringList HYDROData_Stream::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
108 QStringList aResList = dumpObjectCreation( theTreatedObjects );
109 QString aName = GetObjPyName();
111 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
112 setPythonReferenceObject( theTreatedObjects, aResList, aHydAxis, "SetHydraulicAxis" );
114 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
115 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; ++i )
117 const Handle(HYDROData_Entity) aProfile = aSeqOfProfiles.Value( i );
118 setPythonReferenceObject( theTreatedObjects, aResList, aProfile, "AddProfile" );
121 // Set bottom polyline if exists
122 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
123 if ( !aBottomPolyline.IsNull() ) {
124 setPythonReferenceObject( theTreatedObjects, aResList, aBottomPolyline, "SetBottomPolyline" );
127 aResList << QString( "" );
128 aResList << QString( "%1.Update();" ).arg( aName );
129 aResList << QString( "" );
134 HYDROData_SequenceOfObjects HYDROData_Stream::GetAllReferenceObjects() const
136 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
138 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
139 if ( !aHydAxis.IsNull() )
140 aResSeq.Append( aHydAxis );
142 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
143 aResSeq.Append( aSeqOfProfiles );
148 TopoDS_Shape HYDROData_Stream::GetTopShape() const
150 return getTopShape();
153 TopoDS_Shape HYDROData_Stream::GetShape3D() const
158 Handle(Geom_BSplineCurve) HYDROData_Stream::buildInterpolationCurve(
159 const Handle(TColgp_HArray1OfPnt)& theArrayOfPnt )
161 Handle(Geom_BSplineCurve) aBSpline;
162 GeomAPI_Interpolate anInterpolator (theArrayOfPnt, Standard_False, 1.0e-5);
163 anInterpolator.Perform() ;
164 if (anInterpolator.IsDone())
165 aBSpline = anInterpolator.Curve();
169 void HYDROData_Stream::Update()
171 updateProfilesOrder();
173 // Update bottom polyline if exists
174 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
175 if ( !aBottomPolyline.IsNull() ) {
176 if ( GenerateBottomPolyline() ) {
177 Handle(HYDROData_PolylineXY) aPolylineXY = aBottomPolyline->GetPolylineXY();
178 if ( !aPolylineXY.IsNull() ) {
179 aPolylineXY->Update();
181 aBottomPolyline->Update();
188 bool HYDROData_Stream::IsHas2dPrs() const
193 bool HYDROData_Stream::CreatePresentations( const Handle(HYDROData_PolylineXY)& theHydAxis,
194 const HYDROData_SequenceOfObjects& theProfiles,
195 PrsDefinition& thePrs )
197 if ( theHydAxis.IsNull() || theProfiles.Length() < 2 )
200 gp_Pnt aPrevFirstPoint, aPrevLastPoint;
201 Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, theProfiles.Length());
202 Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, theProfiles.Length());
203 TopTools_Array1OfShape anArrOfProfiles(1, theProfiles.Length());
204 TopTools_Array1OfShape anArrOf2DProfiles(1, theProfiles.Length());
207 HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
208 for (int i=1 ; anIter.More(); anIter.Next(),i++ )
210 Handle(HYDROData_Profile) aProfile =
211 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
212 if ( aProfile.IsNull() )
215 const TopoDS_Shape& aProf3d = aProfile->GetShape3D();
217 if ( !aProfile->GetLeftPoint( aPnt1, false ) || !aProfile->GetRightPoint( aPnt2, false ) )
220 anArrOfProfiles.SetValue(i,aProfile->GetShape3D());//aProfile->GetTopShape();
221 anArrOf2DProfiles.SetValue(i,aProfile->GetTopShape());
223 gp_Pnt aCurFirstPoint( aPnt1.X(), aPnt1.Y(), 0 ), aCurFP;
224 gp_Pnt aCurLastPoint( aPnt2.X(), aPnt2.Y(), 0 ), aCurLP;
225 TopoDS_Vertex aV1, aV2;
226 TopExp::Vertices(TopoDS::Wire(aProf3d), aV1, aV2);
227 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
228 if(aP1.X() == aPnt1.X() && aP1.Y() == aPnt1.Y())
232 aP1 = BRep_Tool::Pnt(aV2);
233 if(aP1.X() == aPnt2.X() && aP1.Y() == aPnt2.Y())
237 anArrayOfFPnt->SetValue(i,aCurFP);
238 anArrayOfLPnt->SetValue(i,aCurLP);
241 // Construct of the 3D presentation
242 Handle(Geom_BSplineCurve) aBSpline = buildInterpolationCurve (anArrayOfFPnt);
243 if(aBSpline.IsNull())
246 TopoDS_Edge anEdgLeft, anEdgRight;
248 BRepBuilderAPI_MakeEdge aMakeEdge(aBSpline);
249 if(aMakeEdge.IsDone())
250 anEdgLeft = aMakeEdge.Edge();
252 if(anEdgLeft.IsNull())
256 aBSpline = buildInterpolationCurve (anArrayOfLPnt);
257 if(aBSpline.IsNull())
260 aMakeEdge.Init(aBSpline);
261 if(aMakeEdge.IsDone())
262 anEdgRight = aMakeEdge.Edge();
264 if(anEdgRight.IsNull())
268 TopoDS_Compound aCmp;
269 aBB.MakeCompound(aCmp);
270 anIter.Init( theProfiles );
271 for (int i=1 ; i < anArrOfProfiles.Length() +1; i++ )
272 aBB.Add(aCmp, anArrOfProfiles.Value(i));
274 aBB.Add(aCmp,anEdgLeft);
275 aBB.Add(aCmp,anEdgRight);
276 BRepCheck_Analyzer aCh(aCmp);
278 thePrs.myPrs3D = aCmp;
281 BRepTools::Write(aCmp, "str3d.brep");
282 thePrs.myPrs3D = aCmp;
286 // Construct the top presentation
287 for(int i=1;i<= anArrayOfLPnt->Length();i++) {
288 gp_Pnt aPnt = anArrayOfFPnt->Value(i);
289 aPnt.SetZ(.0); // make 2d
290 anArrayOfFPnt->SetValue(i, aPnt);
291 aPnt = anArrayOfLPnt->Value(i);
293 anArrayOfLPnt->SetValue(i, aPnt);
297 aBSpline = buildInterpolationCurve (anArrayOfFPnt);
298 if(aBSpline.IsNull())
301 aMakeEdge.Init(aBSpline);
302 if(aMakeEdge.IsDone())
303 anEdgLeft = aMakeEdge.Edge();
306 aBSpline = buildInterpolationCurve (anArrayOfLPnt);
307 if(aBSpline.IsNull())
310 aMakeEdge.Init(aBSpline);
311 if(aMakeEdge.IsDone())
312 anEdgRight = aMakeEdge.Edge();
313 if(anEdgRight.IsNull())
316 BRepBuilderAPI_MakeEdge aMakeEdge2(anArrayOfFPnt->Value(1),anArrayOfLPnt->Value(1));
317 TopoDS_Edge aBotEdge, aTopEdge;
318 if(aMakeEdge2.IsDone())
319 aBotEdge = aMakeEdge2.Edge();
321 BRepBuilderAPI_MakeEdge aMakeEdge3(anArrayOfFPnt->Value(anArrayOfFPnt->Length()),anArrayOfLPnt->Value(anArrayOfLPnt->Length()));
322 if(aMakeEdge3.IsDone())
323 aTopEdge = aMakeEdge3.Edge();
325 // Make wire for 2D presentation with updating of corresponding edges
326 BRepBuilderAPI_MakeWire aMakeWire;
328 aMakeWire.Add( aBotEdge );
329 thePrs.myInlet = aMakeWire.Edge();
331 aMakeWire.Add( anEdgLeft );
332 thePrs.myLeftBank = aMakeWire.Edge();
334 aMakeWire.Add( aTopEdge );
335 thePrs.myOutlet = aMakeWire.Edge();
337 aMakeWire.Add( anEdgRight );
338 thePrs.myRightBank = aMakeWire.Edge();
340 TopoDS_Wire aSectProfileWire;
341 if(aMakeWire.IsDone())
342 aSectProfileWire = aMakeWire.Wire();
344 BRepBuilderAPI_MakeFace aMakeFace( aSectProfileWire, Standard_True );
347 if( aMakeFace.IsDone() )
348 aFace = aMakeFace.Face();
351 aBB.MakeCompound(aCmp);
353 for(int i=1;i <= anArrOf2DProfiles.Length(); i++)
354 aBB.Add(aCmp,anArrOf2DProfiles.Value(i));
358 thePrs.myPrs2D = aCmp;
361 BRepTools::Write(aCmp, "str2d.brep");
362 thePrs.myPrs2D = aCmp;
369 void HYDROData_Stream::UpdatePrs()
371 HYDROData_NaturalObject::Update();
373 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
374 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
376 PrsDefinition aResultPrs;
377 if ( !CreatePresentations( aHydAxis, aRefProfiles, aResultPrs ) )
380 SetShape3D( aResultPrs.myPrs3D );
381 SetTopShape( aResultPrs.myPrs2D );
383 // Create the stream groups
384 QString aLeftGroupName = GetName() + "_Left_Bank";
386 Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
387 aLeftGroup->SetName( aLeftGroupName );
388 aLeftGroup->AddShape( aResultPrs.myLeftBank );
390 QString aRightGroupName = GetName() + "_Right_Bank";
392 Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
393 aRightGroup->SetName( aRightGroupName );
394 aRightGroup->AddShape( aResultPrs.myRightBank );
396 QString anInGroupName = GetName() + "_Inlet";
398 Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
399 anInGroup->SetName( anInGroupName );
400 anInGroup->AddShape( aResultPrs.myInlet );
402 QString anOutGroupName = GetName() + "_Outlet";
404 Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
405 anOutGroup->SetName( anOutGroupName );
406 anOutGroup->AddShape( aResultPrs.myOutlet );
409 QColor HYDROData_Stream::DefaultFillingColor()
411 return QColor( Qt::green );
414 QColor HYDROData_Stream::DefaultBorderColor()
416 return QColor( Qt::transparent );
419 bool HYDROData_Stream::IsValidAsAxis( const Handle(HYDROData_PolylineXY)& theHydAxis )
421 if ( theHydAxis.IsNull() )
424 TopoDS_Shape aHydraulicShape = theHydAxis->GetShape();
425 if ( aHydraulicShape.IsNull() ||
426 aHydraulicShape.ShapeType() != TopAbs_WIRE ||
427 BRep_Tool::IsClosed( aHydraulicShape ) )
428 return false; // The polyline must be a single not closed wire
433 TopoDS_Shape getShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
434 const int theGroupId )
436 TopoDS_Shape aResShape;
437 if ( theGroups.Length() != 4 )
440 Handle(HYDROData_ShapesGroup) aGroup =
441 Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
442 if ( aGroup.IsNull() )
445 TopTools_SequenceOfShape aGroupShapes;
446 aGroup->GetShapes( aGroupShapes );
448 if ( !aGroupShapes.IsEmpty() )
449 aResShape = aGroupShapes.First();
454 TopoDS_Shape HYDROData_Stream::GetLeftShape() const
456 HYDROData_SequenceOfObjects aGroups = GetGroups();
457 return getShapeFromGroup( aGroups, 1 );
460 TopoDS_Shape HYDROData_Stream::GetRightShape() const
462 HYDROData_SequenceOfObjects aGroups = GetGroups();
463 return getShapeFromGroup( aGroups, 2 );
466 TopoDS_Shape HYDROData_Stream::GetInletShape() const
468 HYDROData_SequenceOfObjects aGroups = GetGroups();
469 return getShapeFromGroup( aGroups, 3 );
472 TopoDS_Shape HYDROData_Stream::GetOutletShape() const
474 HYDROData_SequenceOfObjects aGroups = GetGroups();
475 return getShapeFromGroup( aGroups, 4 );
478 QColor HYDROData_Stream::getDefaultFillingColor() const
480 return DefaultFillingColor();
483 QColor HYDROData_Stream::getDefaultBorderColor() const
485 return DefaultBorderColor();
488 bool HYDROData_Stream::SetHydraulicAxis( const Handle(HYDROData_PolylineXY)& theAxis )
490 if ( !IsValidAsAxis( theAxis ) )
493 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
494 if ( IsEqual( aPrevAxis, theAxis ) )
497 SetReferenceObject( theAxis, DataTag_HydraulicAxis );
499 // Update the order of profiles
500 updateProfilesOrder();
502 // Indicate model of the need to update the stream presentation
508 Handle(HYDROData_PolylineXY) HYDROData_Stream::GetHydraulicAxis() const
510 return Handle(HYDROData_PolylineXY)::DownCast(
511 GetReferenceObject( DataTag_HydraulicAxis ) );
514 void HYDROData_Stream::RemoveHydraulicAxis()
516 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
517 if ( aPrevAxis.IsNull() )
520 ClearReferenceObjects( DataTag_HydraulicAxis );
522 // We remove the reference profiles
525 // Indicate model of the need to update the stream presentation
529 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile,
530 const TopoDS_Face& thePlane,
531 Standard_Real& theOutPar ) const
533 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
534 return HasIntersection( aHydAxis, theProfile, thePlane, theOutPar );
537 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_PolylineXY)& theHydAxis,
538 const Handle(HYDROData_Profile)& theProfile,
539 const TopoDS_Face& thePlane,
540 Standard_Real& theOutPar )
542 if ( theProfile.IsNull() || !IsValidAsAxis( theHydAxis ) )
545 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() ); //guide line
546 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
547 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
550 BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
551 if(!aProjector.IsDone())
553 TopoDS_Shape aPrjProfile = aProjector.Shape();
554 if(aPrjProfile.IsNull())
556 TopoDS_Vertex aV1, aV2;
557 if(aPrjProfile.ShapeType() == TopAbs_EDGE)
558 TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);
559 else if(aPrjProfile.ShapeType() == TopAbs_WIRE)
560 TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);
561 else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
562 TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
564 TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);
566 anExp.Init(aPrjProfile, TopAbs_EDGE);
568 TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);
572 if(aV1.IsNull() || aV2.IsNull())
574 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
575 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
578 BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2);
581 const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
582 Standard_Integer aNum(0);
584 TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
585 for(;anExplo.More();anExplo.Next()) aNum++;
586 // check for self-intersection
587 const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
588 Standard_Boolean hasInt(false);
589 Standard_Real aSqDist(DBL_MAX);
590 Standard_Integer anIndx(0);
591 BRepExtrema_ExtCC aCC;
592 aCC.Initialize(anEdg2);
594 anExplo.Init(aHydraulicWire, TopAbs_EDGE);
595 for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
596 const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
599 Standard_Boolean hasSol(false);
603 for(Standard_Integer i=1; i<= aCC.NbExt();i++)
604 if(aCC.SquareDistance(i) < aSqDist) {
605 aSqDist = aCC.SquareDistance(i);
611 if(aSqDist <= SquareTolerance) { // hasInt
612 const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
614 TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
615 theOutPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
617 Standard_Real aPar = aCC.ParameterOnE1(anIndx);
625 TopExp::Vertices(anEdg1, aV1, aV2);
626 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
629 } else if(aNum > 1) {
630 TopExp::Vertices(anEdg1, aV1, aV2);
631 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
639 bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
641 if ( theProfile.IsNull() )
644 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
645 if ( aHydAxis.IsNull() )
649 if(!BuildFace(aHydAxis, aPlane))
652 Standard_Real aPar(.0);
653 if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
654 return false; // Object is already in reference list or it has no intersection
656 int aProfileIndex = insertParameter( aPar );
657 insertProfileInToOrder( theProfile, aProfileIndex );
659 // Indicate model of the need to update the stream presentation
665 bool HYDROData_Stream::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles,
666 const bool& theIsToOrder )
670 for ( int i = 1; i <= theProfiles.Length(); ++i )
672 Handle(HYDROData_Profile) aProfile =
673 Handle(HYDROData_Profile)::DownCast( theProfiles.Value( i ) );
674 if ( aProfile.IsNull() )
677 if ( !AddProfile( aProfile ) )
681 else // Just store the sequence of objects as is
683 bool anIsToUpdate = true;
685 HYDROData_SequenceOfObjects anOldProfiles = GetProfiles();
686 if ( anOldProfiles.Length() == theProfiles.Length() )
688 anIsToUpdate = false;
690 for ( int i = 1; i <= theProfiles.Length(); ++i )
692 Handle(HYDROData_Entity) anOldProfile = anOldProfiles.Value( i );
693 Handle(HYDROData_Entity) aNewProfile = theProfiles.Value( i );
694 if ( !IsEqual( anOldProfile, aNewProfile ) )
702 SetReferenceObjects( theProfiles, DataTag_Profile );
711 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
713 return GetReferenceObjects( DataTag_Profile );
716 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
718 if ( theProfile.IsNull() )
721 int aProfileIndex = -1;
723 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
724 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
725 for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
727 Handle(HYDROData_Profile) aProfile =
728 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
729 if ( aProfile.IsNull() )
732 if ( IsEqual( theProfile, aProfile ) )
739 if ( aProfileIndex == -1 )
742 RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
744 // Remove parameter for removed profile
745 removeParameter( aProfileIndex );
747 // Indicate model of the need to update the stream presentation
753 void HYDROData_Stream::RemoveProfiles()
755 bool anIsToUpdate = IsMustBeUpdated() || NbReferenceObjects( DataTag_Profile ) > 0;
757 ClearReferenceObjects( DataTag_Profile );
759 // Remove the parameters array
760 removeParametersArray();
762 // Indicate model of the need to update the stream presentation
763 SetToUpdate( anIsToUpdate );
766 void HYDROData_Stream::insertProfileInToOrder( const Handle(HYDROData_Profile)& theProfile,
767 const int theBeforeIndex )
769 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
770 if ( theProfile.IsNull() || aHydAxis.IsNull() )
773 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
774 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
775 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
778 if ( theBeforeIndex == -1 )
779 AddReferenceObject( theProfile, DataTag_Profile );
781 InsertReferenceObject( theProfile, DataTag_Profile, theBeforeIndex );
784 bool HYDROData_Stream::BuildFace( const Handle(HYDROData_PolylineXY)& theHydAxis,
785 TopoDS_Face& thePlane )
787 if ( !IsValidAsAxis( theHydAxis ) )
790 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() );
792 gp_Ax2 aX2(gp::XOY());
796 BRepBndLib::Add(aHydraulicWire,B);
797 Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
798 B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
799 BRepBuilderAPI_MakeFace aMkr(aPln, axmin-500., axmax+500., aymin-500., aymax+500.); // to be tuned later according max/ Profile deviation
800 if(!aMkr.IsDone() || aMkr.Shape().IsNull()) return false;
801 thePlane = TopoDS::Face(aMkr.Shape());
805 void HYDROData_Stream::updateProfilesOrder()
807 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
808 if ( aRefProfiles.IsEmpty() )
811 // At first we remove all profiles from order
814 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
815 if ( aHydAxis.IsNull() )
819 if ( !BuildFace( aHydAxis, aPlane ) )
822 Standard_Real aPar( .0 );
826 TopoDS_Compound aCmp;
827 aBB.MakeCompound(aCmp);
830 HYDROData_DataMapOfRealOfHDProfile aDM;
831 TColStd_ListOfReal aList;
832 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
833 for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
835 Handle(HYDROData_Profile) aProfile =
836 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
838 TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
839 aBB.Add( aCmp, aProfileWire );
841 if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
844 aDM.Bind( aPar, aProfile );
845 aList.Append( aPar );
848 if ( aList.IsEmpty() )
851 TColStd_Array1OfReal anArr( 1, aList.Extent() );
853 TColStd_ListIteratorOfListOfReal it( aList );
854 for ( int j = 1; it.More(); it.Next(), j++ )
855 anArr( j ) = it.Value();
858 if ( aList.Extent() > 1 )
860 TCollection_CompareOfReal Compar;
861 SortTools_QuickSortOfReal::Sort( anArr, Compar );
863 for (int j = 1; j <= anArr.Length(); j++) {
864 const Standard_Real aKey = anArr(j);
865 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
866 insertProfileInToOrder( aProfile );
868 } else if ( aList.Extent() == 1 ) {
869 const Standard_Real aKey = aList.Last();
870 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
871 insertProfileInToOrder( aProfile );
874 setParametersArray( anArr );
877 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
878 BRepTools::Write(aHydraulicWire, "Path.brep");
879 BRepTools::Write(aCmp, "Prof.brep");
883 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
885 return KIND_STREAM_ALTITUDE;
888 void HYDROData_Stream::setParametersArray( const TColStd_Array1OfReal& theArray )
890 if ( theArray.Length() == 0 )
892 removeParametersArray();
896 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
898 Handle(TDataStd_RealArray) aParamsArray =
899 TDataStd_RealArray::Set( aLabel, theArray.Lower(), theArray.Upper() );
901 for ( int i = theArray.Lower(), n = theArray.Upper(); i <= n; ++i )
903 const Standard_Real& aParam = theArray( i );
904 aParamsArray->SetValue( i, aParam );
908 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
910 TColStd_Array1OfReal* anArray = NULL;
912 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
913 if ( !aLabel.IsNull() )
915 Handle(TDataStd_RealArray) aParamsArray;
916 if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
918 anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
919 for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
921 const Standard_Real& aParam = aParamsArray->Value( i );
922 anArray->SetValue( i, aParam );
930 void HYDROData_Stream::removeParametersArray()
932 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
933 if ( !aLabel.IsNull() )
934 aLabel.ForgetAllAttributes();
937 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
941 TColStd_Array1OfReal* anArr = getParametersArray();
946 TColStd_Array1OfReal aNewArr( anArr->Lower(), anArr->Upper() + 1 );
948 bool isInserted = false;
949 for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
951 const Standard_Real& aStoredParam = anArr->Value( i );
954 if ( theParam > aStoredParam )
960 aNewArr( j ) = theParam;
966 aNewArr( j ) = aStoredParam;
972 aNewArr( aNewArr.Upper() ) = theParam;
975 setParametersArray( aNewArr );
980 TColStd_Array1OfReal aNewArr( 1, 1 );
981 aNewArr.SetValue( 1, theParam );
982 setParametersArray( aNewArr );
988 void HYDROData_Stream::removeParameter( const int& theIndex )
990 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
991 if ( aLabel.IsNull() )
994 Handle(TDataStd_RealArray) aParamsArray;
995 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
998 if ( aParamsArray->Length() == 1 )
1000 removeParametersArray();
1004 TColStd_Array1OfReal aNewArr( aParamsArray->Lower(), aParamsArray->Upper() - 1 );
1006 for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
1008 const Standard_Real& aStoredParam = aParamsArray->Value( i );
1009 if ( k == theIndex )
1012 aNewArr.SetValue( j, aStoredParam );
1016 setParametersArray( aNewArr );
1019 bool HYDROData_Stream::GenerateBottomPolyline()
1022 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1023 if ( aDocument.IsNull() ) {
1027 // Collect bottom points ( one bottom point from each profile of the stream )
1028 HYDROData_Profile::ProfilePoints aBottomPoints;
1030 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
1031 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; i++ ) {
1032 const Handle(HYDROData_Profile) aProfile =
1033 Handle(HYDROData_Profile)::DownCast( aSeqOfProfiles.Value( i ) );
1034 if ( aProfile.IsNull() ) {
1038 aBottomPoints.Append( aProfile->GetBottomPoint() );
1041 int aNbBottomPoints = aBottomPoints.Size();
1043 if ( aNbBottomPoints < 2 ) {
1047 // Create bottom polyline object if the stream doesn't contain it yet
1048 Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1049 if ( aBottom.IsNull() ) {
1050 aBottom = Handle(HYDROData_Polyline3D)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
1051 QString aBaseName = GetName() + "_bottom";
1052 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
1053 aBottom->SetName( aName );
1055 SetReferenceObject( aBottom, DataTag_BottomPolyline );
1058 // Create 2D polyline if the bottom polyline doesn't contain it yet
1059 Handle(HYDROData_PolylineXY) aPolylineXY = aBottom->GetPolylineXY();
1060 if ( aPolylineXY.IsNull() ) {
1061 aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
1062 QString aBaseName = GetName() + "_bottom_2d";
1063 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
1064 aPolylineXY->SetName( aName );
1065 aBottom->SetPolylineXY( aPolylineXY, false );
1068 aPolylineXY->RemoveSections();
1069 aPolylineXY->AddSection( "", HYDROData_PolylineXY::SECTION_SPLINE, false );
1071 // Create profile if the bottom polyline doesn't contain it yet
1072 Handle(HYDROData_ProfileUZ) aProfileUZ = aBottom->GetProfileUZ();
1073 if ( aProfileUZ.IsNull() ) {
1074 Handle(HYDROData_Profile) aProfile =
1075 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
1076 QString aBaseName = GetName() + "_bottom_profile";
1077 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
1078 aProfile->SetName( aName );
1079 aProfileUZ = aProfile->GetProfileUZ( true );
1080 aBottom->SetProfileUZ( aProfileUZ );
1083 aProfileUZ->RemoveSection( 0 );
1086 for ( int i = 1; i <= aNbBottomPoints; i++ ) {
1087 const HYDROData_Profile::ProfilePoint& aBottomPoint = aBottomPoints.Value( i );
1088 aPolylineXY->AddPoint( 0, HYDROData_PolylineXY::Point( aBottomPoint.X(), aBottomPoint.Y() ) );
1091 // Calculate profile UZ points
1094 const HYDROData_Profile::ProfilePoint& aFirstBottomPoint = aBottomPoints.First();
1095 aProfileUZ->AddPoint( 0, HYDROData_ProfileUZ::Point( 0, aFirstBottomPoint.Z() ) );
1097 // Intermediate points
1098 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylineXY->NbPoints( 0 ) - 1 );
1100 for ( int i = 2, aNbPoints = aBottomPoints.Size(); i < aNbPoints; i++ ) {
1101 const HYDROData_Profile::ProfilePoint& aBottomPoint = aBottomPoints.Value( i );
1103 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
1105 Standard_Real anU = aDistance; // = ( aDistance / aPolylineCommonDist ) * aPolylineCommonDist;
1106 aProfileUZ->AddPoint( 0, HYDROData_ProfileUZ::Point( anU, aBottomPoint.Z() ) );
1110 const HYDROData_Profile::ProfilePoint& aLastBottomPoint = aBottomPoints.Last();
1111 aProfileUZ->AddPoint( 0, HYDROData_ProfileUZ::Point( aPolylineCommonDist, aLastBottomPoint.Z() ) );
1116 Handle(HYDROData_Polyline3D) HYDROData_Stream::GetBottomPolyline() const
1118 return Handle(HYDROData_Polyline3D)::DownCast(
1119 GetReferenceObject( DataTag_BottomPolyline ) );
1122 bool HYDROData_Stream::SetBottomPolyline( const Handle(HYDROData_Polyline3D)& theBottom )
1124 if ( theBottom.IsNull() ) {
1128 SetReferenceObject( theBottom, DataTag_BottomPolyline );
1133 bool HYDROData_Stream::Interpolate( HYDROData_IProfilesInterpolator* theInterpolator )
1136 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1137 if ( aDocument.IsNull() ) {
1141 if ( theInterpolator->GetCalculatedProfilesNumber() < 1 ) {
1142 theInterpolator->Calculate();
1145 if ( theInterpolator->GetErrorCode() != OK ) {
1151 for ( int aProfileInd = 0; aProfileInd < theInterpolator->GetCalculatedProfilesNumber(); aProfileInd++ ) {
1152 // Get calculated point coordinates
1153 HYDROData_Profile::ProfilePoints aResultPoints = theInterpolator->GetResultProfilePoints( aProfileInd );
1154 if ( aResultPoints.IsEmpty() ) {
1159 // Create profile object
1160 Handle(HYDROData_Profile) aProfile =
1161 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
1162 QString aBaseName = GetName() + "_interp_profile";
1163 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName );
1164 aProfile->SetName( aName );
1166 // Fill the profile with points
1167 aProfile->SetProfilePoints( aResultPoints );
1169 // Add profile to the stream
1170 bool isAdded = AddProfile( aProfile );
1184 void HYDROData_Stream::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
1187 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1188 if ( aDocument.IsNull() ) {
1193 HYDROData_Entity::CopyTo( theDestination );
1195 Handle(HYDROData_Stream) aStreamCopy =
1196 Handle(HYDROData_Stream)::DownCast( theDestination );
1198 // Copy bottom polyline if exists
1199 if ( !aStreamCopy.IsNull() ) {
1200 const Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1201 if ( !aBottom.IsNull() ) {
1202 aStreamCopy->ClearReferenceObjects( DataTag_BottomPolyline );
1203 aStreamCopy->GenerateBottomPolyline();
1204 const Handle(HYDROData_Polyline3D) aBottomCopy = aStreamCopy->GetBottomPolyline();
1205 if ( !aBottomCopy.IsNull() && !aBottomCopy->GetPolylineXY().IsNull() ) {
1206 aBottomCopy->GetPolylineXY()->Update();
1207 aBottomCopy->Update();