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>
26 #include <BOPAlgo_BOP.hxx>
27 #include <BOPAlgo_Builder.hxx>
28 #include <BOPAlgo_PaveFiller.hxx>
29 #include <BOPCol_ListOfShape.hxx>
30 #include <BRep_Builder.hxx>
31 #include <BRepAdaptor_Curve.hxx>
32 #include <BRepAlgoAPI_Fuse.hxx>
33 #include <BRepBuilderAPI_MakeFace.hxx>
34 #include <GCPnts_QuasiUniformDeflection.hxx>
36 #include <TopoDS_Compound.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <TopoDS_Iterator.hxx>
40 #include <TopoDS_Shell.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
43 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
44 #include <BOPAlgo_PaveFiller.hxx>
45 #include <BRepTools.hxx>
46 #include <TopExp_Explorer.hxx>
47 #include <ShapeUpgrade_UnifySameDomain.hxx>
49 #include <TDataStd_Real.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <ShapeBuild_ReShape.hxx>
52 #include <ShapeFix_Shape.hxx>
53 #include <BRepCheck_Shell.hxx>
54 #include <BRepCheck_ListOfStatus.hxx>
59 #include <QTextStream>
61 const char TELEMAC_FORMAT = 'f';
62 const int TELEMAC_PRECISION = 3;
65 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
66 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
70 @param theMap the land cover map to iterate through
72 HYDROData_LandCoverMap::Explorer::Explorer( const HYDROData_LandCoverMap& theMap )
77 HYDROData_LandCoverMap::Explorer::Explorer( const Handle( HYDROData_LandCoverMap )& theMap )
89 Initialize the iterator
90 @param theMap the land cover map to iterate through
92 void HYDROData_LandCoverMap::Explorer::Init( const HYDROData_LandCoverMap& theMap )
94 TopoDS_Shape aShape = theMap.GetShape();
99 myExplorer = new TopExp_Explorer();
100 myExplorer->Init( aShape, TopAbs_FACE );
103 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
104 if( myArray.IsNull() )
107 myIndex = myArray->Lower();
113 HYDROData_LandCoverMap::Explorer::~Explorer()
119 Return the current 0-based index of the iterator
120 @return the current index
122 int HYDROData_LandCoverMap::Explorer::Index() const
124 if( myArray.IsNull() )
127 return myIndex - myArray->Lower();
131 Return if the iterator has more elements
132 @return if the iterator has more elements
134 bool HYDROData_LandCoverMap::Explorer::More() const
136 return !myArray.IsNull() && myExplorer && myExplorer->More();
140 Move iterator to the next element
142 void HYDROData_LandCoverMap::Explorer::Next()
152 Get the current land cover (face)
153 @return the land cover's face
155 TopoDS_Face HYDROData_LandCoverMap::Explorer::Face() const
158 return TopoDS::Face( myExplorer->Current() );
160 return TopoDS_Face();
164 Get the current land cover's Strickler type
165 @return the land cover's Strickler type
167 QString HYDROData_LandCoverMap::Explorer::StricklerType() const
169 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
172 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
176 Set the Strickler type for the current land cover
177 @param theType the Strickler type
179 void HYDROData_LandCoverMap::Explorer::SetStricklerType( const QString& theType )
181 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
184 myArray->SetValue( myIndex, HYDROData_Tool::toExtString( theType ) );
190 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
191 : HYDROData_Entity( Geom_No )
198 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
204 @return object's kind
206 const ObjectKind HYDROData_LandCoverMap::GetKind() const
208 return KIND_LAND_COVER_MAP;
211 int HYDROData_LandCoverMap::GetLCCount() const
213 Explorer anIt( *this );
215 for( ; anIt.More(); anIt.Next() )
220 bool HYDROData_LandCoverMap::IsEmpty() const
222 Explorer anIt( *this );
230 Load attributes from DBF File
233 HYDROData_LandCoverMap::DBFStatus HYDROData_LandCoverMap::ImportDBF( const QString& theDBFFileName,
234 const QString& theFieldName,
235 const QStringList& theDBFValues,
236 const QStringList& theStricklerTypes,
237 const QList<int>& theIndices )
239 if (theDBFValues.size() != theStricklerTypes.size())
240 return DBFStatus_DIFF_SIZE_ERROR;
241 HYDROData_ShapeFile aDBFImporter;
242 if (!aDBFImporter.DBF_OpenDBF(theDBFFileName))
243 return DBFStatus_OPEN_FILE_ERROR; //cant open file
245 QStringList FieldList = aDBFImporter.DBF_GetFieldList();
246 int FieldNameIndex = FieldList.indexOf(theFieldName);
247 if (FieldNameIndex == -1)
248 return DBFStatus_NO_SUCH_FIELD_ERROR; //no such field
250 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
251 aDBFImporter.DBF_GetAttributeList(FieldNameIndex, theAttrV );
254 Explorer anIt( *this );
255 for( ; anIt.More(); anIt.Next() )
257 int CurIndex = anIt.Index();
258 int anIndex = CurIndex;
259 if( !theIndices.isEmpty() )
260 anIndex = theIndices[CurIndex];
262 HYDROData_ShapeFile::DBF_AttrValue AValue = theAttrV[anIndex];
263 int StricklerTypesInd = theDBFValues.indexOf( QString( AValue.myStrVal ) );
264 if ( StricklerTypesInd != -1)
265 anIt.SetStricklerType( theStricklerTypes[StricklerTypesInd] );
272 return DBFStatus_NO_DBFVALUES_CORRESPONDENCE_WARNING;
276 Export attributes to DBF File
279 void HYDROData_LandCoverMap::ExportDBF( const QString& theDBFFileName,
280 const QString& theFieldName,
281 const QStringList& theDBFValues,
282 const QStringList& theStricklerTypes) const
284 if (theDBFValues.size() != theStricklerTypes.size())
286 HYDROData_ShapeFile anExporter;
287 std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
288 Explorer anIt( *this );
289 for( ; anIt.More(); anIt.Next() )
291 QString CurST = anIt.StricklerType();
292 HYDROData_ShapeFile::DBF_AttrValue aCurAttrV;
293 aCurAttrV.myIsNull = false;
294 int StricklerTypesInd = theStricklerTypes.indexOf(CurST);
295 if (StricklerTypesInd != -1)
297 aCurAttrV.myStrVal = theDBFValues[StricklerTypesInd];
298 aCurAttrV.myFieldType = HYDROData_ShapeFile::DBF_FieldType_String;
299 theAttrV.push_back(aCurAttrV);
302 aCurAttrV.myIsNull = true;
304 //use actual str value; not the raw value
305 anExporter.DBF_WriteFieldAndValues(theDBFFileName, theFieldName, HYDROData_ShapeFile::DBF_FieldType_String, theAttrV, false);
309 int HashCode( const gp_Pnt& thePoint, const Standard_Integer theUpper )
311 int aHashX = HashCode( thePoint.X(), theUpper );
312 int aHashY = HashCode( thePoint.Y(), theUpper );
313 return (aHashX^aHashY)%theUpper;
316 bool operator == ( const gp_Pnt& thePoint1, const gp_Pnt& thePoint2 )
318 return thePoint1.IsEqual( thePoint2, Precision::Confusion() );
321 bool EdgeDiscretization( const TopoDS_Edge& theEdge,
322 Standard_Real theDeflection,
323 NCollection_IndexedMap<gp_Pnt>& theVerticesMap,
324 QList<int>& theVerticesIds )
326 BRepAdaptor_Curve aCurve( theEdge );
327 GCPnts_QuasiUniformDeflection aDiscrete( aCurve, theDeflection );
328 if( !aDiscrete.IsDone() )
331 int n = aDiscrete.NbPoints();
332 for( int i=1; i<=n; i++ )
334 gp_Pnt aPnt = aDiscrete.Value( i );
336 if( theVerticesMap.Contains( aPnt ) )
337 anId = theVerticesMap.FindIndex( aPnt );
340 anId = theVerticesMap.Size();
341 theVerticesMap.Add( aPnt );
343 theVerticesIds.append( anId );
349 Export the land cover map for the solver (Telemac)
350 @param theFileName the name of file
351 @return if the export is successful
353 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName, Standard_Real theDeflection ) const
355 TopoDS_Shape aLandCoverMapShape = GetShape();
356 TopTools_ListOfShape aListOfFaces;
357 Explorer anIt( *this );
358 QMap<Handle(TopoDS_TShape), QString> aTypesMap;
359 for( ; anIt.More(); anIt.Next() )
361 aListOfFaces.Append( anIt.Face() );
362 aTypesMap.insert( anIt.Face().TShape(), anIt.StricklerType() );
365 TopoDS_Shape aShape = MergeFaces( aListOfFaces, false );
367 NCollection_IndexedMap<gp_Pnt> aVerticesMap;
368 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> > anEdgesMap;
369 typedef QPair< QString, QList<int> > FaceData;
370 NCollection_IndexedDataMap< TopoDS_Face, FaceData > aFacesMap;
372 // add into the map all edges existing in the shell
373 TopExp_Explorer anExp1( aShape, TopAbs_EDGE );
374 for( ; anExp1.More(); anExp1.Next() )
376 TopoDS_Edge anEdge = TopoDS::Edge( anExp1.Current() );
377 QList<int> aVerticesIdsList;
378 if( EdgeDiscretization( anEdge, theDeflection, aVerticesMap, aVerticesIdsList ) )
379 anEdgesMap.Add( anEdge, aVerticesIdsList );
382 // add into the map all faces existing in the shell and correspondence between face and edges ids
383 TopExp_Explorer anExp2( aShape, TopAbs_FACE );
384 for( ; anExp2.More(); anExp2.Next() )
386 TopoDS_Face aFace = TopoDS::Face( anExp2.Current() );
387 TopExp_Explorer anExp3( aFace, TopAbs_EDGE );
388 QList<int> anEdgesIdsList;
389 for( ; anExp3.More(); anExp3.Next() )
391 TopoDS_Edge anEdge = TopoDS::Edge( anExp3.Current() );
392 int anEdgeId = anEdgesMap.FindIndex( anEdge );
393 anEdgesIdsList.append( anEdgeId );
397 aData.first = aTypesMap[aFace.TShape()];
398 aData.second = anEdgesIdsList;
399 aFacesMap.Add( aFace, aData );
402 QFile aFile( theFileName );
403 if( !aFile.open( QFile::WriteOnly | QFile::Text ) )
406 QTextStream aStream( &aFile );
407 aStream << "# nodes\n";
408 NCollection_IndexedMap<gp_Pnt>::Iterator anIt1( aVerticesMap );
409 for( ; anIt1.More(); anIt1.Next() )
411 gp_Pnt aPnt = anIt1.Value();
412 aStream << QString::number( aPnt.X(), TELEMAC_FORMAT, TELEMAC_PRECISION );
414 aStream << QString::number( aPnt.Y(), TELEMAC_FORMAT, TELEMAC_PRECISION );
416 aStream << QString::number( aPnt.Z(), TELEMAC_FORMAT, TELEMAC_PRECISION );
421 aStream << "# edges\n";
422 NCollection_IndexedDataMap< TopoDS_Edge, QList<int> >::Iterator anIt2( anEdgesMap );
423 for( ; anIt2.More(); anIt2.Next() )
425 QList<int> aVerticesIds = anIt2.Value();
426 foreach( int anId, aVerticesIds )
427 aStream << anId << " ";
432 aStream << "# faces\n";
433 NCollection_IndexedDataMap< TopoDS_Face, FaceData >::Iterator anIt3( aFacesMap );
434 for( ; anIt3.More(); anIt3.Next() )
436 QString aType = anIt3.Value().first;
437 QList<int> anEdgesIds = anIt3.Value().second;
438 aStream << "\"" << aType << "\" ";
439 foreach( int anId, anEdgesIds )
440 aStream << anId << " ";
450 Add a new object as land cover
451 @param theObject the object to add as land cover
452 @param theType the Strickler type for the new land cover
453 @return if the addition is successful
455 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
457 if( theObject.IsNull() )
460 TopoDS_Shape aShape = theObject->GetTopShape();
461 if( aShape.ShapeType()!=TopAbs_FACE )
464 TopoDS_Face aFace = TopoDS::Face( aShape );
465 return LocalPartition( aFace, theType );
468 bool HYDROData_LandCoverMap::Add( const TopoDS_Wire& theWire, const QString& theType )
470 if( !theWire.Closed() )
473 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( theWire, Standard_True ).Face();
474 return LocalPartition( aFace, theType );
478 Add a new polyline as land cover
479 @param thePolyline the polyline to add as land cover
480 @param theType the Strickler type for the new land cover
481 @return if the addition is successful
483 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
485 if( thePolyline.IsNull() )
488 TopoDS_Shape aShape = thePolyline->GetShape();
489 if( aShape.ShapeType()==TopAbs_WIRE )
490 return Add( TopoDS::Wire( aShape ), theType );
492 if( aShape.ShapeType()==TopAbs_COMPOUND )
494 TopExp_Explorer anExp( aShape, TopAbs_WIRE );
495 for( ; anExp.More(); anExp.Next() )
497 TopoDS_Wire aPart = TopoDS::Wire( anExp.Current() );
498 if( !Add( aPart, theType ) )
508 Remove the given face from land cover map
509 @param theFace the face to be removed
510 @return if the removing is successful
512 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
514 TopTools_ListOfShape aList;
515 aList.Append( theFace );
516 return Remove( aList );
520 Remove the given faces from land cover map
521 @param theFacesToRemove the face list to be removed
522 @return if the removing is successful
524 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
526 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
527 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
528 for( ; aFIt.More(); aFIt.Next() )
530 TopoDS_Shape aShape = aFIt.Value();
531 if( aShape.ShapeType()==TopAbs_FACE )
532 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
535 Explorer anIt( *this );
536 for( ; anIt.More(); anIt.Next() )
537 if( !aFacesToRemove.Contains( anIt.Face() ) )
538 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
540 if ( aNewFaces.IsEmpty() )
543 StoreLandCovers( aNewFaces );
548 Split the land cover map by the given polyline
549 @param thePolyline the tool polyline to split the land cover map
550 @return if the removing is successful
552 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
554 if( thePolyline.IsNull() )
557 TopoDS_Shape aShape = thePolyline->GetShape();
558 return Split( aShape );
563 Split the land cover map by the given polyline
564 @param theShape the tool polyline to split the land cover map
565 @return if the removing is successful
567 bool HYDROData_LandCoverMap::Split( const TopoDS_Shape& theShape )
569 return LocalPartition( theShape, "" );
574 Merge the given faces in the land cover
575 @param theFaces the faces to merge in the land cover map
576 @param theType the Strickler type for the merged land cover
577 @return if the merge is successful
579 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
581 // 1. to fuse the faces into the new face
582 TopoDS_Shape aMergedFace = MergeFaces( theFaces, true );
583 if( !aMergedFace.IsNull() && aMergedFace.ShapeType()==TopAbs_FACE )
585 // 2. to remove the merged faces from the current map
588 // 3. to add the face into the map
589 return LocalPartition( TopoDS::Face( aMergedFace ), theType );
595 Merge the given faces into the shell/face
596 @param theFaces the faces to merge
597 @param IsToUnify if the common edges should be removed (fused)
598 @param theTolerance the operation's tolerance
599 @return result shape (face or shell)
601 TopoDS_Shape HYDROData_LandCoverMap::MergeFaces( const TopTools_ListOfShape& theFaces,
602 bool IsToUnify, double theTolerance )
605 TopTools_ListIteratorOfListOfShape anIt;
606 BOPCol_ListOfShape aLC;
607 anIt.Initialize(theFaces);
608 for( ; anIt.More(); anIt.Next() )
610 if (anIt.Value().ShapeType() != TopAbs_FACE)
611 return TopoDS_Shape();
612 aLC.Append( anIt.Value() );
615 BOPAlgo_PaveFiller aPF;
616 aPF.SetArguments( aLC );
617 aPF.SetRunParallel( Standard_False );
618 aPF.SetFuzzyValue( theTolerance );
621 anError = aPF.ErrorStatus();
623 return TopoDS_Shape();
625 BOPAlgo_Builder anAlgo;
626 anIt.Initialize( theFaces );
627 for( ; anIt.More(); anIt.Next() )
628 anAlgo.AddArgument( anIt.Value() );
630 anAlgo.PerformWithFiller( aPF );
631 anError = anAlgo.ErrorStatus();
633 return TopoDS_Shape();
635 const TopoDS_Shape& aMergedShape = anAlgo.Shape();
637 BRep_Builder aBuilder;
639 aBuilder.MakeShell( aShell );
640 aShell.Closed( Standard_False );
641 TopExp_Explorer anExplorer( aMergedShape, TopAbs_FACE );
642 for( ; anExplorer.More(); anExplorer.Next() )
644 const TopoDS_Face& aFace = TopoDS::Face(anExplorer.Current());
647 if( aFace.ShapeType() == TopAbs_FACE )
649 aBuilder.Add( aShell, aFace );
650 aShell.Closed( Standard_False );
654 TopoDS_Shape aResult;
657 ShapeUpgrade_UnifySameDomain aUSD;
658 aUSD.Initialize( aShell );
660 aResult = aUSD.Shape();
665 anExplorer.Init( aResult, TopAbs_FACE );
667 TopoDS_Face anOneFace;
668 for( ; anExplorer.More(); anExplorer.Next(), n++ )
669 anOneFace = TopoDS::Face( anExplorer.Current() );
673 else if (aResult.ShapeType() == TopAbs_SHELL)
675 BRepCheck_Shell aBCS(TopoDS::Shell(aResult));
676 if (aBCS.Status().First() != BRepCheck_NoError)
678 ShapeFix_Shell aFixer;
679 aFixer.FixFaceOrientation(TopoDS::Shell(aResult), 1);
680 aResult = aFixer.Shape();
688 Change Strickler type for the list of faces to the given one
689 @param theFaces the faces to change type
690 @param theType the Strickler type for the given land cover(s)
691 @return if the change type operation is successful
693 bool HYDROData_LandCoverMap::ChangeType( const TopTools_ListOfShape& theFaces, const QString& theType )
695 HYDROData_MapOfFaceToStricklerType aFacesToChangeType;
696 TopTools_ListIteratorOfListOfShape aFIt( theFaces );
697 for( ; aFIt.More(); aFIt.Next() )
699 TopoDS_Shape aShape = aFIt.Value();
700 if( aShape.ShapeType()==TopAbs_FACE )
701 aFacesToChangeType.Add( TopoDS::Face( aShape ), "" );
705 Explorer anIt( *this );
706 for( ; anIt.More(); anIt.Next() )
707 if( aFacesToChangeType.Contains( anIt.Face() ) )
709 anIt.SetStricklerType( theType );
712 if ( aNbChanges != theFaces.Extent() )
719 Get the shape of the land cover map
721 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
723 return HYDROData_Entity::GetShape( DataTag_Shape );
727 Get Strickler type of the given land cover
728 @param theLandCover the land cover to get Strickler type of
729 @return name of Strickler type
731 QString HYDROData_LandCoverMap::StricklerType( const TopoDS_Face& theLandCover ) const
735 Explorer anIt( *this );
736 for( ; anIt.More(); anIt.Next() )
737 if( anIt.Face().IsEqual( theLandCover) )
739 aType = anIt.StricklerType();
747 Set the shape of the land cover map
748 @param theShape the new shape for the land cover map
750 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
752 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
756 Perform the local partition algorithm on the land cover
757 @param theNewShape the new shape to add into the land cover
758 @param theNewType the new Strickler type for the new land cover
759 @return if the local partition is successful
761 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
763 if( theNewShape.IsNull() )
766 BOPCol_ListOfShape aShapesList;
767 BOPAlgo_PaveFiller aPaveFiller;
768 HYDROData_MapOfFaceToStricklerType aNewFaces;
770 // add faces to shapes list
771 Explorer anIt( *this );
772 for( ; anIt.More(); anIt.Next() )
773 aShapesList.Append( anIt.Face() );
774 aShapesList.Append( theNewShape );
776 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
778 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
779 StoreLandCovers( aNewFaces );
783 // prepare pave filler
784 aPaveFiller.SetArguments( aShapesList );
785 aPaveFiller.Perform();
786 Standard_Integer anError = aPaveFiller.ErrorStatus();
790 // add faces to builder
791 BOPAlgo_Builder aBuilder;
793 for( ; anIt.More(); anIt.Next() )
794 aBuilder.AddArgument( anIt.Face() );
795 aBuilder.AddArgument( theNewShape );
797 // perform the partition with the pave filler
798 aBuilder.PerformWithFiller( aPaveFiller );
799 anError = aBuilder.ErrorStatus();
803 //std::cout << "History:" << std::endl;
804 // analysis of the history
805 // a. to fill map of shapes which come from the new face
806 NCollection_IndexedMap<int> aShapesFromNewFace;
807 //std::cout << "from NEW " << theNewShape << ":" << theNewType << std::endl;
808 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
809 TopTools_ListIteratorOfListOfShape aMIt( aModified );
810 for( ; aMIt.More(); aMIt.Next() )
812 //std::cout << " " << aMIt.Value() << std::endl;
813 int aKey = (int)(uintptr_t)aMIt.Value().TShape().operator->();
814 aShapesFromNewFace.Add( aKey );
817 // b. to fill map of parts except parts from new face
819 for( ; anIt.More(); anIt.Next() )
821 QString aSType = anIt.StricklerType();
822 //std::cout << "from " << anIt.Face() << ": " << anIt.StricklerType() << std::endl;
823 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
824 if( aModified.Extent() == 0 )
825 aModified.Append( anIt.Face() );
827 TopTools_ListIteratorOfListOfShape aMIt( aModified );
828 for( ; aMIt.More(); aMIt.Next() )
830 TopoDS_Shape aShape = aMIt.Value();
831 bool isFace = aShape.ShapeType()==TopAbs_FACE;
832 int aKey = (int)(uintptr_t)aShape.TShape().operator->();
833 bool isAlsoFromNew = aShapesFromNewFace.Contains( aKey );
834 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
835 if( isFace && !isAlsoFromNew )
836 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
840 // c. add the new shape if it is face with its type
841 if( theNewShape.ShapeType()==TopAbs_FACE )
842 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
844 // convert map of shape to type to compound and list of types
845 StoreLandCovers( aNewFaces );
850 Replace the set of land covers in the land cover map
851 @param theMap the map of shape (face) to Strickler type (string)
853 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
855 TopTools_ListOfShape aListOfFaces;
857 int n = theMap.Size();
859 Handle( TDataStd_ExtStringArray ) aTypes =
860 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
862 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
863 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
865 TopoDS_Face aFace = aNFIt.Key();
868 QString aType = aNFIt.Value();
869 aListOfFaces.Append(aFace);
870 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
873 TopoDS_Shape aResult;
874 if( aListOfFaces.Extent() == 1 )
875 aResult = aListOfFaces.First();
876 else if( aListOfFaces.Extent() > 1 )
877 aResult = MergeFaces( aListOfFaces, false );
879 //remove internal edges
880 aResult = RemoveInternal(aResult);
885 Checks that object has 2D presentation. Reimlemented to retun true.
887 bool HYDROData_LandCoverMap::IsHas2dPrs() const
893 Find the land cover for the given point
894 @param thePoint the point laying in some land cover
895 @param theType the returned type
896 @return the found land cover's face
898 TopoDS_Face HYDROData_LandCoverMap::FindByPoint( const gp_Pnt2d& thePoint, QString& theType ) const
900 //TODO: some more optimal algorithm
901 Explorer anIt( *this );
902 for( ; anIt.More(); anIt.Next() )
903 if( HYDROData_Tool::ComputePointState( thePoint.XY(), anIt.Face() ) == TopAbs_IN )
905 theType = anIt.StricklerType();
910 return TopoDS_Face();
913 void Dump( const QString& theName, const QStringList& theList, QStringList& theLines )
915 theLines.append( QString( "%1 = QStringList()" ).arg( theName ) );
916 foreach( QString anItem, theList )
917 theLines.append( QString( "%1.append( u\"%2\" )" ).arg( theName ).arg( anItem ) );
922 @param theTreatedObjects the map of treated objects
924 QStringList HYDROData_LandCoverMap::DumpToPython( const QString& thePyScriptPath,
925 MapOfTreatedObjects& theTreatedObjects ) const
927 QStringList aResList = dumpObjectCreation( theTreatedObjects );
928 QString aName = GetObjPyName();
930 QString aShapeFileName = thePyScriptPath;
931 aShapeFileName.replace( ".py", ".shp" );
932 QString aDbfFileName = thePyScriptPath;
933 aDbfFileName.replace( ".py", ".dbf" );
935 //TODO: export shape file
937 QString anAttr = "CODE_06"; //TODO: some custom choice
938 QStringList anAttrValues, aTypes;
939 HYDROData_Document::Document( myLab )->CollectQGISValues( anAttr, anAttrValues, aTypes );
940 ExportDBF( aDbfFileName, anAttr, anAttrValues, aTypes );
942 //TODO: insert command to import shape file
944 Dump( "attr_values", anAttrValues, aResList );
945 Dump( "types", aTypes, aResList );
946 aResList << QString( "%1.ImportDBF( '%2', '%3', attr_values, types )" ).
947 arg( aName ).arg( aDbfFileName ).arg( anAttr );
952 TopoDS_Shape HYDROData_LandCoverMap::RemoveInternal(const TopoDS_Shape& InSh)
954 //Shape must be topologically correct
955 TopExp_Explorer anExp(InSh, TopAbs_EDGE);
956 TopTools_ListOfShape anEdgesToRemove;
958 for(; anExp.More(); anExp.Next() )
960 TopoDS_Edge CurEdge = TopoDS::Edge(anExp.Current());
961 if (CurEdge.Orientation() == TopAbs_INTERNAL)
962 anEdgesToRemove.Append(CurEdge);
965 Handle_ShapeBuild_ReShape aReshape = new ShapeBuild_ReShape();
966 TopoDS_Shape OutSh = aReshape->Apply(InSh);
967 TopTools_ListIteratorOfListOfShape aIt(anEdgesToRemove);
968 for (; aIt.More(); aIt.Next())
969 aReshape->Remove(aIt.Value());
970 OutSh = aReshape->Apply(InSh);
972 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
979 void HYDROData_LandCoverMap::SetTransparency( double theTransparency )
981 Handle(TDataStd_Real) anAttr;
982 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
983 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
984 aLabel.AddAttribute( anAttr = new TDataStd_Real() );
985 anAttr->Set( theTransparency );
988 double HYDROData_LandCoverMap::GetTransparency() const
990 Handle(TDataStd_Real) anAttr;
991 TDF_Label aLabel = myLab.FindChild( DataTag_Transparency );
992 if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
995 return anAttr->Get();