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