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>
37 IMPLEMENT_STANDARD_HANDLE(HYDROData_LandCoverMap, HYDROData_Entity)
38 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_LandCoverMap, HYDROData_Entity)
40 class HYDROData_MapOfShapeToStricklerType : public NCollection_IndexedDataMap<TopoDS_Shape, QString>
46 @param theMap the land cover map to iterate through
48 HYDROData_LandCoverMap::Iterator::Iterator( const HYDROData_LandCoverMap& theMap )
54 Initialize the iterator
55 @param theMap the land cover map to iterate through
57 void HYDROData_LandCoverMap::Iterator::Init( const HYDROData_LandCoverMap& theMap )
59 myIterator = new TopoDS_Iterator( theMap.GetShape() );
61 theMap.myLab.FindChild( DataTag_Types ).FindAttribute( TDataStd_ExtStringArray::GetID(), myArray );
67 HYDROData_LandCoverMap::Iterator::~Iterator()
73 Return if the iterator has more elements
74 @return if the iterator has more elements
76 bool HYDROData_LandCoverMap::Iterator::More() const
78 return !myArray.IsNull() && myIterator->More();
82 Move iterator to the next element
84 void HYDROData_LandCoverMap::Iterator::Next()
90 Get the current land cover (face)
91 @return the land cover's face
93 TopoDS_Face HYDROData_LandCoverMap::Iterator::Face() const
95 return TopoDS::Face( myIterator->Value() );
99 Get the current land cover's Strickler type
100 @return the land cover's Strickler type
102 QString HYDROData_LandCoverMap::Iterator::StricklerType() const
104 if( myArray.IsNull() || myIndex < myArray->Lower() || myIndex > myArray->Upper() )
107 return HYDROData_Tool::toQString( myArray->Value( myIndex ) );
113 HYDROData_LandCoverMap::HYDROData_LandCoverMap()
120 HYDROData_LandCoverMap::~HYDROData_LandCoverMap()
126 @return object's kind
128 const ObjectKind HYDROData_LandCoverMap::GetKind() const
130 return KIND_LAND_COVER_MAP;
134 Import the land cover map from QGIS
135 @param theFileName the name of file
136 @return if the import is successful
138 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
145 Export the land cover map to QGIS
146 @param theFileName the name of file
147 @return if the export is successful
149 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
156 Export the land cover map for the solver (Telemac)
157 @param theFileName the name of file
158 @return if the export is successful
160 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
167 Add a new object as land cover
168 @param theObject the object to add as land cover
169 @param theType the Strickler type for the new land cover
170 @return if the addition is successful
172 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
179 Add a new polyline as land cover
180 @param thePolyline the polyline to add as land cover
181 @param theType the Strickler type for the new land cover
182 @return if the addition is successful
184 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Polyline )& thePolyline, const QString& theType )
191 Remove the given face from land cover map
192 @param theFace the face to be removed
193 @return if the removing is successful
195 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
202 Split the land cover map by the given polyline
203 @param thePolyline the tool polyline to split the land cover map
204 @return if the removing is successful
206 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_Polyline )& thePolyline )
213 Merge the given faces in the land cover
214 @param theFaces the faces to merge in the land cover map
215 @param theType the Strickler type for the merged land cover
216 @return if the merge is successful
218 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
225 Get the shape of the land cover map
227 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
229 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
230 if ( !aLabel.IsNull() )
232 Handle(TNaming_NamedShape) aNamedShape;
233 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
234 return aNamedShape->Get();
236 return TopoDS_Shape();
240 Set the shape of the land cover map
241 @param theShape the new shape for the land cover map
243 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
245 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
246 aBuilder.Generated( theShape );
250 Perform the local partition algorithm on the land cover
251 @param theNewShape the new shape to add into the land cover
252 @param theNewType the new Strickler type for the new land cover
253 @return if the local partition is successful
255 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
257 BOPCol_ListOfShape aShapesList;
258 BOPAlgo_PaveFiller aPaveFiller;
260 // add faces to shapes list
261 Iterator anIt( *this );
262 for( ; anIt.More(); anIt.Next() )
263 aShapesList.Append( anIt.Face() );
264 aShapesList.Append( theNewShape );
266 // prepare pave filler
267 aPaveFiller.SetArguments( aShapesList );
268 aPaveFiller.Perform();
269 Standard_Integer anError = aPaveFiller.ErrorStatus();
273 // add faces to builder
274 BOPAlgo_Builder aBuilder;
276 for( ; anIt.More(); anIt.Next() )
277 aBuilder.AddArgument( anIt.Face() );
278 aBuilder.AddArgument( theNewShape );
280 // perform the partition with the pave filler
281 aBuilder.PerformWithFiller( aPaveFiller );
282 anError = aBuilder.ErrorStatus();
286 // analysis of the history
287 BOPCol_DataMapOfShapeListOfShape aSplits = aBuilder.Splits();
289 // a. fill map of shape => type for initial faces
290 HYDROData_MapOfShapeToStricklerType anOldFaces;
292 for( ; anIt.More(); anIt.Next() )
293 anOldFaces.Add( anIt.Face(), anIt.StricklerType() );
295 // b. fill map of shape => type for split faces without the new face
296 HYDROData_MapOfShapeToStricklerType aNewFaces;
297 BOPCol_DataMapOfShapeListOfShape::Iterator aSplitIt( aSplits );
298 for( ; aSplitIt.More(); aSplitIt.Next() )
300 TopoDS_Shape anInitial = aSplitIt.Key();
301 if( anInitial==theNewShape )
303 QString aType = anOldFaces.FindFromKey( anInitial );
304 BOPCol_ListOfShape aSplitFaces = aSplitIt.Value();
305 BOPCol_ListOfShape::Iterator aSFIt( aSplitFaces );
306 for( ; aSFIt.More(); aSFIt.Next() )
307 aNewFaces.Add( aSFIt.Value(), aType );
310 // c. add the new shape if it is face with its type
311 if( theNewShape.ShapeType()==TopAbs_FACE )
312 aNewFaces.Add( theNewShape, theNewType );
314 // convert map of shape to type to compound and list of types
315 StoreLandCovers( aNewFaces );
320 Replace the set of land covers in the land cover map
321 @param theMap the map of shape (face) to Strickler type (string)
323 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfShapeToStricklerType& theMap )
325 TopoDS_Compound aCompound;
326 BRep_Builder aCompoundBuilder;
327 aCompoundBuilder.MakeCompound( aCompound );
329 int n = theMap.Size();
330 Handle( TDataStd_ExtStringArray ) aTypes =
331 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
332 HYDROData_MapOfShapeToStricklerType::Iterator aNFIt( theMap );
333 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
335 aCompoundBuilder.Add( aCompound, aNFIt.Key() );
336 aTypes->SetValue( i, HYDROData_Tool::toExtString( aNFIt.Value() ) );
339 SetShape( aCompound );