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>
27 #include <TNaming_Builder.hxx>
28 #include <TNaming_NamedShape.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>
43 #include <QStringList>
45 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_LandCover, HYDROData_Entity )
47 HYDROData_LandCover::HYDROData_LandCover()
48 : HYDROData_Entity( Geom_2d )
52 HYDROData_LandCover::~HYDROData_LandCover()
56 const ObjectKind HYDROData_LandCover::GetKind() const
58 return KIND_LAND_COVER;
61 QStringList HYDROData_LandCover::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
63 QStringList aResList = dumpObjectCreation( theTreatedObjects );
64 QString aName = GetObjPyName();
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( "" );
76 ///< \TODO to be implemented:
78 aResList << QString( "" );
79 aResList << QString( "%1.Update();" ).arg( aName );
80 aResList << QString( "" );
85 HYDROData_SequenceOfObjects HYDROData_LandCover::GetAllReferenceObjects() const
87 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
89 HYDROData_SequenceOfObjects aSeqOfPolylines = GetPolylines();
90 aResSeq.Append( aSeqOfPolylines );
95 bool HYDROData_LandCover::IsHas2dPrs() const
100 void HYDROData_LandCover::SetStricklerType( const QString& theType )
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());
108 QString HYDROData_LandCover::GetStricklerType() const
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() );
123 void HYDROData_LandCover::Update()
125 HYDROData_Entity::Update();
129 TCollection_AsciiString anErrorMsg;
130 TopoDS_Shape aResShape = buildShape( GetPolylines(), anErrorMsg );
132 setShape( aResShape );
135 void HYDROData_LandCover::SetPolylines( const HYDROData_SequenceOfObjects& thePolylines )
137 SetReferenceObjects( thePolylines, DataTag_Polylines );
141 HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
143 return GetReferenceObjects( DataTag_Polylines );
146 TopoDS_Shape HYDROData_LandCover::GetShape() const
150 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
151 if ( !aLabel.IsNull() )
153 Handle(TNaming_NamedShape) aNamedShape;
154 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) ) {
155 aShape = aNamedShape->Get();
162 void HYDROData_LandCover::SetFillingColor( const QColor& theColor )
164 SetColor( theColor, DataTag_FillingColor );
167 QColor HYDROData_LandCover::GetFillingColor() const
169 return GetColor( DefaultFillingColor(), DataTag_FillingColor );
172 void HYDROData_LandCover::SetBorderColor( const QColor& theColor )
174 SetColor( theColor, DataTag_BorderColor );
177 QColor HYDROData_LandCover::GetBorderColor() const
179 return GetColor( DefaultBorderColor(), DataTag_BorderColor );
182 QColor HYDROData_LandCover::DefaultFillingColor()
184 return QColor( Qt::magenta );
187 QColor HYDROData_LandCover::DefaultBorderColor()
189 return QColor( Qt::transparent );
192 void HYDROData_LandCover::setShape( const TopoDS_Shape& theShape )
194 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
195 aBuilder.Generated( theShape );
198 void HYDROData_LandCover::removeShape()
200 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
201 if ( !aLabel.IsNull() ) {
202 aLabel.ForgetAllAttributes();
206 TopoDS_Shape HYDROData_LandCover::buildShape( const HYDROData_SequenceOfObjects& thePolylines,
207 TCollection_AsciiString& theErrorMsg )
210 TopoDS_Shape aResShape;
212 BRepBuilderAPI_MakeWire aMakeWire;
214 TopTools_ListOfShape aClosedWires;
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 ) );
221 if ( aPolyline.IsNull() ) {
225 TopoDS_Shape aPolyShape = aPolyline->GetShape();
226 if ( aPolyShape.IsNull() ) {
230 // Extract polyline wire(s)
231 TopTools_ListOfShape aPolylineWires;
233 if ( aPolyShape.ShapeType() == TopAbs_WIRE ) {
234 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
235 if ( !aPolylineWire.IsNull() ) {
236 aPolylineWires.Append( aPolylineWire );
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 );
248 TopTools_ListIteratorOfListOfShape anIt( aPolylineWires );
249 for ( ; anIt.More(); anIt.Next() ) {
250 TopoDS_Wire& aWire = TopoDS::Wire( anIt.Value() );
252 if ( aWire.Closed() ) {
253 aClosedWires.Append( aWire );
255 aMakeWire.Add( aWire );
257 if ( aMakeWire.IsDone() ) {
258 if ( aMakeWire.Wire().Closed() ) {
259 aClosedWires.Append( aMakeWire.Wire() );
260 aMakeWire = BRepBuilderAPI_MakeWire();
267 if ( aClosedWires.Extent() == 1 ) {
269 TopoDS_Wire aW = TopoDS::Wire( aClosedWires.First());
270 BRepBuilderAPI_MakeFace aMakeFace( aW );
272 if( aMakeFace.IsDone() )
274 Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
275 if (!aSAW->CheckSelfIntersection())
276 aResShape = aMakeFace.Face();
278 theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire have been detected";
280 } else if ( aClosedWires.Extent() > 1 ) {
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() )
290 TopoDS_Wire aW = TopoDS::Wire( aWiresIter.Value() );
291 BRepBuilderAPI_MakeFace aMakeFace( aW );
293 if( aMakeFace.IsDone() )
295 Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
296 if (!aSAW->CheckSelfIntersection())
297 aSeq.Append( aMakeFace.Face() );
301 theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire(s) have been detected";
307 for (int i = 1; i <= aSeq.Length(); i++)
308 aBuilder.Add( aCompound, aSeq(i) );
309 aResShape = aCompound;
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;
319 ///< \TODO to be reimplemented
321 TopoDS_Shape anArgShape;
322 TopTools_ListOfShape aToolShapes;
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 ) );
329 if ( aPolyline.IsNull() ) {
333 if ( !aPolyline->IsClosed() ) {
337 TopoDS_Shape aPolyShape = aPolyline->GetShape();
338 if ( aPolyShape.IsNull() || aPolyShape.ShapeType() != TopAbs_WIRE ) {
342 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
343 if ( aPolylineWire.IsNull() ) {
347 TopoDS_Face aResultFace = TopoDS_Face();
348 BRepBuilderAPI_MakeFace aMakeFace( aPolylineWire, Standard_True );
350 if( aMakeFace.IsDone() ) {
351 aResultFace = aMakeFace.Face();
354 if( aResultFace.IsNull() ) {
358 if ( anArgShape.IsNull() ) {
359 anArgShape = aResultFace;
361 aToolShapes.Append( aResultFace );
365 aResShape = anArgShape;
367 if ( !anArgShape.IsNull() && aToolShapes.Extent() > 0 ) {
368 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
369 BOPAlgo_BOP aBOP(aAL);
371 aBOP.AddArgument( anArgShape );
373 TopTools_ListIteratorOfListOfShape anIt(aToolShapes);
374 for( ; anIt.More(); anIt.Next() ) {
375 aBOP.AddTool( anIt.Value() );
378 aBOP.SetOperation( BOPAlgo_CUT );
381 if ( !aBOP.Shape().IsNull() ) {
382 aResShape = aBOP.Shape();