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_HANDLE( HYDROData_LandCover, HYDROData_Entity )
46 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_LandCover, HYDROData_Entity )
48 HYDROData_LandCover::HYDROData_LandCover()
49 : HYDROData_Entity( Geom_2d )
53 HYDROData_LandCover::~HYDROData_LandCover()
57 const ObjectKind HYDROData_LandCover::GetKind() const
59 return KIND_LAND_COVER;
62 QStringList HYDROData_LandCover::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
64 QStringList aResList = dumpObjectCreation( theTreatedObjects );
65 QString aName = GetObjPyName();
68 QString aType = GetStricklerType();
69 if ( !aType.isEmpty() ) {
70 aResList << QString( "" );
71 ///< \TODO to be implemented:
72 // aResList << QString( "%1.SetStricklerType( \"%2\" );" ).arg( aName ).arg( aType );
73 aResList << QString( "" );
77 ///< \TODO to be implemented:
79 aResList << QString( "" );
80 aResList << QString( "%1.Update();" ).arg( aName );
81 aResList << QString( "" );
86 HYDROData_SequenceOfObjects HYDROData_LandCover::GetAllReferenceObjects() const
88 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
90 HYDROData_SequenceOfObjects aSeqOfPolylines = GetPolylines();
91 aResSeq.Append( aSeqOfPolylines );
96 bool HYDROData_LandCover::IsHas2dPrs() const
101 void HYDROData_LandCover::SetStricklerType( const QString& theType )
103 TCollection_AsciiString anAsciiStr( theType.toStdString().c_str() );
104 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_StricklerType ), anAsciiStr );
107 QString HYDROData_LandCover::GetStricklerType() const
111 TDF_Label aLabel = myLab.FindChild( DataTag_StricklerType, false );
112 if ( !aLabel.IsNull() ) {
113 Handle(TDataStd_AsciiString) anAsciiStr;
114 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) ) {
115 aType = QString( anAsciiStr->Get().ToCString() );
122 void HYDROData_LandCover::Update()
124 HYDROData_Entity::Update();
128 TCollection_AsciiString anErrorMsg;
129 TopoDS_Shape aResShape = buildShape( GetPolylines(), anErrorMsg );
131 setShape( aResShape );
134 void HYDROData_LandCover::SetPolylines( const HYDROData_SequenceOfObjects& thePolylines )
136 SetReferenceObjects( thePolylines, DataTag_Polylines );
140 HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
142 return GetReferenceObjects( DataTag_Polylines );
145 TopoDS_Shape HYDROData_LandCover::GetShape() const
149 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
150 if ( !aLabel.IsNull() )
152 Handle(TNaming_NamedShape) aNamedShape;
153 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) ) {
154 aShape = aNamedShape->Get();
161 void HYDROData_LandCover::SetFillingColor( const QColor& theColor )
163 SetColor( theColor, DataTag_FillingColor );
166 QColor HYDROData_LandCover::GetFillingColor() const
168 return GetColor( DefaultFillingColor(), DataTag_FillingColor );
171 void HYDROData_LandCover::SetBorderColor( const QColor& theColor )
173 SetColor( theColor, DataTag_BorderColor );
176 QColor HYDROData_LandCover::GetBorderColor() const
178 return GetColor( DefaultBorderColor(), DataTag_BorderColor );
181 QColor HYDROData_LandCover::DefaultFillingColor()
183 return QColor( Qt::magenta );
186 QColor HYDROData_LandCover::DefaultBorderColor()
188 return QColor( Qt::transparent );
191 void HYDROData_LandCover::setShape( const TopoDS_Shape& theShape )
193 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
194 aBuilder.Generated( theShape );
197 void HYDROData_LandCover::removeShape()
199 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
200 if ( !aLabel.IsNull() ) {
201 aLabel.ForgetAllAttributes();
205 TopoDS_Shape HYDROData_LandCover::buildShape( const HYDROData_SequenceOfObjects& thePolylines,
206 TCollection_AsciiString& theErrorMsg )
209 TopoDS_Shape aResShape;
211 BRepBuilderAPI_MakeWire aMakeWire;
213 TopTools_ListOfShape aClosedWires;
215 int aNbPolylines = thePolylines.Length();
216 for ( int i = 1; i <= aNbPolylines; ++i ) {
217 Handle(HYDROData_PolylineXY) aPolyline =
218 Handle(HYDROData_PolylineXY)::DownCast( thePolylines.Value( i ) );
220 if ( aPolyline.IsNull() ) {
224 TopoDS_Shape aPolyShape = aPolyline->GetShape();
225 if ( aPolyShape.IsNull() ) {
229 // Extract polyline wire(s)
230 TopTools_ListOfShape aPolylineWires;
232 if ( aPolyShape.ShapeType() == TopAbs_WIRE ) {
233 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
234 if ( !aPolylineWire.IsNull() ) {
235 aPolylineWires.Append( aPolylineWire );
237 } else if ( aPolyShape.ShapeType() == TopAbs_COMPOUND ) {
238 TopExp_Explorer anExp( aPolyShape, TopAbs_WIRE );
239 for (; anExp.More(); anExp.Next() ) {
240 if(!anExp.Current().IsNull()) {
241 const TopoDS_Wire& aWire = TopoDS::Wire( anExp.Current() );
242 aPolylineWires.Append( aWire );
247 TopTools_ListIteratorOfListOfShape anIt( aPolylineWires );
248 for ( ; anIt.More(); anIt.Next() ) {
249 TopoDS_Wire& aWire = TopoDS::Wire( anIt.Value() );
251 if ( aWire.Closed() ) {
252 aClosedWires.Append( aWire );
254 aMakeWire.Add( aWire );
256 if ( aMakeWire.IsDone() ) {
257 if ( aMakeWire.Wire().Closed() ) {
258 aClosedWires.Append( aMakeWire.Wire() );
259 aMakeWire = BRepBuilderAPI_MakeWire();
266 if ( aClosedWires.Extent() == 1 ) {
268 TopoDS_Wire aW = TopoDS::Wire( aClosedWires.First());
269 BRepBuilderAPI_MakeFace aMakeFace( aW );
271 if( aMakeFace.IsDone() )
273 Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
274 if (!aSAW->CheckSelfIntersection())
275 aResShape = aMakeFace.Face();
277 theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire have been detected";
279 } else if ( aClosedWires.Extent() > 1 ) {
281 BRep_Builder aBuilder;
282 TopoDS_Compound aCompound;
283 aBuilder.MakeCompound( aCompound );
284 TopTools_SequenceOfShape aSeq;
285 TopTools_ListIteratorOfListOfShape aWiresIter( aClosedWires );
286 bool anErrStat = false;
287 for ( ; aWiresIter.More() && !anErrStat; aWiresIter.Next() )
289 TopoDS_Wire aW = TopoDS::Wire( aWiresIter.Value() );
290 BRepBuilderAPI_MakeFace aMakeFace( aW );
292 if( aMakeFace.IsDone() )
294 Handle(ShapeAnalysis_Wire) aSAW = new ShapeAnalysis_Wire(aW, aMakeFace.Face(), Precision::Confusion());
295 if (!aSAW->CheckSelfIntersection())
296 aSeq.Append( aMakeFace.Face() );
300 theErrorMsg = "Can't create landcover on the given polyline\nSelf-intersection of wire(s) have been detected";
306 for (int i = 1; i <= aSeq.Length(); i++)
307 aBuilder.Add( aCompound, aSeq(i) );
308 aResShape = aCompound;
311 aResShape = TopoDS_Shape();
312 } else if ( aNbPolylines > 0 ) {
313 TCollection_AsciiString aSourceName = aNbPolylines > 1 ? "polylines" : "polyline";
314 theErrorMsg = "Can't build closed contour on the given ";
315 theErrorMsg += aSourceName;
318 ///< \TODO to be reimplemented
320 TopoDS_Shape anArgShape;
321 TopTools_ListOfShape aToolShapes;
323 HYDROData_SequenceOfObjects aRefPolylines = GetPolylines();
324 for ( int i = 1, n = aRefPolylines.Length(); i <= n; ++i ) {
325 Handle(HYDROData_PolylineXY) aPolyline =
326 Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
328 if ( aPolyline.IsNull() ) {
332 if ( !aPolyline->IsClosed() ) {
336 TopoDS_Shape aPolyShape = aPolyline->GetShape();
337 if ( aPolyShape.IsNull() || aPolyShape.ShapeType() != TopAbs_WIRE ) {
341 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
342 if ( aPolylineWire.IsNull() ) {
346 TopoDS_Face aResultFace = TopoDS_Face();
347 BRepBuilderAPI_MakeFace aMakeFace( aPolylineWire, Standard_True );
349 if( aMakeFace.IsDone() ) {
350 aResultFace = aMakeFace.Face();
353 if( aResultFace.IsNull() ) {
357 if ( anArgShape.IsNull() ) {
358 anArgShape = aResultFace;
360 aToolShapes.Append( aResultFace );
364 aResShape = anArgShape;
366 if ( !anArgShape.IsNull() && aToolShapes.Extent() > 0 ) {
367 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
368 BOPAlgo_BOP aBOP(aAL);
370 aBOP.AddArgument( anArgShape );
372 TopTools_ListIteratorOfListOfShape anIt(aToolShapes);
373 for( ; anIt.More(); anIt.Next() ) {
374 aBOP.AddTool( anIt.Value() );
377 aBOP.SetOperation( BOPAlgo_CUT );
380 if ( !aBOP.Shape().IsNull() ) {
381 aResShape = aBOP.Shape();