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_Tool.h>
22 #include <BOPAlgo_Builder.hxx>
23 #include <BOPAlgo_PaveFiller.hxx>
24 #include <BOPCol_ListOfShape.hxx>
25 #include <BRep_Builder.hxx>
26 #include <NCollection_IndexedMap.hxx>
27 #include <TNaming_Builder.hxx>
28 #include <TNaming_NamedShape.hxx>
30 #include <TopoDS_Compound.hxx>
31 #include <TopoDS_Face.hxx>
32 #include <TopoDS_Iterator.hxx>
33 #include <TopoDS_Shell.hxx>
34 #include <TopTools_ListIteratorOfListOfShape.hxx>
38 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
39 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
41 class HYDROData_MapOfFaceToStricklerType : public NCollection_IndexedDataMap<TopoDS_Face, QString>
47 @param theMap the land cover map to iterate through
49 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
55 Initialize the iterator
56 @param theMap the land cover map to iterate through
58 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
60 TopoDS_Shape aShape = theMap.GetShape();
64 myIterator = new TopoDS_Iterator( aShape );
66 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
72 HYDROData_LandCoverMap::Iterator::~Iterator()
78 Return if the iterator has more elements
79 @return if the iterator has more elements
81 bool HYDROData_LandCoverMap::Iterator::More() const
83 return !myArray.IsNull() && myIterator && myIterator->More();
87 Move iterator to the next element
89 void HYDROData_LandCoverMap::Iterator::Next()
96 Get the current land cover (face)
97 @return the land cover's face
99 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
102 return TopoDS::Face( myIterator->Value() );
104 return TopoDS_Face();
108 Get the current land cover's Strickler type
109 @return the land cover's Strickler type
111 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
113 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
116 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
122 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
129 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
135 @return object's kind
137 const ObjectKind HYDROData_LandCoverMap::GetKind() const
139 return KIND_LAND_COVER_MAP;
143 Import the land cover map from QGIS
144 @param theFileName the name of file
145 @return if the import is successful
147 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
154 Export the land cover map to QGIS
155 @param theFileName the name of file
156 @return if the export is successful
158 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
165 Export the land cover map for the solver (Telemac)
166 @param theFileName the name of file
167 @return if the export is successful
169 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
176 Add a new object as land cover
177 @param theObject the object to add as land cover
178 @param theType the Strickler type for the new land cover
179 @return if the addition is successful
181 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
188 Add a new polyline as land cover
189 @param thePolyline the polyline to add as land cover
190 @param theType the Strickler type for the new land cover
191 @return if the addition is successful
193 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Polyline )& thePolyline, const QString& theType )
200 Remove the given face from land cover map
201 @param theFace the face to be removed
202 @return if the removing is successful
204 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
211 Split the land cover map by the given polyline
212 @param thePolyline the tool polyline to split the land cover map
213 @return if the removing is successful
215 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_Polyline )& thePolyline )
222 Merge the given faces in the land cover
223 @param theFaces the faces to merge in the land cover map
224 @param theType the Strickler type for the merged land cover
225 @return if the merge is successful
227 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
234 Get the shape of the land cover map
236 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
238 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
239 if ( !aLabel.IsNull() )
241 Handle(TNaming_NamedShape) aNamedShape;
242 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
243 return aNamedShape->Get();
245 return TopoDS_Shape();
249 Set the shape of the land cover map
250 @param theShape the new shape for the land cover map
252 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
254 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
255 aBuilder.Generated( theShape );
259 Perform the local partition algorithm on the land cover
260 @param theNewShape the new shape to add into the land cover
261 @param theNewType the new Strickler type for the new land cover
262 @return if the local partition is successful
264 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
266 if( theNewShape.IsNull() )
269 BOPCol_ListOfShape aShapesList;
270 BOPAlgo_PaveFiller aPaveFiller;
271 HYDROData_MapOfFaceToStricklerType aNewFaces;
273 // add faces to shapes list
274 Iterator anIt( *this );
275 for( ; anIt.More(); anIt.Next() )
276 aShapesList.Append( anIt.Face() );
277 aShapesList.Append( theNewShape );
279 if( aShapesList.Size()==1 && theNewShape.ShapeType()==TopAbs_FACE )
281 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
282 StoreLandCovers( aNewFaces );
286 // prepare pave filler
287 aPaveFiller.SetArguments( aShapesList );
288 aPaveFiller.Perform();
289 Standard_Integer anError = aPaveFiller.ErrorStatus();
293 // add faces to builder
294 BOPAlgo_Builder aBuilder;
296 for( ; anIt.More(); anIt.Next() )
297 aBuilder.AddArgument( anIt.Face() );
298 aBuilder.AddArgument( theNewShape );
300 // perform the partition with the pave filler
301 aBuilder.PerformWithFiller( aPaveFiller );
302 anError = aBuilder.ErrorStatus();
306 // analysis of the history
307 // a. to fill map of shapes which come from the new face
308 NCollection_IndexedMap<TopoDS_Shape> aShapesFromNewFace;
309 //std::cout << "new: " << theNewShape << " " << theNewType << std::endl;
310 TopTools_ListOfShape aModified = aBuilder.Modified( theNewShape );
311 TopTools_ListIteratorOfListOfShape aMIt( aModified );
312 for( ; aMIt.More(); aMIt.Next() )
314 //std::cout << " " << aMIt.Value() << std::endl;
315 aShapesFromNewFace.Add( aMIt.Value() );
318 // b. to fill map of parts except parts from new face
320 for( ; anIt.More(); anIt.Next() )
322 QString aSType = anIt.StricklerType();
323 //std::cout << anIt.Face() << " " << anIt.StricklerType() << std::endl;
324 TopTools_ListOfShape aModified = aBuilder.Modified( anIt.Face() );
325 TopTools_ListIteratorOfListOfShape aMIt( aModified );
326 for( ; aMIt.More(); aMIt.Next() )
328 TopoDS_Shape aShape = aMIt.Value();
329 bool isFace = aShape.ShapeType()==TopAbs_FACE;
330 bool isAlsoFromNew = aShapesFromNewFace.Contains( aShape );
331 //std::cout << " " << aShape << " " << isAlsoFromNew << std::endl;
332 if( isFace && !isAlsoFromNew )
333 aNewFaces.Add( TopoDS::Face( aShape ), aSType );
337 // c. add the new shape if it is face with its type
338 if( theNewShape.ShapeType()==TopAbs_FACE )
339 aNewFaces.Add( TopoDS::Face( theNewShape ), theNewType );
341 // convert map of shape to type to compound and list of types
342 StoreLandCovers( aNewFaces );
347 Replace the set of land covers in the land cover map
348 @param theMap the map of shape (face) to Strickler type (string)
350 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfFaceToStricklerType& theMap )
352 TopoDS_Compound aCompound;
353 BRep_Builder aCompoundBuilder;
354 aCompoundBuilder.MakeCompound( aCompound );
356 int n = theMap.Size();
357 Handle( TDataStd_ExtStringArray ) aTypes =
358 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
359 HYDROData_MapOfFaceToStricklerType::Iterator aNFIt( theMap );
360 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
362 aCompoundBuilder.Add( aCompound, aNFIt.Key() );
363 aTypes->SetValue( i, HYDROData_Tool::toExtString( aNFIt.Value() ) );
366 SetShape( aCompound );