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_Tool.h"
34 #include <TDataStd_RealArray.hxx>
36 #include <Precision.hxx>
38 #include <NCollection_DataMap.hxx>
40 #include <TColStd_Array1OfReal.hxx>
41 #include <TColStd_ListOfReal.hxx>
42 #include <TColStd_ListIteratorOfListOfReal.hxx>
43 #include <TCollection_CompareOfReal.hxx>
44 #include <TColgp_Array1OfPnt.hxx>
45 #include <TColgp_HArray1OfPnt.hxx>
48 #include <TopoDS_Wire.hxx>
49 #include <TopoDS_Shell.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Vertex.hxx>
54 #include <TopExp_Explorer.hxx>
56 #include <Bnd_Box.hxx>
58 #include <BRep_Builder.hxx>
59 #include <BRepBuilderAPI_MakeEdge.hxx>
60 #include <BRepBuilderAPI_MakeWire.hxx>
61 #include <BRepBuilderAPI_MakeFace.hxx>
63 #include <BRepBndLib.hxx>
64 #include <BRepProj_Projection.hxx>
65 #include <BRepExtrema_ExtCC.hxx>
66 #include <BRepCheck_Analyzer.hxx>
76 #include <GeomAPI_Interpolate.hxx>
77 #include <Geom_BSplineCurve.hxx>
79 #include <TopTools_Array1OfShape.hxx>
81 #include <SortTools_QuickSortOfReal.hxx>
84 #include <QStringList>
86 //#define DEB_STREAM 1
88 //#define DEB_HASINT 1
89 //#define DEB_UPDATE 1
90 #include <BRepTools.hxx>
91 #include <TCollection_AsciiString.hxx>
94 typedef NCollection_DataMap<Standard_Real, Handle(HYDROData_Profile)> HYDROData_DataMapOfRealOfHDProfile;
96 IMPLEMENT_STANDARD_HANDLE(HYDROData_Stream,HYDROData_NaturalObject)
97 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Stream,HYDROData_NaturalObject)
100 HYDROData_Stream::HYDROData_Stream()
101 : HYDROData_NaturalObject()
105 HYDROData_Stream::~HYDROData_Stream()
109 QStringList HYDROData_Stream::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
111 QStringList aResList = dumpObjectCreation( theTreatedObjects );
112 QString aName = GetObjPyName();
114 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
115 setPythonReferenceObject( theTreatedObjects, aResList, aHydAxis, "SetHydraulicAxis" );
117 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
118 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; ++i )
120 const Handle(HYDROData_Entity) aProfile = aSeqOfProfiles.Value( i );
121 setPythonReferenceObject( theTreatedObjects, aResList, aProfile, "AddProfile" );
124 // Create bottom polyline if exists
125 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
126 if ( !aBottomPolyline.IsNull() ) {
127 aResList << QString( "%1.GenerateBottomPolyline();" ).arg( aName );
130 aResList << QString( "" );
131 aResList << QString( "%1.Update();" ).arg( aName );
132 aResList << QString( "" );
137 HYDROData_SequenceOfObjects HYDROData_Stream::GetAllReferenceObjects() const
139 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
141 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
142 if ( !aHydAxis.IsNull() )
143 aResSeq.Append( aHydAxis );
145 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
146 aResSeq.Append( aSeqOfProfiles );
151 TopoDS_Shape HYDROData_Stream::GetTopShape() const
153 return getTopShape();
156 TopoDS_Shape HYDROData_Stream::GetShape3D() const
161 Handle(Geom_BSplineCurve) HYDROData_Stream::buildInterpolationCurve(
162 const Handle(TColgp_HArray1OfPnt)& theArrayOfPnt )
164 Handle(Geom_BSplineCurve) aBSpline;
165 GeomAPI_Interpolate anInterpolator (theArrayOfPnt, Standard_False, 1.0e-5);
166 anInterpolator.Perform() ;
167 if (anInterpolator.IsDone())
168 aBSpline = anInterpolator.Curve();
172 void HYDROData_Stream::Update()
174 updateProfilesOrder();
176 // Update bottom polyline if exists
177 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
178 if ( !aBottomPolyline.IsNull() ) {
179 if ( GenerateBottomPolyline() ) {
180 Handle(HYDROData_PolylineXY) aPolylineXY = aBottomPolyline->GetPolylineXY();
181 if ( !aPolylineXY.IsNull() ) {
182 aPolylineXY->Update();
184 aBottomPolyline->Update();
191 bool HYDROData_Stream::IsHas2dPrs() const
196 bool HYDROData_Stream::CreatePresentations( const Handle(HYDROData_PolylineXY)& theHydAxis,
197 const HYDROData_SequenceOfObjects& theProfiles,
198 PrsDefinition& thePrs )
200 if ( theHydAxis.IsNull() || theProfiles.Length() < 2 )
203 gp_Pnt aPrevFirstPoint, aPrevLastPoint;
204 Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, theProfiles.Length());
205 Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, theProfiles.Length());
206 TopTools_Array1OfShape anArrOfProfiles(1, theProfiles.Length());
207 TopTools_Array1OfShape anArrOf2DProfiles(1, theProfiles.Length());
210 HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
211 for (int i=1 ; anIter.More(); anIter.Next(),i++ )
213 Handle(HYDROData_Profile) aProfile =
214 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
215 if ( aProfile.IsNull() )
218 const TopoDS_Shape& aProf3d = aProfile->GetShape3D();
220 if ( !aProfile->GetLeftPoint( aPnt1, false ) || !aProfile->GetRightPoint( aPnt2, false ) )
223 anArrOfProfiles.SetValue(i,aProfile->GetShape3D());//aProfile->GetTopShape();
224 anArrOf2DProfiles.SetValue(i,aProfile->GetTopShape());
226 gp_Pnt aCurFirstPoint( aPnt1.X(), aPnt1.Y(), 0 ), aCurFP;
227 gp_Pnt aCurLastPoint( aPnt2.X(), aPnt2.Y(), 0 ), aCurLP;
228 TopoDS_Vertex aV1, aV2;
229 TopExp::Vertices(TopoDS::Wire(aProf3d), aV1, aV2);
230 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
231 if(aP1.X() == aPnt1.X() && aP1.Y() == aPnt1.Y())
235 aP1 = BRep_Tool::Pnt(aV2);
236 if(aP1.X() == aPnt2.X() && aP1.Y() == aPnt2.Y())
240 anArrayOfFPnt->SetValue(i,aCurFP);
241 anArrayOfLPnt->SetValue(i,aCurLP);
244 // Construct of the 3D presentation
245 Handle(Geom_BSplineCurve) aBSpline = buildInterpolationCurve (anArrayOfFPnt);
246 if(aBSpline.IsNull())
249 TopoDS_Edge anEdgLeft, anEdgRight;
251 BRepBuilderAPI_MakeEdge aMakeEdge(aBSpline);
252 if(aMakeEdge.IsDone())
253 anEdgLeft = aMakeEdge.Edge();
255 if(anEdgLeft.IsNull())
259 aBSpline = buildInterpolationCurve (anArrayOfLPnt);
260 if(aBSpline.IsNull())
263 aMakeEdge.Init(aBSpline);
264 if(aMakeEdge.IsDone())
265 anEdgRight = aMakeEdge.Edge();
267 if(anEdgRight.IsNull())
271 TopoDS_Compound aCmp;
272 aBB.MakeCompound(aCmp);
273 anIter.Init( theProfiles );
274 for (int i=1 ; i < anArrOfProfiles.Length() +1; i++ )
275 aBB.Add(aCmp, anArrOfProfiles.Value(i));
277 aBB.Add(aCmp,anEdgLeft);
278 aBB.Add(aCmp,anEdgRight);
279 BRepCheck_Analyzer aCh(aCmp);
281 thePrs.myPrs3D = aCmp;
284 BRepTools::Write(aCmp, "str3d.brep");
285 thePrs.myPrs3D = aCmp;
289 // Construct the top presentation
290 for(int i=1;i<= anArrayOfLPnt->Length();i++) {
291 gp_Pnt aPnt = anArrayOfFPnt->Value(i);
292 aPnt.SetZ(.0); // make 2d
293 anArrayOfFPnt->SetValue(i, aPnt);
294 aPnt = anArrayOfLPnt->Value(i);
296 anArrayOfLPnt->SetValue(i, aPnt);
300 aBSpline = buildInterpolationCurve (anArrayOfFPnt);
301 if(aBSpline.IsNull())
304 aMakeEdge.Init(aBSpline);
305 if(aMakeEdge.IsDone())
306 anEdgLeft = aMakeEdge.Edge();
309 aBSpline = buildInterpolationCurve (anArrayOfLPnt);
310 if(aBSpline.IsNull())
313 aMakeEdge.Init(aBSpline);
314 if(aMakeEdge.IsDone())
315 anEdgRight = aMakeEdge.Edge();
316 if(anEdgRight.IsNull())
319 BRepBuilderAPI_MakeEdge aMakeEdge2(anArrayOfFPnt->Value(1),anArrayOfLPnt->Value(1));
320 TopoDS_Edge aBotEdge, aTopEdge;
321 if(aMakeEdge2.IsDone())
322 aBotEdge = aMakeEdge2.Edge();
324 BRepBuilderAPI_MakeEdge aMakeEdge3(anArrayOfFPnt->Value(anArrayOfFPnt->Length()),anArrayOfLPnt->Value(anArrayOfLPnt->Length()));
325 if(aMakeEdge3.IsDone())
326 aTopEdge = aMakeEdge3.Edge();
328 // Make wire for 2D presentation with updating of corresponding edges
329 BRepBuilderAPI_MakeWire aMakeWire;
331 aMakeWire.Add( aBotEdge );
332 thePrs.myInlet = aMakeWire.Edge();
334 aMakeWire.Add( anEdgLeft );
335 thePrs.myLeftBank = aMakeWire.Edge();
337 aMakeWire.Add( aTopEdge );
338 thePrs.myOutlet = aMakeWire.Edge();
340 aMakeWire.Add( anEdgRight );
341 thePrs.myRightBank = aMakeWire.Edge();
343 TopoDS_Wire aSectProfileWire;
344 if(aMakeWire.IsDone())
345 aSectProfileWire = aMakeWire.Wire();
347 BRepBuilderAPI_MakeFace aMakeFace( aSectProfileWire, Standard_True );
350 if( aMakeFace.IsDone() )
351 aFace = aMakeFace.Face();
354 aBB.MakeCompound(aCmp);
356 for(int i=1;i <= anArrOf2DProfiles.Length(); i++)
357 aBB.Add(aCmp,anArrOf2DProfiles.Value(i));
361 thePrs.myPrs2D = aCmp;
364 BRepTools::Write(aCmp, "str2d.brep");
365 thePrs.myPrs2D = aCmp;
372 void HYDROData_Stream::UpdatePrs()
374 HYDROData_NaturalObject::Update();
376 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
377 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
379 PrsDefinition aResultPrs;
380 if ( !CreatePresentations( aHydAxis, aRefProfiles, aResultPrs ) )
383 SetShape3D( aResultPrs.myPrs3D );
384 SetTopShape( aResultPrs.myPrs2D );
386 // Create the stream groups
387 QString aLeftGroupName = GetName() + "_Left_Bank";
389 Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
390 aLeftGroup->SetName( aLeftGroupName );
391 aLeftGroup->AddShape( aResultPrs.myLeftBank );
393 QString aRightGroupName = GetName() + "_Right_Bank";
395 Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
396 aRightGroup->SetName( aRightGroupName );
397 aRightGroup->AddShape( aResultPrs.myRightBank );
399 QString anInGroupName = GetName() + "_Inlet";
401 Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
402 anInGroup->SetName( anInGroupName );
403 anInGroup->AddShape( aResultPrs.myInlet );
405 QString anOutGroupName = GetName() + "_Outlet";
407 Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
408 anOutGroup->SetName( anOutGroupName );
409 anOutGroup->AddShape( aResultPrs.myOutlet );
412 QColor HYDROData_Stream::DefaultFillingColor()
414 return QColor( Qt::green );
417 QColor HYDROData_Stream::DefaultBorderColor()
419 return QColor( Qt::transparent );
422 bool HYDROData_Stream::IsValidAsAxis( const Handle(HYDROData_PolylineXY)& theHydAxis )
424 if ( theHydAxis.IsNull() )
427 TopoDS_Shape aHydraulicShape = theHydAxis->GetShape();
428 if ( aHydraulicShape.IsNull() ||
429 aHydraulicShape.ShapeType() != TopAbs_WIRE ||
430 BRep_Tool::IsClosed( aHydraulicShape ) )
431 return false; // The polyline must be a single not closed wire
436 TopoDS_Shape getShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
437 const int theGroupId )
439 TopoDS_Shape aResShape;
440 if ( theGroups.Length() != 4 )
443 Handle(HYDROData_ShapesGroup) aGroup =
444 Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
445 if ( aGroup.IsNull() )
448 TopTools_SequenceOfShape aGroupShapes;
449 aGroup->GetShapes( aGroupShapes );
451 if ( !aGroupShapes.IsEmpty() )
452 aResShape = aGroupShapes.First();
457 TopoDS_Shape HYDROData_Stream::GetLeftShape() const
459 HYDROData_SequenceOfObjects aGroups = GetGroups();
460 return getShapeFromGroup( aGroups, 1 );
463 TopoDS_Shape HYDROData_Stream::GetRightShape() const
465 HYDROData_SequenceOfObjects aGroups = GetGroups();
466 return getShapeFromGroup( aGroups, 2 );
469 TopoDS_Shape HYDROData_Stream::GetInletShape() const
471 HYDROData_SequenceOfObjects aGroups = GetGroups();
472 return getShapeFromGroup( aGroups, 3 );
475 TopoDS_Shape HYDROData_Stream::GetOutletShape() const
477 HYDROData_SequenceOfObjects aGroups = GetGroups();
478 return getShapeFromGroup( aGroups, 4 );
481 QColor HYDROData_Stream::getDefaultFillingColor() const
483 return DefaultFillingColor();
486 QColor HYDROData_Stream::getDefaultBorderColor() const
488 return DefaultBorderColor();
491 bool HYDROData_Stream::SetHydraulicAxis( const Handle(HYDROData_PolylineXY)& theAxis )
493 if ( !IsValidAsAxis( theAxis ) )
496 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
497 if ( IsEqual( aPrevAxis, theAxis ) )
500 SetReferenceObject( theAxis, DataTag_HydraulicAxis );
502 // Update the order of profiles
503 updateProfilesOrder();
505 // Indicate model of the need to update the stream presentation
511 Handle(HYDROData_PolylineXY) HYDROData_Stream::GetHydraulicAxis() const
513 return Handle(HYDROData_PolylineXY)::DownCast(
514 GetReferenceObject( DataTag_HydraulicAxis ) );
517 void HYDROData_Stream::RemoveHydraulicAxis()
519 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
520 if ( aPrevAxis.IsNull() )
523 ClearReferenceObjects( DataTag_HydraulicAxis );
525 // We remove the reference profiles
528 // Indicate model of the need to update the stream presentation
532 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile,
533 const TopoDS_Face& thePlane,
534 Standard_Real& theOutPar ) const
536 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
537 return HasIntersection( aHydAxis, theProfile, thePlane, theOutPar );
540 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_PolylineXY)& theHydAxis,
541 const Handle(HYDROData_Profile)& theProfile,
542 const TopoDS_Face& thePlane,
543 Standard_Real& theOutPar )
545 if ( theProfile.IsNull() || !IsValidAsAxis( theHydAxis ) )
548 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() ); //guide line
549 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
550 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
553 BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
554 if(!aProjector.IsDone())
556 TopoDS_Shape aPrjProfile = aProjector.Shape();
557 if(aPrjProfile.IsNull())
559 TopoDS_Vertex aV1, aV2;
560 if(aPrjProfile.ShapeType() == TopAbs_EDGE)
561 TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);
562 else if(aPrjProfile.ShapeType() == TopAbs_WIRE)
563 TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);
564 else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
565 TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
567 TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);
569 anExp.Init(aPrjProfile, TopAbs_EDGE);
571 TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);
575 if(aV1.IsNull() || aV2.IsNull())
577 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
578 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
581 BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2);
584 const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
585 Standard_Integer aNum(0);
587 TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
588 for(;anExplo.More();anExplo.Next()) aNum++;
589 // check for self-intersection
590 const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
591 Standard_Boolean hasInt(false);
592 Standard_Real aSqDist(DBL_MAX);
593 Standard_Integer anIndx(0);
594 BRepExtrema_ExtCC aCC;
595 aCC.Initialize(anEdg2);
597 anExplo.Init(aHydraulicWire, TopAbs_EDGE);
598 for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
599 const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
602 Standard_Boolean hasSol(false);
606 for(Standard_Integer i=1; i<= aCC.NbExt();i++)
607 if(aCC.SquareDistance(i) < aSqDist) {
608 aSqDist = aCC.SquareDistance(i);
614 if(aSqDist <= SquareTolerance) { // hasInt
615 const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
617 TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
618 theOutPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
620 Standard_Real aPar = aCC.ParameterOnE1(anIndx);
628 TopExp::Vertices(anEdg1, aV1, aV2);
629 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
632 } else if(aNum > 1) {
633 TopExp::Vertices(anEdg1, aV1, aV2);
634 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
642 bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
644 if ( theProfile.IsNull() )
647 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
648 if ( aHydAxis.IsNull() )
652 if(!BuildFace(aHydAxis, aPlane))
655 Standard_Real aPar(.0);
656 if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
657 return false; // Object is already in reference list or it has no intersection
659 int aProfileIndex = insertParameter( aPar );
660 insertProfileInToOrder( theProfile, aProfileIndex );
662 // Indicate model of the need to update the stream presentation
668 bool HYDROData_Stream::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles,
669 const bool& theIsToOrder )
673 for ( int i = 1; i <= theProfiles.Length(); ++i )
675 Handle(HYDROData_Profile) aProfile =
676 Handle(HYDROData_Profile)::DownCast( theProfiles.Value( i ) );
677 if ( aProfile.IsNull() )
680 if ( !AddProfile( aProfile ) )
684 else // Just store the sequence of objects as is
686 bool anIsToUpdate = true;
688 HYDROData_SequenceOfObjects anOldProfiles = GetProfiles();
689 if ( anOldProfiles.Length() == theProfiles.Length() )
691 anIsToUpdate = false;
693 for ( int i = 1; i <= theProfiles.Length(); ++i )
695 Handle(HYDROData_Entity) anOldProfile = anOldProfiles.Value( i );
696 Handle(HYDROData_Entity) aNewProfile = theProfiles.Value( i );
697 if ( !IsEqual( anOldProfile, aNewProfile ) )
705 SetReferenceObjects( theProfiles, DataTag_Profile );
714 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
716 return GetReferenceObjects( DataTag_Profile );
719 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
721 if ( theProfile.IsNull() )
724 int aProfileIndex = -1;
726 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
727 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
728 for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
730 Handle(HYDROData_Profile) aProfile =
731 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
732 if ( aProfile.IsNull() )
735 if ( IsEqual( theProfile, aProfile ) )
742 if ( aProfileIndex == -1 )
745 RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
747 // Remove parameter for removed profile
748 removeParameter( aProfileIndex );
750 // Indicate model of the need to update the stream presentation
756 void HYDROData_Stream::RemoveProfiles()
758 bool anIsToUpdate = IsMustBeUpdated() || NbReferenceObjects( DataTag_Profile ) > 0;
760 ClearReferenceObjects( DataTag_Profile );
762 // Remove the parameters array
763 removeParametersArray();
765 // Indicate model of the need to update the stream presentation
766 SetToUpdate( anIsToUpdate );
769 void HYDROData_Stream::insertProfileInToOrder( const Handle(HYDROData_Profile)& theProfile,
770 const int theBeforeIndex )
772 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
773 if ( theProfile.IsNull() || aHydAxis.IsNull() )
776 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
777 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
778 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
781 if ( theBeforeIndex == -1 )
782 AddReferenceObject( theProfile, DataTag_Profile );
784 InsertReferenceObject( theProfile, DataTag_Profile, theBeforeIndex );
787 bool HYDROData_Stream::BuildFace( const Handle(HYDROData_PolylineXY)& theHydAxis,
788 TopoDS_Face& thePlane )
790 if ( !IsValidAsAxis( theHydAxis ) )
793 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() );
795 gp_Ax2 aX2(gp::XOY());
799 BRepBndLib::Add(aHydraulicWire,B);
800 Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
801 B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
802 BRepBuilderAPI_MakeFace aMkr(aPln, axmin-500., axmax+500., aymin-500., aymax+500.); // to be tuned later according max/ Profile deviation
803 if(!aMkr.IsDone() || aMkr.Shape().IsNull()) return false;
804 thePlane = TopoDS::Face(aMkr.Shape());
808 void HYDROData_Stream::updateProfilesOrder()
810 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
811 if ( aRefProfiles.IsEmpty() )
814 // At first we remove all profiles from order
817 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
818 if ( aHydAxis.IsNull() )
822 if ( !BuildFace( aHydAxis, aPlane ) )
825 Standard_Real aPar( .0 );
829 TopoDS_Compound aCmp;
830 aBB.MakeCompound(aCmp);
833 HYDROData_DataMapOfRealOfHDProfile aDM;
834 TColStd_ListOfReal aList;
835 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
836 for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
838 Handle(HYDROData_Profile) aProfile =
839 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
841 TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
842 aBB.Add( aCmp, aProfileWire );
844 if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
847 aDM.Bind( aPar, aProfile );
848 aList.Append( aPar );
851 if ( aList.IsEmpty() )
854 TColStd_Array1OfReal anArr( 1, aList.Extent() );
856 TColStd_ListIteratorOfListOfReal it( aList );
857 for ( int j = 1; it.More(); it.Next(), j++ )
858 anArr( j ) = it.Value();
861 if ( aList.Extent() > 1 )
863 TCollection_CompareOfReal Compar;
864 SortTools_QuickSortOfReal::Sort( anArr, Compar );
866 for (int j = 1; j <= anArr.Length(); j++) {
867 const Standard_Real aKey = anArr(j);
868 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
869 insertProfileInToOrder( aProfile );
871 } else if ( aList.Extent() == 1 ) {
872 const Standard_Real aKey = aList.Last();
873 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
874 insertProfileInToOrder( aProfile );
877 setParametersArray( anArr );
880 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
881 BRepTools::Write(aHydraulicWire, "Path.brep");
882 BRepTools::Write(aCmp, "Prof.brep");
886 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
888 return KIND_STREAM_ALTITUDE;
891 void HYDROData_Stream::setParametersArray( const TColStd_Array1OfReal& theArray )
893 if ( theArray.Length() == 0 )
895 removeParametersArray();
899 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
901 Handle(TDataStd_RealArray) aParamsArray =
902 TDataStd_RealArray::Set( aLabel, theArray.Lower(), theArray.Upper() );
904 for ( int i = theArray.Lower(), n = theArray.Upper(); i <= n; ++i )
906 const Standard_Real& aParam = theArray( i );
907 aParamsArray->SetValue( i, aParam );
911 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
913 TColStd_Array1OfReal* anArray = NULL;
915 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
916 if ( !aLabel.IsNull() )
918 Handle(TDataStd_RealArray) aParamsArray;
919 if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
921 anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
922 for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
924 const Standard_Real& aParam = aParamsArray->Value( i );
925 anArray->SetValue( i, aParam );
933 void HYDROData_Stream::removeParametersArray()
935 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
936 if ( !aLabel.IsNull() )
937 aLabel.ForgetAllAttributes();
940 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
944 TColStd_Array1OfReal* anArr = getParametersArray();
949 TColStd_Array1OfReal aNewArr( anArr->Lower(), anArr->Upper() + 1 );
951 bool isInserted = false;
952 for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
954 const Standard_Real& aStoredParam = anArr->Value( i );
957 if ( theParam > aStoredParam )
963 aNewArr( j ) = theParam;
969 aNewArr( j ) = aStoredParam;
975 aNewArr( aNewArr.Upper() ) = theParam;
978 setParametersArray( aNewArr );
983 TColStd_Array1OfReal aNewArr( 1, 1 );
984 aNewArr.SetValue( 1, theParam );
985 setParametersArray( aNewArr );
991 void HYDROData_Stream::removeParameter( const int& theIndex )
993 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
994 if ( aLabel.IsNull() )
997 Handle(TDataStd_RealArray) aParamsArray;
998 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
1001 if ( aParamsArray->Length() == 1 )
1003 removeParametersArray();
1007 TColStd_Array1OfReal aNewArr( aParamsArray->Lower(), aParamsArray->Upper() - 1 );
1009 for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
1011 const Standard_Real& aStoredParam = aParamsArray->Value( i );
1012 if ( k == theIndex )
1015 aNewArr.SetValue( j, aStoredParam );
1019 setParametersArray( aNewArr );
1022 bool HYDROData_Stream::GenerateBottomPolyline()
1025 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1026 if ( aDocument.IsNull() ) {
1030 // Collect bottom points ( one bottom point from each profile of the stream )
1031 HYDROData_Profile::ProfilePoints aBottomPoints;
1033 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
1034 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; i++ ) {
1035 const Handle(HYDROData_Profile) aProfile =
1036 Handle(HYDROData_Profile)::DownCast( aSeqOfProfiles.Value( i ) );
1037 if ( aProfile.IsNull() ) {
1041 aBottomPoints.Append( aProfile->GetBottomPoint() );
1044 int aNbBottomPoints = aBottomPoints.Size();
1046 if ( aNbBottomPoints < 2 ) {
1050 // Create bottom polyline object if the stream doesn't contain it yet
1051 Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1052 if ( aBottom.IsNull() ) {
1053 aBottom = Handle(HYDROData_Polyline3D)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
1054 QString aBaseName = GetName() + "_bottom";
1055 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
1056 aBottom->SetName( aName );
1058 SetReferenceObject( aBottom, DataTag_BottomPolyline );
1061 // Create 2D polyline if the bottom polyline doesn't contain it yet
1062 Handle(HYDROData_PolylineXY) aPolylineXY = aBottom->GetPolylineXY();
1063 if ( aPolylineXY.IsNull() ) {
1064 aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
1065 QString aBaseName = GetName() + "_bottom_2d";
1066 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
1067 aPolylineXY->SetName( aName );
1068 aBottom->SetPolylineXY( aPolylineXY, false );
1071 aPolylineXY->RemoveSections();
1072 aPolylineXY->AddSection( "", HYDROData_PolylineXY::SECTION_SPLINE, false );
1074 // Create profile if the bottom polyline doesn't contain it yet
1075 Handle(HYDROData_ProfileUZ) aProfileUZ = aBottom->GetProfileUZ();
1076 if ( aProfileUZ.IsNull() ) {
1077 Handle(HYDROData_Profile) aProfile =
1078 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
1079 QString aBaseName = GetName() + "_bottom_profile";
1080 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
1081 aProfile->SetName( aName );
1082 aProfileUZ = aProfile->GetProfileUZ( true );
1083 aBottom->SetProfileUZ( aProfileUZ );
1086 aProfileUZ->RemoveSection( 0 );
1089 for ( int i = 1; i <= aNbBottomPoints; i++ ) {
1090 const HYDROData_Profile::ProfilePoint& aBottomPoint = aBottomPoints.Value( i );
1091 aPolylineXY->AddPoint( 0, HYDROData_PolylineXY::Point( aBottomPoint.X(), aBottomPoint.Y() ) );
1094 // Calculate profile UZ points
1097 const HYDROData_Profile::ProfilePoint& aFirstBottomPoint = aBottomPoints.First();
1098 aProfileUZ->AddPoint( 0, HYDROData_ProfileUZ::Point( 0, aFirstBottomPoint.Z() ) );
1100 // Intermediate points
1101 double aPolylineCommonDist = aPolylineXY->GetDistance( 0, aPolylineXY->NbPoints( 0 ) - 1 );
1103 for ( int i = 2, aNbPoints = aBottomPoints.Size(); i < aNbPoints; i++ ) {
1104 const HYDROData_Profile::ProfilePoint& aBottomPoint = aBottomPoints.Value( i );
1106 double aDistance = aPolylineXY->GetDistance( 0, i - 1 );
1108 Standard_Real anU = aDistance; // = ( aDistance / aPolylineCommonDist ) * aPolylineCommonDist;
1109 aProfileUZ->AddPoint( 0, HYDROData_ProfileUZ::Point( anU, aBottomPoint.Z() ) );
1113 const HYDROData_Profile::ProfilePoint& aLastBottomPoint = aBottomPoints.Last();
1114 aProfileUZ->AddPoint( 0, HYDROData_ProfileUZ::Point( aPolylineCommonDist, aLastBottomPoint.Z() ) );
1119 Handle(HYDROData_Polyline3D) HYDROData_Stream::GetBottomPolyline() const
1121 return Handle(HYDROData_Polyline3D)::DownCast(
1122 GetReferenceObject( DataTag_BottomPolyline ) );
1125 bool HYDROData_Stream::Interpolate( const HYDROData_IProfilesInterpolator* theInterpolator )
1127 ///< \todo this is just a stub