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