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()
125 Import the land cover map from QGIS
126 @param theFileName the name of file
127 @return if the import is successful
129 bool HYDROData_LandCoverMap::ImportQGIS( const QString& theFileName )
136 Export the land cover map to QGIS
137 @param theFileName the name of file
138 @return if the export is successful
140 bool HYDROData_LandCoverMap::ExportQGIS( const QString& theFileName ) const
147 Export the land cover map for the solver (Telemac)
148 @param theFileName the name of file
149 @return if the export is successful
151 bool HYDROData_LandCoverMap::ExportTelemac( const QString& theFileName ) const
158 Add a new object as land cover
159 @param theObject the object to add as land cover
160 @param theType the Strickler type for the new land cover
161 @return if the addition is successful
163 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Object )& theObject, const QString& theType )
170 Add a new polyline as land cover
171 @param thePolyline the polyline to add as land cover
172 @param theType the Strickler type for the new land cover
173 @return if the addition is successful
175 bool HYDROData_LandCoverMap::Add( const Handle( HYDROData_Polyline )& thePolyline, const QString& theType )
182 Remove the given face from land cover map
183 @param theFace the face to be removed
184 @return if the removing is successful
186 bool HYDROData_LandCoverMap::Remove( const TopoDS_Face& theFace )
193 Split the land cover map by the given polyline
194 @param thePolyline the tool polyline to split the land cover map
195 @return if the removing is successful
197 bool HYDROData_LandCoverMap::Split( const Handle( HYDROData_Polyline )& thePolyline )
204 Merge the given faces in the land cover
205 @param theFaces the faces to merge in the land cover map
206 @param theType the Strickler type for the merged land cover
207 @return if the merge is successful
209 bool HYDROData_LandCoverMap::Merge( const TopTools_ListOfShape& theFaces, const QString& theType )
216 Get the shape of the land cover map
218 TopoDS_Shape HYDROData_LandCoverMap::GetShape() const
220 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
221 if ( !aLabel.IsNull() )
223 Handle(TNaming_NamedShape) aNamedShape;
224 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
225 return aNamedShape->Get();
227 return TopoDS_Shape();
231 Set the shape of the land cover map
232 @param theShape the new shape for the land cover map
234 void HYDROData_LandCoverMap::SetShape( const TopoDS_Shape& theShape )
236 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
237 aBuilder.Generated( theShape );
241 Perform the local partition algorithm on the land cover
242 @param theNewShape the new shape to add into the land cover
243 @param theNewType the new Strickler type for the new land cover
244 @return if the local partition is successful
246 bool HYDROData_LandCoverMap::LocalPartition( const TopoDS_Shape& theNewShape, const QString& theNewType )
248 BOPCol_ListOfShape aShapesList;
249 BOPAlgo_PaveFiller aPaveFiller;
251 // add faces to shapes list
252 Iterator anIt( *this );
253 for( ; anIt.More(); anIt.Next() )
254 aShapesList.Append( anIt.Face() );
255 aShapesList.Append( theNewShape );
257 // prepare pave filler
258 aPaveFiller.SetArguments( aShapesList );
259 aPaveFiller.Perform();
260 Standard_Integer anError = aPaveFiller.ErrorStatus();
264 // add faces to builder
265 BOPAlgo_Builder aBuilder;
267 for( ; anIt.More(); anIt.Next() )
268 aBuilder.AddArgument( anIt.Face() );
269 aBuilder.AddArgument( theNewShape );
271 // perform the partition with the pave filler
272 aBuilder.PerformWithFiller( aPaveFiller );
273 anError = aBuilder.ErrorStatus();
277 // analysis of the history
278 BOPCol_DataMapOfShapeListOfShape aSplits = aBuilder.Splits();
280 // a. fill map of shape => type for initial faces
281 HYDROData_MapOfShapeToStricklerType anOldFaces;
283 for( ; anIt.More(); anIt.Next() )
284 anOldFaces.Add( anIt.Face(), anIt.StricklerType() );
286 // b. fill map of shape => type for split faces without the new face
287 HYDROData_MapOfShapeToStricklerType aNewFaces;
288 BOPCol_DataMapOfShapeListOfShape::Iterator aSplitIt( aSplits );
289 for( ; aSplitIt.More(); aSplitIt.Next() )
291 TopoDS_Shape anInitial = aSplitIt.Key();
292 if( anInitial==theNewShape )
294 QString aType = anOldFaces.FindFromKey( anInitial );
295 BOPCol_ListOfShape aSplitFaces = aSplitIt.Value();
296 BOPCol_ListOfShape::Iterator aSFIt( aSplitFaces );
297 for( ; aSFIt.More(); aSFIt.Next() )
298 aNewFaces.Add( aSFIt.Value(), aType );
301 // c. add the new shape if it is face with its type
302 if( theNewShape.ShapeType()==TopAbs_FACE )
303 aNewFaces.Add( theNewShape, theNewType );
305 // convert map of shape to type to compound and list of types
306 StoreLandCovers( aNewFaces );
311 Replace the set of land covers in the land cover map
312 @param theMap the map of shape (face) to Strickler type (string)
314 void HYDROData_LandCoverMap::StoreLandCovers( const HYDROData_MapOfShapeToStricklerType& theMap )
316 TopoDS_Compound aCompound;
317 BRep_Builder aCompoundBuilder;
318 aCompoundBuilder.MakeCompound( aCompound );
320 int n = theMap.Size();
321 Handle( TDataStd_ExtStringArray ) aTypes =
322 TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_Types ), 0, n-1, Standard_True );
323 HYDROData_MapOfShapeToStricklerType::Iterator aNFIt( theMap );
324 for( int i=0; aNFIt.More(); aNFIt.Next(), i++ )
326 aCompoundBuilder.Add( aCompound, aNFIt.Key() );
327 aTypes->SetValue( i, HYDROData_Tool::toExtString( aNFIt.Value() ) );
330 SetShape( aCompound );