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_PolylineXY.h"
21 #include <HYDROData_Polyline3D.h>
22 #include <HYDROData_Bathymetry.h>
23 #include <HYDROData_Iterator.h>
24 #include <HYDROData_ShapeFile.h>
25 #include <HYDROData_Profile.h>
27 #include "HYDROData_BSplineOperation.h"
28 #include "HYDROData_Document.h"
29 #include "HYDROData_ShapesTool.h"
30 #include "HYDROData_Tool.h"
31 #include "HYDROData_PolylineOperator.h"
32 #include "HYDROData_TopoCurve.h"
34 #include <BRep_Builder.hxx>
35 #include <BRepBuilderAPI_MakeEdge.hxx>
36 #include <BRepBuilderAPI_MakeWire.hxx>
37 #include <BRepBuilderAPI_MakePolygon.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39 #include <BRepOffsetAPI_NormalProjection.hxx>
40 #include <BRepAdaptor_Curve.hxx>
41 #include <Quantity_Parameter.hxx>
47 #include <GeomAPI_ProjectPointOnCurve.hxx>
48 #include <GeomAdaptor_Curve.hxx>
49 #include <Geom_Line.hxx>
50 #include <Geom_BSplineCurve.hxx>
52 #include <GCPnts_AbscissaPoint.hxx>
53 #include <GCPnts_QuasiUniformDeflection.hxx>
54 #include <GCPnts_UniformDeflection.hxx>
56 #include <ImageComposer_MetaTypes.h>
62 #include <NCollection_Map.hxx>
64 #include <TCollection_ExtendedString.hxx>
66 #include <TDataStd_ListIteratorOfListOfByte.hxx>
67 #include <TColStd_ListIteratorOfListOfInteger.hxx>
68 #include <TColStd_ListIteratorOfListOfReal.hxx>
70 #include <TColStd_Array1OfReal.hxx>
71 #include <TColgp_Array1OfPnt.hxx>
73 #include <TDataStd_AsciiString.hxx>
74 #include <TDataStd_BooleanList.hxx>
75 #include <TDataStd_ExtStringList.hxx>
76 #include <TDataStd_IntegerList.hxx>
77 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
78 #include <TDataStd_RealList.hxx>
79 #include <TDataStd_UAttribute.hxx>
80 #include <TDataStd_ExtStringArray.hxx>
82 #include <TopoDS_Iterator.hxx>
83 #include <TopTools_ListIteratorOfListOfShape.hxx>
84 #include <TopTools_HSequenceOfShape.hxx>
85 #include <TopExp_Explorer.hxx>
86 #include <ShapeAnalysis_FreeBounds.hxx>
90 #include <QPainterPath>
94 #include "HYDRO_trace.hxx"
95 #include <BRepTools.hxx>
99 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
101 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
103 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
105 NCollection_Map<TCollection_AsciiString> aNamesMap;
107 for ( int i = 1, n = theNamesSeq.Size(); i <= n; ++i )
109 const TCollection_AsciiString& aSectName = theNamesSeq.Value( i );
110 aNamesMap.Add( aSectName );
113 TCollection_AsciiString aResName;
118 aResName = TCollection_AsciiString( "Section_" ) + aPrefIdx;
121 while ( aNamesMap.Contains( aResName ) );
126 TCollection_AsciiString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
128 NCollection_Sequence<TCollection_AsciiString> aNamesSeq;
130 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
131 for ( ; aNamesIter.More(); aNamesIter.Next() )
132 aNamesSeq.Append( aNamesIter.Value() );
134 return getUniqueSectionName( aNamesSeq );
137 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
139 HYDROData_PolylineXY::HYDROData_PolylineXY()
140 : HYDROData_IPolyline(),
141 myIsInCustomFlag( false )
145 HYDROData_PolylineXY::~HYDROData_PolylineXY()
149 QStringList HYDROData_PolylineXY::DumpToPython( const QString& thePyScriptPath,
150 MapOfTreatedObjects& theTreatedObjects ) const
152 QStringList aResList = dumpObjectCreation( theTreatedObjects );
153 QString aPolylineName = GetObjPyName();
155 // Set the wire color
156 QStringList aWireColorDef;
158 //QColor aWireColor = GetWireColor();
159 //setPythonObjectColor( aWireColorDef, aWireColor, DefaultWireColor(), "SetWireColor" );
161 int nbSec = NbSections();
162 for (int i = 0; i < nbSec; i++)
165 GetSectionColor(i, aColor);
166 setPythonPolylineSectionColor(aWireColorDef, i, aColor );
167 //aWireColorDef << "";
170 if ( !aWireColorDef.isEmpty() )
172 aResList << aWireColorDef;
173 aResList << QString( "" );
176 bool anIsEditable = IsEditable();
179 // If polyline is not editable we try to import the shape from geom
180 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
181 if ( !aGeomObjectEntry.IsEmpty() )
183 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "polyline_sobj" );
184 aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" )" )
185 .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
187 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() )" )
188 .arg( aPolylineName ).arg( aSalomeObjName );
190 aResList << QString( "%1.SetGeomObjectEntry( \"%2\" )" )
191 .arg( aPolylineName ).arg( aGeomObjectEntry.ToCString() );
197 NCollection_Sequence<TCollection_AsciiString> aSectNames;
198 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
199 NCollection_Sequence<bool> aSectClosures;
200 GetSections( aSectNames, aSectTypes, aSectClosures );
202 for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
204 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
205 const SectionType& aSectType = aSectTypes.Value( i );
206 bool aSectClosure = aSectClosures.Value( i );
208 aResList << QString( "%1.AddSection( \"%2\", %3, %4 )" ).arg( aPolylineName )
209 .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
211 HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
212 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
214 const Point& aSectPoint = aSectPointsList.Value( k );
216 QString anXStr = QString::number( aSectPoint.X(), 'f', 2 );
217 QString anYStr = QString::number( aSectPoint.Y(), 'f', 2 );
218 aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) )" ).arg( aPolylineName )
219 .arg( i - 1 ).arg( anXStr ).arg( anYStr );
223 aResList << QString( "" );
224 aResList << QString( "%1.Update()" ).arg( aPolylineName );
225 aResList << QString( "" );
230 QVariant HYDROData_PolylineXY::GetDataVariant()
232 QPainterPath aPath = GetPainterPath();
235 aVarData.setValue<QPainterPath>( aPath );
240 QColor HYDROData_PolylineXY::DefaultWireColor()
242 return QColor( Qt::black );
245 bool HYDROData_PolylineXY::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
250 if ( theIOR.IsEmpty() )
253 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
254 if ( aShape.IsNull() )
257 return ImportShape( aShape, false, NULL );
261 void HYDROData_PolylineXY::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
263 Handle(TDataStd_AsciiString) anAttr = TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
264 anAttr->SetID(TDataStd_AsciiString::GetID());
267 TCollection_AsciiString HYDROData_PolylineXY::GetGeomObjectEntry() const
269 TCollection_AsciiString aRes;
271 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
272 if ( !aLabel.IsNull() )
274 Handle(TDataStd_AsciiString) anAsciiStr;
275 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
276 aRes = anAsciiStr->Get();
282 bool convertEdgeToSection( const TopoDS_Edge& theEdge,
283 NCollection_Sequence<TCollection_AsciiString>& theSectNames,
284 NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
285 NCollection_Sequence<bool>& theSectClosures,
286 NCollection_Sequence<HYDROData_PolylineXY::PointsList>& theSectPoints,
288 bool IsInterpolationAllowed,
289 double theDeflection )
291 Standard_Real aFirst = 0.0, aLast = 0.0;
292 Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
293 if ( anEdgeGeomCurve.IsNull() )
296 TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
297 bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
299 HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
300 HYDROData_PolylineXY::PointsList aPointsList;
302 if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
304 Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
306 gp_Pnt aFirstPoint, aLastPoint;
307 aGeomLine->D0( aFirst, aFirstPoint );
308 aGeomLine->D0( aLast, aLastPoint );
310 HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
311 aPointsList.Append( aSectFirstPoint );
313 HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
314 aPointsList.Append( aSectLastPoint );
316 else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
318 aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
320 BRepAdaptor_Curve anAdaptorCurve( theEdge );
321 GCPnts_QuasiUniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
322 //GCPnts_UniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
324 int aNbPoints = aDiscrete.NbPoints();
326 // Decrease the number of imported poles because of last one
327 // pole is the closing point which are the start point
328 if ( anIsEdgeClosed ) aNbPoints--;
330 for ( int i = 1; i <= aNbPoints; ++i )
332 const gp_Pnt& aPoint = aDiscrete.Value( i );
334 HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
335 aPointsList.Append( aSectPoint );
340 // Other curve types are not supported
344 if ( aPointsList.IsEmpty() )
347 theSectNames.Append( aSectName );
348 theSectTypes.Append( aSectionType );
349 theSectClosures.Append( anIsEdgeClosed );
350 theSectPoints.Append( aPointsList );
356 bool convertEdgesToSections( const TopoDS_Edge& theEdge,
357 NCollection_Sequence<TCollection_AsciiString>& theSectNames,
358 NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
359 NCollection_Sequence<bool>& theSectClosures,
360 NCollection_Sequence<HYDROData_PolylineXY::PointsList>& theSectPoints,
362 bool IsInterpolationAllowed,
363 double theDeflection,
364 const Handle( HYDROData_PolylineXY )& theOldPolyline )
366 DEBTRACE("convertEdgesToSections")
367 Standard_Real aFirst = 0.0, aLast = 0.0;
368 Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
369 if ( anEdgeGeomCurve.IsNull() )
372 bool isPrevious = (theSectTypes.Size() > 0);
373 DEBTRACE("nb sections: " << theSectTypes.Size());
374 HYDROData_PolylineXY::SectionType prevSectType = HYDROData_PolylineXY::SECTION_SPLINE;;
375 HYDROData_PolylineXY::PointsList prevPointList;
376 bool isPrevClosed = true;
379 prevSectType = theSectTypes.Last();
380 prevPointList = theSectPoints.Last();
381 isPrevClosed = theSectClosures.Last();
384 bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
385 anIsEdgeClosed &= IsCanBeClosed; // on split, resulting edges are normally not closed...
386 HYDROData_PolylineXY::SectionType aSectionType ;
387 if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
389 aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
391 else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
393 aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
397 // Other curve types are not supported
401 bool isNewSection = !isPrevious || isPrevClosed || anIsEdgeClosed || prevSectType != aSectionType;
402 DEBTRACE(isNewSection <<": " << !isPrevious << " " << isPrevClosed << " " << anIsEdgeClosed << " " << (prevSectType != aSectionType));
404 HYDROData_PolylineXY::PointsList aPointsList;
407 aPointsList = prevPointList;
410 if( aSectionType == HYDROData_PolylineXY::SECTION_POLYLINE )
412 DEBTRACE("SECTION_POLYLINE");
413 Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
415 gp_Pnt aFirstPoint, aLastPoint;
416 aGeomLine->D0( aFirst, aFirstPoint );
417 aGeomLine->D0( aLast, aLastPoint );
418 HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
419 HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
422 if (aSectFirstPoint == prevPointList.Last())
424 DEBTRACE("points shared: a");//aPointsList.Append( aSectFirstPoint );
425 aPointsList.Append( aSectLastPoint );
427 else if (aSectLastPoint == prevPointList.Last())
429 DEBTRACE("points shared: b");//aPointsList.Append( aSectLastPoint );
430 aPointsList.Append( aSectFirstPoint );
432 else if (aSectFirstPoint == prevPointList.First())
434 DEBTRACE("points shared: c");//aPointsList.Prepend( aSectFirstPoint );
435 aPointsList.Prepend( aSectLastPoint );
437 else if (aSectLastPoint == prevPointList.First())
439 DEBTRACE("points shared: d");//aPointsList.Prepend( aSectLastPoint );
440 aPointsList.Prepend( aSectFirstPoint );
444 DEBTRACE("no point shared")
445 isNewSection = true; // no point shared, new section
447 aPointsList.Append( aSectFirstPoint );
448 aPointsList.Append( aSectLastPoint );
453 DEBTRACE("new section");
454 aPointsList.Append( aSectFirstPoint );
455 aPointsList.Append( aSectLastPoint );
458 else // aSectionType == HYDROData_PolylineXY::SECTION_SPLINE
460 DEBTRACE("SECTION_SPLINE");
464 BRepAdaptor_Curve anAdaptorCurve(theEdge);
465 if (theOldPolyline.IsNull()) // --- no previous polyline: build a set of points from scratch for the spline
467 GCPnts_QuasiUniformDeflection aDiscrete(anAdaptorCurve, theDeflection);
469 int aNbPoints = aDiscrete.NbPoints();
471 // Decrease the number of imported poles because of last one
472 // pole is the closing point which are the start point
476 for (int i = 1; i <= aNbPoints; ++i)
478 const gp_Pnt& aPoint = aDiscrete.Value(i);
479 HYDROData_PolylineXY::Point aSectPoint(aPoint.X(), aPoint.Y());
480 aPointsList.Append(aSectPoint);
483 gp_Pnt endPts[] = {gp_Pnt(aPointsList.First().X(),aPointsList.First().Y(), 0),
484 gp_Pnt(aPointsList.Last().X(),aPointsList.Last().Y(), 0) };
485 DEBTRACE("curve start: "<< endPts[0].X() << " " << endPts[0].Y());
486 DEBTRACE("curve end: "<< endPts[1].X() << " " << endPts[1].Y());
488 else // --- split of a previous polyline: try to retrieve old sets of points and add intersection points
490 const gp_Pnt aEndPs[] = { anAdaptorCurve.Value(anAdaptorCurve.FirstParameter()).XYZ(),
491 anAdaptorCurve.Value(anAdaptorCurve.LastParameter()).XYZ() };
492 double midPar = (anAdaptorCurve.LastParameter() + anAdaptorCurve.FirstParameter())/2;
494 anAdaptorCurve.D0(midPar, midPnt);
495 DEBTRACE("curve first point: " << aEndPs[0].X() << " " << aEndPs[0].Y() << " " << aEndPs[0].Z());
496 DEBTRACE("curve last point: " << aEndPs[1].X() << " " << aEndPs[1].Y() << " " << aEndPs[1].Z());
497 DEBTRACE("curve mid point: " << midPnt.X() << " " << midPnt.Y() << " " << midPnt.Z());
499 std::vector<TopoDS_Wire> aCurves;
500 HYDROData_PolylineOperator::GetWires(theOldPolyline, aCurves);
502 int nbSections = theOldPolyline->NbSections();
503 DEBTRACE("nbSections: "<< nbSections << ", nbCurves: " << aCurves.size() );
504 for (int isec = 0; isec < nbSections; isec++)
506 DEBTRACE("section: "<< isec);
507 bool isOldSectionclosed = theOldPolyline->IsClosedSection(isec);
508 TopoDS_Wire aWire = aCurves[isec]; // we suppose sections and wires are in the same order
509 TopExp_Explorer anExp(aWire, TopAbs_EDGE);
510 TopoDS_Edge anEdge = TopoDS::Edge(anExp.Current()); // the first is OK: only one normally with splines
511 BRepAdaptor_Curve adaptorOldCurve(anEdge);
512 double pfirst = adaptorOldCurve.FirstParameter();
513 double plast = adaptorOldCurve.LastParameter();
514 DEBTRACE("previous curve first last : "<< pfirst << " " << plast);
515 double p[3] = {-1, -1};
516 double d0= ProjectPointToCurve(aEndPs[0].XYZ(), adaptorOldCurve,p[0]);
517 double d1= ProjectPointToCurve(aEndPs[1].XYZ(), adaptorOldCurve,p[1]);
518 double d2= ProjectPointToCurve(midPnt.XYZ(), adaptorOldCurve, p[2]);
519 DEBTRACE("d0: "<<d0<<" d1: "<<d1<<" d2: "<<d2<<" p0: "<<p[0]<<" p1: "<<p[1]<<" p2: "<<p[2]);
520 if ((d0 < 1.e-3) && (d1 < 1.e-3) && (d2 < 1.e-3)) // we got the good old curve (and the good section)
525 DEBTRACE("isOldSectionclosed: " << isOldSectionclosed);
526 if (!isOldSectionclosed) // no need to check first and last points on an open curve
535 else // old section closed: check if we use first or last points...
537 if((pmin < pmax) && ((std::fabs(pmin - pfirst)> 1.e-3) && (std::fabs(pmax - plast) >1.e-3))) // internal points forward
539 else if ((pmin > pmax) && ((std::fabs(pmin - plast)> 1.e-3) && (std::fabs(pmax - pfirst) >1.e-3))) // internal points reverse
545 else if ((std::fabs(pmin - plast) <1.e-3) && (p[2] < pmax)) // forward, replace pmin par pfirst
547 else if ((std::fabs(pmin - plast) <1.e-3) && (p[2] > pmax)) // reverse
553 else if ((std::fabs(pmax - pfirst) <1.e-3) && (p[2] < pmin)) // reverse
559 else if ((std::fabs(pmax - pfirst) <1.e-3) && (p[2] > pmin)) // forward, replace pmax par plast
562 DEBTRACE("forward: "<< forward << " pmin " << pmin << " pmax " << pmax);
563 HYDROData_PolylineXY::Point aFirstPoint, aLastPoint;
566 aFirstPoint = HYDROData_PolylineXY::Point(aEndPs[0].X(), aEndPs[0].Y());
567 aLastPoint = HYDROData_PolylineXY::Point(aEndPs[1].X(), aEndPs[1].Y());
571 aFirstPoint = HYDROData_PolylineXY::Point(aEndPs[1].X(), aEndPs[1].Y());
572 aLastPoint = HYDROData_PolylineXY::Point(aEndPs[0].X(), aEndPs[0].Y());
574 aPointsList.Append(aFirstPoint);
576 HYDROData_PolylineXY::PointsList aSectPoints = theOldPolyline->GetPoints(isec, false);
577 int nbPoints = aSectPoints.Length();
578 DEBTRACE("nbPoints " << nbPoints);
580 for (int i=1; i<=nbPoints; i++)
582 HYDROData_PolylineXY::Point aPoint = aSectPoints.Value(i);
583 gp_XYZ p(aPoint.X(), aPoint.Y(), 0);
585 double d = ProjectPointToCurve(p, adaptorOldCurve, param);
586 if ((param > pmin) && (param < pmax))
588 DEBTRACE("param: " << param);
589 aPointsList.Append(aPoint);
593 for (int i=nbPoints; i>0; i--)
595 HYDROData_PolylineXY::Point aPoint = aSectPoints.Value(i);
596 gp_XYZ p(aPoint.X(), aPoint.Y(), 0);
598 double d = ProjectPointToCurve(p, adaptorOldCurve, param);
599 if ((param > pmin) && (param < pmax))
601 DEBTRACE("param: " << param);
602 aPointsList.Append(aPoint);
606 aPointsList.Append(aLastPoint);
613 if ( aPointsList.IsEmpty() )
616 TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
619 DEBTRACE("isNewSection");
620 theSectNames.Append( aSectName );
621 theSectTypes.Append( aSectionType );
622 theSectClosures.Append( anIsEdgeClosed );
623 theSectPoints.Append( aPointsList );
627 DEBTRACE("sameSection");
628 theSectPoints.SetValue(theSectPoints.Length(), aPointsList);
634 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape,
635 bool IsInterpolationAllowed,
636 const Handle( HYDROData_PolylineXY )& theOldPolyline,
637 bool IsClosureAllowed,
638 double theDeviation )
640 DEBTRACE("ImportShape");
641 if ( theShape.IsNull() )
644 //std::string brepName = this->GetName().toStdString();
645 //brepName += ".brep";
646 //BRepTools::Write( theShape, brepName.c_str() );
650 bool anIsCanBeImported = false;
652 NCollection_Sequence<TCollection_AsciiString> aSectNames;
653 NCollection_Sequence<SectionType> aSectTypes;
654 NCollection_Sequence<bool> aSectClosures;
655 NCollection_Sequence<PointsList> aSectPoints;
657 if ( theShape.ShapeType() == TopAbs_EDGE )
659 DEBTRACE("TopAbs_EDGE");
660 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
661 // anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes,
662 // aSectClosures, aSectPoints, true, IsInterpolationAllowed, theDeviation );
663 anIsCanBeImported = convertEdgesToSections( anEdge, aSectNames, aSectTypes, aSectClosures,
664 aSectPoints, IsClosureAllowed, IsInterpolationAllowed,
665 theDeviation, theOldPolyline );
667 else if ( theShape.ShapeType() == TopAbs_WIRE )
669 DEBTRACE("TopAbs_WIRE");
670 TopTools_SequenceOfShape anEdges;
671 HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
673 anIsCanBeImported = !anEdges.IsEmpty();
674 for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
676 TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
677 anIsCanBeImported = convertEdgesToSections( aWireEdge, aSectNames, aSectTypes, aSectClosures,
678 aSectPoints, IsClosureAllowed, IsInterpolationAllowed,
679 theDeviation, theOldPolyline );
683 if ( anIsCanBeImported )
685 for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
687 const TCollection_AsciiString& aSectName = aSectNames.Value( i );
688 const SectionType& aSectType = aSectTypes.Value( i );
689 bool anIsSectionClosed = aSectClosures.Value( i );
690 const PointsList& aSectPointsList = aSectPoints( i );
692 AddSection( aSectName, aSectType, anIsSectionClosed );
693 SetPoints( i - 1, aSectPointsList );
698 TopoDS_Shape aShape = theShape;
700 if ( theShape.ShapeType() == TopAbs_EDGE )
702 // We make the wire from incoming edge because of other algorithms
703 // are waiting at least the wire from polyline
704 TopoDS_Edge anEdge = TopoDS::Edge( theShape );
705 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
707 if ( aMakeWire.IsDone() )
708 aShape = aMakeWire.Wire();
711 gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
712 BRepBuilderAPI_MakeFace aMakeFace( aPlane );
714 BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
717 TopoDS_Shape aResult;
719 aResult = aProj.Shape();
724 setEditable( anIsCanBeImported );
729 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType& theType,
730 const bool& theIsClosed,
731 const NCollection_Sequence<gp_XYZ>& thePoints )
734 if( theType == SECTION_POLYLINE )
736 int aNbPoints = thePoints.Length();
737 BRepBuilderAPI_MakePolygon aMakeWire;
738 for ( int i = 1, n = aNbPoints; i <= n ; ++i )
740 gp_XYZ aPoint = thePoints.Value( i );
741 gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
742 aMakeWire.Add( aPnt );
744 if( theIsClosed && ( aNbPoints > 2 ) )
747 if ( aMakeWire.IsDone() )
748 aWire = aMakeWire.Wire();
750 else //if( theType == PolylineSection::SECTION_SPLINE )
752 BRepBuilderAPI_MakeWire aMakeWire;
754 if ( thePoints.Size() > 1 )
756 Handle(Geom_BSplineCurve) aCurve =
757 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
759 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aCurve ).Edge();
760 aMakeWire.Add( anEdge );
763 if ( aMakeWire.IsDone() )
770 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath& thePath,
771 const SectionType& theType,
772 const bool& theIsClosed,
773 const NCollection_Sequence<gp_XYZ>& thePoints )
775 if ( thePoints.IsEmpty() )
778 if ( theType == SECTION_POLYLINE )
780 const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
781 thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
783 for( int i = 2, n = thePoints.Size(); i <= n; ++i )
785 const gp_XYZ& aSectPoint = thePoints.Value( i );
787 thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
791 thePath.closeSubpath();
795 Handle(Geom_BSplineCurve) aCurve =
796 HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
797 HYDROData_BSplineOperation::ComputePath( aCurve, thePath );
801 void HYDROData_PolylineXY::Update()
805 // If polyline is not editable we no need to update it wire
810 HYDROData_IPolyline::Update();
812 NCollection_Sequence<TCollection_AsciiString> aSectNames;
813 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
814 NCollection_Sequence<bool> aSectClosures;
815 GetSections( aSectNames, aSectTypes, aSectClosures );
817 //BRepBuilderAPI_MakeWire aMakeWire;
819 TopTools_ListOfShape aSectionWiresList;
821 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
823 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
824 SectionType aSectionType = aSectTypes.Value( aSectionId );
825 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
827 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
828 if ( aSectPointsList.IsEmpty() )
831 NCollection_Sequence<gp_XYZ> aPoints;
832 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
834 const Point& aSectPoint = aSectPointsList.Value( i );
836 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
837 aPoints.Append( aPoint );
840 TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
841 if ( !aSectionWire.IsNull() ) {
842 aSectionWiresList.Append( aSectionWire );
843 //aMakeWire.Add( aSectionWire );
846 // all input wires in the <aSectionWiresList>
848 TopoDS_Shape aResult;
850 TopoDS_Compound aCmp;
851 aBB.MakeCompound(aCmp);
852 if (aSectionWiresList.Size() == 1)
853 aResult = aSectionWiresList.First();
854 else if (aSectionWiresList.Size() > 1)
856 TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
857 for(;it.More();it.Next())
859 aBB.Add(aCmp, it.Value());
864 //Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
865 //Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
866 //TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
867 //for(;it.More();it.Next())
869 // TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
870 // for(;it2.More();it2.Next())
871 // aSeqEdges->Append(it2.Current());
875 //TopoDS_Compound aCmp;
876 //TopoDS_Shape aResult;
877 //aBB.MakeCompound(aCmp);
878 //if(aSeqEdges->Length() >1)
880 // ShapeAnalysis_FreeBounds::ConnectEdgesToWires( aSeqEdges, 1E-5, Standard_True, aSeqWires );
882 // if( aSeqWires->Length()==1 )
883 // aResult = aSeqWires->Value( 1 );
886 // for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
888 // const TopoDS_Shape& aS1 = aSeqWires->Value(i);
889 // aBB.Add(aCmp, aS1);
894 //else if (aSeqEdges->Length() == 1)
896 // BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
897 // if (mkWire.IsDone())
898 // aResult = mkWire.Wire();
904 bool HYDROData_PolylineXY::IsHas2dPrs() const
909 bool HYDROData_PolylineXY::IsEditable() const
911 return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
914 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
916 if ( !theIsEditable )
917 TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
919 myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
923 * Returns true if polyline is closed
925 bool HYDROData_PolylineXY::IsClosed(const bool theIsSimpleCheck) const
927 DEBTRACE("IsClosed " << theIsSimpleCheck << " " << GetName());
928 bool anIsClosed = false;
930 TopoDS_Shape aShape = GetShape();
934 TopTools_SequenceOfShape aWires;
935 HYDROData_ShapesTool::ExploreShapeToShapes(aShape, TopAbs_WIRE, aWires);
937 int aNbWires = aWires.Length();
938 if (theIsSimpleCheck)
940 anIsClosed = aNbWires > 0;
941 for (int i = 1; i <= aNbWires && anIsClosed; ++i)
943 const TopoDS_Shape& aWire = aWires.Value(i);
944 anIsClosed = BRep_Tool::IsClosed(aWire);
950 anIsClosed = BRep_Tool::IsClosed(aWires.First());
953 DEBTRACE("aNbWires " << aNbWires);
954 Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
955 Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
956 for (int i = 1; i <= aNbWires; ++i)
958 const TopoDS_Shape& aWire = aWires.Value(i);
959 TopExp_Explorer it2(aWire, TopAbs_EDGE);
960 for (; it2.More(); it2.Next())
961 aSeqEdges->Append(it2.Current());
963 if (aSeqEdges->Length() > 1)
965 DEBTRACE(aSeqEdges->Length());
966 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, 1E-5, Standard_False, aSeqWires);
967 if (aSeqWires->Length() == 1)
969 DEBTRACE(aSeqWires->Length());
970 const TopoDS_Wire& aPolylineWire = TopoDS::Wire(aSeqWires->Value(1));
971 anIsClosed = BRep_Tool::IsClosed(aPolylineWire);
980 int HYDROData_PolylineXY::GetNbConnectedWires(Handle(TopTools_HSequenceOfShape)& aConnectedWires) const
982 TopoDS_Shape aShape = GetShape();
985 int aNbconnectedWires = 0;
986 TopTools_SequenceOfShape aWires;
987 HYDROData_ShapesTool::ExploreShapeToShapes(aShape, TopAbs_WIRE, aWires);
988 int aNbWires = aWires.Length();
991 aNbconnectedWires = aNbWires;
992 aConnectedWires->Append(aWires.First());
996 DEBTRACE("aNbWires " << aNbWires);
997 Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
998 Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
999 for (int i = 1; i <= aNbWires; ++i)
1001 const TopoDS_Shape& aWire = aWires.Value(i);
1002 TopExp_Explorer it2(aWire, TopAbs_EDGE);
1003 for (; it2.More(); it2.Next())
1004 aSeqEdges->Append(it2.Current());
1006 if (aSeqEdges->Length() > 1)
1008 DEBTRACE(aSeqEdges->Length());
1009 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, 1E-5, Standard_False, aSeqWires);
1011 aConnectedWires = aSeqWires;
1012 aNbconnectedWires = aConnectedWires->Length();
1013 DEBTRACE("aNbconnectedWires " << aNbconnectedWires);
1015 return aNbconnectedWires;
1018 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
1019 const int thePointIndex ) const
1021 double aResDistance = -1;
1022 if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
1023 return aResDistance;
1025 if ( thePointIndex == 0 )
1028 SectionType aSectionType = GetSectionType( theSectionIndex );
1029 bool anIsSectionClosed = IsClosedSection( theSectionIndex );
1030 PointsList aSectPointsList = GetPoints( theSectionIndex );
1031 if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size() )
1032 return aResDistance;
1034 if ( aSectionType == SECTION_POLYLINE )
1038 Point aPrevPoint = aSectPointsList.Value( 1 );
1039 for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
1041 const Point& aSectPoint = aSectPointsList.Value( i );
1042 aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
1043 aPrevPoint = aSectPoint;
1045 if ( thePointIndex == i - 1 )
1051 gp_XYZ aPointToTest;
1053 int aSectNbPoints = aSectPointsList.Size();
1054 NCollection_Sequence<gp_XYZ> aPoints;
1055 for( int i = 1 ; i <= aSectNbPoints; ++i )
1057 const Point& aSectPoint = aSectPointsList.Value( i );
1059 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1060 aPoints.Append( aPoint );
1062 if ( thePointIndex == i - 1 )
1063 aPointToTest = aPoint;
1066 Handle(Geom_BSplineCurve) aCurve =
1067 HYDROData_BSplineOperation::ComputeCurve( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
1069 Quantity_Parameter aFirstParam = aCurve->FirstParameter();
1070 Quantity_Parameter aSecondParam = aCurve->LastParameter();
1072 if ( thePointIndex != aSectNbPoints - 1 )
1074 GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aCurve );
1075 aSecondParam = aProject.LowerDistanceParameter();
1078 GeomAdaptor_Curve anAdap( aCurve );
1080 aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
1083 return aResDistance;
1086 int HYDROData_PolylineXY::NbSections() const
1088 Handle(TDataStd_ExtStringList) aNamesList;
1089 Handle(TDataStd_IntegerList) aTypesList;
1090 Handle(TDataStd_BooleanList) aClosuresList;
1091 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1093 return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
1096 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
1097 const SectionType theSectionType,
1098 const bool theIsClosed )
1100 Handle(TDataStd_ExtStringList) aNamesList;
1101 Handle(TDataStd_IntegerList) aTypesList;
1102 Handle(TDataStd_BooleanList) aClosuresList;
1103 getSectionsLists( aNamesList, aTypesList, aClosuresList );
1105 TCollection_ExtendedString aSectName( theSectName );
1106 if ( aSectName.Length() <= 0 )
1107 aSectName = getUniqueSectionName( aNamesList );
1109 aNamesList->Append( aSectName );
1110 aTypesList->Append( theSectionType );
1111 aClosuresList->Append( theIsClosed );
1116 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
1118 TCollection_AsciiString aResName;
1120 Handle(TDataStd_ExtStringList) aNamesList;
1121 Handle(TDataStd_IntegerList) aTypesList;
1122 Handle(TDataStd_BooleanList) aClosuresList;
1123 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1124 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
1127 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
1128 for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
1130 if ( aNamesIter.More() )
1131 aResName = aNamesIter.Value();
1136 void HYDROData_PolylineXY::SetSectionName( const int theSectionIndex,
1137 const TCollection_AsciiString& theSectionName )
1139 Handle(TDataStd_ExtStringList) aNamesList;
1140 Handle(TDataStd_IntegerList) aTypesList;
1141 Handle(TDataStd_BooleanList) aClosuresList;
1142 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1143 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
1146 TDataStd_ListOfExtendedString anOldNamesList;
1147 anOldNamesList = aNamesList->List();
1149 // Refill the existing list
1150 aNamesList->Clear();
1152 TCollection_ExtendedString aNewSectName = theSectionName;
1154 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
1155 for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
1156 aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
1159 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
1161 Handle(TDataStd_ExtStringList) aNamesList;
1162 Handle(TDataStd_IntegerList) aTypesList;
1163 Handle(TDataStd_BooleanList) aClosuresList;
1164 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1165 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
1166 return SECTION_POLYLINE;
1168 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
1169 for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
1171 return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
1174 void HYDROData_PolylineXY::SetSectionType( const int theSectionIndex,
1175 const SectionType theSectionType )
1177 Handle(TDataStd_ExtStringList) aNamesList;
1178 Handle(TDataStd_IntegerList) aTypesList;
1179 Handle(TDataStd_BooleanList) aClosuresList;
1180 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1181 if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
1184 TColStd_ListOfInteger anOldTypesList;
1185 anOldTypesList = aTypesList->List();
1187 // Refill the existing list
1188 aTypesList->Clear();
1190 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
1191 for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
1192 aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
1197 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
1199 Handle(TDataStd_ExtStringList) aNamesList;
1200 Handle(TDataStd_IntegerList) aTypesList;
1201 Handle(TDataStd_BooleanList) aClosuresList;
1202 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1203 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
1206 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
1207 for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
1209 return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
1212 void HYDROData_PolylineXY::SetSectionClosed( const int theSectionIndex,
1213 const bool theIsClosed )
1215 Handle(TDataStd_ExtStringList) aNamesList;
1216 Handle(TDataStd_IntegerList) aTypesList;
1217 Handle(TDataStd_BooleanList) aClosuresList;
1218 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1219 if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
1222 TDataStd_ListOfByte anOldClosuresList;
1223 anOldClosuresList = aClosuresList->List();
1225 // Refill the existing list
1226 aClosuresList->Clear();
1228 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
1229 for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
1230 aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
1235 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
1236 NCollection_Sequence<SectionType>& theSectTypes,
1237 NCollection_Sequence<bool>& theSectClosures ) const
1239 theSectNames.Clear();
1240 theSectTypes.Clear();
1241 theSectClosures.Clear();
1245 const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1248 Handle(TDataStd_ExtStringList) aNamesList;
1249 Handle(TDataStd_IntegerList) aTypesList;
1250 Handle(TDataStd_BooleanList) aClosuresList;
1251 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1252 if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
1255 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
1256 TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
1257 TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
1258 for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
1259 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
1261 const TCollection_ExtendedString& aSectName = aNamesIter.Value();
1262 SectionType aSectType = (SectionType)aTypesIter.Value();
1263 bool aSectClosures = aClosuresIter.Value();
1265 theSectNames.Append( aSectName );
1266 theSectTypes.Append( aSectType );
1267 theSectClosures.Append( aSectClosures );
1271 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
1273 Handle(TDataStd_ExtStringList) aNamesList;
1274 Handle(TDataStd_IntegerList) aTypesList;
1275 Handle(TDataStd_BooleanList) aClosuresList;
1276 getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1277 if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
1280 if ( aNamesList->Extent() == 1 )
1282 removeSectionsLists();
1283 removePointsLists();
1284 removeSectionColor();
1288 TDataStd_ListOfExtendedString anOldNamesList;
1289 anOldNamesList = aNamesList->List();
1291 TColStd_ListOfInteger anOldTypesList;
1292 anOldTypesList = aTypesList->List();
1294 TDataStd_ListOfByte anOldClosuresList;
1295 anOldClosuresList = aClosuresList->List();
1297 // Refill the existing lists
1298 aNamesList->Clear();
1299 aTypesList->Clear();
1300 aClosuresList->Clear();
1302 TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
1303 TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
1304 TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
1305 for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
1306 aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
1308 if ( i == theSectionIndex )
1309 continue; // skip index to remove
1311 aNamesList->Append( aNamesIter.Value() );
1312 aTypesList->Append( aTypesIter.Value() );
1313 aClosuresList->Append( (bool)aClosuresIter.Value() );
1316 // Remove points that belongs to removed section
1317 removePointsLists( theSectionIndex );
1318 removeSectionColor (theSectionIndex);
1324 void HYDROData_PolylineXY::RemoveSections()
1326 removeSectionsLists();
1327 removePointsLists();
1331 void HYDROData_PolylineXY::AddPoint( const int theSectionIndex,
1332 const Point& thePoint,
1333 const int thePointIndex )
1335 Handle(TDataStd_RealList) aListX, aListY;
1336 getPointsLists( theSectionIndex, aListX, aListY );
1338 if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
1340 aListX->Append( thePoint.X() );
1341 aListY->Append( thePoint.Y() );
1345 TColStd_ListOfReal anOldListX;
1346 anOldListX = aListX->List();
1348 TColStd_ListOfReal anOldListY;
1349 anOldListY = aListY->List();
1351 // Refill the existing lists
1355 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1356 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1357 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1359 double aCoordX = anIterX.Value();
1360 double aCoordY = anIterY.Value();
1362 if ( i == thePointIndex )
1364 // Insert our new point
1365 aListX->Append( thePoint.X() );
1366 aListY->Append( thePoint.Y() );
1369 aListX->Append( aCoordX );
1370 aListY->Append( aCoordY );
1377 void HYDROData_PolylineXY::SetPoint( const int theSectionIndex,
1378 const Point& thePoint,
1379 const int thePointIndex )
1381 Handle(TDataStd_RealList) aListX, aListY;
1382 getPointsLists( theSectionIndex, aListX, aListY );
1384 if ( thePointIndex < 0 )
1386 aListX->Prepend( thePoint.X() );
1387 aListY->Prepend( thePoint.Y() );
1389 else if ( thePointIndex >= aListX->Extent() )
1391 aListX->Append( thePoint.X() );
1392 aListY->Append( thePoint.Y() );
1396 TColStd_ListOfReal anOldListX;
1397 anOldListX = aListX->List();
1399 TColStd_ListOfReal anOldListY;
1400 anOldListY = aListY->List();
1402 // Refill the existing lists
1406 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1407 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1408 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1410 double aCoordX = anIterX.Value();
1411 double aCoordY = anIterY.Value();
1413 if ( i == thePointIndex )
1415 // Insert our new point instead of old one
1416 aCoordX = thePoint.X();
1417 aCoordY = thePoint.Y();
1420 aListX->Append( aCoordX );
1421 aListY->Append( aCoordY );
1428 void HYDROData_PolylineXY::SetPoints( const int theSectionIndex,
1429 const PointsList& thePoints )
1431 Handle(TDataStd_RealList) aListX, aListY;
1432 getPointsLists( theSectionIndex, aListX, aListY );
1437 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1439 const Point& aPoint = thePoints.Value( i );
1440 aListX->Append( aPoint.X() );
1441 aListY->Append( aPoint.Y() );
1445 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1446 const int thePointIndex )
1448 Handle(TDataStd_RealList) aListX, aListY;
1449 getPointsLists( theSectionIndex, aListX, aListY, false );
1450 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1453 if ( aListX->Extent() == 1 )
1455 removePointsLists( theSectionIndex );
1459 TColStd_ListOfReal anOldListX;
1460 anOldListX = aListX->List();
1462 TColStd_ListOfReal anOldListY;
1463 anOldListY = aListY->List();
1465 // Refill the existing lists
1469 TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1470 TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1471 for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1473 if ( i == thePointIndex )
1474 continue; // skip index to remove
1476 aListX->Append( anIterX.Value() );
1477 aListY->Append( anIterY.Value() );
1484 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
1486 PointsList aResList;
1490 const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1493 Handle(TDataStd_RealList) aListX, aListY;
1494 getPointsLists( theSectionIndex, aListX, aListY, false );
1495 if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1498 TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1499 TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1500 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document();
1501 for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1503 Point aPoint( anIterX.Value(), anIterY.Value() );
1504 if (IsConvertToGlobal)
1505 aDoc->Transform( aPoint, false );
1506 aResList.Append( aPoint );
1512 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1516 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1517 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1518 NCollection_Sequence<bool> aSectClosures;
1519 GetSections( aSectNames, aSectTypes, aSectClosures );
1521 for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1523 TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1524 SectionType aSectionType = aSectTypes.Value( aSectionId );
1525 bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1527 PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1528 if ( aSectPointsList.IsEmpty() )
1531 NCollection_Sequence<gp_XYZ> aPoints;
1532 for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1534 const Point& aSectPoint = aSectPointsList.Value( i );
1536 gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1537 aPoints.Append( aPoint );
1540 BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1546 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1548 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1549 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1550 NCollection_Sequence<bool> aSectClosures;
1551 GetSections( aSectNames, aSectTypes, aSectClosures );
1553 gp_XY aDelta( theDx, theDy );
1554 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1556 PointsList aPoints = GetPoints( i );
1557 for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1559 Point& aPoint = aPoints.ChangeValue( j );
1562 SetPoints( i, aPoints );
1567 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1569 NCollection_Sequence<TCollection_AsciiString> aSectNames;
1570 NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1571 NCollection_Sequence<bool> aSectClosures;
1572 GetSections( aSectNames, aSectTypes, aSectClosures );
1574 for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1575 PointsList aPoints = GetPoints( i );
1576 for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1577 Point& aPoint = aPoints.ChangeValue( j );
1579 QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1581 aPoint.SetX( aTrsfPoint.x() );
1582 aPoint.SetY( aTrsfPoint.y() );
1584 SetPoints( i, aPoints );
1590 bool HYDROData_PolylineXY::IsCustom() const
1592 if( myIsInCustomFlag )
1595 bool isNull = GetShape().IsNull();
1598 HYDROData_PolylineXY* aThat = const_cast<HYDROData_PolylineXY*>( this );
1599 aThat->myIsInCustomFlag = true;
1600 for( int i=0, n=NbSections(); i<n; i++ )
1601 aNbPoints += NbPoints( i );
1602 aThat->myIsInCustomFlag = false;
1604 return !isNull && aNbPoints == 0;
1607 bool HYDROData_PolylineXY::GetIsInCustomFlag() const
1609 return myIsInCustomFlag;
1612 void HYDROData_PolylineXY::SetIsInCustomFlag( bool theValue )
1614 myIsInCustomFlag = theValue;
1617 void HYDROData_PolylineXY::Interpolate()
1619 ImportShape( GetShape(), true, NULL );
1622 void HYDROData_PolylineXY::SetDBFInfo( const QStringList& theDBFTable )
1625 Handle_TDataStd_ExtStringArray TExtStrArr =
1626 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_DBFTableInfo), 1, theDBFTable.size() );
1627 foreach (QString val, theDBFTable)
1629 std::string StdVal = val.toStdString();
1630 const char* aCVal = StdVal.c_str();
1631 TExtStrArr->SetValue(i, TCollection_ExtendedString(aCVal));
1636 bool HYDROData_PolylineXY::GetDBFInfo(QStringList& theDBFTable) const
1638 TDF_Label aLabel = myLab.FindChild( DataTag_DBFTableInfo, false );
1639 if ( !aLabel.IsNull() )
1641 Handle(TDataStd_ExtStringArray) anExtStrArr;
1642 if ( aLabel.FindAttribute( TDataStd_ExtStringArray::GetID(), anExtStrArr ) )
1644 for (int i = anExtStrArr->Lower(); i <= anExtStrArr->Upper(); i++ )
1646 Standard_ExtString str = anExtStrArr->Value(i).ToExtString();
1647 TCollection_AsciiString aText (str);
1648 theDBFTable << QString(aText.ToCString());
1658 HYDROData_SequenceOfObjects HYDROData_PolylineXY::ImportShapesFromFile(const QString& aFileName)
1660 HYDROData_SequenceOfObjects importedEntities;
1661 if (aFileName.isEmpty())
1662 return importedEntities;
1664 QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
1666 bool importXY = false;
1667 Handle(HYDROData_Document) theDocument = HYDROData_Document::Document();
1672 HYDROData_ShapeFile anImporter;
1673 NCollection_IndexedDataMap<Handle(HYDROData_Entity), SHPObject*> theEntitiesToSHPObj;
1674 NCollection_Sequence<Handle(HYDROData_Entity)> PolyEnt;
1675 int aShapeTypeOfFile = -1;
1676 int aStat = anImporter.OpenAndParse(aFileName);
1679 aStat = anImporter.ImportPolylines(theDocument, aFileName, theEntitiesToSHPObj,
1680 HYDROData_ShapeFile::ImportShapeType_All);
1681 for (int k = 1; k <= theEntitiesToSHPObj.Extent(); k++)
1683 PolyEnt.Append(theEntitiesToSHPObj.FindKey(k));
1685 if (aStat == 1 || aStat == 2)
1688 QString aDBFFileName;
1689 aDBFFileName = aFileName.simplified().replace(aFileName.simplified().size() - 4, 4, ".dbf");
1690 bool DBF_Stat = anImporter.DBF_OpenDBF(aDBFFileName);
1693 QStringList aFieldList = anImporter.DBF_GetFieldList();
1694 int nbRecords = anImporter.DBF_GetNbRecords();
1695 if (theEntitiesToSHPObj.Extent() == nbRecords)
1697 int indNameAttrFound = -1;
1699 bool bUseNameAttrFound = false;
1700 for (QStringList::iterator it = aFieldList.begin(); it != aFieldList.end(); it++, k++)
1701 if (QString::compare(*it, "name", Qt::CaseInsensitive) == 0)
1703 indNameAttrFound = k;
1707 if (indNameAttrFound != -1)
1708 bUseNameAttrFound = true;
1710 std::vector<std::vector<HYDROData_ShapeFile::DBF_AttrValue>> anAttrVV;
1711 for (int i = 0; i < aFieldList.size(); i++)
1713 std::vector<HYDROData_ShapeFile::DBF_AttrValue> anAttrV;
1714 anAttrVV.push_back(anAttrV);
1715 anImporter.DBF_GetAttributeList(i, anAttrVV[i]);
1719 for (int i = 1; i <= theEntitiesToSHPObj.Extent(); i++)
1721 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast(
1722 theEntitiesToSHPObj.FindKey(i));
1723 QStringList aDBFinfo;
1724 aDBFinfo << aFieldList; //first, the table header
1725 for (int j = 0; j < aFieldList.size(); j++)
1728 const HYDROData_ShapeFile::DBF_AttrValue &attrV = anAttrVV[j][i - 1];
1729 if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_String)
1730 aDBFinfo << attrV.myStrVal;
1731 else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Integer)
1732 aDBFinfo << QString::number(attrV.myIntVal);
1733 else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Double)
1734 aDBFinfo << QString::number(attrV.myDoubleVal);
1738 aPolylineXY->SetDBFInfo(aDBFinfo);
1739 if (bUseNameAttrFound)
1741 QString nameOfPoly = aDBFinfo[aDBFinfo.size() / 2 + indNameAttrFound];
1742 if (!nameOfPoly.isEmpty())
1743 aPolylineXY->SetName(nameOfPoly);
1746 aPolylineXY->SetName("null_name_" + QString::number(indNULL));
1755 if (anImporter.GetShapeType() == 13) //Polyline 3D
1757 NCollection_Sequence<Handle(HYDROData_Entity)> CheckedPolylines = PolyEnt; //aDLG->GetCheckedPolylines();
1758 NCollection_IndexedDataMap<Handle(HYDROData_Entity), SHPObject*> CheckedEntitiesToSHPObj;
1759 for (int k = 1; k <= CheckedPolylines.Size(); k++)
1761 SHPObject *const*SHPObkP = theEntitiesToSHPObj.Seek(CheckedPolylines(k));
1762 if (SHPObkP != NULL)
1763 CheckedEntitiesToSHPObj.Add(CheckedPolylines(k), *SHPObkP);
1765 NCollection_Sequence<Handle(HYDROData_Entity)> theEntitiesPoly3D;
1766 anImporter.ImportPolylines3D(theDocument, CheckedEntitiesToSHPObj, theEntitiesPoly3D,
1767 HYDROData_ShapeFile::ImportShapeType_All);
1768 PolyEnt.Append(theEntitiesPoly3D);
1773 importedEntities.Append(PolyEnt);
1775 else if (anExt == "xy" || (importXY && anExt == "xyz"))
1777 if (!HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, true, importedEntities))
1778 DEBTRACE("no polyline imported");
1780 else if (anExt == "xyz")
1782 if (!HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, false, importedEntities))
1783 DEBTRACE("no polyline imported");
1785 return importedEntities;
1789 void HYDROData_PolylineXY::ExportShapeXY(Handle(HYDROData_Document) theDocument,
1790 const QString& aFileName,
1791 const NCollection_Sequence<Handle(HYDROData_PolylineXY)>& aPolyXYSeq,
1792 QStringList& aNonExpList)
1794 HYDROData_ShapeFile shp = HYDROData_ShapeFile();
1795 NCollection_Sequence<Handle(HYDROData_Polyline3D)> emptySeq;
1796 shp.Export(theDocument, aFileName, aPolyXYSeq, emptySeq, aNonExpList);