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>
24 #include <BOPAlgo_Builder.hxx>
25 #include <BOPAlgo_PaveFiller.hxx>
26 #include <BOPCol_ListOfShape.hxx>
27 #include <BRep_Builder.hxx>
28 #include <BRepAlgoAPI_Fuse.hxx>
29 #include <BRepBuilderAPI_MakeFace.hxx>
30 #include <NCollection_IndexedMap.hxx>
31 #include <TNaming_Builder.hxx>
32 #include <TNaming_NamedShape.hxx>
34 #include <TopoDS_Compound.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TopoDS_Iterator.hxx>
37 #include <TopoDS_Shell.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
43 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
45 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
51 @param theMap the land cover map to iterate through
53 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
58 HYDROData_LandCoverMap::Iterator::Iterator( const Handle( HYDROData_LandCoverMap )& theMap )
70 Initialize the iterator
71 @param theMap the land cover map to iterate through
73 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
75 TopoDS_Shape aShape = theMap.GetShape();
79 myIterator = new TopoDS_Iterator( aShape );
81 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
87 HYDROData_LandCoverMap::Iterator::~Iterator()
93 Return if the iterator has more elements
94 @return if the iterator has more elements
96 bool HYDROData_LandCoverMap::Iterator::More() const
98 return !myArray.IsNull() && myIterator && myIterator->More();
102 Move iterator to the next element
104 void HYDROData_LandCoverMap::Iterator::Next()
114 Get the current land cover (face)
115 @return the land cover's face
117 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
120 return TopoDS::Face( myIterator->Value() );
122 return TopoDS_Face();
126 Get the current land cover's Strickler type
127 @return the land cover's Strickler type
129 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
131 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
134 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
140 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
147 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
153 @return object's kind
155 const ObjectKind HYDROData_LandCoverMap::GetKind() const
157 return KIND_LAND_COVER_MAP;
161 Import the land cover map from QGIS
162 @param theFileName the name of file
163 @return if the import is successful
165 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
172 Export the land cover map to QGIS
173 @param theFileName the name of file
174 @return if the export is successful
176 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
183 Export the land cover map for the solver (Telemac)
184 @param theFileName the name of file
185 @return if the export is successful
187 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
194 Add a new object as land cover
195 @param theObject the object to add as land cover
196 @param theType the Strickler type for the new land cover
197 @return if the addition is successful
199 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
201 if( theObject.IsNull() )
204 TopoDS_Shape aShape = theObject->GetTopShape();
205 if( aShape.ShapeType()!=TopAbs_FACE )
208 TopoDS_Face aFace = TopoDS::Face( aShape );
209 return LocalPartition( aFace, theType );
213 Add a new polyline as land cover
214 @param thePolyline the polyline to add as land cover
215 @param theType the Strickler type for the new land cover
216 @return if the addition is successful
218 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
220 if( thePolyline.IsNull() )
223 TopoDS_Shape aShape = thePolyline->GetShape();
224 if( aShape.ShapeType()!=TopAbs_WIRE )
227 TopoDS_Wire aWire = TopoDS::Wire( aShape );
228 if( !aWire.Closed() )
231 TopoDS_Face aFace = BRepBuilderAPI_MakeFace( aWire, Standard_True ).Face();
232 return LocalPartition( aFace, theType );
236 Remove the given face from land cover map
237 @param theFace the face to be removed
238 @return if the removing is successful
240 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
242 TopTools_ListOfShape aList;
243 aList.Append( theFace );
244 return Remove( aList );
248 Remove the given faces from land cover map
249 @param theFacesToRemove the face list to be removed
250 @return if the removing is successful
252 bool HYDROData_LandCoverMap::Remove( const TopTools_ListOfShape& theFacesToRemove )
254 HYDROData_MapOfFaceToStricklerType aFacesToRemove, aNewFaces;
255 TopTools_ListIteratorOfListOfShape aFIt( theFacesToRemove );
256 for( ; aFIt.More(); aFIt.Next() )
258 TopoDS_Shape aShape = aFIt.Value();
259 if( aShape.ShapeType()==TopAbs_FACE )
260 aFacesToRemove.Add( TopoDS::Face( aShape ), "" );
263 Iterator anIt( *this );
264 for( ; anIt.More(); anIt.Next() )
265 if( !aFacesToRemove.Contains( anIt.Face() ) )
266 aNewFaces.Add( anIt.Face(), anIt.StricklerType() );
268 StoreLandCovers( aNewFaces );
273 Split the land cover map by the given polyline
274 @param thePolyline the tool polyline to split the land cover map
275 @return if the removing is successful
277 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
279 if( thePolyline.IsNull() )
282 TopoDS_Shape aShape = thePolyline->GetShape();
283 return LocalPartition( aShape, "" );
287 Merge the given faces in the land cover
288 @param theFaces the faces to merge in the land cover map
289 @param theType the Strickler type for the merged land cover
290 @return if the merge is successful
292 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
294 // 1. to remove the merged faces from the current map
297 // 2. to fuse the faces into the new face
298 BRepAlgoAPI_Fuse aFuse;
299 aFuse.SetArguments( theFaces );
301 if( !aFuse.IsDone() )
304 TopoDS_Shape aMergedShape = aFuse.Shape();
305 if( aMergedShape.ShapeType()!=TopAbs_FACE )
308 // 3. to add the face into the map
309 return LocalPartition( TopoDS::Face( aMergedShape ), theType );
313 Get the shape of the land cover map
315 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
317 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
318 if ( !aLabel.IsNull() )
320 Handle(TNaming_NamedShape) aNamedShape;
321 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
322 return aNamedShape->Get();
324 return TopoDS_Shape();
328 Set the shape of the land cover map
329 @param theShape the new shape for the land cover map
331 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
333 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
334 aBuilder.Generated( theShape );
338 Perform the local partition algorithm on the land cover
339 @param theNewShape the new shape to add into the land cover
340 @param theNewType the new Strickler type for the new land cover
341 @return if the local partition is successful
343 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
345 if( theNewShape.IsNull() )
348 BOPCol_ListOfShape aShapesList;
349 BOPAlgo_PaveFiller aPaveFiller;
350 HYDROData_MapOfFaceToStricklerType aNewFaces;
352 // add faces to shapes list
353 Iterator anIt( *this );
354 for( ; anIt.More(); anIt.Next() )
355 aShapesList.Append( anIt.Face() );
356 aShapesList.Append( theNewShape );
358 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
360 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
361 StoreLandCovers( aNewFaces );
365 // prepare pave filler
366 aPaveFiller.SetArguments( aShapesList );
367 aPaveFiller.Perform();
368 Standard_Integer anError = aPaveFiller.ErrorStatus();
372 // add faces to builder
373 BOPAlgo_Builder aBuilder;
375 for( ; anIt.More(); anIt.Next() )
376 aBuilder.AddArgument( anIt.Face() );
377 aBuilder.AddArgument( theNewShape );
379 // perform the partition with the pave filler
380 aBuilder.PerformWithFiller( aPaveFiller );
381 anError = aBuilder.ErrorStatus();
385 // analysis of the history
386 // a. to fill map of shapes which come from the new face
387 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
388 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
389 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
390 TopTools_ListIteratorOfListOfShape aMIt( aModified );
391 for( ; aMIt.More(); aMIt.Next() )
393 //std::cout << " " << aMIt.Value() << std::endl;
394 aShapesFromNewFace.Add( aMIt.Value() );
397 // b. to fill map of parts except parts from new face
399 for( ; anIt.More(); anIt.Next() )
401 QString aSType = anIt.StricklerType();
402 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
403 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
404 TopTools_ListIteratorOfListOfShape aMIt( aModified );
405 for( ; aMIt.More(); aMIt.Next() )
407 TopoDS_Shape aShape = aMIt.Value();
408 bool isFace = aShape.ShapeType()==TopAbs_FACE;
409 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
410 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
411 if( isFace && !isAlsoFromNew )
412 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
416 // c. add the new shape if it is face with its type
417 if( theNewShape.ShapeType()==TopAbs_FACE )
418 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
420 // convert map of shape to type to compound and list of types
421 StoreLandCovers( aNewFaces );
426 Replace the set of land covers in the land cover map
427 @param theMap the map of shape (face) to Strickler type (string)
429 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
431 TopoDS_Compound aCompound;
432 BRep_Builder aCompoundBuilder;
433 aCompoundBuilder.MakeCompound( aCompound );
435 int n = theMap.Size();
436 Handle( TDataStd_ExtStringArray ) aTypes =
437 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
438 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
439 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
441 TopoDS_Face aFace = aNFIt.Key();
442 QString aType = aNFIt.Value();
443 aCompoundBuilder.Add( aCompound, aFace );
444 aTypes->SetValue( i, HYDROData_Tool::toExtString( aType ) );
447 SetShape( aCompound );