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