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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_LandCover.h"
21 #include "HYDROData_PolylineXY.h"
23 #include <BRep_Builder.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <BRepBuilderAPI_MakeWire.hxx>
26 #include <TDataStd_AsciiString.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>
41 #include <QStringList>
43 IMPLEMENT_STANDARD_HANDLE( HYDROData_LandCover, HYDROData_Entity )
44 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_LandCover, HYDROData_Entity )
46 HYDROData_LandCover::HYDROData_LandCover()
50 HYDROData_LandCover::~HYDROData_LandCover()
54 const ObjectKind HYDROData_LandCover::GetKind() const
56 return KIND_LAND_COVER;
59 QStringList HYDROData_LandCover::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
61 QStringList aResList = dumpObjectCreation( theTreatedObjects );
62 QString aName = GetObjPyName();
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( "" );
74 ///< \TODO to be implemented:
76 aResList << QString( "" );
77 aResList << QString( "%1.Update();" ).arg( aName );
78 aResList << QString( "" );
83 HYDROData_SequenceOfObjects HYDROData_LandCover::GetAllReferenceObjects() const
85 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
87 HYDROData_SequenceOfObjects aSeqOfPolylines = GetPolylines();
88 aResSeq.Append( aSeqOfPolylines );
93 bool HYDROData_LandCover::IsHas2dPrs() const
98 void HYDROData_LandCover::SetStricklerType( const QString& theType )
100 TCollection_AsciiString anAsciiStr( theType.toStdString().c_str() );
101 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_StricklerType ), anAsciiStr );
104 QString HYDROData_LandCover::GetStricklerType() const
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() );
119 void HYDROData_LandCover::Update()
121 HYDROData_Entity::Update();
125 TCollection_AsciiString anErrorMsg;
126 TopoDS_Shape aResShape = buildShape( GetPolylines(), anErrorMsg );
128 SetShape( aResShape );
131 void HYDROData_LandCover::SetPolylines( const HYDROData_SequenceOfObjects& thePolylines )
133 SetReferenceObjects( thePolylines, DataTag_Polylines );
137 HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
139 return GetReferenceObjects( DataTag_Polylines );
142 TopoDS_Shape HYDROData_LandCover::GetShape() const
144 return HYDROData_Entity::GetShape( DataTag_Shape );
147 void HYDROData_LandCover::SetFillingColor( const QColor& theColor )
149 SetColor( theColor, DataTag_FillingColor );
152 QColor HYDROData_LandCover::GetFillingColor() const
154 return GetColor( DefaultFillingColor(), DataTag_FillingColor );
157 void HYDROData_LandCover::SetBorderColor( const QColor& theColor )
159 SetColor( theColor, DataTag_BorderColor );
162 QColor HYDROData_LandCover::GetBorderColor() const
164 return GetColor( DefaultBorderColor(), DataTag_BorderColor );
167 QColor HYDROData_LandCover::DefaultFillingColor()
169 return QColor( Qt::magenta );
172 QColor HYDROData_LandCover::DefaultBorderColor()
174 return QColor( Qt::transparent );
177 void HYDROData_LandCover::SetShape( const TopoDS_Shape& theShape )
179 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
182 void HYDROData_LandCover::removeShape()
184 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
185 if ( !aLabel.IsNull() ) {
186 aLabel.ForgetAllAttributes();
190 TopoDS_Shape HYDROData_LandCover::buildShape( const HYDROData_SequenceOfObjects& thePolylines,
191 TCollection_AsciiString& theErrorMsg )
194 TopoDS_Shape aResShape;
196 BRepBuilderAPI_MakeWire aMakeWire;
198 TopTools_ListOfShape aClosedWires;
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 ) );
205 if ( aPolyline.IsNull() ) {
209 TopoDS_Shape aPolyShape = aPolyline->GetShape();
210 if ( aPolyShape.IsNull() ) {
214 // Extract polyline wire(s)
215 TopTools_ListOfShape aPolylineWires;
217 if ( aPolyShape.ShapeType() == TopAbs_WIRE ) {
218 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
219 if ( !aPolylineWire.IsNull() ) {
220 aPolylineWires.Append( aPolylineWire );
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 );
232 TopTools_ListIteratorOfListOfShape anIt( aPolylineWires );
233 for ( ; anIt.More(); anIt.Next() ) {
234 TopoDS_Wire& aWire = TopoDS::Wire( anIt.Value() );
236 if ( aWire.Closed() ) {
237 aClosedWires.Append( aWire );
239 aMakeWire.Add( aWire );
241 if ( aMakeWire.IsDone() ) {
242 if ( aMakeWire.Wire().Closed() ) {
243 aClosedWires.Append( aMakeWire.Wire() );
244 aMakeWire = BRepBuilderAPI_MakeWire();
251 if ( aClosedWires.Extent() == 1 ) {
253 TopoDS_Wire aW = TopoDS::Wire( aClosedWires.First());
254 BRepBuilderAPI_MakeFace aMakeFace( aW );
256 if( aMakeFace.IsDone() )
258 Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
259 if (!aSAW->CheckSelfIntersection())
260 aResShape = aMakeFace.Face();
262 theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire have been detected";
264 } else if ( aClosedWires.Extent() > 1 ) {
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() )
274 TopoDS_Wire aW = TopoDS::Wire( aWiresIter.Value() );
275 BRepBuilderAPI_MakeFace aMakeFace( aW );
277 if( aMakeFace.IsDone() )
279 Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
280 if (!aSAW->CheckSelfIntersection())
281 aSeq.Append( aMakeFace.Face() );
285 theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire(s) have been detected";
291 for (int i = 1; i <= aSeq.Length(); i++)
292 aBuilder.Add( aCompound, aSeq(i) );
293 aResShape = aCompound;
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;
303 ///< \TODO to be reimplemented
305 TopoDS_Shape anArgShape;
306 TopTools_ListOfShape aToolShapes;
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 ) );
313 if ( aPolyline.IsNull() ) {
317 if ( !aPolyline->IsClosed() ) {
321 TopoDS_Shape aPolyShape = aPolyline->GetShape();
322 if ( aPolyShape.IsNull() || aPolyShape.ShapeType() != TopAbs_WIRE ) {
326 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
327 if ( aPolylineWire.IsNull() ) {
331 TopoDS_Face aResultFace = TopoDS_Face();
332 BRepBuilderAPI_MakeFace aMakeFace( aPolylineWire, Standard_True );
334 if( aMakeFace.IsDone() ) {
335 aResultFace = aMakeFace.Face();
338 if( aResultFace.IsNull() ) {
342 if ( anArgShape.IsNull() ) {
343 anArgShape = aResultFace;
345 aToolShapes.Append( aResultFace );
349 aResShape = anArgShape;
351 if ( !anArgShape.IsNull() && aToolShapes.Extent() > 0 ) {
352 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
353 BOPAlgo_BOP aBOP(aAL);
355 aBOP.AddArgument( anArgShape );
357 TopTools_ListIteratorOfListOfShape anIt(aToolShapes);
358 for( ; anIt.More(); anIt.Next() ) {
359 aBOP.AddTool( anIt.Value() );
362 aBOP.SetOperation( BOPAlgo_CUT );
365 if ( !aBOP.Shape().IsNull() ) {
366 aResShape = aBOP.Shape();