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 <TColgp_Array1OfPnt.hxx>
41 #include <TColgp_HArray1OfPnt.hxx>
44 #include <TopoDS_Wire.hxx>
45 #include <TopoDS_Shell.hxx>
46 #include <TopoDS_Face.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Vertex.hxx>
50 #include <TopExp_Explorer.hxx>
52 #include <Bnd_Box.hxx>
54 #include <BRep_Builder.hxx>
55 #include <BRepBuilderAPI_MakeEdge.hxx>
56 #include <BRepBuilderAPI_MakeWire.hxx>
57 #include <BRepBuilderAPI_MakeFace.hxx>
59 #include <BRepBndLib.hxx>
60 #include <BRepProj_Projection.hxx>
61 #include <BRepExtrema_ExtCC.hxx>
62 #include <BRepCheck_Analyzer.hxx>
72 #include <GeomAPI_Interpolate.hxx>
73 #include <Geom_BSplineCurve.hxx>
75 #include <TopTools_HArray1OfShape.hxx>
78 #include <QStringList>
81 //#define DEB_STREAM 1
83 //#define DEB_HASINT 1
84 //#define DEB_UPDATE 1
85 #include <BRepTools.hxx>
86 #include <TCollection_AsciiString.hxx>
89 typedef NCollection_DataMap<Standard_Real, Handle(HYDROData_Profile)> HYDROData_DataMapOfRealOfHDProfile;
91 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Stream,HYDROData_NaturalObject)
94 HYDROData_Stream::HYDROData_Stream()
95 : HYDROData_NaturalObject( Geom_3d )
99 HYDROData_Stream::~HYDROData_Stream()
103 QStringList HYDROData_Stream::DumpToPython( const QString& thePyScriptPath,
104 MapOfTreatedObjects& theTreatedObjects ) const
106 QStringList aResList = dumpObjectCreation( theTreatedObjects );
107 QString aName = GetObjPyName();
109 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
110 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aHydAxis, "SetHydraulicAxis" );
112 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
113 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; ++i )
115 const Handle(HYDROData_Entity) aProfile = aSeqOfProfiles.Value( i );
116 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aProfile, "AddProfile" );
119 // Set bottom polyline if exists
120 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
121 if ( !aBottomPolyline.IsNull() ) {
122 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aBottomPolyline, "SetBottomPolyline" );
125 aResList << QString( "" );
126 aResList << QString( "%1.Update()" ).arg( aName );
127 aResList << QString( "" );
132 HYDROData_SequenceOfObjects HYDROData_Stream::GetAllReferenceObjects() const
134 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
136 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
137 if ( !aHydAxis.IsNull() )
138 aResSeq.Append( aHydAxis );
140 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
141 aResSeq.Append( aSeqOfProfiles );
146 Handle(Geom_BSplineCurve) HYDROData_Stream::buildInterpolationCurve(
147 const Handle(TColgp_HArray1OfPnt)& theArrayOfPnt )
149 Handle(Geom_BSplineCurve) aBSpline;
150 GeomAPI_Interpolate anInterpolator (theArrayOfPnt, Standard_False, 1.0e-5);
151 anInterpolator.Perform() ;
152 if (anInterpolator.IsDone())
153 aBSpline = anInterpolator.Curve();
157 void HYDROData_Stream::Update()
159 updateProfilesOrder();
161 // Update bottom polyline if exists
162 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
163 if ( !aBottomPolyline.IsNull() ) {
164 if ( GenerateBottomPolyline() ) {
165 Handle(HYDROData_PolylineXY) aPolylineXY = aBottomPolyline->GetPolylineXY();
166 if ( !aPolylineXY.IsNull() ) {
167 aPolylineXY->Update();
169 aBottomPolyline->Update();
176 bool HYDROData_Stream::IsHas2dPrs() const
181 bool HYDROData_Stream::CreatePresentations( const Handle(HYDROData_PolylineXY)& theHydAxis,
182 const HYDROData_SequenceOfObjects& theProfiles,
183 PrsDefinition& thePrs )
185 if ( theHydAxis.IsNull() || theProfiles.Length() < 2 )
188 Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, theProfiles.Length());
189 Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, theProfiles.Length());
190 Handle(TopTools_HArray1OfShape) anArrOfProfiles = new TopTools_HArray1OfShape(1, theProfiles.Length());
191 Handle(TopTools_HArray1OfShape) anArrOf2DProfiles = new TopTools_HArray1OfShape(1, theProfiles.Length());
194 HYDROData_SequenceOfObjects::Iterator anIter( theProfiles );
195 for (int i=1 ; anIter.More(); anIter.Next(),i++ )
197 Handle(HYDROData_Profile) aProfile =
198 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
199 if ( aProfile.IsNull() )
202 const TopoDS_Shape& aProf3d = aProfile->GetShape3D();
204 if ( !aProfile->GetLeftPoint( aPnt1, false ) || !aProfile->GetRightPoint( aPnt2, false ) )
207 anArrOfProfiles->SetValue(i,aProfile->GetShape3D());//aProfile->GetTopShape();
208 anArrOf2DProfiles->SetValue(i,aProfile->GetTopShape());
210 gp_Pnt aCurFP, aCurLP;
211 TopoDS_Vertex aV1, aV2;
212 TopExp::Vertices(TopoDS::Wire(aProf3d), aV1, aV2);
213 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
214 if(aP1.X() == aPnt1.X() && aP1.Y() == aPnt1.Y())
218 aP1 = BRep_Tool::Pnt(aV2);
219 if(aP1.X() == aPnt2.X() && aP1.Y() == aPnt2.Y())
223 anArrayOfFPnt->SetValue(i,aCurFP);
224 anArrayOfLPnt->SetValue(i,aCurLP);
227 return CreatePresentations( anArrayOfFPnt, anArrayOfLPnt, anArrOfProfiles, anArrOf2DProfiles, thePrs );
230 void HYDROData_Stream::UpdatePrs()
232 HYDROData_NaturalObject::Update();
234 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
235 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
237 PrsDefinition aResultPrs;
238 if ( !CreatePresentations( aHydAxis, aRefProfiles, aResultPrs ) )
241 SetShape3D( aResultPrs.myPrs3D );
242 SetTopShape( aResultPrs.myPrs2D );
244 // Create the stream groups
245 QString aLeftGroupName = GetName() + "_Left_Bank";
247 Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
248 aLeftGroup->SetName( aLeftGroupName );
249 aLeftGroup->AddShape( aResultPrs.myLeftBank );
251 QString aRightGroupName = GetName() + "_Right_Bank";
253 Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
254 aRightGroup->SetName( aRightGroupName );
255 aRightGroup->AddShape( aResultPrs.myRightBank );
257 QString anInGroupName = GetName() + "_Inlet";
259 Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
260 anInGroup->SetName( anInGroupName );
261 anInGroup->AddShape( aResultPrs.myInlet );
263 QString anOutGroupName = GetName() + "_Outlet";
265 Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
266 anOutGroup->SetName( anOutGroupName );
267 anOutGroup->AddShape( aResultPrs.myOutlet );
270 QColor HYDROData_Stream::DefaultFillingColor() const
272 return QColor( Qt::green );
275 QColor HYDROData_Stream::DefaultBorderColor() const
277 return QColor( Qt::transparent );
280 bool HYDROData_Stream::IsValidAsAxis( const Handle(HYDROData_PolylineXY)& theHydAxis )
282 if ( theHydAxis.IsNull() )
285 TopoDS_Shape aHydraulicShape = theHydAxis->GetShape();
286 if ( aHydraulicShape.IsNull() ||
287 aHydraulicShape.ShapeType() != TopAbs_WIRE ||
288 BRep_Tool::IsClosed( aHydraulicShape ) )
289 return false; // The polyline must be a single not closed wire
294 TopoDS_Shape HYDROData_Stream::GetLeftShape() const
296 HYDROData_SequenceOfObjects aGroups = GetGroups();
297 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 1);
300 TopoDS_Shape HYDROData_Stream::GetRightShape() const
302 HYDROData_SequenceOfObjects aGroups = GetGroups();
303 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 2);
306 TopoDS_Shape HYDROData_Stream::GetInletShape() const
308 HYDROData_SequenceOfObjects aGroups = GetGroups();
309 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 3);
312 TopoDS_Shape HYDROData_Stream::GetOutletShape() const
314 HYDROData_SequenceOfObjects aGroups = GetGroups();
315 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 4);
318 bool HYDROData_Stream::SetHydraulicAxis( const Handle(HYDROData_PolylineXY)& theAxis )
320 if ( !IsValidAsAxis( theAxis ) )
323 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
324 if ( IsEqual( aPrevAxis, theAxis ) )
327 SetReferenceObject( theAxis, DataTag_HydraulicAxis );
329 // Update the order of profiles
330 updateProfilesOrder();
332 // Indicate model of the need to update the stream presentation
338 Handle(HYDROData_PolylineXY) HYDROData_Stream::GetHydraulicAxis() const
340 return Handle(HYDROData_PolylineXY)::DownCast(
341 GetReferenceObject( DataTag_HydraulicAxis ) );
344 void HYDROData_Stream::RemoveHydraulicAxis()
346 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
347 if ( aPrevAxis.IsNull() )
350 ClearReferenceObjects( DataTag_HydraulicAxis );
352 // We remove the reference profiles
355 // Indicate model of the need to update the stream presentation
359 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile,
360 const TopoDS_Face& thePlane,
361 Standard_Real& theOutPar ) const
363 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
364 return HasIntersection( aHydAxis, theProfile, thePlane, theOutPar );
367 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_PolylineXY)& theHydAxis,
368 const Handle(HYDROData_Profile)& theProfile,
369 const TopoDS_Face& thePlane,
370 Standard_Real& theOutPar )
372 if ( theProfile.IsNull() || !IsValidAsAxis( theHydAxis ) )
375 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() ); //guide line
376 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
377 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
380 BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
381 if(!aProjector.IsDone())
383 TopoDS_Shape aPrjProfile = aProjector.Shape();
384 if(aPrjProfile.IsNull())
386 TopoDS_Vertex aV1, aV2;
387 if(aPrjProfile.ShapeType() == TopAbs_EDGE)
388 TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);
389 else if(aPrjProfile.ShapeType() == TopAbs_WIRE)
390 TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);
391 else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
392 TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
394 TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);
396 anExp.Init(aPrjProfile, TopAbs_EDGE);
398 TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);
402 if(aV1.IsNull() || aV2.IsNull())
404 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
405 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
408 BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2);
411 const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
412 Standard_Integer aNum(0);
414 TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
415 for(;anExplo.More();anExplo.Next()) aNum++;
416 // check for self-intersection
417 const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
418 Standard_Boolean hasInt(false);
419 Standard_Real aSqDist(DBL_MAX);
420 Standard_Integer anIndx(0);
421 BRepExtrema_ExtCC aCC;
422 aCC.Initialize(anEdg2);
424 anExplo.Init(aHydraulicWire, TopAbs_EDGE);
425 for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
426 const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
429 Standard_Boolean hasSol(false);
433 for(Standard_Integer i=1; i<= aCC.NbExt();i++)
434 if(aCC.SquareDistance(i) < aSqDist) {
435 aSqDist = aCC.SquareDistance(i);
441 if(aSqDist <= SquareTolerance) { // hasInt
442 const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
444 TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
445 theOutPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
447 Standard_Real aPar = aCC.ParameterOnE1(anIndx);
455 TopExp::Vertices(anEdg1, aV1, aV2);
456 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
459 } else if(aNum > 1) {
460 TopExp::Vertices(anEdg1, aV1, aV2);
461 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
469 bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
471 if ( theProfile.IsNull() )
474 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
475 if ( aHydAxis.IsNull() )
479 if(!BuildFace(aHydAxis, aPlane))
482 Standard_Real aPar(.0);
483 if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
484 return false; // Object is already in reference list or it has no intersection
486 int aProfileIndex = insertParameter( aPar );
487 insertProfileInToOrder( theProfile, aProfileIndex );
489 // Indicate model of the need to update the stream presentation
495 bool HYDROData_Stream::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles,
496 const bool& theIsToOrder )
500 for ( int i = 1; i <= theProfiles.Length(); ++i )
502 Handle(HYDROData_Profile) aProfile =
503 Handle(HYDROData_Profile)::DownCast( theProfiles.Value( i ) );
504 if ( aProfile.IsNull() )
507 if ( !AddProfile( aProfile ) )
511 else // Just store the sequence of objects as is
513 bool anIsToUpdate = true;
515 HYDROData_SequenceOfObjects anOldProfiles = GetProfiles();
516 if ( anOldProfiles.Length() == theProfiles.Length() )
518 anIsToUpdate = false;
520 for ( int i = 1; i <= theProfiles.Length(); ++i )
522 Handle(HYDROData_Entity) anOldProfile = anOldProfiles.Value( i );
523 Handle(HYDROData_Entity) aNewProfile = theProfiles.Value( i );
524 if ( !IsEqual( anOldProfile, aNewProfile ) )
532 SetReferenceObjects( theProfiles, DataTag_Profile );
541 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
543 return GetReferenceObjects( DataTag_Profile );
546 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
548 if ( theProfile.IsNull() )
551 int aProfileIndex = -1;
553 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
554 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
555 for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
557 Handle(HYDROData_Profile) aProfile =
558 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
559 if ( aProfile.IsNull() )
562 if ( IsEqual( theProfile, aProfile ) )
569 if ( aProfileIndex == -1 )
572 RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
574 // Remove parameter for removed profile
575 removeParameter( aProfileIndex );
577 // Indicate model of the need to update the stream presentation
583 void HYDROData_Stream::RemoveProfiles()
585 ClearReferenceObjects( DataTag_Profile );
587 // Remove the parameters array
588 removeParametersArray();
590 // Indicate model of the need to update the stream presentation
594 void HYDROData_Stream::insertProfileInToOrder( const Handle(HYDROData_Profile)& theProfile,
595 const int theBeforeIndex )
597 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
598 if ( theProfile.IsNull() || aHydAxis.IsNull() )
601 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
602 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
603 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
606 if ( theBeforeIndex == -1 )
607 AddReferenceObject( theProfile, DataTag_Profile );
609 InsertReferenceObject( theProfile, DataTag_Profile, theBeforeIndex );
612 bool HYDROData_Stream::BuildFace( const Handle(HYDROData_PolylineXY)& theHydAxis,
613 TopoDS_Face& thePlane )
615 if ( !IsValidAsAxis( theHydAxis ) )
618 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() );
620 gp_Ax2 aX2(gp::XOY());
624 BRepBndLib::Add(aHydraulicWire,B);
625 Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
626 B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
627 BRepBuilderAPI_MakeFace aMkr(aPln, axmin-500., axmax+500., aymin-500., aymax+500.); // to be tuned later according max/ Profile deviation
628 if(!aMkr.IsDone() || aMkr.Shape().IsNull()) return false;
629 thePlane = TopoDS::Face(aMkr.Shape());
633 void HYDROData_Stream::updateProfilesOrder()
635 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
636 if ( aRefProfiles.IsEmpty() )
639 // At first we remove all profiles from order
642 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
643 if ( aHydAxis.IsNull() )
647 if ( !BuildFace( aHydAxis, aPlane ) )
650 Standard_Real aPar( .0 );
654 TopoDS_Compound aCmp;
655 aBB.MakeCompound(aCmp);
658 HYDROData_DataMapOfRealOfHDProfile aDM;
659 TColStd_ListOfReal aList;
660 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
661 for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
663 Handle(HYDROData_Profile) aProfile =
664 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
666 TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
667 aBB.Add( aCmp, aProfileWire );
669 if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
672 aDM.Bind( aPar, aProfile );
673 aList.Append( aPar );
676 if ( aList.IsEmpty() )
679 QVector<double> anArr( aList.Extent() );
681 TColStd_ListIteratorOfListOfReal it( aList );
682 for ( int j = 1; it.More(); it.Next(), j++ )
683 anArr[j-1] = it.Value();
686 if ( aList.Extent() > 1 )
688 //TCollection_CompareOfReal Compar;
689 //SortTools_QuickSortOfReal::Sort( anArr, Compar );
690 std::sort( anArr.begin(), anArr.end() );
692 for (int j = 1; j <= anArr.size(); j++) {
693 const Standard_Real aKey = anArr[j-1];
694 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
695 insertProfileInToOrder( aProfile );
697 } else if ( aList.Extent() == 1 ) {
698 const Standard_Real aKey = aList.Last();
699 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
700 insertProfileInToOrder( aProfile );
703 setParametersArray( anArr );
706 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
707 BRepTools::Write(aHydraulicWire, "Path.brep");
708 BRepTools::Write(aCmp, "Prof.brep");
712 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
714 return KIND_STREAM_ALTITUDE;
717 void HYDROData_Stream::setParametersArray( const QVector<double>& theArray )
719 if ( theArray.size() == 0 )
721 removeParametersArray();
725 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
727 int n = theArray.size();
728 Handle(TDataStd_RealArray) aParamsArray =
729 TDataStd_RealArray::Set( aLabel, 1, n );
730 for ( int i = 0; i < n; ++i )
732 const Standard_Real& aParam = theArray[i];
733 aParamsArray->SetValue( i+1, aParam );
737 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
739 TColStd_Array1OfReal* anArray = NULL;
741 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
742 if ( !aLabel.IsNull() )
744 Handle(TDataStd_RealArray) aParamsArray;
745 if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
747 anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
748 for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
750 const Standard_Real& aParam = aParamsArray->Value( i );
751 anArray->SetValue( i, aParam );
759 void HYDROData_Stream::removeParametersArray()
761 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
762 if ( !aLabel.IsNull() )
763 aLabel.ForgetAllAttributes();
766 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
770 TColStd_Array1OfReal* anArr = getParametersArray();
775 QVector<double> aNewArr( anArr->Upper() );
777 bool isInserted = false;
778 for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
780 const Standard_Real& aStoredParam = anArr->Value( i );
783 if ( theParam > aStoredParam )
789 aNewArr[j-1] = theParam;
795 aNewArr[j-1] = aStoredParam;
801 aNewArr[aNewArr.size()-1] = theParam;
804 setParametersArray( aNewArr );
809 QVector<double> aNewArr( 1 );
810 aNewArr[0] = theParam;
811 setParametersArray( aNewArr );
817 void HYDROData_Stream::removeParameter( const int& theIndex )
819 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
820 if ( aLabel.IsNull() )
823 Handle(TDataStd_RealArray) aParamsArray;
824 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
827 if ( aParamsArray->Length() == 1 )
829 removeParametersArray();
833 QVector<double> aNewArr( aParamsArray->Upper() - 2 );
835 for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
837 const Standard_Real& aStoredParam = aParamsArray->Value( i );
841 aNewArr[j-1] = aStoredParam;
845 setParametersArray( aNewArr );
848 bool HYDROData_Stream::GenerateBottomPolyline()
851 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
852 if ( aDocument.IsNull() ) {
856 // Collect bottom points ( one bottom point from each profile of the stream )
857 HYDROData_Profile::ProfilePoints aBottomPoints;
859 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
860 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; i++ ) {
861 const Handle(HYDROData_Profile) aProfile =
862 Handle(HYDROData_Profile)::DownCast( aSeqOfProfiles.Value( i ) );
863 if ( aProfile.IsNull() ) {
867 aBottomPoints.Append( aProfile->GetBottomPoint() );
870 int aNbBottomPoints = aBottomPoints.Size();
872 if ( aNbBottomPoints < 2 ) {
876 // Create bottom polyline object if the stream doesn't contain it yet
877 Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
878 if ( aBottom.IsNull() ) {
879 aBottom = Handle(HYDROData_Polyline3D)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
880 QString aBaseName = GetName() + "_bottom";
881 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
882 aBottom->SetName( aName );
884 SetReferenceObject( aBottom, DataTag_BottomPolyline );
887 // Create 2D polyline if the bottom polyline doesn't contain it yet
888 Handle(HYDROData_PolylineXY) aPolylineXY = aBottom->GetPolylineXY();
889 if ( aPolylineXY.IsNull() ) {
890 aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
891 QString aBaseName = GetName() + "_bottom_2d";
892 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
893 aPolylineXY->SetName( aName );
894 aBottom->SetPolylineXY( aPolylineXY, false );
897 aPolylineXY->RemoveSections();
898 aPolylineXY->AddSection( "", HYDROData_PolylineXY::SECTION_SPLINE, false );
900 // Create profile if the bottom polyline doesn't contain it yet
901 Handle(HYDROData_ProfileUZ) aProfileUZ = aBottom->GetProfileUZ();
902 if ( aProfileUZ.IsNull() ) {
903 Handle(HYDROData_Profile) aProfile =
904 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
905 QString aBaseName = GetName() + "_bottom_profile";
906 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
907 aProfile->SetName( aName );
908 aProfileUZ = aProfile->GetProfileUZ( true );
909 aBottom->SetProfileUZ( aProfileUZ );
912 aProfileUZ->RemoveSection( 0 );
914 aProfileUZ->CalculateAndAddPoints(aBottomPoints, aPolylineXY);
919 Handle(HYDROData_Polyline3D) HYDROData_Stream::GetBottomPolyline() const
921 return Handle(HYDROData_Polyline3D)::DownCast(
922 GetReferenceObject( DataTag_BottomPolyline ) );
925 bool HYDROData_Stream::SetBottomPolyline( const Handle(HYDROData_Polyline3D)& theBottom )
927 if ( theBottom.IsNull() ) {
931 SetReferenceObject( theBottom, DataTag_BottomPolyline );
936 bool HYDROData_Stream::Interpolate( HYDROData_IProfilesInterpolator* theInterpolator )
939 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
940 if ( aDocument.IsNull() ) {
944 if ( theInterpolator->GetCalculatedProfilesNumber() < 1 ) {
945 theInterpolator->Calculate();
948 if ( theInterpolator->GetErrorCode() != OK ) {
954 for ( int aProfileInd = 0; aProfileInd < theInterpolator->GetCalculatedProfilesNumber(); aProfileInd++ ) {
955 // Get calculated point coordinates
956 HYDROData_Profile::ProfilePoints aResultPoints = theInterpolator->GetResultProfilePoints( aProfileInd );
957 if ( aResultPoints.IsEmpty() ) {
962 // Create profile object
963 Handle(HYDROData_Profile) aProfile =
964 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
965 QString aBaseName = GetName() + "_interp_profile";
966 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName );
967 aProfile->SetName( aName );
969 // Fill the profile with points
970 aProfile->SetProfilePoints( aResultPoints );
972 // Add profile to the stream
973 bool isAdded = AddProfile( aProfile );
987 void HYDROData_Stream::CopyTo( const Handle(HYDROData_Entity)& theDestination,
988 bool isGenerateNewName ) const
991 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
992 if ( aDocument.IsNull() ) {
997 HYDROData_Entity::CopyTo( theDestination, isGenerateNewName );
999 Handle(HYDROData_Stream) aStreamCopy =
1000 Handle(HYDROData_Stream)::DownCast( theDestination );
1002 // Copy bottom polyline if exists
1003 if ( !aStreamCopy.IsNull() ) {
1004 const Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1005 if ( !aBottom.IsNull() ) {
1006 aStreamCopy->ClearReferenceObjects( DataTag_BottomPolyline );
1007 aStreamCopy->GenerateBottomPolyline();
1008 const Handle(HYDROData_Polyline3D) aBottomCopy = aStreamCopy->GetBottomPolyline();
1009 if ( !aBottomCopy.IsNull() && !aBottomCopy->GetPolylineXY().IsNull() ) {
1010 aBottomCopy->GetPolylineXY()->Update();
1011 aBottomCopy->Update();
1017 bool HYDROData_Stream::CreatePresentations( const Handle(TColgp_HArray1OfPnt) theArrayOfFPnt,
1018 const Handle(TColgp_HArray1OfPnt) theArrayOfLPnt,
1019 const Handle(TopTools_HArray1OfShape) theArrOfProfiles,
1020 const Handle(TopTools_HArray1OfShape) theArrOf2DProfiles,
1021 PrsDefinition& thePrs )
1023 if ( theArrayOfFPnt.IsNull() || theArrayOfLPnt.IsNull() || theArrOfProfiles.IsNull() ) {
1027 if ( theArrayOfFPnt->Length() != theArrayOfLPnt->Length() ) {
1031 // Construct of the 3D presentation
1032 Handle(Geom_BSplineCurve) aBSpline = buildInterpolationCurve (theArrayOfFPnt);
1033 if(aBSpline.IsNull())
1036 TopoDS_Edge anEdgLeft, anEdgRight;
1038 BRepBuilderAPI_MakeEdge aMakeEdge(aBSpline);
1039 if(aMakeEdge.IsDone())
1040 anEdgLeft = aMakeEdge.Edge();
1042 if(anEdgLeft.IsNull())
1046 aBSpline = buildInterpolationCurve (theArrayOfLPnt);
1047 if(aBSpline.IsNull())
1050 aMakeEdge.Init(aBSpline);
1051 if(aMakeEdge.IsDone())
1052 anEdgRight = aMakeEdge.Edge();
1054 if(anEdgRight.IsNull())
1058 TopoDS_Compound aCmp;
1059 aBB.MakeCompound(aCmp);
1060 for (int i=1 ; i < theArrOfProfiles->Length() +1; i++ )
1061 aBB.Add(aCmp, theArrOfProfiles->Value(i));
1063 aBB.Add(aCmp,anEdgLeft);
1064 aBB.Add(aCmp,anEdgRight);
1065 BRepCheck_Analyzer aCh(aCmp);
1067 thePrs.myPrs3D = aCmp;
1070 BRepTools::Write(aCmp, "str3d.brep");
1071 thePrs.myPrs3D = aCmp;
1075 // Construct the top presentation
1076 int aNbPoints = theArrayOfFPnt->Length();
1077 Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints);
1078 Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints);
1079 for( int i=1; i <= aNbPoints; i++ ) {
1080 gp_Pnt aPnt = theArrayOfFPnt->Value(i);
1081 aPnt.SetZ(.0); // make 2d
1082 anArrayOfFPnt->SetValue(i, aPnt);
1083 aPnt = theArrayOfLPnt->Value(i);
1085 anArrayOfLPnt->SetValue(i, aPnt);
1089 aBSpline = buildInterpolationCurve (anArrayOfFPnt);
1090 if(aBSpline.IsNull())
1093 aMakeEdge.Init(aBSpline);
1094 if(aMakeEdge.IsDone())
1095 anEdgLeft = aMakeEdge.Edge();
1098 aBSpline = buildInterpolationCurve (anArrayOfLPnt);
1099 if(aBSpline.IsNull())
1102 aMakeEdge.Init(aBSpline);
1103 if(aMakeEdge.IsDone())
1104 anEdgRight = aMakeEdge.Edge();
1105 if(anEdgRight.IsNull())
1108 BRepBuilderAPI_MakeEdge aMakeEdge2(anArrayOfFPnt->Value(1),anArrayOfLPnt->Value(1));
1109 TopoDS_Edge aBotEdge, aTopEdge;
1110 if(aMakeEdge2.IsDone())
1111 aBotEdge = aMakeEdge2.Edge();
1113 BRepBuilderAPI_MakeEdge aMakeEdge3(anArrayOfFPnt->Value(anArrayOfFPnt->Length()),anArrayOfLPnt->Value(anArrayOfLPnt->Length()));
1114 if(aMakeEdge3.IsDone())
1115 aTopEdge = aMakeEdge3.Edge();
1117 // Make wire for 2D presentation with updating of corresponding edges
1118 BRepBuilderAPI_MakeWire aMakeWire;
1120 aMakeWire.Add( aBotEdge );
1121 thePrs.myInlet = aMakeWire.Edge();
1123 aMakeWire.Add( anEdgLeft );
1124 thePrs.myLeftBank = aMakeWire.Edge();
1126 aMakeWire.Add( aTopEdge );
1127 thePrs.myOutlet = aMakeWire.Edge();
1129 aMakeWire.Add( anEdgRight );
1130 thePrs.myRightBank = aMakeWire.Edge();
1132 TopoDS_Wire aSectProfileWire;
1133 if(aMakeWire.IsDone())
1134 aSectProfileWire = aMakeWire.Wire();
1136 BRepBuilderAPI_MakeFace aMakeFace( aSectProfileWire, Standard_True );
1139 if( aMakeFace.IsDone() )
1140 aFace = aMakeFace.Face();
1142 TopoDS_Shape aPrs2D;
1144 if ( !theArrOf2DProfiles.IsNull() ) {
1146 aBB.MakeCompound(aCmp);
1147 aBB.Add(aCmp,aFace);
1148 for(int i=1;i <= theArrOf2DProfiles->Length(); i++)
1149 aBB.Add(aCmp, theArrOf2DProfiles->Value(i));
1158 thePrs.myPrs2D = aPrs2D;
1161 BRepTools::Write(aPrs2D, "str2d.brep");
1162 thePrs.myPrs2D = aPrs2D;