Salome HOME
98fadcf4755e40cf719e468bf2fccc7eaf89c0c4
[modules/hydro.git] / src / HYDROData / HYDROData_LandCover.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_LandCover.h"
20
21 #include "HYDROData_PolylineXY.h"
22
23 #include <BRep_Builder.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <BRepBuilderAPI_MakeWire.hxx>
26 #include <TDataStd_AsciiString.hxx>
27 #include <TopoDS.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopoDS_Wire.hxx>
30 #include <TopoDS_Face.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <TopTools_ListOfShape.hxx>
33 #include <TopTools_ListIteratorOfListOfShape.hxx>
34 #include <NCollection_IncAllocator.hxx>
35 #include <BOPAlgo_BOP.hxx>
36 #include <ShapeAnalysis_Wire.hxx>
37 #include <Precision.hxx>
38 #include <TopTools_SequenceOfShape.hxx>
39
40 #include <QColor>
41 #include <QStringList>
42
43 IMPLEMENT_STANDARD_HANDLE( HYDROData_LandCover, HYDROData_Entity )
44 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_LandCover, HYDROData_Entity )
45
46 HYDROData_LandCover::HYDROData_LandCover()
47 : HYDROData_Entity( Geom_2d )
48 {
49 }
50
51 HYDROData_LandCover::~HYDROData_LandCover()
52 {
53 }
54
55 const ObjectKind HYDROData_LandCover::GetKind() const
56 {
57   return KIND_LAND_COVER;
58 }
59
60 QStringList HYDROData_LandCover::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
61 {
62   QStringList aResList = dumpObjectCreation( theTreatedObjects );
63   QString aName = GetObjPyName();
64   
65   // Set Strickler type
66   QString aType = GetStricklerType();
67   if ( !aType.isEmpty() ) {
68     aResList << QString( "" );
69     ///< \TODO to be implemented:
70     // aResList << QString( "%1.SetStricklerType( \"%2\" );" ).arg( aName ).arg( aType );
71     aResList << QString( "" );
72   }
73
74   // Set polylines
75   ///< \TODO to be implemented: 
76   
77   aResList << QString( "" );
78   aResList << QString( "%1.Update();" ).arg( aName );
79   aResList << QString( "" );
80
81   return aResList;
82 }
83
84 HYDROData_SequenceOfObjects HYDROData_LandCover::GetAllReferenceObjects() const
85 {
86   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
87
88   HYDROData_SequenceOfObjects aSeqOfPolylines = GetPolylines();
89   aResSeq.Append( aSeqOfPolylines );
90
91   return aResSeq;
92 }
93
94 bool HYDROData_LandCover::IsHas2dPrs() const
95 {
96   return true;
97 }
98
99 void HYDROData_LandCover::SetStricklerType( const QString& theType )
100 {
101   TCollection_AsciiString anAsciiStr( theType.toStdString().c_str() );
102   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_StricklerType ), anAsciiStr );
103 }
104
105 QString HYDROData_LandCover::GetStricklerType() const
106 {
107   QString aType;
108
109   TDF_Label aLabel = myLab.FindChild( DataTag_StricklerType, false );
110   if ( !aLabel.IsNull() ) {
111     Handle(TDataStd_AsciiString) anAsciiStr;
112     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) ) {
113       aType = QString( anAsciiStr->Get().ToCString() );
114     }
115   }
116
117   return aType;
118 }
119
120 void HYDROData_LandCover::Update()
121 {
122   HYDROData_Entity::Update();
123   
124   removeShape();
125
126   TCollection_AsciiString anErrorMsg;
127   TopoDS_Shape aResShape = buildShape( GetPolylines(), anErrorMsg );
128   
129   SetShape( aResShape );
130 }
131
132 void HYDROData_LandCover::SetPolylines( const HYDROData_SequenceOfObjects& thePolylines )
133 {
134   SetReferenceObjects( thePolylines, DataTag_Polylines );
135   Changed( Geom_2d );
136 }
137
138 HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
139 {
140   return GetReferenceObjects( DataTag_Polylines );
141 }
142
143 TopoDS_Shape HYDROData_LandCover::GetShape() const
144 {
145   return HYDROData_Entity::GetShape( DataTag_Shape );
146 }
147
148 void HYDROData_LandCover::SetFillingColor( const QColor& theColor )
149 {
150   SetColor( theColor, DataTag_FillingColor );
151 }
152
153 QColor HYDROData_LandCover::GetFillingColor() const
154 {
155   return GetColor( DefaultFillingColor(), DataTag_FillingColor );
156 }
157
158 void HYDROData_LandCover::SetBorderColor( const QColor& theColor )
159 {
160   SetColor( theColor, DataTag_BorderColor );
161 }
162
163 QColor HYDROData_LandCover::GetBorderColor() const
164 {
165   return GetColor( DefaultBorderColor(), DataTag_BorderColor );
166 }
167
168 QColor HYDROData_LandCover::DefaultFillingColor() const
169 {
170   return QColor( Qt::magenta );
171 }
172
173 QColor HYDROData_LandCover::DefaultBorderColor() const
174 {
175   return QColor( Qt::transparent );
176 }
177
178 void HYDROData_LandCover::SetShape( const TopoDS_Shape& theShape )
179 {
180   HYDROData_Entity::SetShape( DataTag_Shape, theShape );
181 }
182
183 void HYDROData_LandCover::removeShape()
184 {
185   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
186   if ( !aLabel.IsNull() ) {
187     aLabel.ForgetAllAttributes();
188   }
189 }
190
191 TopoDS_Shape HYDROData_LandCover::buildShape( const HYDROData_SequenceOfObjects& thePolylines,
192                                               TCollection_AsciiString& theErrorMsg )
193 {
194   theErrorMsg.Clear();
195   TopoDS_Shape aResShape;
196
197   BRepBuilderAPI_MakeWire aMakeWire;
198   
199   TopTools_ListOfShape aClosedWires;
200
201   int aNbPolylines = thePolylines.Length();
202   for ( int i = 1; i <= aNbPolylines; ++i ) {
203     Handle(HYDROData_PolylineXY) aPolyline = 
204       Handle(HYDROData_PolylineXY)::DownCast( thePolylines.Value( i ) );
205     
206     if ( aPolyline.IsNull() ) {
207       continue;
208     }
209
210     TopoDS_Shape aPolyShape = aPolyline->GetShape();
211     if ( aPolyShape.IsNull() ) {
212       continue;
213     }
214
215     // Extract polyline wire(s)
216     TopTools_ListOfShape aPolylineWires;
217       
218     if ( aPolyShape.ShapeType() == TopAbs_WIRE ) {
219       const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
220       if ( !aPolylineWire.IsNull() ) {
221         aPolylineWires.Append( aPolylineWire );
222       }
223     } else if ( aPolyShape.ShapeType() == TopAbs_COMPOUND ) {
224       TopExp_Explorer anExp( aPolyShape, TopAbs_WIRE );
225       for (; anExp.More(); anExp.Next() ) {
226         if(!anExp.Current().IsNull()) {
227           const TopoDS_Wire& aWire = TopoDS::Wire( anExp.Current() );
228           aPolylineWires.Append( aWire );
229         }
230       }
231     }
232     
233     TopTools_ListIteratorOfListOfShape anIt( aPolylineWires );
234     for ( ; anIt.More(); anIt.Next() ) {
235       TopoDS_Wire& aWire = TopoDS::Wire( anIt.Value() );
236       
237       if ( aWire.Closed() ) {
238         aClosedWires.Append( aWire );
239       } else {
240         aMakeWire.Add( aWire );
241         aMakeWire.Build();
242         if ( aMakeWire.IsDone() ) {
243           if ( aMakeWire.Wire().Closed() ) {
244             aClosedWires.Append( aMakeWire.Wire() );
245             aMakeWire = BRepBuilderAPI_MakeWire();
246           }
247         }
248       }
249     }
250   }
251
252   if ( aClosedWires.Extent() == 1 ) {
253     // make face
254     TopoDS_Wire aW = TopoDS::Wire( aClosedWires.First());
255     BRepBuilderAPI_MakeFace aMakeFace( aW );
256     aMakeFace.Build();
257     if( aMakeFace.IsDone() ) 
258     {
259       Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
260       if (!aSAW->CheckSelfIntersection())
261         aResShape = aMakeFace.Face();
262       else
263         theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire have been detected";
264     }
265   } else if ( aClosedWires.Extent() > 1 ) {
266     // make compound
267     BRep_Builder aBuilder;
268     TopoDS_Compound aCompound;
269     aBuilder.MakeCompound( aCompound );
270     TopTools_SequenceOfShape aSeq;
271     TopTools_ListIteratorOfListOfShape aWiresIter( aClosedWires );
272     bool anErrStat = false;
273     for ( ; aWiresIter.More() && !anErrStat; aWiresIter.Next() )
274     {
275       TopoDS_Wire aW = TopoDS::Wire( aWiresIter.Value() );
276       BRepBuilderAPI_MakeFace aMakeFace( aW );
277       aMakeFace.Build();
278       if( aMakeFace.IsDone() ) 
279       {
280         Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
281         if (!aSAW->CheckSelfIntersection())
282           aSeq.Append( aMakeFace.Face() );
283         else
284         {
285           anErrStat = true;
286           theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire(s) have been detected";
287         }
288       }
289     }
290     if (!anErrStat)
291     {
292       for (int i = 1; i <= aSeq.Length(); i++)
293         aBuilder.Add( aCompound, aSeq(i) );
294       aResShape = aCompound;
295     }
296     else
297       aResShape = TopoDS_Shape();
298   } else if ( aNbPolylines > 0 ) {
299     TCollection_AsciiString aSourceName = aNbPolylines > 1 ? "polylines" : "polyline";
300     theErrorMsg = "Can't build closed contour on the given ";
301     theErrorMsg += aSourceName;
302   }
303
304   ///< \TODO to be reimplemented
305   /*
306   TopoDS_Shape anArgShape;
307   TopTools_ListOfShape aToolShapes;
308   
309   HYDROData_SequenceOfObjects aRefPolylines = GetPolylines();
310   for ( int i = 1, n = aRefPolylines.Length(); i <= n; ++i ) {
311     Handle(HYDROData_PolylineXY) aPolyline = 
312       Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
313     
314     if ( aPolyline.IsNull() ) {
315       continue;
316     }
317
318     if ( !aPolyline->IsClosed() ) {
319       continue;
320     }
321
322     TopoDS_Shape aPolyShape = aPolyline->GetShape();
323     if ( aPolyShape.IsNull() || aPolyShape.ShapeType() != TopAbs_WIRE ) {
324       continue;
325     }
326
327     const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
328     if ( aPolylineWire.IsNull() ) {
329       continue;
330     }
331
332     TopoDS_Face aResultFace = TopoDS_Face();
333     BRepBuilderAPI_MakeFace aMakeFace( aPolylineWire, Standard_True );
334     aMakeFace.Build();
335     if( aMakeFace.IsDone() ) {
336       aResultFace = aMakeFace.Face();
337     }
338
339     if( aResultFace.IsNull() ) {
340       continue;
341     }
342
343     if ( anArgShape.IsNull() ) {
344       anArgShape = aResultFace;
345     } else {
346       aToolShapes.Append( aResultFace );
347     }
348   }
349
350   aResShape = anArgShape;
351
352   if ( !anArgShape.IsNull() && aToolShapes.Extent() > 0 ) {
353     Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
354     BOPAlgo_BOP aBOP(aAL);
355  
356     aBOP.AddArgument( anArgShape );
357
358     TopTools_ListIteratorOfListOfShape anIt(aToolShapes);
359     for( ; anIt.More(); anIt.Next() ) {
360       aBOP.AddTool( anIt.Value() );
361     }
362
363     aBOP.SetOperation( BOPAlgo_CUT );
364     aBOP.Perform();
365
366     if ( !aBOP.Shape().IsNull() ) {
367       aResShape = aBOP.Shape();
368     }
369   }
370   */
371   
372   return aResShape;
373 }