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_LandCoverMap.h>
20 #include <HYDROData_Object.h>
21 #include <HYDROData_PolylineXY.h>
22 #include <HYDROData_Tool.h>
23 #include <HYDROData_ShapeFile.h>
24 #include <HYDROData_Document.h>
25 #include <HYDROData_StricklerTable.h>
27 #include <BOPAlgo_BOP.hxx>
28 #include <BOPAlgo_Builder.hxx>
29 #include <BOPAlgo_PaveFiller.hxx>
30 #include <BOPCol_ListOfShape.hxx>
31 #include <BRep_Builder.hxx>
32 #include <BRepAdaptor_Curve.hxx>
33 #include <BRepAlgoAPI_Fuse.hxx>
34 #include <BRepBuilderAPI_MakeFace.hxx>
35 #include <GCPnts_QuasiUniformDeflection.hxx>
37 #include <TopoDS_Compound.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TopoDS_Shell.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
45 #include <TopTools_SequenceOfShape.hxx>
46 #include <BOPAlgo_PaveFiller.hxx>
47 #include <BRepTools.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <ShapeUpgrade_UnifySameDomain.hxx>
51 #include <TDataStd_Real.hxx>
52 #include <TopTools_IndexedMapOfShape.hxx>
53 #include <ShapeBuild_ReShape.hxx>
54 #include <ShapeFix_Shape.hxx>
55 #include <BRepCheck_Shell.hxx>
56 #include <BRepCheck_ListOfStatus.hxx>
57 #include <TopTools_SequenceOfShape.hxx>
58 #include <Handle_Geom_Curve.hxx>
59 #include <Handle_Geom_Line.hxx>
60 #include <Handle_Geom_TrimmedCurve.hxx>
61 #include <Geom_TrimmedCurve.hxx>
66 #include <QTextStream>
68 const char TELEMAC_FORMAT = 'f';
69 const int TELEMAC_PRECISION = 3;
72 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
73 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
77 @param theMap the land cover map to iterate through
79 HYDROData_LandCoverMap::Explorer::Explorer( const HYDROData_LandCoverMap& theMap )
84 HYDROData_LandCoverMap::Explorer::Explorer( const Handle( HYDROData_LandCoverMap )& theMap )
96 Initialize the iterator
97 @param theMap the land cover map to iterate through
99 void HYDROData_LandCoverMap::Explorer::Init( const HYDROData_LandCoverMap& theMap )
101 TopoDS_Shape aShape = theMap.GetShape();
102 if( aShape.IsNull() )
106 myExplorer = new TopExp_Explorer();
107 myExplorer->Init( aShape, TopAbs_FACE );
110 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
111 if( myArray.IsNull() )
114 myIndex = myArray->Lower();
120 HYDROData_LandCoverMap::Explorer::~Explorer()
126 Return the current 0-based index of the iterator
127 @return the current index
129 int HYDROData_LandCoverMap::Explorer::Index() const
131 if( myArray.IsNull() )
134 return myIndex - myArray->Lower();
138 Return if the iterator has more elements
139 @return if the iterator has more elements
141 bool HYDROData_LandCoverMap::Explorer::More() const
143 return !myArray.IsNull() && myExplorer && myExplorer->More();
147 Move iterator to the next element
149 void HYDROData_LandCoverMap::Explorer::Next()
159 Get the current land cover (face)
160 @return the land cover's face
162 TopoDS_Face HYDROData_LandCoverMap::Explorer::Face() const
165 return TopoDS::Face( myExplorer->Current() );
167 return TopoDS_Face();
171 Get the current land cover's Strickler type
172 @return the land cover's Strickler type
174 QString HYDROData_LandCoverMap::Explorer::StricklerType() const
176 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
179 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
183 Set the Strickler type for the current land cover
184 @param theType the Strickler type
186 void HYDROData_LandCoverMap::Explorer::SetStricklerType( const QString& theType )
188 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
191 myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
197 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
198 : HYDROData_Entity( Geom_No )
205 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
211 @return object's kind
213 const ObjectKind HYDROData_LandCoverMap::GetKind() const
215 return KIND_LAND_COVER_MAP;
218 int HYDROData_LandCoverMap::GetLCCount() const
220 Explorer anIt( *this );
222 for( ; anIt.More(); anIt.Next() )
227 bool HYDROData_LandCoverMap::IsEmpty() const
229 Explorer anIt( *this );
237 Load attributes from DBF File
240 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName,
241 const QString& theFieldName,
242 const QStringList& theDBFValues,
243 const QStringList& theStricklerTypes,
244 const QList<int>& theIndices )
246 if (theDBFValues.size() != theStricklerTypes.size())
247 return DBFStatus_DIFF_SIZE_ERROR;
248 HYDROData_ShapeFile aDBFImporter;
249 if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
250 return DBFStatus_OPEN_FILE_ERROR; //cant open file
252 QStringList FieldList = aDBFImporter.DBF_GetFieldList();
253 int FieldNameIndex = FieldList.indexOf(theFieldName);
254 if (FieldNameIndex == -1)
255 return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
257 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
258 aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV );
261 Explorer anIt( *this );
262 for( ; anIt.More(); anIt.Next() )
264 int CurIndex = anIt.Index();
265 int anIndex = CurIndex;
266 if( !theIndices.isEmpty() )
267 anIndex = theIndices[CurIndex];
269 HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[anIndex];
270 int StricklerTypesInd = theDBFValues.indexOf( QString( AValue.myStrVal ) );
271 if ( StricklerTypesInd != -1)
272 anIt.SetStricklerType( theStricklerTypes[StricklerTypesInd] );
279 return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
283 Export attributes to DBF File
286 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName,
287 const QString& theFieldName,
288 const QStringList& theDBFValues,
289 const QStringList& theStricklerTypes) const
291 if (theDBFValues.size() != theStricklerTypes.size())
293 HYDROData_ShapeFile anExporter;
294 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
295 Explorer anIt( *this );
296 for( ; anIt.More(); anIt.Next() )
298 QString CurST = anIt.StricklerType();
299 HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
300 aCurAttrV.myIsNull = false;
301 int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
302 if (StricklerTypesInd != -1)
304 aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
305 aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
306 theAttrV.push_back(aCurAttrV);
309 aCurAttrV.myIsNull = true;
311 //use actual str value; not the raw value
312 anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
316 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
318 int aHashX = HashCode( thePoint.X(), theUpper );
319 int aHashY = HashCode( thePoint.Y(), theUpper );
320 return (aHashX^aHashY)%theUpper;
323 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
325 return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
328 bool EdgeDiscretization( const TopoDS_Edge& theEdge,
329 Standard_Real theDeflection,
330 NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
331 QList<int>& theVerticesIds )
333 BRepAdaptor_Curve aCurve( theEdge );
334 GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
335 if( !aDiscrete.IsDone() )
338 int n = aDiscrete.NbPoints();
339 for( int i=1; i<=n; i++ )
341 gp_Pnt aPnt = aDiscrete.Value( i );
343 if( theVerticesMap.Contains( aPnt ) )
344 anId = theVerticesMap.FindIndex( aPnt );
347 anId = theVerticesMap.Size();
348 theVerticesMap.Add( aPnt );
350 theVerticesIds.append( anId );
356 Export the land cover map for the solver (Telemac)
357 @param theFileName the name of file
358 @return if the export is successful
360 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName,
361 double theDeflection,
362 const Handle(HYDROData_StricklerTable)& theTable ) const
364 TopoDS_Shape aLandCoverMapShape = GetShape();
365 TopTools_ListOfShape aListOfFaces;
366 Explorer anIt( *this );
367 QMap<Handle(TopoDS_TShape), QString> aTypesMap;
368 for( ; anIt.More(); anIt.Next() )
370 aListOfFaces.Append( anIt.Face() );
371 aTypesMap.insert( anIt.Face().TShape(), anIt.StricklerType() );
374 TopoDS_Shape aShape = MergeFaces( aListOfFaces, false );
376 NCollection_IndexedMap<gp_Pnt> aVerticesMap;
377 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
378 typedef QPair< QString, QList<int> > FaceData;
379 NCollection_IndexedDataMap< TopoDS_Face, FaceData > aFacesMap;
381 // add into the map all edges existing in the shell
382 TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
383 for( ; anExp1.More(); anExp1.Next() )
385 TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
386 QList<int> aVerticesIdsList;
387 if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
388 anEdgesMap.Add( anEdge, aVerticesIdsList );
391 // add into the map all faces existing in the shell and correspondence between face and edges ids
392 TopExp_Explorer anExp2( aShape, TopAbs_FACE );
393 for( ; anExp2.More(); anExp2.Next() )
395 TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
396 TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
397 QList<int> anEdgesIdsList;
398 for( ; anExp3.More(); anExp3.Next() )
400 TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
401 int anEdgeId = anEdgesMap.FindIndex( anEdge );
402 anEdgesIdsList.append( anEdgeId );
406 aData.first = aTypesMap[aFace.TShape()];
407 aData.second = anEdgesIdsList;
408 aFacesMap.Add( aFace, aData );
411 QFile aFile( theFileName );
412 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
415 QTextStream aStream( &aFile );
416 aStream << "# nodes\n";
417 NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
418 for( ; anIt1.More(); anIt1.Next() )
420 gp_Pnt aPnt = anIt1.Value();
421 aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
423 aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
425 aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
430 aStream << "# edges\n";
431 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
432 for( ; anIt2.More(); anIt2.Next() )
434 QList<int> aVerticesIds = anIt2.Value();
435 foreach( int anId, aVerticesIds )
436 aStream << anId << " ";
441 aStream << "# faces\n";
442 NCollection_IndexedDataMap< TopoDS_Face, FaceData >::Iterator anIt3( aFacesMap );
443 for( ; anIt3.More(); anIt3.Next() )
445 QString aType = anIt3.Value().first;
446 double aCoeff = theTable->Get( aType, 0.0 );
447 QList<int> anEdgesIds = anIt3.Value().second;
448 //aStream << "\"" << aType << "\" ";
449 aStream << QString::number( aCoeff, TELEMAC_FORMAT, TELEMAC_PRECISION ) << " ";
450 foreach( int anId, anEdgesIds )
451 aStream << anId << " ";
461 Add a new object as land cover
462 @param theObject the object to add as land cover
463 @param theType the Strickler type for the new land cover
464 @return if the addition is successful
466 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
468 if( theObject.IsNull() )
471 TopoDS_Shape aShape = theObject->GetTopShape();
472 if( aShape.ShapeType()!=TopAbs_FACE )
475 TopoDS_Face aFace = TopoDS::Face( aShape );
476 return LocalPartition( aFace, theType );
479 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
481 if( !theWire.Closed() )
484 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
485 return LocalPartition( aFace, theType );
489 Add a new polyline as land cover
490 @param thePolyline the polyline to add as land cover
491 @param theType the Strickler type for the new land cover
492 @return if the addition is successful
494 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
496 if( thePolyline.IsNull() )
499 TopoDS_Shape aShape = thePolyline->GetShape();
500 if( aShape.ShapeType()==TopAbs_WIRE )
501 return Add( TopoDS::Wire( aShape ), theType );
503 if( aShape.ShapeType()==TopAbs_COMPOUND )
505 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
506 for( ; anExp.More(); anExp.Next() )
508 TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
509 if( !Add( aPart, theType ) )
519 Remove the given face from land cover map
520 @param theFace the face to be removed
521 @return if the removing is successful
523 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
525 TopTools_ListOfShape aList;
526 aList.Append( theFace );
527 return Remove( aList );
531 Remove the given faces from land cover map
532 @param theFacesToRemove the face list to be removed
533 @return if the removing is successful
535 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
537 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
538 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
539 for( ; aFIt.More(); aFIt.Next() )
541 TopoDS_Shape aShape = aFIt.Value();
542 if( aShape.ShapeType()==TopAbs_FACE )
543 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
546 Explorer anIt( *this );
547 for( ; anIt.More(); anIt.Next() )
548 if( !aFacesToRemove.Contains( anIt.Face() ) )
549 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
551 if ( aNewFaces.IsEmpty() )
554 StoreLandCovers( aNewFaces );
559 Split the land cover map by the given polyline
560 @param thePolyline the tool polyline to split the land cover map
561 @return if the removing is successful
563 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
565 if( thePolyline.IsNull() )
568 TopoDS_Shape aShape = thePolyline->GetShape();
569 return Split( aShape );
574 Split the land cover map by the given polyline
575 @param theShape the tool polyline to split the land cover map
576 @return if the removing is successful
578 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
580 int aNbCL = GetLCCount();
581 bool aResult = LocalPartition( theShape, "" );
582 return aResult && aNbCL != GetLCCount();
587 Merge the given faces in the land cover
588 @param theFaces the faces to merge in the land cover map
589 @param theType the Strickler type for the merged land cover
590 @return if the merge is successful
592 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
594 // 1. to fuse the faces into the new face
595 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
597 if( !aMergedFace.IsNull() )
599 // 2. to remove the merged faces from the current map
601 TopExp_Explorer Exp(aMergedFace, TopAbs_FACE);
602 for( ; Exp.More(); Exp.Next() )
604 const TopoDS_Face& aCF = TopoDS::Face(Exp.Current());
605 // 3. to add the face into the map
606 aStat = aStat && LocalPartition( aCF, theType );
615 Merge the given faces into the shell/face
616 @param theFaces the faces to merge
617 @param IsToUnify if the common edges should be removed (fused)
618 @param theTolerance the operation's tolerance
619 @return result shape (face or shell)
621 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
622 bool IsToUnify, double theTolerance )
625 TopTools_ListIteratorOfListOfShape anIt;
626 BOPCol_ListOfShape aLC;
627 anIt.Initialize(theFaces);
628 for( ; anIt.More(); anIt.Next() )
630 if (anIt.Value().ShapeType() != TopAbs_FACE)
631 return TopoDS_Shape();
632 aLC.Append( anIt.Value() );
635 BOPAlgo_PaveFiller aPF;
636 aPF.SetArguments( aLC );
637 aPF.SetRunParallel( Standard_False );
638 aPF.SetFuzzyValue( theTolerance );
641 anError = aPF.ErrorStatus();
643 return TopoDS_Shape();
645 BOPAlgo_Builder anAlgo;
646 anIt.Initialize( theFaces );
647 for( ; anIt.More(); anIt.Next() )
648 anAlgo.AddArgument( anIt.Value() );
650 anAlgo.PerformWithFiller( aPF );
651 anError = anAlgo.ErrorStatus();
653 return TopoDS_Shape();
655 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
657 BRep_Builder aBuilder;
659 aBuilder.MakeShell( aShell );
660 aShell.Closed( Standard_False );
661 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
662 for( ; anExplorer.More(); anExplorer.Next() )
664 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
667 if( aFace.ShapeType() == TopAbs_FACE )
669 aBuilder.Add( aShell, aFace );
670 aShell.Closed( Standard_False );
674 TopoDS_Shape aResult;
677 ShapeUpgrade_UnifySameDomain aUSD;
678 aUSD.Initialize( aShell );
680 aResult = aUSD.Shape();
685 anExplorer.Init( aResult, TopAbs_FACE );
687 TopoDS_Face anOneFace;
688 for( ; anExplorer.More(); anExplorer.Next(), n++ )
689 anOneFace = TopoDS::Face( anExplorer.Current() );
693 else if (aResult.ShapeType() == TopAbs_SHELL)
695 BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
696 if (aBCS.Status().First() != BRepCheck_NoError)
698 ShapeFix_Shell aFixer;
699 aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
700 aResult = aFixer.Shape();
708 Change Strickler type for the list of faces to the given one
709 @param theFaces the faces to change type
710 @param theType the Strickler type for the given land cover(s)
711 @return if the change type operation is successful
713 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
715 HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
716 TopTools_ListIteratorOfListOfShape aFIt( theFaces );
717 for( ; aFIt.More(); aFIt.Next() )
719 TopoDS_Shape aShape = aFIt.Value();
720 if( aShape.ShapeType()==TopAbs_FACE )
721 aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
725 Explorer anIt( *this );
726 for( ; anIt.More(); anIt.Next() )
727 if( aFacesToChangeType.Contains( anIt.Face() ) )
729 anIt.SetStricklerType( theType );
732 if ( aNbChanges != theFaces.Extent() )
739 Get the shape of the land cover map
741 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
743 return HYDROData_Entity::GetShape( DataTag_Shape );
747 Get Strickler type of the given land cover
748 @param theLandCover the land cover to get Strickler type of
749 @return name of Strickler type
751 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
755 Explorer anIt( *this );
756 for( ; anIt.More(); anIt.Next() )
757 if( anIt.Face().IsEqual( theLandCover) )
759 aType = anIt.StricklerType();
767 Set the shape of the land cover map
768 @param theShape the new shape for the land cover map
770 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
772 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
776 Perform the local partition algorithm on the land cover
777 @param theNewShape the new shape to add into the land cover
778 @param theNewType the new Strickler type for the new land cover
779 @return if the local partition is successful
781 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
783 if( theNewShape.IsNull() )
786 BOPCol_ListOfShape aShapesList;
787 BOPAlgo_PaveFiller aPaveFiller;
788 HYDROData_MapOfFaceToStricklerType aNewFaces;
790 // add faces to shapes list
791 Explorer anIt( *this );
792 for( ; anIt.More(); anIt.Next() )
793 aShapesList.Append( anIt.Face() );
794 aShapesList.Append( theNewShape );
796 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
798 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
799 StoreLandCovers( aNewFaces );
803 // prepare pave filler
804 aPaveFiller.SetArguments( aShapesList );
805 aPaveFiller.Perform();
806 Standard_Integer anError = aPaveFiller.ErrorStatus();
810 // add faces to builder
811 BOPAlgo_Builder aBuilder;
813 for( ; anIt.More(); anIt.Next() )
814 aBuilder.AddArgument( anIt.Face() );
815 aBuilder.AddArgument( theNewShape );
817 // perform the partition with the pave filler
818 aBuilder.PerformWithFiller( aPaveFiller );
819 anError = aBuilder.ErrorStatus();
823 //std::cout << "History:" << std::endl;
824 // analysis of the history
825 // a. to fill map of shapes which come from the new face
826 NCollection_IndexedMap<int> aShapesFromNewFace;
827 //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
828 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
829 TopTools_ListIteratorOfListOfShape aMIt( aModified );
830 for( ; aMIt.More(); aMIt.Next() )
832 //std::cout << " " << aMIt.Value() << std::endl;
833 int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
834 aShapesFromNewFace.Add( aKey );
837 // b. to fill map of parts except parts from new face
839 for( ; anIt.More(); anIt.Next() )
841 QString aSType = anIt.StricklerType();
842 //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
843 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
845 if( aModified.Extent() == 0 )
846 aModified.Append( anIt.Face() );
848 TopTools_ListIteratorOfListOfShape aMIt( aModified );
849 for( ; aMIt.More(); aMIt.Next() )
851 TopoDS_Shape aShape = aMIt.Value();
852 bool isFace = aShape.ShapeType()==TopAbs_FACE;
853 int aKey = (int)(uintptr_t)aShape.TShape().operator->();
854 bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
855 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
856 if( isFace && !isAlsoFromNew )
857 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
861 // c. add the new shape if it is face with its type
862 if( theNewShape.ShapeType()==TopAbs_FACE )
863 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
865 // convert map of shape to type to compound and list of types
866 StoreLandCovers( aNewFaces );
871 Replace the set of land covers in the land cover map
872 @param theMap the map of shape (face) to Strickler type (string)
874 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
876 TopTools_ListOfShape aListOfFaces;
878 int n = theMap.Size();
880 Handle( TDataStd_ExtStringArray ) aTypes =
881 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
883 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
884 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
886 TopoDS_Face aFace = aNFIt.Key();
889 QString aType = aNFIt.Value();
890 aListOfFaces.Append(aFace);
891 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
894 TopoDS_Shape aResult;
895 if( aListOfFaces.Extent() == 1 )
896 aResult = aListOfFaces.First();
897 else if( aListOfFaces.Extent() > 1 )
898 aResult = MergeFaces( aListOfFaces, false );
900 //remove internal edges
901 aResult = RemoveInternal(aResult);
906 Checks that object has 2D presentation. Reimlemented to retun true.
908 bool HYDROData_LandCoverMap::IsHas2dPrs() const
914 Find the land cover for the given point
915 @param thePoint the point laying in some land cover
916 @param theType the returned type
917 @return the found land cover's face
919 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
921 //TODO: some more optimal algorithm
922 Explorer anIt( *this );
923 for( ; anIt.More(); anIt.Next() )
924 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
926 theType = anIt.StricklerType();
931 return TopoDS_Face();
934 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
936 theLines.append( QString( "%1 = QStringList()" ).arg( theName ) );
937 foreach( QString anItem, theList )
938 theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
943 @param theTreatedObjects the map of treated objects
945 QStringList HYDROData_LandCoverMap::DumpToPython( const QString& thePyScriptPath,
946 MapOfTreatedObjects& theTreatedObjects ) const
948 QStringList aResList = dumpObjectCreation( theTreatedObjects );
949 QString aName = GetObjPyName();
951 QString aShpFileName = thePyScriptPath;
952 aShpFileName.replace( ".py", ".shp" );
953 QString aDbfFileName = thePyScriptPath;
954 aDbfFileName.replace( ".py", ".dbf" );
956 ExportSHP( aShpFileName, true, 0.1 );
958 QString anAttr = "CODE_06"; //TODO: some custom choice
959 QStringList anAttrValues, aTypes;
960 HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
961 ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
963 aResList << QString( "%1.ImportSHP( '%2' )" ).
964 arg( aName ).arg( aShpFileName );
966 Dump( "attr_values", anAttrValues, aResList );
967 Dump( "types", aTypes, aResList );
968 aResList << QString( "%1.ImportDBF( '%2', '%3', attr_values, types )" ).
969 arg( aName ).arg( aDbfFileName ).arg( anAttr );
974 TopoDS_Shape HYDROData_LandCoverMap::RemoveInternal(const TopoDS_Shape& InSh)
976 //Shape must be topologically correct
977 TopExp_Explorer anExp(InSh, TopAbs_EDGE);
978 TopTools_ListOfShape anEdgesToRemove;
980 for(; anExp.More(); anExp.Next() )
982 TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
983 if (CurEdge.Orientation() == TopAbs_INTERNAL)
984 anEdgesToRemove.Append(CurEdge);
987 Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
988 TopoDS_Shape OutSh = aReshape->Apply(InSh);
989 TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
990 for (; aIt.More(); aIt.Next())
991 aReshape->Remove(aIt.Value());
992 OutSh = aReshape->Apply(InSh);
994 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
1001 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
1003 Handle(TDataStd_Real) anAttr;
1004 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
1005 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
1006 aLabel.AddAttribute( anAttr = new TDataStd_Real() );
1007 anAttr->Set( theTransparency );
1010 double HYDROData_LandCoverMap::GetTransparency() const
1012 Handle(TDataStd_Real) anAttr;
1013 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
1014 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
1017 return anAttr->Get();
1021 bool HYDROData_LandCoverMap::ImportSHP( const QString& theSHPFileName,
1022 const QList<int>& theIndices )
1024 HYDROData_ShapeFile anImporter;
1025 QStringList aPolyList;
1026 TopTools_SequenceOfShape aFaces;
1027 int aSHapeType = -1;
1028 int Stat = anImporter.ImportPolygons(theSHPFileName, aPolyList, aFaces, aSHapeType);
1033 HYDROData_MapOfFaceToStricklerType aMapFace2ST;
1034 int maxInd = *std::max_element(theIndices.begin(), theIndices.end());
1035 if (maxInd > aPolyList.length())
1038 if (theIndices.empty())
1041 for ( int i = 1; i <=aFaces.Length(); i++ )
1043 TopoDS_Shape aShape = aFaces(i);
1044 if ( aShape.IsNull() )
1046 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1051 //import given indices
1052 foreach ( int Ind, theIndices )
1054 TopoDS_Shape aShape = aFaces(Ind + 1);
1055 if ( aShape.IsNull() )
1057 aMapFace2ST.Add( TopoDS::Face( aShape ), "" );
1061 StoreLandCovers(aMapFace2ST);
1065 bool HYDROData_LandCoverMap::ExportSHP( const QString& theSHPFileName, bool bUseDiscr, double theDefl) const
1067 HYDROData_ShapeFile anExporter;
1069 anExporter.Export(theSHPFileName, this, aList, bUseDiscr, theDefl );
1076 bool HYDROData_LandCoverMap::CheckLinear()
1078 TopoDS_Shape InpShape = GetShape();
1079 TopExp_Explorer anEdgeEx(InpShape, TopAbs_EDGE);
1080 for (; anEdgeEx.More(); anEdgeEx.Next())
1082 TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
1084 Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
1085 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
1088 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
1091 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());