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_PolylineXY.h>
21 #include <HYDROData_Tool.h>
23 #include <BOPAlgo_Builder.hxx>
24 #include <BOPAlgo_PaveFiller.hxx>
25 #include <BOPCol_ListOfShape.hxx>
26 #include <BRep_Builder.hxx>
27 #include <NCollection_IndexedMap.hxx>
28 #include <TNaming_Builder.hxx>
29 #include <TNaming_NamedShape.hxx>
31 #include <TopoDS_Compound.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <TopoDS_Iterator.hxx>
34 #include <TopoDS_Shell.hxx>
35 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
40 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
42 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
48 @param theMap the land cover map to iterate through
50 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
56 Initialize the iterator
57 @param theMap the land cover map to iterate through
59 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
61 TopoDS_Shape aShape = theMap.GetShape();
65 myIterator = new TopoDS_Iterator( aShape );
67 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
73 HYDROData_LandCoverMap::Iterator::~Iterator()
79 Return if the iterator has more elements
80 @return if the iterator has more elements
82 bool HYDROData_LandCoverMap::Iterator::More() const
84 return !myArray.IsNull() && myIterator && myIterator->More();
88 Move iterator to the next element
90 void HYDROData_LandCoverMap::Iterator::Next()
97 Get the current land cover (face)
98 @return the land cover's face
100 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
103 return TopoDS::Face( myIterator->Value() );
105 return TopoDS_Face();
109 Get the current land cover's Strickler type
110 @return the land cover's Strickler type
112 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
114 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
117 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
123 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
130 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
136 @return object's kind
138 const ObjectKind HYDROData_LandCoverMap::GetKind() const
140 return KIND_LAND_COVER_MAP;
144 Import the land cover map from QGIS
145 @param theFileName the name of file
146 @return if the import is successful
148 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
155 Export the land cover map to QGIS
156 @param theFileName the name of file
157 @return if the export is successful
159 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
166 Export the land cover map for the solver (Telemac)
167 @param theFileName the name of file
168 @return if the export is successful
170 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
177 Add a new object as land cover
178 @param theObject the object to add as land cover
179 @param theType the Strickler type for the new land cover
180 @return if the addition is successful
182 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
189 Add a new polyline as land cover
190 @param thePolyline the polyline to add as land cover
191 @param theType the Strickler type for the new land cover
192 @return if the addition is successful
194 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_PolylineXY )& thePolyline, const QString& theType )
201 Remove the given face from land cover map
202 @param theFace the face to be removed
203 @return if the removing is successful
205 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
212 Split the land cover map by the given polyline
213 @param thePolyline the tool polyline to split the land cover map
214 @return if the removing is successful
216 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_PolylineXY )& thePolyline )
218 if( thePolyline.IsNull() )
221 TopoDS_Shape aShape = thePolyline->GetShape();
222 return LocalPartition( aShape, "" );
226 Merge the given faces in the land cover
227 @param theFaces the faces to merge in the land cover map
228 @param theType the Strickler type for the merged land cover
229 @return if the merge is successful
231 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
238 Get the shape of the land cover map
240 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
242 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
243 if ( !aLabel.IsNull() )
245 Handle(TNaming_NamedShape) aNamedShape;
246 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
247 return aNamedShape->Get();
249 return TopoDS_Shape();
253 Set the shape of the land cover map
254 @param theShape the new shape for the land cover map
256 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
258 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
259 aBuilder.Generated( theShape );
263 Perform the local partition algorithm on the land cover
264 @param theNewShape the new shape to add into the land cover
265 @param theNewType the new Strickler type for the new land cover
266 @return if the local partition is successful
268 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
270 if( theNewShape.IsNull() )
273 BOPCol_ListOfShape aShapesList;
274 BOPAlgo_PaveFiller aPaveFiller;
275 HYDROData_MapOfFaceToStricklerType aNewFaces;
277 // add faces to shapes list
278 Iterator anIt( *this );
279 for( ; anIt.More(); anIt.Next() )
280 aShapesList.Append( anIt.Face() );
281 aShapesList.Append( theNewShape );
283 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
285 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
286 StoreLandCovers( aNewFaces );
290 // prepare pave filler
291 aPaveFiller.SetArguments( aShapesList );
292 aPaveFiller.Perform();
293 Standard_Integer anError = aPaveFiller.ErrorStatus();
297 // add faces to builder
298 BOPAlgo_Builder aBuilder;
300 for( ; anIt.More(); anIt.Next() )
301 aBuilder.AddArgument( anIt.Face() );
302 aBuilder.AddArgument( theNewShape );
304 // perform the partition with the pave filler
305 aBuilder.PerformWithFiller( aPaveFiller );
306 anError = aBuilder.ErrorStatus();
310 // analysis of the history
311 // a. to fill map of shapes which come from the new face
312 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
313 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
314 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
315 TopTools_ListIteratorOfListOfShape aMIt( aModified );
316 for( ; aMIt.More(); aMIt.Next() )
318 //std::cout << " " << aMIt.Value() << std::endl;
319 aShapesFromNewFace.Add( aMIt.Value() );
322 // b. to fill map of parts except parts from new face
324 for( ; anIt.More(); anIt.Next() )
326 QString aSType = anIt.StricklerType();
327 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
328 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
329 TopTools_ListIteratorOfListOfShape aMIt( aModified );
330 for( ; aMIt.More(); aMIt.Next() )
332 TopoDS_Shape aShape = aMIt.Value();
333 bool isFace = aShape.ShapeType()==TopAbs_FACE;
334 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
335 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
336 if( isFace && !isAlsoFromNew )
337 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
341 // c. add the new shape if it is face with its type
342 if( theNewShape.ShapeType()==TopAbs_FACE )
343 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
345 // convert map of shape to type to compound and list of types
346 StoreLandCovers( aNewFaces );
351 Replace the set of land covers in the land cover map
352 @param theMap the map of shape (face) to Strickler type (string)
354 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
356 TopoDS_Compound aCompound;
357 BRep_Builder aCompoundBuilder;
358 aCompoundBuilder.MakeCompound( aCompound );
360 int n = theMap.Size();
361 Handle( TDataStd_ExtStringArray ) aTypes =
362 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
363 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
364 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
366 aCompoundBuilder.Add( aCompound, aNFIt.Key() );
367 aTypes->SetValue( i, HYDROData_Tool::toExtString( aNFIt.Value() ) );
370 SetShape( aCompound );