Salome HOME
refs #567, 569: build face, coloring.
[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 <BRepBuilderAPI_MakeFace.hxx>
24 #include <TDataStd_AsciiString.hxx>
25 #include <TNaming_Builder.hxx>
26 #include <TNaming_NamedShape.hxx>
27 #include <TopoDS.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopoDS_Wire.hxx>
30 #include <TopoDS_Face.hxx>
31 #include <TopTools_ListOfShape.hxx>
32 #include <TopTools_ListIteratorOfListOfShape.hxx>
33 #include <NCollection_IncAllocator.hxx>
34 #include <BOPAlgo_BOP.hxx>
35
36 #include <QColor>
37 #include <QStringList>
38
39 IMPLEMENT_STANDARD_HANDLE( HYDROData_LandCover, HYDROData_Entity )
40 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_LandCover, HYDROData_Entity )
41
42 HYDROData_LandCover::HYDROData_LandCover()
43 {
44 }
45
46 HYDROData_LandCover::~HYDROData_LandCover()
47 {
48 }
49
50 const ObjectKind HYDROData_LandCover::GetKind() const
51 {
52   return KIND_LAND_COVER;
53 }
54
55 QStringList HYDROData_LandCover::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
56 {
57   QStringList aResList = dumpObjectCreation( theTreatedObjects );
58   QString aName = GetObjPyName();
59   
60   // Set Strickler type
61   QString aType = GetStricklerType();
62   if ( !aType.isEmpty() ) {
63     aResList << QString( "" );
64     ///< \TODO to be implemented:
65     // aResList << QString( "%1.SetStricklerType( \"%2\" );" ).arg( aName ).arg( aType );
66     aResList << QString( "" );
67   }
68
69   // Set polylines
70   ///< \TODO to be implemented: 
71   
72   aResList << QString( "" );
73   aResList << QString( "%1.Update();" ).arg( aName );
74   aResList << QString( "" );
75
76   return aResList;
77 }
78
79 HYDROData_SequenceOfObjects HYDROData_LandCover::GetAllReferenceObjects() const
80 {
81   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
82
83   HYDROData_SequenceOfObjects aSeqOfPolylines = GetPolylines();
84   aResSeq.Append( aSeqOfPolylines );
85
86   return aResSeq;
87 }
88
89 bool HYDROData_LandCover::IsHas2dPrs() const
90 {
91   return true;
92 }
93
94 void HYDROData_LandCover::SetStricklerType( const QString& theType )
95 {
96   TCollection_AsciiString anAsciiStr( theType.toStdString().c_str() );
97   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_StricklerType ), anAsciiStr );
98 }
99
100 QString HYDROData_LandCover::GetStricklerType() const
101 {
102   QString aType;
103
104   TDF_Label aLabel = myLab.FindChild( DataTag_StricklerType, false );
105   if ( !aLabel.IsNull() ) {
106     Handle(TDataStd_AsciiString) anAsciiStr;
107     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) ) {
108       aType = QString( anAsciiStr->Get().ToCString() );
109     }
110   }
111
112   return aType;
113 }
114
115 void HYDROData_LandCover::Update()
116 {
117   HYDROData_Entity::Update();
118   
119   removeShape();
120
121   TopoDS_Shape aResShape = buildShape();
122   
123   setShape( aResShape );
124 }
125
126 void HYDROData_LandCover::SetPolylines( const HYDROData_SequenceOfObjects& thePolylines )
127 {
128   SetReferenceObjects( thePolylines, DataTag_Polylines );
129   SetToUpdate( true );
130 }
131
132 HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
133 {
134   return GetReferenceObjects( DataTag_Polylines );
135 }
136
137 TopoDS_Shape HYDROData_LandCover::GetShape() const
138 {
139   TopoDS_Shape aShape;
140
141   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
142   if ( !aLabel.IsNull() )
143   {
144     Handle(TNaming_NamedShape) aNamedShape;
145     if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) ) {
146       aShape = aNamedShape->Get();
147     }
148   }
149
150   return aShape;
151 }
152
153 void HYDROData_LandCover::SetFillingColor( const QColor& theColor )
154 {
155   SetColor( theColor, DataTag_FillingColor );
156 }
157
158 QColor HYDROData_LandCover::GetFillingColor() const
159 {
160   return GetColor( DefaultFillingColor(), DataTag_FillingColor );
161 }
162
163 void HYDROData_LandCover::SetBorderColor( const QColor& theColor )
164 {
165   SetColor( theColor, DataTag_BorderColor );
166 }
167
168 QColor HYDROData_LandCover::GetBorderColor() const
169 {
170   return GetColor( DefaultBorderColor(), DataTag_BorderColor );
171 }
172
173 QColor HYDROData_LandCover::DefaultFillingColor()
174 {
175   return QColor( Qt::magenta );
176 }
177
178 QColor HYDROData_LandCover::DefaultBorderColor()
179 {
180   return QColor( Qt::transparent );
181 }
182
183 void HYDROData_LandCover::setShape( const TopoDS_Shape& theShape )
184 {
185   TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
186   aBuilder.Generated( theShape );
187 }
188
189 void HYDROData_LandCover::removeShape()
190 {
191   TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
192   if ( !aLabel.IsNull() ) {
193     aLabel.ForgetAllAttributes();
194   }
195 }
196  
197 TopoDS_Shape HYDROData_LandCover::buildShape() const
198 {
199   TopoDS_Shape aResShape;
200
201   ///< \TODO to be reimplemented
202   TopoDS_Shape anArgShape;
203   TopTools_ListOfShape aToolShapes;
204   
205   HYDROData_SequenceOfObjects aRefPolylines = GetPolylines();
206   for ( int i = 1, n = aRefPolylines.Length(); i <= n; ++i ) {
207     Handle(HYDROData_PolylineXY) aPolyline = 
208       Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
209     
210     if ( aPolyline.IsNull() ) {
211       continue;
212     }
213
214     if ( !aPolyline->IsClosed() ) {
215       continue;
216     }
217
218     TopoDS_Shape aPolyShape = aPolyline->GetShape();
219     if ( aPolyShape.IsNull() || aPolyShape.ShapeType() != TopAbs_WIRE ) {
220       continue;
221     }
222
223     const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
224     if ( aPolylineWire.IsNull() ) {
225       continue;
226     }
227
228     TopoDS_Face aResultFace = TopoDS_Face();
229     BRepBuilderAPI_MakeFace aMakeFace( aPolylineWire, Standard_True );
230     aMakeFace.Build();
231     if( aMakeFace.IsDone() ) {
232       aResultFace = aMakeFace.Face();
233     }
234
235     if( aResultFace.IsNull() ) {
236       continue;
237     }
238
239     if ( anArgShape.IsNull() ) {
240       anArgShape = aResultFace;
241     } else {
242       aToolShapes.Append( aResultFace );
243     }
244   }
245
246   aResShape = anArgShape;
247
248   if ( !anArgShape.IsNull() && aToolShapes.Extent() > 0 ) {
249     Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
250     BOPAlgo_BOP aBOP(aAL);
251  
252     aBOP.AddArgument( anArgShape );
253
254     TopTools_ListIteratorOfListOfShape anIt(aToolShapes);
255     for( ; anIt.More(); anIt.Next() ) {
256       aBOP.AddTool( anIt.Value() );
257     }
258
259     aBOP.SetOperation( BOPAlgo_CUT );
260     aBOP.Perform();
261
262     if ( !aBOP.Shape().IsNull() ) {
263       aResShape = aBOP.Shape();
264     }
265   }
266   
267   return aResShape;
268 }