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()
47 : HYDROData_Entity( Geom_2d )
51 HYDROData_LandCover::~HYDROData_LandCover()
55 const ObjectKind HYDROData_LandCover::GetKind() const
57 return KIND_LAND_COVER;
60 QStringList HYDROData_LandCover::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
62 QStringList aResList = dumpObjectCreation( theTreatedObjects );
63 QString aName = GetObjPyName();
66 QString aType = GetStricklerType();
67 if ( !aType.isEmpty() ) {
68 aResList << QString( "" );
69 ///< \TODO to be implemented:
70 // aResList << QString( "%1.SetStricklerType( \"%2\" );" ).arg( aName ).arg( aType );
71 aResList << QString( "" );
75 ///< \TODO to be implemented:
77 aResList << QString( "" );
78 aResList << QString( "%1.Update();" ).arg( aName );
79 aResList << QString( "" );
84 HYDROData_SequenceOfObjects HYDROData_LandCover::GetAllReferenceObjects() const
86 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
88 HYDROData_SequenceOfObjects aSeqOfPolylines = GetPolylines();
89 aResSeq.Append( aSeqOfPolylines );
94 bool HYDROData_LandCover::IsHas2dPrs() const
99 void HYDROData_LandCover::SetStricklerType( const QString& theType )
101 TCollection_AsciiString anAsciiStr( theType.toStdString().c_str() );
102 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_StricklerType ), anAsciiStr );
105 QString HYDROData_LandCover::GetStricklerType() const
109 TDF_Label aLabel = myLab.FindChild( DataTag_StricklerType, false );
110 if ( !aLabel.IsNull() ) {
111 Handle(TDataStd_AsciiString) anAsciiStr;
112 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) ) {
113 aType = QString( anAsciiStr->Get().ToCString() );
120 void HYDROData_LandCover::Update()
122 HYDROData_Entity::Update();
126 TCollection_AsciiString anErrorMsg;
127 TopoDS_Shape aResShape = buildShape( GetPolylines(), anErrorMsg );
129 SetShape( aResShape );
132 void HYDROData_LandCover::SetPolylines( const HYDROData_SequenceOfObjects& thePolylines )
134 SetReferenceObjects( thePolylines, DataTag_Polylines );
138 HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
140 return GetReferenceObjects( DataTag_Polylines );
143 TopoDS_Shape HYDROData_LandCover::GetShape() const
145 return HYDROData_Entity::GetShape( DataTag_Shape );
148 void HYDROData_LandCover::SetFillingColor( const QColor& theColor )
150 SetColor( theColor, DataTag_FillingColor );
153 QColor HYDROData_LandCover::GetFillingColor() const
155 return GetColor( DefaultFillingColor(), DataTag_FillingColor );
158 void HYDROData_LandCover::SetBorderColor( const QColor& theColor )
160 SetColor( theColor, DataTag_BorderColor );
163 QColor HYDROData_LandCover::GetBorderColor() const
165 return GetColor( DefaultBorderColor(), DataTag_BorderColor );
168 QColor HYDROData_LandCover::DefaultFillingColor() const
170 return QColor( Qt::magenta );
173 QColor HYDROData_LandCover::DefaultBorderColor() const
175 return QColor( Qt::transparent );
178 void HYDROData_LandCover::SetShape( const TopoDS_Shape& theShape )
180 HYDROData_Entity::SetShape( DataTag_Shape, theShape );
183 void HYDROData_LandCover::removeShape()
185 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
186 if ( !aLabel.IsNull() ) {
187 aLabel.ForgetAllAttributes();
191 TopoDS_Shape HYDROData_LandCover::buildShape( const HYDROData_SequenceOfObjects& thePolylines,
192 TCollection_AsciiString& theErrorMsg )
195 TopoDS_Shape aResShape;
197 BRepBuilderAPI_MakeWire aMakeWire;
199 TopTools_ListOfShape aClosedWires;
201 int aNbPolylines = thePolylines.Length();
202 for ( int i = 1; i <= aNbPolylines; ++i ) {
203 Handle(HYDROData_PolylineXY) aPolyline =
204 Handle(HYDROData_PolylineXY)::DownCast( thePolylines.Value( i ) );
206 if ( aPolyline.IsNull() ) {
210 TopoDS_Shape aPolyShape = aPolyline->GetShape();
211 if ( aPolyShape.IsNull() ) {
215 // Extract polyline wire(s)
216 TopTools_ListOfShape aPolylineWires;
218 if ( aPolyShape.ShapeType() == TopAbs_WIRE ) {
219 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
220 if ( !aPolylineWire.IsNull() ) {
221 aPolylineWires.Append( aPolylineWire );
223 } else if ( aPolyShape.ShapeType() == TopAbs_COMPOUND ) {
224 TopExp_Explorer anExp( aPolyShape, TopAbs_WIRE );
225 for (; anExp.More(); anExp.Next() ) {
226 if(!anExp.Current().IsNull()) {
227 const TopoDS_Wire& aWire = TopoDS::Wire( anExp.Current() );
228 aPolylineWires.Append( aWire );
233 TopTools_ListIteratorOfListOfShape anIt( aPolylineWires );
234 for ( ; anIt.More(); anIt.Next() ) {
235 TopoDS_Wire& aWire = TopoDS::Wire( anIt.Value() );
237 if ( aWire.Closed() ) {
238 aClosedWires.Append( aWire );
240 aMakeWire.Add( aWire );
242 if ( aMakeWire.IsDone() ) {
243 if ( aMakeWire.Wire().Closed() ) {
244 aClosedWires.Append( aMakeWire.Wire() );
245 aMakeWire = BRepBuilderAPI_MakeWire();
252 if ( aClosedWires.Extent() == 1 ) {
254 TopoDS_Wire aW = TopoDS::Wire( aClosedWires.First());
255 BRepBuilderAPI_MakeFace aMakeFace( aW );
257 if( aMakeFace.IsDone() )
259 Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
260 if (!aSAW->CheckSelfIntersection())
261 aResShape = aMakeFace.Face();
263 theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire have been detected";
265 } else if ( aClosedWires.Extent() > 1 ) {
267 BRep_Builder aBuilder;
268 TopoDS_Compound aCompound;
269 aBuilder.MakeCompound( aCompound );
270 TopTools_SequenceOfShape aSeq;
271 TopTools_ListIteratorOfListOfShape aWiresIter( aClosedWires );
272 bool anErrStat = false;
273 for ( ; aWiresIter.More() && !anErrStat; aWiresIter.Next() )
275 TopoDS_Wire aW = TopoDS::Wire( aWiresIter.Value() );
276 BRepBuilderAPI_MakeFace aMakeFace( aW );
278 if( aMakeFace.IsDone() )
280 Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
281 if (!aSAW->CheckSelfIntersection())
282 aSeq.Append( aMakeFace.Face() );
286 theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire(s) have been detected";
292 for (int i = 1; i <= aSeq.Length(); i++)
293 aBuilder.Add( aCompound, aSeq(i) );
294 aResShape = aCompound;
297 aResShape = TopoDS_Shape();
298 } else if ( aNbPolylines > 0 ) {
299 TCollection_AsciiString aSourceName = aNbPolylines > 1 ? "polylines" : "polyline";
300 theErrorMsg = "Can't build closed contour on the given ";
301 theErrorMsg += aSourceName;
304 ///< \TODO to be reimplemented
306 TopoDS_Shape anArgShape;
307 TopTools_ListOfShape aToolShapes;
309 HYDROData_SequenceOfObjects aRefPolylines = GetPolylines();
310 for ( int i = 1, n = aRefPolylines.Length(); i <= n; ++i ) {
311 Handle(HYDROData_PolylineXY) aPolyline =
312 Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
314 if ( aPolyline.IsNull() ) {
318 if ( !aPolyline->IsClosed() ) {
322 TopoDS_Shape aPolyShape = aPolyline->GetShape();
323 if ( aPolyShape.IsNull() || aPolyShape.ShapeType() != TopAbs_WIRE ) {
327 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
328 if ( aPolylineWire.IsNull() ) {
332 TopoDS_Face aResultFace = TopoDS_Face();
333 BRepBuilderAPI_MakeFace aMakeFace( aPolylineWire, Standard_True );
335 if( aMakeFace.IsDone() ) {
336 aResultFace = aMakeFace.Face();
339 if( aResultFace.IsNull() ) {
343 if ( anArgShape.IsNull() ) {
344 anArgShape = aResultFace;
346 aToolShapes.Append( aResultFace );
350 aResShape = anArgShape;
352 if ( !anArgShape.IsNull() && aToolShapes.Extent() > 0 ) {
353 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
354 BOPAlgo_BOP aBOP(aAL);
356 aBOP.AddArgument( anArgShape );
358 TopTools_ListIteratorOfListOfShape anIt(aToolShapes);
359 for( ; anIt.More(); anIt.Next() ) {
360 aBOP.AddTool( anIt.Value() );
363 aBOP.SetOperation( BOPAlgo_CUT );
366 if ( !aBOP.Shape().IsNull() ) {
367 aResShape = aBOP.Shape();