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"
30 #include "HYDROData_DTM.h"
32 #include <TDataStd_RealArray.hxx>
34 #include <Precision.hxx>
36 #include <NCollection_DataMap.hxx>
38 #include <TColStd_Array1OfReal.hxx>
39 #include <TColStd_ListOfReal.hxx>
40 #include <TColStd_ListIteratorOfListOfReal.hxx>
41 #include <TCollection_CompareOfReal.hxx>
42 #include <TColgp_Array1OfPnt.hxx>
43 #include <TColgp_HArray1OfPnt.hxx>
46 #include <TopoDS_Wire.hxx>
47 #include <TopoDS_Shell.hxx>
48 #include <TopoDS_Face.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Vertex.hxx>
52 #include <TopExp_Explorer.hxx>
54 #include <Bnd_Box.hxx>
56 #include <BRep_Builder.hxx>
57 #include <BRepBuilderAPI_MakeEdge.hxx>
58 #include <BRepBuilderAPI_MakeWire.hxx>
59 #include <BRepBuilderAPI_MakeFace.hxx>
61 #include <BRepBndLib.hxx>
62 #include <BRepProj_Projection.hxx>
63 #include <BRepExtrema_ExtCC.hxx>
64 #include <BRepCheck_Analyzer.hxx>
74 #include <GeomAPI_Interpolate.hxx>
75 #include <Geom_BSplineCurve.hxx>
77 #include <TopTools_HArray1OfShape.hxx>
79 #include <SortTools_QuickSortOfReal.hxx>
82 #include <QStringList>
84 //#define DEB_STREAM 1
86 //#define DEB_HASINT 1
87 //#define DEB_UPDATE 1
88 #include <BRepTools.hxx>
89 #include <TCollection_AsciiString.hxx>
92 typedef NCollection_DataMap<Standard_Real, Handle(HYDROData_Profile)> HYDROData_DataMapOfRealOfHDProfile;
94 IMPLEMENT_STANDARD_HANDLE(HYDROData_Stream,HYDROData_NaturalObject)
95 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Stream,HYDROData_NaturalObject)
98 HYDROData_Stream::HYDROData_Stream()
99 : HYDROData_NaturalObject( Geom_3d )
103 HYDROData_Stream::~HYDROData_Stream()
107 QStringList HYDROData_Stream::DumpToPython( const QString& thePyScriptPath,
108 MapOfTreatedObjects& theTreatedObjects ) const
110 QStringList aResList = dumpObjectCreation( theTreatedObjects );
111 QString aName = GetObjPyName();
113 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
114 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aHydAxis, "SetHydraulicAxis" );
116 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
117 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; ++i )
119 const Handle(HYDROData_Entity) aProfile = aSeqOfProfiles.Value( i );
120 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aProfile, "AddProfile" );
123 // Set bottom polyline if exists
124 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
125 if ( !aBottomPolyline.IsNull() ) {
126 setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aBottomPolyline, "SetBottomPolyline" );
129 QString aDDZs = QString::number( GetDDZ(), 'f', 3 );
130 QString aSSteps = QString::number( GetSpatialStep(), 'f', 3 );
131 aResList << QString( "%1.SetDDZ( %2 )" ).arg( aName ).arg( aDDZs );
132 aResList << QString( "%1.SetSpatialStep( %2 )" ).arg( aName ).arg( aSSteps );
134 aResList << QString( "" );
135 aResList << QString( "%1.Update()" ).arg( aName );
136 aResList << QString( "" );
141 HYDROData_SequenceOfObjects HYDROData_Stream::GetAllReferenceObjects() const
143 HYDROData_SequenceOfObjects aResSeq = HYDROData_Object::GetAllReferenceObjects();
145 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
146 if ( !aHydAxis.IsNull() )
147 aResSeq.Append( aHydAxis );
149 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
150 aResSeq.Append( aSeqOfProfiles );
155 Handle(Geom_BSplineCurve) HYDROData_Stream::buildInterpolationCurve(
156 const Handle(TColgp_HArray1OfPnt)& theArrayOfPnt )
158 Handle(Geom_BSplineCurve) aBSpline;
159 GeomAPI_Interpolate anInterpolator (theArrayOfPnt, Standard_False, 1.0e-5);
160 anInterpolator.Perform() ;
161 if (anInterpolator.IsDone())
162 aBSpline = anInterpolator.Curve();
166 void HYDROData_Stream::Update()
168 updateProfilesOrder();
170 // Update bottom polyline if exists
171 const Handle(HYDROData_Polyline3D) aBottomPolyline = GetBottomPolyline();
172 if ( !aBottomPolyline.IsNull() ) {
173 if ( GenerateBottomPolyline() ) {
174 Handle(HYDROData_PolylineXY) aPolylineXY = aBottomPolyline->GetPolylineXY();
175 if ( !aPolylineXY.IsNull() ) {
176 aPolylineXY->Update();
178 aBottomPolyline->Update();
182 Handle_HYDROData_DTM dtm = DTM();
187 bool HYDROData_Stream::IsHas2dPrs() const
192 bool HYDROData_Stream::CreatePresentations( const Handle_HYDROData_DTM& theDTM,
193 PrsDefinition& thePrs )
195 if ( theDTM.IsNull() )
198 HYDROData_SequenceOfObjects profiles = theDTM->GetProfiles();
199 if( profiles.Length() < 2 )
202 std::vector<TopoDS_Wire> profiles3d;
203 profiles3d.reserve(profiles.Length());
206 HYDROData_SequenceOfObjects::Iterator anIter( profiles );
207 for (int i=1 ; anIter.More(); anIter.Next(),i++ )
209 Handle(HYDROData_Profile) aProfile =
210 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
212 if ( aProfile.IsNull() )
215 const TopoDS_Shape& aProfileShape = aProfile->GetShape3D();
216 //TopExp_Explorer exp(aProfileShape, TopAbs_EDGE);
217 profiles3d.push_back( TopoDS::Wire(aProfileShape) );
220 TopoDS_Edge aLeftBank, aRightBank;
221 //TODO: theDTM->CreateBankShapes( aLeftBank, aRightBank );
222 return CreatePresentations( aLeftBank, aRightBank, profiles3d, thePrs );
225 void HYDROData_Stream::UpdatePrs( const Handle_HYDROData_DTM& theDTM )
227 HYDROData_NaturalObject::Update();
229 PrsDefinition aResultPrs;
230 if ( !CreatePresentations( theDTM, aResultPrs ) )
233 SetShape3D( aResultPrs.myPrs3D );
234 SetTopShape( aResultPrs.myPrs2D );
236 // Create the stream groups
237 QString aLeftGroupName = GetName() + "_Left_Bank";
239 Handle(HYDROData_ShapesGroup) aLeftGroup = createGroupObject();
240 aLeftGroup->SetName( aLeftGroupName );
241 aLeftGroup->AddShape( aResultPrs.myLeftBank );
243 QString aRightGroupName = GetName() + "_Right_Bank";
245 Handle(HYDROData_ShapesGroup) aRightGroup = createGroupObject();
246 aRightGroup->SetName( aRightGroupName );
247 aRightGroup->AddShape( aResultPrs.myRightBank );
249 QString anInGroupName = GetName() + "_Inlet";
251 Handle(HYDROData_ShapesGroup) anInGroup = createGroupObject();
252 anInGroup->SetName( anInGroupName );
253 anInGroup->AddShape( aResultPrs.myInlet );
255 QString anOutGroupName = GetName() + "_Outlet";
257 Handle(HYDROData_ShapesGroup) anOutGroup = createGroupObject();
258 anOutGroup->SetName( anOutGroupName );
259 anOutGroup->AddShape( aResultPrs.myOutlet );
262 QColor HYDROData_Stream::DefaultFillingColor() const
264 return QColor( Qt::green );
267 QColor HYDROData_Stream::DefaultBorderColor() const
269 return QColor( Qt::transparent );
272 bool HYDROData_Stream::IsValidAsAxis( const Handle(HYDROData_PolylineXY)& theHydAxis )
274 if ( theHydAxis.IsNull() )
277 TopoDS_Shape aHydraulicShape = theHydAxis->GetShape();
278 if ( aHydraulicShape.IsNull() ||
279 aHydraulicShape.ShapeType() != TopAbs_WIRE ||
280 BRep_Tool::IsClosed( aHydraulicShape ) )
281 return false; // The polyline must be a single not closed wire
286 TopoDS_Shape HYDROData_Stream::GetLeftShape() const
288 HYDROData_SequenceOfObjects aGroups = GetGroups();
289 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 1);
292 TopoDS_Shape HYDROData_Stream::GetRightShape() const
294 HYDROData_SequenceOfObjects aGroups = GetGroups();
295 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 2);
298 TopoDS_Shape HYDROData_Stream::GetInletShape() const
300 HYDROData_SequenceOfObjects aGroups = GetGroups();
301 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 3);
304 TopoDS_Shape HYDROData_Stream::GetOutletShape() const
306 HYDROData_SequenceOfObjects aGroups = GetGroups();
307 return HYDROData_Tool::getFirstShapeFromGroup( aGroups, 4);
310 Handle_HYDROData_DTM HYDROData_Stream::DTM() const
312 const_cast<HYDROData_Stream*>( this )->checkAndSetAltitudeObject();
313 return Handle(HYDROData_DTM)::DownCast( GetAltitudeObject() );
316 double HYDROData_Stream::GetDDZ() const
318 return DTM()->GetDDZ();
321 void HYDROData_Stream::SetDDZ( double theDDZ )
323 DTM()->SetDDZ( theDDZ );
326 double HYDROData_Stream::GetSpatialStep() const
328 return DTM()->GetSpatialStep();
331 void HYDROData_Stream::SetSpatialStep( double theSpatialStep )
333 DTM()->SetSpatialStep( theSpatialStep );
336 bool HYDROData_Stream::SetHydraulicAxis( const Handle(HYDROData_PolylineXY)& theAxis )
338 if ( !IsValidAsAxis( theAxis ) )
341 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
342 if ( IsEqual( aPrevAxis, theAxis ) )
345 SetReferenceObject( theAxis, DataTag_HydraulicAxis );
347 // Update the order of profiles
348 updateProfilesOrder();
350 // Indicate model of the need to update the stream presentation
356 Handle(HYDROData_PolylineXY) HYDROData_Stream::GetHydraulicAxis() const
358 return Handle(HYDROData_PolylineXY)::DownCast(
359 GetReferenceObject( DataTag_HydraulicAxis ) );
362 void HYDROData_Stream::RemoveHydraulicAxis()
364 Handle(HYDROData_PolylineXY) aPrevAxis = GetHydraulicAxis();
365 if ( aPrevAxis.IsNull() )
368 ClearReferenceObjects( DataTag_HydraulicAxis );
370 // We remove the reference profiles
373 // Indicate model of the need to update the stream presentation
377 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile,
378 const TopoDS_Face& thePlane,
379 Standard_Real& theOutPar ) const
381 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
382 return HasIntersection( aHydAxis, theProfile, thePlane, theOutPar );
385 bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_PolylineXY)& theHydAxis,
386 const Handle(HYDROData_Profile)& theProfile,
387 const TopoDS_Face& thePlane,
388 Standard_Real& theOutPar )
390 if ( theProfile.IsNull() || !IsValidAsAxis( theHydAxis ) )
393 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() ); //guide line
394 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
395 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
398 BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
399 if(!aProjector.IsDone())
401 TopoDS_Shape aPrjProfile = aProjector.Shape();
402 if(aPrjProfile.IsNull())
404 TopoDS_Vertex aV1, aV2;
405 if(aPrjProfile.ShapeType() == TopAbs_EDGE)
406 TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);
407 else if(aPrjProfile.ShapeType() == TopAbs_WIRE)
408 TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);
409 else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
410 TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
412 TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);
414 anExp.Init(aPrjProfile, TopAbs_EDGE);
416 TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);
420 if(aV1.IsNull() || aV2.IsNull())
422 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
423 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
426 BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2);
429 const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
430 Standard_Integer aNum(0);
432 TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
433 for(;anExplo.More();anExplo.Next()) aNum++;
434 // check for self-intersection
435 const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
436 Standard_Boolean hasInt(false);
437 Standard_Real aSqDist(DBL_MAX);
438 Standard_Integer anIndx(0);
439 BRepExtrema_ExtCC aCC;
440 aCC.Initialize(anEdg2);
442 anExplo.Init(aHydraulicWire, TopAbs_EDGE);
443 for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
444 const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
447 Standard_Boolean hasSol(false);
451 for(Standard_Integer i=1; i<= aCC.NbExt();i++)
452 if(aCC.SquareDistance(i) < aSqDist) {
453 aSqDist = aCC.SquareDistance(i);
459 if(aSqDist <= SquareTolerance) { // hasInt
460 const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
462 TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
463 theOutPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
465 Standard_Real aPar = aCC.ParameterOnE1(anIndx);
473 TopExp::Vertices(anEdg1, aV1, aV2);
474 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
477 } else if(aNum > 1) {
478 TopExp::Vertices(anEdg1, aV1, aV2);
479 theOutPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
487 bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
489 if ( theProfile.IsNull() )
492 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
493 if ( aHydAxis.IsNull() )
497 if(!BuildFace(aHydAxis, aPlane))
500 Standard_Real aPar(.0);
501 if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
502 return false; // Object is already in reference list or it has no intersection
504 int aProfileIndex = insertParameter( aPar );
505 insertProfileInToOrder( theProfile, aProfileIndex );
507 // Indicate model of the need to update the stream presentation
513 bool HYDROData_Stream::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles,
514 const bool& theIsToOrder )
518 for ( int i = 1; i <= theProfiles.Length(); ++i )
520 Handle(HYDROData_Profile) aProfile =
521 Handle(HYDROData_Profile)::DownCast( theProfiles.Value( i ) );
522 if ( aProfile.IsNull() )
525 if ( !AddProfile( aProfile ) )
527 DTM()->SetProfiles( HYDROData_SequenceOfObjects() );
532 else // Just store the sequence of objects as is
534 bool anIsToUpdate = true;
536 HYDROData_SequenceOfObjects anOldProfiles = GetProfiles();
537 if ( anOldProfiles.Length() == theProfiles.Length() )
539 anIsToUpdate = false;
541 for ( int i = 1; i <= theProfiles.Length(); ++i )
543 Handle(HYDROData_Entity) anOldProfile = anOldProfiles.Value( i );
544 Handle(HYDROData_Entity) aNewProfile = theProfiles.Value( i );
545 if ( !IsEqual( anOldProfile, aNewProfile ) )
553 SetReferenceObjects( theProfiles, DataTag_Profile );
559 DTM()->SetProfiles( GetProfiles() );
563 HYDROData_SequenceOfObjects HYDROData_Stream::GetProfiles() const
565 return GetReferenceObjects( DataTag_Profile );
568 bool HYDROData_Stream::RemoveProfile( const Handle(HYDROData_Profile)& theProfile )
570 if ( theProfile.IsNull() )
573 int aProfileIndex = -1;
575 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
576 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
577 for ( int i = 0 ; anIter.More(); anIter.Next(), ++i )
579 Handle(HYDROData_Profile) aProfile =
580 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
581 if ( aProfile.IsNull() )
584 if ( IsEqual( theProfile, aProfile ) )
591 if ( aProfileIndex == -1 )
594 RemoveReferenceObject( theProfile->Label(), DataTag_Profile );
596 // Remove parameter for removed profile
597 removeParameter( aProfileIndex );
599 // Indicate model of the need to update the stream presentation
605 void HYDROData_Stream::RemoveProfiles()
607 ClearReferenceObjects( DataTag_Profile );
609 // Remove the parameters array
610 removeParametersArray();
612 // Indicate model of the need to update the stream presentation
616 void HYDROData_Stream::insertProfileInToOrder( const Handle(HYDROData_Profile)& theProfile,
617 const int theBeforeIndex )
619 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
620 if ( theProfile.IsNull() || aHydAxis.IsNull() )
623 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
624 TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
625 if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
628 if ( theBeforeIndex == -1 )
629 AddReferenceObject( theProfile, DataTag_Profile );
631 InsertReferenceObject( theProfile, DataTag_Profile, theBeforeIndex );
634 bool HYDROData_Stream::BuildFace( const Handle(HYDROData_PolylineXY)& theHydAxis,
635 TopoDS_Face& thePlane )
637 if ( !IsValidAsAxis( theHydAxis ) )
640 TopoDS_Wire aHydraulicWire = TopoDS::Wire( theHydAxis->GetShape() );
642 gp_Ax2 aX2(gp::XOY());
646 BRepBndLib::Add(aHydraulicWire,B);
647 Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
648 B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
649 BRepBuilderAPI_MakeFace aMkr(aPln, axmin-500., axmax+500., aymin-500., aymax+500.); // to be tuned later according max/ Profile deviation
650 if(!aMkr.IsDone() || aMkr.Shape().IsNull()) return false;
651 thePlane = TopoDS::Face(aMkr.Shape());
655 void HYDROData_Stream::updateProfilesOrder()
657 HYDROData_SequenceOfObjects aRefProfiles = GetProfiles();
658 if ( aRefProfiles.IsEmpty() )
661 // At first we remove all profiles from order
664 Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
665 if ( aHydAxis.IsNull() )
669 if ( !BuildFace( aHydAxis, aPlane ) )
672 Standard_Real aPar( .0 );
676 TopoDS_Compound aCmp;
677 aBB.MakeCompound(aCmp);
680 HYDROData_DataMapOfRealOfHDProfile aDM;
681 TColStd_ListOfReal aList;
682 HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
683 for (int i = 1 ; anIter.More(); anIter.Next(), i++ )
685 Handle(HYDROData_Profile) aProfile =
686 Handle(HYDROData_Profile)::DownCast( anIter.Value() );
688 TopoDS_Wire aProfileWire = TopoDS::Wire( aProfile->GetTopShape() );
689 aBB.Add( aCmp, aProfileWire );
691 if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
694 aDM.Bind( aPar, aProfile );
695 aList.Append( aPar );
698 if ( aList.IsEmpty() )
701 TColStd_Array1OfReal anArr( 1, aList.Extent() );
703 TColStd_ListIteratorOfListOfReal it( aList );
704 for ( int j = 1; it.More(); it.Next(), j++ )
705 anArr( j ) = it.Value();
708 if ( aList.Extent() > 1 )
710 TCollection_CompareOfReal Compar;
711 SortTools_QuickSortOfReal::Sort( anArr, Compar );
713 for (int j = 1; j <= anArr.Length(); j++) {
714 const Standard_Real aKey = anArr(j);
715 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
716 insertProfileInToOrder( aProfile );
718 } else if ( aList.Extent() == 1 ) {
719 const Standard_Real aKey = aList.Last();
720 const Handle(HYDROData_Profile)& aProfile = aDM.Find(aKey);
721 insertProfileInToOrder( aProfile );
724 setParametersArray( anArr );
727 TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
728 BRepTools::Write(aHydraulicWire, "Path.brep");
729 BRepTools::Write(aCmp, "Prof.brep");
733 ObjectKind HYDROData_Stream::getAltitudeObjectType() const
738 void HYDROData_Stream::setParametersArray( const TColStd_Array1OfReal& theArray )
740 if ( theArray.Length() == 0 )
742 removeParametersArray();
746 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray );
748 Handle(TDataStd_RealArray) aParamsArray =
749 TDataStd_RealArray::Set( aLabel, theArray.Lower(), theArray.Upper() );
751 for ( int i = theArray.Lower(), n = theArray.Upper(); i <= n; ++i )
753 const Standard_Real& aParam = theArray( i );
754 aParamsArray->SetValue( i, aParam );
758 TColStd_Array1OfReal* HYDROData_Stream::getParametersArray() const
760 TColStd_Array1OfReal* anArray = NULL;
762 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
763 if ( !aLabel.IsNull() )
765 Handle(TDataStd_RealArray) aParamsArray;
766 if ( aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
768 anArray = new TColStd_Array1OfReal( aParamsArray->Lower(), aParamsArray->Upper() );
769 for ( int i = aParamsArray->Lower(), n = aParamsArray->Upper(); i <= n; ++i )
771 const Standard_Real& aParam = aParamsArray->Value( i );
772 anArray->SetValue( i, aParam );
780 void HYDROData_Stream::removeParametersArray()
782 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
783 if ( !aLabel.IsNull() )
784 aLabel.ForgetAllAttributes();
787 int HYDROData_Stream::insertParameter( const Standard_Real& theParam )
791 TColStd_Array1OfReal* anArr = getParametersArray();
796 TColStd_Array1OfReal aNewArr( anArr->Lower(), anArr->Upper() + 1 );
798 bool isInserted = false;
799 for ( int i = anArr->Lower(), j = i, n = anArr->Upper(); i <= n; ++i, ++j )
801 const Standard_Real& aStoredParam = anArr->Value( i );
804 if ( theParam > aStoredParam )
810 aNewArr( j ) = theParam;
816 aNewArr( j ) = aStoredParam;
822 aNewArr( aNewArr.Upper() ) = theParam;
825 setParametersArray( aNewArr );
830 TColStd_Array1OfReal aNewArr( 1, 1 );
831 aNewArr.SetValue( 1, theParam );
832 setParametersArray( aNewArr );
838 void HYDROData_Stream::removeParameter( const int& theIndex )
840 TDF_Label aLabel = myLab.FindChild( DataTag_ParamsArray, false );
841 if ( aLabel.IsNull() )
844 Handle(TDataStd_RealArray) aParamsArray;
845 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aParamsArray ) )
848 if ( aParamsArray->Length() == 1 )
850 removeParametersArray();
854 TColStd_Array1OfReal aNewArr( aParamsArray->Lower(), aParamsArray->Upper() - 1 );
856 for ( int i = aParamsArray->Lower(), j = i, k = 0, n = aParamsArray->Upper(); i <= n; ++i, ++k )
858 const Standard_Real& aStoredParam = aParamsArray->Value( i );
862 aNewArr.SetValue( j, aStoredParam );
866 setParametersArray( aNewArr );
869 bool HYDROData_Stream::GenerateBottomPolyline()
872 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
873 if ( aDocument.IsNull() ) {
877 // Collect bottom points ( one bottom point from each profile of the stream )
878 HYDROData_Profile::ProfilePoints aBottomPoints;
880 HYDROData_SequenceOfObjects aSeqOfProfiles = GetProfiles();
881 for ( int i = 1, aNb = aSeqOfProfiles.Size(); i <= aNb; i++ ) {
882 const Handle(HYDROData_Profile) aProfile =
883 Handle(HYDROData_Profile)::DownCast( aSeqOfProfiles.Value( i ) );
884 if ( aProfile.IsNull() ) {
888 aBottomPoints.Append( aProfile->GetBottomPoint() );
891 int aNbBottomPoints = aBottomPoints.Size();
893 if ( aNbBottomPoints < 2 ) {
897 // Create bottom polyline object if the stream doesn't contain it yet
898 Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
899 if ( aBottom.IsNull() ) {
900 aBottom = Handle(HYDROData_Polyline3D)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
901 QString aBaseName = GetName() + "_bottom";
902 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
903 aBottom->SetName( aName );
905 SetReferenceObject( aBottom, DataTag_BottomPolyline );
908 // Create 2D polyline if the bottom polyline doesn't contain it yet
909 Handle(HYDROData_PolylineXY) aPolylineXY = aBottom->GetPolylineXY();
910 if ( aPolylineXY.IsNull() ) {
911 aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( aDocument->CreateObject( KIND_POLYLINEXY ) );
912 QString aBaseName = GetName() + "_bottom_2d";
913 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
914 aPolylineXY->SetName( aName );
915 aBottom->SetPolylineXY( aPolylineXY, false );
918 aPolylineXY->RemoveSections();
919 aPolylineXY->AddSection( "", HYDROData_PolylineXY::SECTION_SPLINE, false );
921 // Create profile if the bottom polyline doesn't contain it yet
922 Handle(HYDROData_ProfileUZ) aProfileUZ = aBottom->GetProfileUZ();
923 if ( aProfileUZ.IsNull() ) {
924 Handle(HYDROData_Profile) aProfile =
925 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
926 QString aBaseName = GetName() + "_bottom_profile";
927 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName, QStringList(), true );
928 aProfile->SetName( aName );
929 aProfileUZ = aProfile->GetProfileUZ( true );
930 aBottom->SetProfileUZ( aProfileUZ );
933 aProfileUZ->RemoveSection( 0 );
935 aProfileUZ->CalculateAndAddPoints(aBottomPoints, aPolylineXY);
940 Handle(HYDROData_Polyline3D) HYDROData_Stream::GetBottomPolyline() const
942 return Handle(HYDROData_Polyline3D)::DownCast(
943 GetReferenceObject( DataTag_BottomPolyline ) );
946 bool HYDROData_Stream::SetBottomPolyline( const Handle(HYDROData_Polyline3D)& theBottom )
948 if ( theBottom.IsNull() ) {
952 SetReferenceObject( theBottom, DataTag_BottomPolyline );
957 bool HYDROData_Stream::Interpolate( HYDROData_IProfilesInterpolator* theInterpolator )
960 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
961 if ( aDocument.IsNull() ) {
965 if ( theInterpolator->GetCalculatedProfilesNumber() < 1 ) {
966 theInterpolator->Calculate();
969 if ( theInterpolator->GetErrorCode() != OK ) {
975 for ( int aProfileInd = 0; aProfileInd < theInterpolator->GetCalculatedProfilesNumber(); aProfileInd++ ) {
976 // Get calculated point coordinates
977 HYDROData_Profile::ProfilePoints aResultPoints = theInterpolator->GetResultProfilePoints( aProfileInd );
978 if ( aResultPoints.IsEmpty() ) {
983 // Create profile object
984 Handle(HYDROData_Profile) aProfile =
985 Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
986 QString aBaseName = GetName() + "_interp_profile";
987 QString aName = HYDROData_Tool::GenerateObjectName( aDocument, aBaseName );
988 aProfile->SetName( aName );
990 // Fill the profile with points
991 aProfile->SetProfilePoints( aResultPoints );
993 // Add profile to the stream
994 bool isAdded = AddProfile( aProfile );
1008 void HYDROData_Stream::CopyTo( const Handle(HYDROData_Entity)& theDestination,
1009 bool isGenerateNewName ) const
1012 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
1013 if ( aDocument.IsNull() ) {
1018 HYDROData_Entity::CopyTo( theDestination, isGenerateNewName );
1020 Handle(HYDROData_Stream) aStreamCopy =
1021 Handle(HYDROData_Stream)::DownCast( theDestination );
1023 // Copy bottom polyline if exists
1024 if ( !aStreamCopy.IsNull() ) {
1025 const Handle(HYDROData_Polyline3D) aBottom = GetBottomPolyline();
1026 if ( !aBottom.IsNull() ) {
1027 aStreamCopy->ClearReferenceObjects( DataTag_BottomPolyline );
1028 aStreamCopy->GenerateBottomPolyline();
1029 const Handle(HYDROData_Polyline3D) aBottomCopy = aStreamCopy->GetBottomPolyline();
1030 if ( !aBottomCopy.IsNull() && !aBottomCopy->GetPolylineXY().IsNull() ) {
1031 aBottomCopy->GetPolylineXY()->Update();
1032 aBottomCopy->Update();
1039 bool HYDROData_Stream::CreatePresentations( const TopoDS_Edge& theLeftBank,
1040 const TopoDS_Edge& theRightBank,
1041 const std::vector<TopoDS_Wire>& theProfiles3d,
1042 PrsDefinition& thePrs )
1044 //thePrs.myLeftBank = theLeftBank;
1045 //thePrs.myRightBank = theRightBank;
1048 //TopoDS_Compound aCmp;
1049 //aBB.MakeCompound(aCmp);
1050 //for (size_t i = 0; i < theProfiles3d.size(); i++ )
1051 // aBB.Add(aCmp, theProfiles3d[i]);
1053 //aBB.Add(aCmp, theLeftBank);
1054 //aBB.Add(aCmp, theRightBank);
1056 //thePrs.myPrs3D = aCmp; //3d pres
1058 /*Handle_Geom_Plane RefPlane = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
1059 TopoDS_Vertex V1, V2;
1061 ProjEdgeOnPlane(theProfiles3d[0], RefPlane, W, V1, V2);*/
1063 /* Handle_Geom_Plane RefPlane = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
1064 std::vector<TopoDS_Edge> internProf;
1065 internProf.reserve(theProfiles3d.size() - 2);
1067 //project internal edges/profiles
1068 for (size_t i = 1; i < theProfiles3d.size() - 1; i++ )
1070 TopoDS_Edge ProjEdge;
1071 ProjEdgeOnPlane(theProfiles3d[i], RefPlane, ProjEdge, TopoDS_Vertex(), TopoDS_Vertex());
1072 internProf.push_back (TopoDS::Edge(ProjEdge.Oriented(TopAbs_INTERNAL)));
1075 TopoDS_Edge ProjEdgeIn, ProjEdgeOut;
1076 TopoDS_Vertex V1In, V2In, V1Out, V2Out;
1078 ProjEdgeOnPlane(theProfiles3d.front(), RefPlane, ProjEdgeIn, TopoDS_Vertex(), TopoDS_Vertex());
1079 TopExp::Vertices(ProjEdgeIn, V1In, V2In);
1081 ProjEdgeOnPlane(theProfiles3d.back(), RefPlane, ProjEdgeOut, TopoDS_Vertex(), TopoDS_Vertex());
1082 TopExp::Vertices(ProjEdgeOut, V1Out, V2Out);
1084 TopoDS_Edge ProjEdgeLeftB, ProjEdgeRightB;
1085 ProjEdgeOnPlane(theLeftBank, RefPlane, ProjEdgeLeftB, V1In, V1Out);
1086 ProjEdgeOnPlane(theRightBank, RefPlane, ProjEdgeRightB, V2In, V2Out);
1088 BRepLib_MakeWire WM(ProjEdgeIn, ProjEdgeOut, ProjEdgeLeftB, ProjEdgeRightB);
1089 for (size_t i = 0; i < internProf.size(); i++ )
1090 WM.Add(internProf[i]);
1092 TopoDS_Face outF = BRepLib_MakeFace(RefPlane, WM.Wire()).Face();
1093 thePrs.myPrs2D = outF;*/
1096 /*if ( theArrayOfFPnt.IsNull() || theArrayOfLPnt.IsNull() || theArrOfProfiles.IsNull() ) {
1100 if ( theArrayOfFPnt->Length() != theArrayOfLPnt->Length() ) {
1104 // Construct of the 3D presentation
1105 Handle(Geom_BSplineCurve) aBSpline = buildInterpolationCurve (theArrayOfFPnt);
1106 if(aBSpline.IsNull())
1109 TopoDS_Edge anEdgLeft, anEdgRight;
1111 BRepBuilderAPI_MakeEdge aMakeEdge(aBSpline);
1112 if(aMakeEdge.IsDone())
1113 anEdgLeft = aMakeEdge.Edge();
1115 if(anEdgLeft.IsNull())
1119 aBSpline = buildInterpolationCurve (theArrayOfLPnt);
1120 if(aBSpline.IsNull())
1123 aMakeEdge.Init(aBSpline);
1124 if(aMakeEdge.IsDone())
1125 anEdgRight = aMakeEdge.Edge();
1127 if(anEdgRight.IsNull())
1131 TopoDS_Compound aCmp;
1132 aBB.MakeCompound(aCmp);
1133 for (int i=1 ; i < theArrOfProfiles->Length() +1; i++ )
1134 aBB.Add(aCmp, theArrOfProfiles->Value(i));
1136 aBB.Add(aCmp,anEdgLeft);
1137 aBB.Add(aCmp,anEdgRight);
1138 BRepCheck_Analyzer aCh(aCmp);
1140 thePrs.myPrs3D = aCmp;
1143 BRepTools::Write(aCmp, "str3d.brep");
1144 thePrs.myPrs3D = aCmp;
1148 // Construct the top presentation
1149 int aNbPoints = theArrayOfFPnt->Length();
1150 Handle(TColgp_HArray1OfPnt) anArrayOfFPnt = new TColgp_HArray1OfPnt(1, aNbPoints);
1151 Handle(TColgp_HArray1OfPnt) anArrayOfLPnt = new TColgp_HArray1OfPnt(1, aNbPoints);
1152 for( int i=1; i <= aNbPoints; i++ ) {
1153 gp_Pnt aPnt = theArrayOfFPnt->Value(i);
1154 aPnt.SetZ(.0); // make 2d
1155 anArrayOfFPnt->SetValue(i, aPnt);
1156 aPnt = theArrayOfLPnt->Value(i);
1158 anArrayOfLPnt->SetValue(i, aPnt);
1162 aBSpline = buildInterpolationCurve (anArrayOfFPnt);
1163 if(aBSpline.IsNull())
1166 aMakeEdge.Init(aBSpline);
1167 if(aMakeEdge.IsDone())
1168 anEdgLeft = aMakeEdge.Edge();
1171 aBSpline = buildInterpolationCurve (anArrayOfLPnt);
1172 if(aBSpline.IsNull())
1175 aMakeEdge.Init(aBSpline);
1176 if(aMakeEdge.IsDone())
1177 anEdgRight = aMakeEdge.Edge();
1178 if(anEdgRight.IsNull())
1181 BRepBuilderAPI_MakeEdge aMakeEdge2(anArrayOfFPnt->Value(1),anArrayOfLPnt->Value(1));
1182 TopoDS_Edge aBotEdge, aTopEdge;
1183 if(aMakeEdge2.IsDone())
1184 aBotEdge = aMakeEdge2.Edge();
1186 BRepBuilderAPI_MakeEdge aMakeEdge3(anArrayOfFPnt->Value(anArrayOfFPnt->Length()),anArrayOfLPnt->Value(anArrayOfLPnt->Length()));
1187 if(aMakeEdge3.IsDone())
1188 aTopEdge = aMakeEdge3.Edge();
1190 // Make wire for 2D presentation with updating of corresponding edges
1191 BRepBuilderAPI_MakeWire aMakeWire;
1193 aMakeWire.Add( aBotEdge );
1194 thePrs.myInlet = aMakeWire.Edge();
1196 aMakeWire.Add( anEdgLeft );
1197 thePrs.myLeftBank = aMakeWire.Edge();
1199 aMakeWire.Add( aTopEdge );
1200 thePrs.myOutlet = aMakeWire.Edge();
1202 aMakeWire.Add( anEdgRight );
1203 thePrs.myRightBank = aMakeWire.Edge();
1205 TopoDS_Wire aSectProfileWire;
1206 if(aMakeWire.IsDone())
1207 aSectProfileWire = aMakeWire.Wire();
1209 BRepBuilderAPI_MakeFace aMakeFace( aSectProfileWire, Standard_True );
1212 if( aMakeFace.IsDone() )
1213 aFace = aMakeFace.Face();
1215 TopoDS_Shape aPrs2D;
1217 if ( !theArrOf2DProfiles.IsNull() ) {
1219 aBB.MakeCompound(aCmp);
1220 aBB.Add(aCmp,aFace);
1221 for(int i=1;i <= theArrOf2DProfiles->Length(); i++)
1222 aBB.Add(aCmp, theArrOf2DProfiles->Value(i));
1231 thePrs.myPrs2D = aPrs2D;
1234 BRepTools::Write(aPrs2D, "str2d.brep");
1235 thePrs.myPrs2D = aPrs2D;