1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROData_Stream.h"
25 #include "HYDROData_Document.h"
26 #include "HYDROData_PolylineXY.h"
27 #include "HYDROData_Polyline3D.h"
28 #include "HYDROData_Profile.h"
29 #include "HYDROData_ShapesGroup.h"
30 #include "HYDROData_ShapesTool.h"
31 #include "HYDROData_IAltitudeObject.h"
32 #include "HYDROData_IProfilesInterpolator.h"
33 #include "HYDROData_Tool.h"
35 #include <TDataStd_RealArray.hxx>
37 #include <Precision.hxx>
39 #include <NCollection_DataMap.hxx>
41 #include <TColStd_Array1OfReal.hxx>
42 #include <TColStd_ListOfReal.hxx>
43 #include <TColStd_ListIteratorOfListOfReal.hxx>
44 #include <TCollection_CompareOfReal.hxx>
45 #include <TColgp_Array1OfPnt.hxx>
46 #include <TColgp_HArray1OfPnt.hxx>
49 #include <TopoDS_Wire.hxx>
50 #include <TopoDS_Shell.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Vertex.hxx>
55 #include <TopExp_Explorer.hxx>
57 #include <Bnd_Box.hxx>
59 #include <BRep_Builder.hxx>
60 #include <BRepBuilderAPI_MakeEdge.hxx>
61 #include <BRepBuilderAPI_MakeWire.hxx>
62 #include <BRepBuilderAPI_MakeFace.hxx>
64 #include <BRepBndLib.hxx>
65 #include <BRepProj_Projection.hxx>
66 #include <BRepExtrema_ExtCC.hxx>
67 #include <BRepCheck_Analyzer.hxx>
77 #include <GeomAPI_Interpolate.hxx>
78 #include <Geom_BSplineCurve.hxx>
80 #include <TopTools_Array1OfShape.hxx>
82 #include <SortTools_QuickSortOfReal.hxx>
85 #include <QStringList>
87 //#define DEB_STREAM 1
89 //#define DEB_HASINT 1
90 //#define DEB_UPDATE 1
91 #include <BRepTools.hxx>
92 #include <TCollection_AsciiString.hxx>
95 typedef NCollection_DataMap<Standard_Real, Handle(HYDROData_Profile)> HYDROData_DataMapOfRealOfHDProfile;
97 IMPLEMENT_STANDARD_HANDLE(HYDROData_Stream,HYDROData_NaturalObject)
98 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Stream,HYDROData_NaturalObject)
101 HYDROData_Stream::HYDROData_Stream()
102 : HYDROData_NaturalObject()
106 HYDROData_Stream::~HYDROData_Stream()
110 QStringList HYDROData_Stream::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
112 QStringList aResList = dumpObjectCreation( theTreatedObjects );
113 QString aName = GetObjPyName();
115 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
116 setPythonReferenceObject( theTreatedObjects, aResList, aHydAxis, "SetHydraulicAxis" );
118 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
119 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; ++i )
121 const Handle(HYDROData_Entity) aProfile = aSeqOfProfiles.Value( i );
122 setPythonReferenceObject( theTreatedObjects, aResList, aProfile, "AddProfile" );
125 // Set bottom polyline if exists
126 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
127 if ( !aBottomPolyline.IsNull() ) {
128 setPythonReferenceObject( theTreatedObjects, aResList, aBottomPolyline, "SetBottomPolyline" );
131 aResList << QString( "" );
132 aResList << QString( "%1.Update();" ).arg( aName );
133 aResList << QString( "" );
138 HYDROData_SequenceOfObjects HYDROData_Stream::GetAllReferenceObjects() const
140 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
142 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
143 if ( !aHydAxis.IsNull() )
144 aResSeq.Append( aHydAxis );
146 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
147 aResSeq.Append( aSeqOfProfiles );
152 TopoDS_Shape HYDROData_Stream::GetTopShape() const
154 return getTopShape();
157 TopoDS_Shape HYDROData_Stream::GetShape3D() const
162 Handle(Geom_BSplineCurve) HYDROData_Stream::buildInterpolationCurve(
163 const Handle(TColgp_HArray1OfPnt)& theArrayOfPnt )
165 Handle(Geom_BSplineCurve) aBSpline;
166 GeomAPI_Interpolate anInterpolator (theArrayOfPnt, Standard_False, 1.0e-5);
167 anInterpolator.Perform() ;
168 if (anInterpolator.IsDone())
169 aBSpline = anInterpolator.Curve();
173 void HYDROData_Stream::Update()
175 updateProfilesOrder();
177 // Update bottom polyline if exists
178 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
179 if ( !aBottomPolyline.IsNull() ) {
180 if ( GenerateBottomPolyline() ) {
181 Handle(HYDROData_PolylineXY) aPolylineXY = aBottomPolyline->GetPolylineXY();
182 if ( !aPolylineXY.IsNull() ) {
183 aPolylineXY->Update();
185 aBottomPolyline->Update();
192 bool HYDROData_Stream::IsHas2dPrs() const
197 bool HYDROData_Stream::CreatePresentations( const Handle(HYDROData_PolylineXY)& theHydAxis,
198 const HYDROData_SequenceOfObjects& theProfiles,
199 PrsDefinition& thePrs )
201 if ( theHydAxis.IsNull() || theProfiles.Length() < 2 )
204 gp_Pnt aPrevFirstPoint, aPrevLastPoint;
205 Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, theProfiles.Length());
206 Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, theProfiles.Length());
207 TopTools_Array1OfShape anArrOfProfiles(1, theProfiles.Length());
208 TopTools_Array1OfShape anArrOf2DProfiles(1, theProfiles.Length());
211 HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
212 for (int i=1 ; anIter.More(); anIter.Next(),i++ )
214 Handle(HYDROData_Profile) aProfile =
215 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
216 if ( aProfile.IsNull() )
219 const TopoDS_Shape& aProf3d = aProfile->GetShape3D();
221 if ( !aProfile->GetLeftPoint( aPnt1, false ) || !aProfile->GetRightPoint( aPnt2, false ) )
224 anArrOfProfiles.SetValue(i,aProfile->GetShape3D());//aProfile->GetTopShape();
225 anArrOf2DProfiles.SetValue(i,aProfile->GetTopShape());
227 gp_Pnt aCurFirstPoint( aPnt1.X(), aPnt1.Y(), 0 ), aCurFP;
228 gp_Pnt aCurLastPoint( aPnt2.X(), aPnt2.Y(), 0 ), aCurLP;
229 TopoDS_Vertex aV1, aV2;
230 TopExp::Vertices(TopoDS::Wire(aProf3d), aV1, aV2);
231 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
232 if(aP1.X() == aPnt1.X() && aP1.Y() == aPnt1.Y())
236 aP1 = BRep_Tool::Pnt(aV2);
237 if(aP1.X() == aPnt2.X() && aP1.Y() == aPnt2.Y())
241 anArrayOfFPnt->SetValue(i,aCurFP);
242 anArrayOfLPnt->SetValue(i,aCurLP);
245 // Construct of the 3D presentation
246 Handle(Geom_BSplineCurve) aBSpline = buildInterpolationCurve (anArrayOfFPnt);
247 if(aBSpline.IsNull())
250 TopoDS_Edge anEdgLeft, anEdgRight;
252 BRepBuilderAPI_MakeEdge aMakeEdge(aBSpline);
253 if(aMakeEdge.IsDone())
254 anEdgLeft = aMakeEdge.Edge();
256 if(anEdgLeft.IsNull())
260 aBSpline = buildInterpolationCurve (anArrayOfLPnt);
261 if(aBSpline.IsNull())
264 aMakeEdge.Init(aBSpline);
265 if(aMakeEdge.IsDone())
266 anEdgRight = aMakeEdge.Edge();
268 if(anEdgRight.IsNull())
272 TopoDS_Compound aCmp;
273 aBB.MakeCompound(aCmp);
274 anIter.Init( theProfiles );
275 for (int i=1 ; i < anArrOfProfiles.Length() +1; i++ )
276 aBB.Add(aCmp, anArrOfProfiles.Value(i));
278 aBB.Add(aCmp,anEdgLeft);
279 aBB.Add(aCmp,anEdgRight);
280 BRepCheck_Analyzer aCh(aCmp);
282 thePrs.myPrs3D = aCmp;
285 BRepTools::Write(aCmp, "str3d.brep");
286 thePrs.myPrs3D = aCmp;
290 // Construct the top presentation
291 for(int i=1;i<= anArrayOfLPnt->Length();i++) {
292 gp_Pnt aPnt = anArrayOfFPnt->Value(i);
293 aPnt.SetZ(.0); // make 2d
294 anArrayOfFPnt->SetValue(i, aPnt);
295 aPnt = anArrayOfLPnt->Value(i);
297 anArrayOfLPnt->SetValue(i, aPnt);
301 aBSpline = buildInterpolationCurve (anArrayOfFPnt);
302 if(aBSpline.IsNull())
305 aMakeEdge.Init(aBSpline);
306 if(aMakeEdge.IsDone())
307 anEdgLeft = aMakeEdge.Edge();
310 aBSpline = buildInterpolationCurve (anArrayOfLPnt);
311 if(aBSpline.IsNull())
314 aMakeEdge.Init(aBSpline);
315 if(aMakeEdge.IsDone())
316 anEdgRight = aMakeEdge.Edge();
317 if(anEdgRight.IsNull())
320 BRepBuilderAPI_MakeEdge aMakeEdge2(anArrayOfFPnt->Value(1),anArrayOfLPnt->Value(1));
321 TopoDS_Edge aBotEdge, aTopEdge;
322 if(aMakeEdge2.IsDone())
323 aBotEdge = aMakeEdge2.Edge();
325 BRepBuilderAPI_MakeEdge aMakeEdge3(anArrayOfFPnt->Value(anArrayOfFPnt->Length()),anArrayOfLPnt->Value(anArrayOfLPnt->Length()));
326 if(aMakeEdge3.IsDone())
327 aTopEdge = aMakeEdge3.Edge();
329 // Make wire for 2D presentation with updating of corresponding edges
330 BRepBuilderAPI_MakeWire aMakeWire;
332 aMakeWire.Add( aBotEdge );
333 thePrs.myInlet = aMakeWire.Edge();
335 aMakeWire.Add( anEdgLeft );
336 thePrs.myLeftBank = aMakeWire.Edge();
338 aMakeWire.Add( aTopEdge );
339 thePrs.myOutlet = aMakeWire.Edge();
341 aMakeWire.Add( anEdgRight );
342 thePrs.myRightBank = aMakeWire.Edge();
344 TopoDS_Wire aSectProfileWire;
345 if(aMakeWire.IsDone())
346 aSectProfileWire = aMakeWire.Wire();
348 BRepBuilderAPI_MakeFace aMakeFace( aSectProfileWire, Standard_True );
351 if( aMakeFace.IsDone() )
352 aFace = aMakeFace.Face();
355 aBB.MakeCompound(aCmp);
357 for(int i=1;i <= anArrOf2DProfiles.Length(); i++)
358 aBB.Add(aCmp,anArrOf2DProfiles.Value(i));
362 thePrs.myPrs2D = aCmp;
365 BRepTools::Write(aCmp, "str2d.brep");
366 thePrs.myPrs2D = aCmp;
373 void HYDROData_Stream::UpdatePrs()
375 HYDROData_NaturalObject::Update();
377 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
378 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
380 PrsDefinition aResultPrs;
381 if ( !CreatePresentations( aHydAxis, aRefProfiles, aResultPrs ) )
384 SetShape3D( aResultPrs.myPrs3D );
385 SetTopShape( aResultPrs.myPrs2D );
387 // Create the stream groups
388 QString aLeftGroupName = GetName() + "_Left_Bank";
390 Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
391 aLeftGroup->SetName( aLeftGroupName );
392 aLeftGroup->AddShape( aResultPrs.myLeftBank );
394 QString aRightGroupName = GetName() + "_Right_Bank";
396 Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
397 aRightGroup->SetName( aRightGroupName );
398 aRightGroup->AddShape( aResultPrs.myRightBank );
400 QString anInGroupName = GetName() + "_Inlet";
402 Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
403 anInGroup->SetName( anInGroupName );
404 anInGroup->AddShape( aResultPrs.myInlet );
406 QString anOutGroupName = GetName() + "_Outlet";
408 Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
409 anOutGroup->SetName( anOutGroupName );
410 anOutGroup->AddShape( aResultPrs.myOutlet );
413 QColor HYDROData_Stream::DefaultFillingColor()
415 return QColor( Qt::green );
418 QColor HYDROData_Stream::DefaultBorderColor()
420 return QColor( Qt::transparent );
423 bool HYDROData_Stream::IsValidAsAxis( const Handle(HYDROData_PolylineXY)& theHydAxis )
425 if ( theHydAxis.IsNull() )
428 TopoDS_Shape aHydraulicShape = theHydAxis->GetShape();
429 if ( aHydraulicShape.IsNull() ||
430 aHydraulicShape.ShapeType() != TopAbs_WIRE ||
431 BRep_Tool::IsClosed( aHydraulicShape ) )
432 return false; // The polyline must be a single not closed wire
437 TopoDS_Shape getShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
438 const int theGroupId )
440 TopoDS_Shape aResShape;
441 if ( theGroups.Length() != 4 )
444 Handle(HYDROData_ShapesGroup) aGroup =
445 Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
446 if ( aGroup.IsNull() )
449 TopTools_SequenceOfShape aGroupShapes;
450 aGroup->GetShapes( aGroupShapes );
452 if ( !aGroupShapes.IsEmpty() )
453 aResShape = aGroupShapes.First();
458 TopoDS_Shape HYDROData_Stream::GetLeftShape() const
460 HYDROData_SequenceOfObjects aGroups = GetGroups();
461 return getShapeFromGroup( aGroups, 1 );
464 TopoDS_Shape HYDROData_Stream::GetRightShape() const
466 HYDROData_SequenceOfObjects aGroups = GetGroups();
467 return getShapeFromGroup( aGroups, 2 );
470 TopoDS_Shape HYDROData_Stream::GetInletShape() const
472 HYDROData_SequenceOfObjects aGroups = GetGroups();
473 return getShapeFromGroup( aGroups, 3 );
476 TopoDS_Shape HYDROData_Stream::GetOutletShape() const
478 HYDROData_SequenceOfObjects aGroups = GetGroups();
479 return getShapeFromGroup( aGroups, 4 );
482 QColor HYDROData_Stream::getDefaultFillingColor() const
484 return DefaultFillingColor();
487 QColor HYDROData_Stream::getDefaultBorderColor() const
489 return DefaultBorderColor();
492 bool HYDROData_Stream::SetHydraulicAxis( const Handle(HYDROData_PolylineXY)& theAxis )
494 if ( !IsValidAsAxis( theAxis ) )
497 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
498 if ( IsEqual( aPrevAxis, theAxis ) )
501 SetReferenceObject( theAxis, DataTag_HydraulicAxis );
503 // Update the order of profiles
504 updateProfilesOrder();
506 // Indicate model of the need to update the stream presentation
512 Handle(HYDROData_PolylineXY) HYDROData_Stream::GetHydraulicAxis() const
514 return Handle(HYDROData_PolylineXY)::DownCast(
515 GetReferenceObject( DataTag_HydraulicAxis ) );
518 void HYDROData_Stream::RemoveHydraulicAxis()
520 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
521 if ( aPrevAxis.IsNull() )
524 ClearReferenceObjects( DataTag_HydraulicAxis );
526 // We remove the reference profiles
529 // Indicate model of the need to update the stream presentation
533 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile,
534 const TopoDS_Face& thePlane,
535 Standard_Real& theOutPar ) const
537 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
538 return HasIntersection( aHydAxis, theProfile, thePlane, theOutPar );
541 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_PolylineXY)& theHydAxis,
542 const Handle(HYDROData_Profile)& theProfile,
543 const TopoDS_Face& thePlane,
544 Standard_Real& theOutPar )
546 if ( theProfile.IsNull() || !IsValidAsAxis( theHydAxis ) )
549 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() ); //guide line
550 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
551 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
554 BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
555 if(!aProjector.IsDone())
557 TopoDS_Shape aPrjProfile = aProjector.Shape();
558 if(aPrjProfile.IsNull())
560 TopoDS_Vertex aV1, aV2;
561 if(aPrjProfile.ShapeType() == TopAbs_EDGE)
562 TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);
563 else if(aPrjProfile.ShapeType() == TopAbs_WIRE)
564 TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);
565 else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
566 TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
568 TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);
570 anExp.Init(aPrjProfile, TopAbs_EDGE);
572 TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);
576 if(aV1.IsNull() || aV2.IsNull())
578 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
579 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
582 BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2);
585 const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
586 Standard_Integer aNum(0);
588 TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
589 for(;anExplo.More();anExplo.Next()) aNum++;
590 // check for self-intersection
591 const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
592 Standard_Boolean hasInt(false);
593 Standard_Real aSqDist(DBL_MAX);
594 Standard_Integer anIndx(0);
595 BRepExtrema_ExtCC aCC;
596 aCC.Initialize(anEdg2);
598 anExplo.Init(aHydraulicWire, TopAbs_EDGE);
599 for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
600 const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
603 Standard_Boolean hasSol(false);
607 for(Standard_Integer i=1; i<= aCC.NbExt();i++)
608 if(aCC.SquareDistance(i) < aSqDist) {
609 aSqDist = aCC.SquareDistance(i);
615 if(aSqDist <= SquareTolerance) { // hasInt
616 const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
618 TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
619 theOutPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
621 Standard_Real aPar = aCC.ParameterOnE1(anIndx);
629 TopExp::Vertices(anEdg1, aV1, aV2);
630 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
633 } else if(aNum > 1) {
634 TopExp::Vertices(anEdg1, aV1, aV2);
635 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
643 bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
645 if ( theProfile.IsNull() )
648 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
649 if ( aHydAxis.IsNull() )
653 if(!BuildFace(aHydAxis, aPlane))
656 Standard_Real aPar(.0);
657 if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
658 return false; // Object is already in reference list or it has no intersection
660 int aProfileIndex = insertParameter( aPar );
661 insertProfileInToOrder( theProfile, aProfileIndex );
663 // Indicate model of the need to update the stream presentation
669 bool HYDROData_Stream::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles,
670 const bool& theIsToOrder )
674 for ( int i = 1; i <= theProfiles.Length(); ++i )
676 Handle(HYDROData_Profile) aProfile =
677 Handle(HYDROData_Profile)::DownCast( theProfiles.Value( i ) );
678 if ( aProfile.IsNull() )
681 if ( !AddProfile( aProfile ) )
685 else // Just store the sequence of objects as is
687 bool anIsToUpdate = true;
689 HYDROData_SequenceOfObjects anOldProfiles = GetProfiles();
690 if ( anOldProfiles.Length() == theProfiles.Length() )
692 anIsToUpdate = false;
694 for ( int i = 1; i <= theProfiles.Length(); ++i )
696 Handle(HYDROData_Entity) anOldProfile = anOldProfiles.Value( i );
697 Handle(HYDROData_Entity) aNewProfile = theProfiles.Value( i );
698 if ( !IsEqual( anOldProfile, aNewProfile ) )
706 SetReferenceObjects( theProfiles, DataTag_Profile );
715 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
717 return GetReferenceObjects( DataTag_Profile );
720 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
722 if ( theProfile.IsNull() )
725 int aProfileIndex = -1;
727 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
728 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
729 for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
731 Handle(HYDROData_Profile) aProfile =
732 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
733 if ( aProfile.IsNull() )
736 if ( IsEqual( theProfile, aProfile ) )
743 if ( aProfileIndex == -1 )
746 RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
748 // Remove parameter for removed profile
749 removeParameter( aProfileIndex );
751 // Indicate model of the need to update the stream presentation
757 void HYDROData_Stream::RemoveProfiles()
759 bool anIsToUpdate = IsMustBeUpdated() || NbReferenceObjects( DataTag_Profile ) > 0;
761 ClearReferenceObjects( DataTag_Profile );
763 // Remove the parameters array
764 removeParametersArray();
766 // Indicate model of the need to update the stream presentation
767 SetToUpdate( anIsToUpdate );
770 void HYDROData_Stream::insertProfileInToOrder( const Handle(HYDROData_Profile)& theProfile,
771 const int theBeforeIndex )
773 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
774 if ( theProfile.IsNull() || aHydAxis.IsNull() )
777 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
778 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
779 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
782 if ( theBeforeIndex == -1 )
783 AddReferenceObject( theProfile, DataTag_Profile );
785 InsertReferenceObject( theProfile, DataTag_Profile, theBeforeIndex );
788 bool HYDROData_Stream::BuildFace( const Handle(HYDROData_PolylineXY)& theHydAxis,
789 TopoDS_Face& thePlane )
791 if ( !IsValidAsAxis( theHydAxis ) )
794 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() );
796 gp_Ax2 aX2(gp::XOY());
800 BRepBndLib::Add(aHydraulicWire,B);
801 Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
802 B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
803 BRepBuilderAPI_MakeFace aMkr(aPln, axmin-500., axmax+500., aymin-500., aymax+500.); // to be tuned later according max/ Profile deviation
804 if(!aMkr.IsDone() || aMkr.Shape().IsNull()) return false;
805 thePlane = TopoDS::Face(aMkr.Shape());
809 void HYDROData_Stream::updateProfilesOrder()
811 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
812 if ( aRefProfiles.IsEmpty() )
815 // At first we remove all profiles from order
818 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
819 if ( aHydAxis.IsNull() )
823 if ( !BuildFace( aHydAxis, aPlane ) )
826 Standard_Real aPar( .0 );
830 TopoDS_Compound aCmp;
831 aBB.MakeCompound(aCmp);
834 HYDROData_DataMapOfRealOfHDProfile aDM;
835 TColStd_ListOfReal aList;
836 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
837 for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
839 Handle(HYDROData_Profile) aProfile =
840 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
842 TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
843 aBB.Add( aCmp, aProfileWire );
845 if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
848 aDM.Bind( aPar, aProfile );
849 aList.Append( aPar );
852 if ( aList.IsEmpty() )
855 TColStd_Array1OfReal anArr( 1, aList.Extent() );
857 TColStd_ListIteratorOfListOfReal it( aList );
858 for ( int j = 1; it.More(); it.Next(), j++ )
859 anArr( j ) = it.Value();
862 if ( aList.Extent() > 1 )
864 TCollection_CompareOfReal Compar;
865 SortTools_QuickSortOfReal::Sort( anArr, Compar );
867 for (int j = 1; j <= anArr.Length(); j++) {
868 const Standard_Real aKey = anArr(j);
869 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
870 insertProfileInToOrder( aProfile );
872 } else if ( aList.Extent() == 1 ) {
873 const Standard_Real aKey = aList.Last();
874 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
875 insertProfileInToOrder( aProfile );
878 setParametersArray( anArr );
881 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
882 BRepTools::Write(aHydraulicWire, "Path.brep");
883 BRepTools::Write(aCmp, "Prof.brep");
887 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
889 return KIND_STREAM_ALTITUDE;
892 void HYDROData_Stream::setParametersArray( const TColStd_Array1OfReal& theArray )
894 if ( theArray.Length() == 0 )
896 removeParametersArray();
900 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
902 Handle(TDataStd_RealArray) aParamsArray =
903 TDataStd_RealArray::Set( aLabel, theArray.Lower(), theArray.Upper() );
905 for ( int i = theArray.Lower(), n = theArray.Upper(); i <= n; ++i )
907 const Standard_Real& aParam = theArray( i );
908 aParamsArray->SetValue( i, aParam );
912 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
914 TColStd_Array1OfReal* anArray = NULL;
916 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
917 if ( !aLabel.IsNull() )
919 Handle(TDataStd_RealArray) aParamsArray;
920 if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
922 anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
923 for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
925 const Standard_Real& aParam = aParamsArray->Value( i );
926 anArray->SetValue( i, aParam );
934 void HYDROData_Stream::removeParametersArray()
936 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
937 if ( !aLabel.IsNull() )
938 aLabel.ForgetAllAttributes();
941 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
945 TColStd_Array1OfReal* anArr = getParametersArray();
950 TColStd_Array1OfReal aNewArr( anArr->Lower(), anArr->Upper() + 1 );
952 bool isInserted = false;
953 for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
955 const Standard_Real& aStoredParam = anArr->Value( i );
958 if ( theParam > aStoredParam )
964 aNewArr( j ) = theParam;
970 aNewArr( j ) = aStoredParam;
976 aNewArr( aNewArr.Upper() ) = theParam;
979 setParametersArray( aNewArr );
984 TColStd_Array1OfReal aNewArr( 1, 1 );
985 aNewArr.SetValue( 1, theParam );
986 setParametersArray( aNewArr );
992 void HYDROData_Stream::removeParameter( const int& theIndex )
994 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
995 if ( aLabel.IsNull() )
998 Handle(TDataStd_RealArray) aParamsArray;
999 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
1002 if ( aParamsArray->Length() == 1 )
1004 removeParametersArray();
1008 TColStd_Array1OfReal aNewArr( aParamsArray->Lower(), aParamsArray->Upper() - 1 );
1010 for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
1012 const Standard_Real& aStoredParam = aParamsArray->Value( i );
1013 if ( k == theIndex )
1016 aNewArr.SetValue( j, aStoredParam );
1020 setParametersArray( aNewArr );
1023 bool HYDROData_Stream::GenerateBottomPolyline()
1026 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1027 if ( aDocument.IsNull() ) {
1031 // Collect bottom points ( one bottom point from each profile of the stream )
1032 HYDROData_Profile::ProfilePoints aBottomPoints;
1034 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
1035 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; i++ ) {
1036 const Handle(HYDROData_Profile) aProfile =
1037 Handle(HYDROData_Profile)::DownCast( aSeqOfProfiles.Value( i ) );
1038 if ( aProfile.IsNull() ) {
1042 aBottomPoints.Append( aProfile->GetBottomPoint() );
1045 int aNbBottomPoints = aBottomPoints.Size();
1047 if ( aNbBottomPoints < 2 ) {
1051 // Create bottom polyline object if the stream doesn't contain it yet
1052 Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1053 if ( aBottom.IsNull() ) {
1054 aBottom = Handle(HYDROData_Polyline3D)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
1055 QString aBaseName = GetName() + "_bottom";
1056 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
1057 aBottom->SetName( aName );
1059 SetReferenceObject( aBottom, DataTag_BottomPolyline );
1062 // Create 2D polyline if the bottom polyline doesn't contain it yet
1063 Handle(HYDROData_PolylineXY) aPolylineXY = aBottom->GetPolylineXY();
1064 if ( aPolylineXY.IsNull() ) {
1065 aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
1066 QString aBaseName = GetName() + "_bottom_2d";
1067 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
1068 aPolylineXY->SetName( aName );
1069 aBottom->SetPolylineXY( aPolylineXY, false );
1072 aPolylineXY->RemoveSections();
1073 aPolylineXY->AddSection( "", HYDROData_PolylineXY::SECTION_SPLINE, false );
1075 // Create profile if the bottom polyline doesn't contain it yet
1076 Handle(HYDROData_ProfileUZ) aProfileUZ = aBottom->GetProfileUZ();
1077 if ( aProfileUZ.IsNull() ) {
1078 Handle(HYDROData_Profile) aProfile =
1079 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
1080 QString aBaseName = GetName() + "_bottom_profile";
1081 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
1082 aProfile->SetName( aName );
1083 aProfileUZ = aProfile->GetProfileUZ( true );
1084 aBottom->SetProfileUZ( aProfileUZ );
1087 aProfileUZ->RemoveSection( 0 );
1090 for ( int i = 1; i <= aNbBottomPoints; i++ ) {
1091 const HYDROData_Profile::ProfilePoint& aBottomPoint = aBottomPoints.Value( i );
1092 aPolylineXY->AddPoint( 0, HYDROData_PolylineXY::Point( aBottomPoint.X(), aBottomPoint.Y() ) );
1095 // Calculate profile UZ points
1098 const HYDROData_Profile::ProfilePoint& aFirstBottomPoint = aBottomPoints.First();
1099 aProfileUZ->AddPoint( 0, HYDROData_ProfileUZ::Point( 0, aFirstBottomPoint.Z() ) );
1101 // Intermediate points
1102 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylineXY->NbPoints( 0 ) - 1 );
1104 for ( int i = 2, aNbPoints = aBottomPoints.Size(); i < aNbPoints; i++ ) {
1105 const HYDROData_Profile::ProfilePoint& aBottomPoint = aBottomPoints.Value( i );
1107 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
1109 Standard_Real anU = aDistance; // = ( aDistance / aPolylineCommonDist ) * aPolylineCommonDist;
1110 aProfileUZ->AddPoint( 0, HYDROData_ProfileUZ::Point( anU, aBottomPoint.Z() ) );
1114 const HYDROData_Profile::ProfilePoint& aLastBottomPoint = aBottomPoints.Last();
1115 aProfileUZ->AddPoint( 0, HYDROData_ProfileUZ::Point( aPolylineCommonDist, aLastBottomPoint.Z() ) );
1120 Handle(HYDROData_Polyline3D) HYDROData_Stream::GetBottomPolyline() const
1122 return Handle(HYDROData_Polyline3D)::DownCast(
1123 GetReferenceObject( DataTag_BottomPolyline ) );
1126 bool HYDROData_Stream::SetBottomPolyline( const Handle(HYDROData_Polyline3D)& theBottom )
1128 if ( theBottom.IsNull() ) {
1132 SetReferenceObject( theBottom, DataTag_BottomPolyline );
1137 bool HYDROData_Stream::Interpolate( HYDROData_IProfilesInterpolator* theInterpolator )
1140 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1141 if ( aDocument.IsNull() ) {
1145 if ( theInterpolator->GetCalculatedProfilesNumber() < 1 ) {
1146 theInterpolator->Calculate();
1149 if ( theInterpolator->GetErrorCode() != OK ) {
1155 for ( int aProfileInd = 0; aProfileInd < theInterpolator->GetCalculatedProfilesNumber(); aProfileInd++ ) {
1156 // Get calculated point coordinates
1157 HYDROData_Profile::ProfilePoints aResultPoints = theInterpolator->GetResultProfilePoints( aProfileInd );
1158 if ( aResultPoints.IsEmpty() ) {
1163 // Create profile object
1164 Handle(HYDROData_Profile) aProfile =
1165 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
1166 QString aBaseName = GetName() + "_interp_profile";
1167 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName );
1168 aProfile->SetName( aName );
1170 // Fill the profile with points
1171 aProfile->SetProfilePoints( aResultPoints );
1173 // Add profile to the stream
1174 bool isAdded = AddProfile( aProfile );
1188 void HYDROData_Stream::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
1191 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1192 if ( aDocument.IsNull() ) {
1197 HYDROData_Entity::CopyTo( theDestination );
1199 Handle(HYDROData_Stream) aStreamCopy =
1200 Handle(HYDROData_Stream)::DownCast( theDestination );
1202 // Copy bottom polyline if exists
1203 if ( !aStreamCopy.IsNull() ) {
1204 const Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1205 if ( !aBottom.IsNull() ) {
1206 aStreamCopy->ClearReferenceObjects( DataTag_BottomPolyline );
1207 aStreamCopy->GenerateBottomPolyline();
1208 const Handle(HYDROData_Polyline3D) aBottomCopy = aStreamCopy->GetBottomPolyline();
1209 if ( !aBottomCopy.IsNull() && !aBottomCopy->GetPolylineXY().IsNull() ) {
1210 aBottomCopy->GetPolylineXY()->Update();
1211 aBottomCopy->Update();