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