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 <TopTools_ListOfShape.hxx>
34 #include <TopTools_ListIteratorOfListOfShape.hxx>
35 #include <NCollection_IncAllocator.hxx>
36 #include <BOPAlgo_BOP.hxx>
39 #include <QStringList>
41 IMPLEMENT_STANDARD_HANDLE( HYDROData_LandCover, HYDROData_Entity )
42 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_LandCover, HYDROData_Entity )
44 HYDROData_LandCover::HYDROData_LandCover()
48 HYDROData_LandCover::~HYDROData_LandCover()
52 const ObjectKind HYDROData_LandCover::GetKind() const
54 return KIND_LAND_COVER;
57 QStringList HYDROData_LandCover::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
59 QStringList aResList = dumpObjectCreation( theTreatedObjects );
60 QString aName = GetObjPyName();
63 QString aType = GetStricklerType();
64 if ( !aType.isEmpty() ) {
65 aResList << QString( "" );
66 ///< \TODO to be implemented:
67 // aResList << QString( "%1.SetStricklerType( \"%2\" );" ).arg( aName ).arg( aType );
68 aResList << QString( "" );
72 ///< \TODO to be implemented:
74 aResList << QString( "" );
75 aResList << QString( "%1.Update();" ).arg( aName );
76 aResList << QString( "" );
81 HYDROData_SequenceOfObjects HYDROData_LandCover::GetAllReferenceObjects() const
83 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
85 HYDROData_SequenceOfObjects aSeqOfPolylines = GetPolylines();
86 aResSeq.Append( aSeqOfPolylines );
91 bool HYDROData_LandCover::IsHas2dPrs() const
96 void HYDROData_LandCover::SetStricklerType( const QString& theType )
98 TCollection_AsciiString anAsciiStr( theType.toStdString().c_str() );
99 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_StricklerType ), anAsciiStr );
102 QString HYDROData_LandCover::GetStricklerType() const
106 TDF_Label aLabel = myLab.FindChild( DataTag_StricklerType, false );
107 if ( !aLabel.IsNull() ) {
108 Handle(TDataStd_AsciiString) anAsciiStr;
109 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) ) {
110 aType = QString( anAsciiStr->Get().ToCString() );
117 void HYDROData_LandCover::Update()
119 HYDROData_Entity::Update();
123 TopoDS_Shape aResShape = buildShape();
125 setShape( aResShape );
128 void HYDROData_LandCover::SetPolylines( const HYDROData_SequenceOfObjects& thePolylines )
130 SetReferenceObjects( thePolylines, DataTag_Polylines );
134 HYDROData_SequenceOfObjects HYDROData_LandCover::GetPolylines() const
136 return GetReferenceObjects( DataTag_Polylines );
139 TopoDS_Shape HYDROData_LandCover::GetShape() const
143 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
144 if ( !aLabel.IsNull() )
146 Handle(TNaming_NamedShape) aNamedShape;
147 if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) ) {
148 aShape = aNamedShape->Get();
155 void HYDROData_LandCover::SetFillingColor( const QColor& theColor )
157 SetColor( theColor, DataTag_FillingColor );
160 QColor HYDROData_LandCover::GetFillingColor() const
162 return GetColor( DefaultFillingColor(), DataTag_FillingColor );
165 void HYDROData_LandCover::SetBorderColor( const QColor& theColor )
167 SetColor( theColor, DataTag_BorderColor );
170 QColor HYDROData_LandCover::GetBorderColor() const
172 return GetColor( DefaultBorderColor(), DataTag_BorderColor );
175 QColor HYDROData_LandCover::DefaultFillingColor()
177 return QColor( Qt::magenta );
180 QColor HYDROData_LandCover::DefaultBorderColor()
182 return QColor( Qt::transparent );
185 void HYDROData_LandCover::setShape( const TopoDS_Shape& theShape )
187 TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape ) );
188 aBuilder.Generated( theShape );
191 void HYDROData_LandCover::removeShape()
193 TDF_Label aLabel = myLab.FindChild( DataTag_Shape, false );
194 if ( !aLabel.IsNull() ) {
195 aLabel.ForgetAllAttributes();
199 TopoDS_Shape HYDROData_LandCover::buildShape() const
201 TopoDS_Shape aResShape;
203 BRepBuilderAPI_MakeWire aMakeWire;
205 TopTools_ListOfShape aClosedWires;
207 HYDROData_SequenceOfObjects aRefPolylines = GetPolylines();
208 for ( int i = 1, n = aRefPolylines.Length(); i <= n; ++i ) {
209 Handle(HYDROData_PolylineXY) aPolyline =
210 Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
212 if ( aPolyline.IsNull() ) {
216 TopoDS_Shape aPolyShape = aPolyline->GetShape();
217 if ( aPolyShape.IsNull() || aPolyShape.ShapeType() != TopAbs_WIRE ) {
221 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
222 if ( aPolylineWire.IsNull() ) {
226 if ( aPolylineWire.Closed() ) {
227 aClosedWires.Append( aPolylineWire );
229 aMakeWire.Add( aPolylineWire );
231 if ( aMakeWire.IsDone() ) {
232 if ( aMakeWire.Wire().Closed() ) {
233 aClosedWires.Append( aMakeWire.Wire() );
234 aMakeWire = BRepBuilderAPI_MakeWire();
240 if ( aClosedWires.Extent() == 1 ) {
242 BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( aClosedWires.First() ) );
244 if( aMakeFace.IsDone() ) {
245 aResShape = aMakeFace.Face();
247 } else if ( aClosedWires.Extent() > 1 ) {
249 BRep_Builder aBuilder;
250 TopoDS_Compound aCompound;
251 aBuilder.MakeCompound( aCompound );
253 TopTools_ListIteratorOfListOfShape aWiresIter( aClosedWires );
254 for ( ; aWiresIter.More(); aWiresIter.Next() ) {
255 BRepBuilderAPI_MakeFace aMakeFace( TopoDS::Wire( aWiresIter.Value() ) );
257 if( aMakeFace.IsDone() ) {
258 aBuilder.Add( aCompound, aMakeFace.Face() );
262 aResShape = aCompound;
265 ///< \TODO to be reimplemented
267 TopoDS_Shape anArgShape;
268 TopTools_ListOfShape aToolShapes;
270 HYDROData_SequenceOfObjects aRefPolylines = GetPolylines();
271 for ( int i = 1, n = aRefPolylines.Length(); i <= n; ++i ) {
272 Handle(HYDROData_PolylineXY) aPolyline =
273 Handle(HYDROData_PolylineXY)::DownCast( aRefPolylines.Value( i ) );
275 if ( aPolyline.IsNull() ) {
279 if ( !aPolyline->IsClosed() ) {
283 TopoDS_Shape aPolyShape = aPolyline->GetShape();
284 if ( aPolyShape.IsNull() || aPolyShape.ShapeType() != TopAbs_WIRE ) {
288 const TopoDS_Wire& aPolylineWire = TopoDS::Wire( aPolyShape );
289 if ( aPolylineWire.IsNull() ) {
293 TopoDS_Face aResultFace = TopoDS_Face();
294 BRepBuilderAPI_MakeFace aMakeFace( aPolylineWire, Standard_True );
296 if( aMakeFace.IsDone() ) {
297 aResultFace = aMakeFace.Face();
300 if( aResultFace.IsNull() ) {
304 if ( anArgShape.IsNull() ) {
305 anArgShape = aResultFace;
307 aToolShapes.Append( aResultFace );
311 aResShape = anArgShape;
313 if ( !anArgShape.IsNull() && aToolShapes.Extent() > 0 ) {
314 Handle(NCollection_BaseAllocator)aAL=new NCollection_IncAllocator;
315 BOPAlgo_BOP aBOP(aAL);
317 aBOP.AddArgument( anArgShape );
319 TopTools_ListIteratorOfListOfShape anIt(aToolShapes);
320 for( ; anIt.More(); anIt.Next() ) {
321 aBOP.AddTool( anIt.Value() );
324 aBOP.SetOperation( BOPAlgo_CUT );
327 if ( !aBOP.Shape().IsNull() ) {
328 aResShape = aBOP.Shape();