1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <HYDROData_ShapeFile.h>
24 #include <HYDROData_PolylineXY.h>
25 #include <HYDROData_Polyline3D.h>
26 #include <HYDROData_Bathymetry.h>
27 #include <HYDROData_LandCover.h>
28 #include <HYDROData_Profile.h>
33 #include <TopExp_Explorer.hxx>
34 #include <TopoDS_Wire.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <BRep_Tool.hxx>
39 #include <Precision.hxx>
40 #include <Handle_Geom_Curve.hxx>
41 #include <Handle_Geom_Line.hxx>
42 #include <Handle_Geom_TrimmedCurve.hxx>
43 #include <Geom_TrimmedCurve.hxx>
44 #include <BRepBuilderAPI_MakeEdge.hxx>
45 #include <BRepBuilderAPI_MakeWire.hxx>
46 #include <BRepBuilderAPI_MakeFace.hxx>
48 #include <BRepLib.hxx>
49 #include <ShapeFix_Shape.hxx>
50 #include <TopTools_SequenceOfShape.hxx>
52 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
56 HYDROData_ShapeFile::~HYDROData_ShapeFile()
61 void HYDROData_ShapeFile::Export(const QString& aFileName,
62 NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
63 NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
64 NCollection_Sequence<Handle_HYDROData_LandCover> aLCSeq,
65 QStringList& aNonExpList)
68 if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
70 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
71 for (int i = 1; i <= aPolyXYSeq.Size(); i++)
72 if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
73 aNonExpList.append(aPolyXYSeq(i)->GetName());
76 else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
78 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
79 for (int i = 1; i <= aPoly3DSeq.Size(); i++)
80 if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
81 aNonExpList.append(aPoly3DSeq(i)->GetName());
83 else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty())
85 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
86 for (int i = 1; i <= aLCSeq.Size(); i++)
87 if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
88 aNonExpList.append(aLCSeq(i)->GetName());
90 SHPClose( hSHPHandle );
94 int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
97 std::vector<double> x, y;
98 std::vector<int> anPartStart;
100 for (int i = 0; i < thePoly->NbSections(); i++)
101 if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
104 for (int i = 0; i < thePoly->NbSections(); i++)
106 anPartStart.push_back(x.size());
107 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
108 for (int j = 1; j <= aPointList.Size(); j++)
110 x.push_back( aPointList(j).X());
111 y.push_back( aPointList(j).Y());
113 if (thePoly->IsClosedSection(i))
115 x.push_back( aPointList(1).X());
116 y.push_back( aPointList(1).Y());
120 aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
121 SHPWriteObject( theShpHandle, -1, aSHPObj );
122 SHPDestroyObject( aSHPObj );
126 int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
129 std::vector<double> x, y, z;
130 std::vector<int> anPartStart;
132 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
133 if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
136 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
138 anPartStart.push_back(x.size());
139 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
140 for (int j = 1; j <= aPointList.Size(); j++)
142 x.push_back( aPointList(j).X());
143 y.push_back( aPointList(j).Y());
144 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
146 if ( thePoly->GetPolylineXY()->IsClosedSection(i))
148 x.push_back( aPointList(1).X());
149 y.push_back( aPointList(1).Y());
150 z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
155 aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
156 SHPWriteObject( theShpHandle, -1, aSHPObj );
157 SHPDestroyObject( aSHPObj );
161 int HYDROData_ShapeFile::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC )
163 TopoDS_Shape aSh = theLC->GetShape();
166 TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE);
167 for (; anEdgeEx.More(); anEdgeEx.Next())
169 TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
171 Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
172 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
175 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
178 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
187 if (aSh.ShapeType() == TopAbs_FACE)
189 ProcessFace(TopoDS::Face(aSh), theShpHandle);
191 else if (aSh.ShapeType() == TopAbs_COMPOUND)
193 TopExp_Explorer Ex(aSh, TopAbs_FACE);
194 for (; Ex.More(); Ex.Next())
196 TopoDS_Face aF = TopoDS::Face(Ex.Current());
199 ProcessFace(aF, theShpHandle);
209 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
212 std::vector<double> x, y;
213 std::vector<int> anPartStart;
214 if (theFace.ShapeType() == TopAbs_FACE)
216 TopExp_Explorer Ex(theFace, TopAbs_WIRE);
218 for (; Ex.More(); Ex.Next())
220 TopoDS_Wire aW = TopoDS::Wire(Ex.Current());
224 anPartStart.push_back(x.size());
225 TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
226 NCollection_Sequence<gp_Pnt2d> aPnts;
227 for (; aVEx.More(); aVEx.Next())
229 TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current());
232 gp_Pnt P = BRep_Tool::Pnt(aV);
233 aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
235 NCollection_Sequence<gp_Pnt2d> aNPnts;
236 aNPnts.Append(aPnts.First());
237 for (int j = 1; j <= aPnts.Size() - 1; j++)
239 if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion()))
240 aNPnts.Append(aPnts(j + 1));
243 for (int j = 1; j <= aNPnts.Size(); j++)
245 x.push_back( aNPnts(j).X());
246 y.push_back( aNPnts(j).Y());
248 //x.push_back( aNPnts(1).X());
249 //y.push_back( aNPnts(1).Y());
253 aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
254 SHPWriteObject( theShpHandle, -1, aSHPObj );
255 SHPDestroyObject( aSHPObj );
261 void HYDROData_ShapeFile::Parse(SHPHandle theHandle)
264 mySHPObjects.clear();
265 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
268 for (int i = 0; i < theHandle->nRecords; i++)
269 mySHPObjects.push_back(SHPReadObject(theHandle, i));
273 void HYDROData_ShapeFile::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
277 int nParts = anObj->nParts;
278 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
279 BRepBuilderAPI_MakeFace aFBuilder(pln);
281 //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
282 //sfs->FixFaceTool()->FixOrientationMode() = 1;
284 for ( int i = 0 ; i < nParts ; i++ )
286 BRepBuilderAPI_MakeWire aBuilder;
287 int StartIndex = anObj->panPartStart[i];
290 EndIndex = anObj->panPartStart[i + 1];
292 EndIndex = anObj->nVertices;
294 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
296 gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
297 gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
298 if (P1.Distance(P2) < Precision::Confusion())
300 BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
301 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
305 W = TopoDS::Wire(aBuilder.Shape());
311 TopoDS_Face DF = aFBuilder.Face();
312 BRepLib::BuildCurves3d(DF);
313 bool IsInf = DF.Infinite();
318 F = DF; //TopoDS::Face(sfs->Shape());
322 bool HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces)
325 myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
327 for (int i = 0; i < mySHPObjects.size(); i++)
328 thePolygonsList.append("polygon_" + QString::number(i + 1));
331 if (myHSHP->nShapeType == 5)
333 for (int i = 0; i < mySHPObjects.size(); i++)
335 ProcessSHP(mySHPObjects[i], i, aF);
344 void HYDROData_ShapeFile::Free()
346 for (size_t i = 0; i < mySHPObjects.size(); i++ )
347 free (mySHPObjects[i]);
349 mySHPObjects.clear();