From 5f19fc7af832224fdb587fbcfe0eed4c41d0d6aa Mon Sep 17 00:00:00 2001 From: isn Date: Mon, 29 Jun 2015 11:03:45 +0300 Subject: [PATCH] Import/Export // API revision. p1 --- src/HYDROData/CMakeLists.txt | 4 +- src/HYDROData/HYDROData_ShapeFile.cxx | 355 ++++++++++++++++++++ src/HYDROData/HYDROData_ShapeFile.h | 72 ++++ src/HYDROGUI/CMakeLists.txt | 2 +- src/HYDROGUI/HYDROGUI_ExportFileOp.cxx | 193 +---------- src/HYDROGUI/HYDROGUI_ExportFileOp.h | 11 +- src/HYDROGUI/HYDROGUI_ImportLandCoverOp.cxx | 109 +----- src/HYDROGUI/HYDROGUI_ImportLandCoverOp.h | 8 +- 8 files changed, 452 insertions(+), 302 deletions(-) create mode 100644 src/HYDROData/HYDROData_ShapeFile.cxx create mode 100644 src/HYDROData/HYDROData_ShapeFile.h diff --git a/src/HYDROData/CMakeLists.txt b/src/HYDROData/CMakeLists.txt index 36dcd976..dfa8bf1b 100644 --- a/src/HYDROData/CMakeLists.txt +++ b/src/HYDROData/CMakeLists.txt @@ -54,6 +54,7 @@ set(PROJECT_HEADERS HYDROData_LinearInterpolator.h HYDROData_InterpolatorsFactory.h HYDROData_SinusX.h + HYDROData_ShapeFile.h ) set(PROJECT_SOURCES @@ -108,6 +109,7 @@ set(PROJECT_SOURCES HYDROData_LinearInterpolator.cxx HYDROData_InterpolatorsFactory.cxx HYDROData_SinusX.cxx + HYDROData_ShapeFile.cxx ) add_definitions( @@ -130,7 +132,7 @@ include_directories( ) add_library(HYDROData SHARED ${PROJECT_SOURCES} ${PROJECT_HEADERS}) -target_link_libraries(HYDROData ${GEOM_GEOMUtils} ${CAS_OCAF} ${CAS_OCAFVIS} ${CAS_TKG3d} ${CAS_TKGeomBase} ${CAS_TKGeomAlgo} +target_link_libraries(HYDROData shapelib ${GEOM_GEOMUtils} ${CAS_OCAF} ${CAS_OCAFVIS} ${CAS_TKG3d} ${CAS_TKGeomBase} ${CAS_TKGeomAlgo} ${CAS_TKBrep} ${CAS_TKIGES} ${CAS_TKSTEP} ${CAS_TKTopAlgo} ${CAS_TKBO} ${CAS_TKBool} ${CAS_TKOffset} ${QT_LIBRARIES} ${GUI_ImageComposer} ${CAS_TKHLR} ${GEOM_GEOM} ${GEOM_GEOMBase} ${GEOM_CurveCreator} ) INSTALL(TARGETS HYDROData EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_LIBS}) diff --git a/src/HYDROData/HYDROData_ShapeFile.cxx b/src/HYDROData/HYDROData_ShapeFile.cxx new file mode 100644 index 00000000..5dfc724a --- /dev/null +++ b/src/HYDROData/HYDROData_ShapeFile.cxx @@ -0,0 +1,355 @@ +// Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL) +{ +} + +HYDROData_ShapeFile::~HYDROData_ShapeFile() +{ + Free(); +} + +void HYDROData_ShapeFile::Export(const QString& aFileName, + NCollection_Sequence aPolyXYSeq, + NCollection_Sequence aPoly3DSeq, + NCollection_Sequence aLCSeq, + QStringList& aNonExpList) +{ + SHPHandle hSHPHandle; + if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty()) + { + hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC ); + for (int i = 1; i <= aPolyXYSeq.Size(); i++) + if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1) + aNonExpList.append(aPolyXYSeq(i)->GetName()); + + } + else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty()) + { + hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ ); + for (int i = 1; i <= aPoly3DSeq.Size(); i++) + if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1) + aNonExpList.append(aPoly3DSeq(i)->GetName()); + } + else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty()) + { + hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON ); + for (int i = 1; i <= aLCSeq.Size(); i++) + if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1) + aNonExpList.append(aLCSeq(i)->GetName()); + } + SHPClose( hSHPHandle ); + +} + +int HYDROData_ShapeFile::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly ) +{ + SHPObject *aSHPObj; + std::vector x, y; + std::vector anPartStart; + + for (int i = 0; i < thePoly->NbSections(); i++) + if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE) + return -1; + + for (int i = 0; i < thePoly->NbSections(); i++) + { + anPartStart.push_back(x.size()); + HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i); + for (int j = 1; j <= aPointList.Size(); j++) + { + x.push_back( aPointList(j).X()); + y.push_back( aPointList(j).Y()); + } + if (thePoly->IsClosedSection(i)) + { + x.push_back( aPointList(1).X()); + y.push_back( aPointList(1).Y()); + } + } + + aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL ); + SHPWriteObject( theShpHandle, -1, aSHPObj ); + SHPDestroyObject( aSHPObj ); + return 1; +} + +int HYDROData_ShapeFile::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly ) +{ + SHPObject *aSHPObj; + std::vector x, y, z; + std::vector anPartStart; + + for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++) + if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE) + return -1; + + for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++) + { + anPartStart.push_back(x.size()); + HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i); + for (int j = 1; j <= aPointList.Size(); j++) + { + x.push_back( aPointList(j).X()); + y.push_back( aPointList(j).Y()); + z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y()))); + } + if ( thePoly->GetPolylineXY()->IsClosedSection(i)) + { + x.push_back( aPointList(1).X()); + y.push_back( aPointList(1).Y()); + z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y()))); + + } + } + + aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL ); + SHPWriteObject( theShpHandle, -1, aSHPObj ); + SHPDestroyObject( aSHPObj ); + return 1; +} + +int HYDROData_ShapeFile::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC ) +{ + TopoDS_Shape aSh = theLC->GetShape(); + if (aSh.IsNull()) + return 0; + TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE); + for (; anEdgeEx.More(); anEdgeEx.Next()) + { + TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current()); + double aFP, aLP; + Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP); + Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur); + if (aLine.IsNull()) + { + Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur); + if (!aTC.IsNull()) + { + Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve()); + if (aLine.IsNull()) + return -1; + } + else + return -1; + } + + } + if (aSh.ShapeType() == TopAbs_FACE) + { + ProcessFace(TopoDS::Face(aSh), theShpHandle); + } + else if (aSh.ShapeType() == TopAbs_COMPOUND) + { + TopExp_Explorer Ex(aSh, TopAbs_FACE); + for (; Ex.More(); Ex.Next()) + { + TopoDS_Face aF = TopoDS::Face(Ex.Current()); + if (aF.IsNull()) + continue; + ProcessFace(aF, theShpHandle); + } + } + else + return 0; + + return 1; + +} + +void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle) +{ + SHPObject *aSHPObj; + std::vector x, y; + std::vector anPartStart; + if (theFace.ShapeType() == TopAbs_FACE) + { + TopExp_Explorer Ex(theFace, TopAbs_WIRE); + int NbWires = 0; + for (; Ex.More(); Ex.Next()) + { + TopoDS_Wire aW = TopoDS::Wire(Ex.Current()); + if (aW.IsNull()) + continue; + NbWires++; + anPartStart.push_back(x.size()); + TopExp_Explorer aVEx(aW, TopAbs_VERTEX); + NCollection_Sequence aPnts; + for (; aVEx.More(); aVEx.Next()) + { + TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current()); + if (aV.IsNull()) + continue; + gp_Pnt P = BRep_Tool::Pnt(aV); + aPnts.Append(gp_Pnt2d(P.X(), P.Y())); + } + NCollection_Sequence aNPnts; + aNPnts.Append(aPnts.First()); + for (int j = 1; j <= aPnts.Size() - 1; j++) + { + if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) + aNPnts.Append(aPnts(j + 1)); + } + + for (int j = 1; j <= aNPnts.Size(); j++) + { + x.push_back( aNPnts(j).X()); + y.push_back( aNPnts(j).Y()); + } + //x.push_back( aNPnts(1).X()); + //y.push_back( aNPnts(1).Y()); + + } + + aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL ); + SHPWriteObject( theShpHandle, -1, aSHPObj ); + SHPDestroyObject( aSHPObj ); + } + else + return; +} + +void HYDROData_ShapeFile::Parse(SHPHandle theHandle) +{ + int aShapeType; + mySHPObjects.clear(); + SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL ); + if (aShapeType == 5) + { + for (int i = 0; i < theHandle->nRecords; i++) + mySHPObjects.push_back(SHPReadObject(theHandle, i)); + } +} + +void HYDROData_ShapeFile::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F) +{ + TopoDS_Wire W; + TopoDS_Edge E; + int nParts = anObj->nParts; + gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1)); + BRepBuilderAPI_MakeFace aFBuilder(pln); + + //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape; + //sfs->FixFaceTool()->FixOrientationMode() = 1; + + for ( int i = 0 ; i < nParts ; i++ ) + { + BRepBuilderAPI_MakeWire aBuilder; + int StartIndex = anObj->panPartStart[i]; + int EndIndex; + if (i != nParts - 1) + EndIndex = anObj->panPartStart[i + 1]; + else + EndIndex = anObj->nVertices; + + for ( int k = StartIndex; k < EndIndex - 1 ; k++ ) + { + gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0); + gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0); + if (P1.Distance(P2) < Precision::Confusion()) + continue; + BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2); + aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape())); + } + + aBuilder.Build(); + W = TopoDS::Wire(aBuilder.Shape()); + W.Reverse(); + aFBuilder.Add(W); + } + + aFBuilder.Build(); + TopoDS_Face DF = aFBuilder.Face(); + BRepLib::BuildCurves3d(DF); + bool IsInf = DF.Infinite(); + if(!DF.IsNull()) + { + //sfs->Init ( DF ); + //sfs->Perform(); + F = DF; //TopoDS::Face(sfs->Shape()); + } +} + +bool HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces) +{ + Free(); + myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" ); + Parse(myHSHP); + for (int i = 0; i < mySHPObjects.size(); i++) + thePolygonsList.append("polygon_" + QString::number(i + 1)); + + TopoDS_Face aF; + if (myHSHP->nShapeType == 5) + { + for (int i = 0; i < mySHPObjects.size(); i++) + { + ProcessSHP(mySHPObjects[i], i, aF); + theFaces.Append(aF); + } + return true; + } + else + return false; +} + +void HYDROData_ShapeFile::Free() +{ + for (size_t i = 0; i < mySHPObjects.size(); i++ ) + free (mySHPObjects[i]); + + mySHPObjects.clear(); + if (myHSHP != NULL) + { + SHPClose(myHSHP); + myHSHP = NULL; + } +} \ No newline at end of file diff --git a/src/HYDROData/HYDROData_ShapeFile.h b/src/HYDROData/HYDROData_ShapeFile.h new file mode 100644 index 00000000..3fef48ff --- /dev/null +++ b/src/HYDROData/HYDROData_ShapeFile.h @@ -0,0 +1,72 @@ +// Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#ifndef HYDRODATA_SHAPEFILE_H +#define HYDRODATA_SHAPEFILE_H + +#include +#include +#include +#include "HYDROData.h" + +//extern "C" { +#include +//}; + +class gp_XYZ; +class Handle_HYDROData_PolylineXY; +class Handle_HYDROData_Polyline3D; +class Handle_HYDROData_LandCover; +class TopTools_SequenceOfShape; +class TopoDS_Face; + +class HYDROData_ShapeFile +{ + +public: + HYDRODATA_EXPORT HYDROData_ShapeFile( ); + virtual HYDRODATA_EXPORT ~HYDROData_ShapeFile(); + +public: + //Export operation + HYDRODATA_EXPORT void Export(const QString& aFileName, + NCollection_Sequence aPolyXYSeq, + NCollection_Sequence aPoly3DSeq, + NCollection_Sequence aLCSeq, + QStringList& aNonExpList); + int WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly ); + int WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly ); + int WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC ); + //Import Landcover + void Parse(SHPHandle theHandle); + void ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F); + HYDRODATA_EXPORT bool ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces); + HYDRODATA_EXPORT void Free(); + /// +private: + void ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle); +private: + std::vector mySHPObjects; + SHPHandle myHSHP; +}; + +#endif diff --git a/src/HYDROGUI/CMakeLists.txt b/src/HYDROGUI/CMakeLists.txt index 5daac868..46728208 100644 --- a/src/HYDROGUI/CMakeLists.txt +++ b/src/HYDROGUI/CMakeLists.txt @@ -310,7 +310,7 @@ target_link_libraries(HYDROGUI HYDROData shapelib INSTALL(TARGETS HYDROGUI EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_LIBS}) -set(PROJECT_LIBRARIES HYDROGUI) +set(PROJECT_LIBRARIES shapelib HYDROGUI) set(GUITS_SOURCES resources/HYDROGUI_images.ts diff --git a/src/HYDROGUI/HYDROGUI_ExportFileOp.cxx b/src/HYDROGUI/HYDROGUI_ExportFileOp.cxx index a902cdad..e2998187 100644 --- a/src/HYDROGUI/HYDROGUI_ExportFileOp.cxx +++ b/src/HYDROGUI/HYDROGUI_ExportFileOp.cxx @@ -53,6 +53,7 @@ #include #include #include +#include HYDROGUI_ExportFileOp::HYDROGUI_ExportFileOp( HYDROGUI_Module* theModule ) : HYDROGUI_Operation( theModule ) @@ -107,31 +108,9 @@ void HYDROGUI_ExportFileOp::startOperation() QString aFileName = SUIT_FileDlg::getFileName( module()->getApp()->desktop(), aName, aFilter, tr( "EXPORT_TO_SHAPE_FILE" ), false ); if (!aFileName.isEmpty()) { - SHPHandle hSHPHandle; QStringList aNonExpList; - if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty()) - { - hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC ); - for (int i = 1; i <= aPolyXYSeq.Size(); i++) - if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1) - aNonExpList.append(aPolyXYSeq(i)->GetName()); - - } - else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty()) - { - hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ ); - for (int i = 1; i <= aPoly3DSeq.Size(); i++) - if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1) - aNonExpList.append(aPoly3DSeq(i)->GetName()); - } - else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty()) - { - hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON ); - for (int i = 1; i <= aLCSeq.Size(); i++) - if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1) - aNonExpList.append(aLCSeq(i)->GetName()); - } - SHPClose( hSHPHandle ); + HYDROData_ShapeFile anExporter; + anExporter.Export(aFileName, aPolyXYSeq, aPoly3DSeq, aLCSeq, aNonExpList); if (!aNonExpList.empty()) { QString aMessage = tr("CANNOT_BE_EXPORTED") + "\n"; @@ -147,169 +126,3 @@ void HYDROGUI_ExportFileOp::startOperation() } -int HYDROGUI_ExportFileOp::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly ) -{ - SHPObject *aSHPObj; - std::vector x, y; - std::vector anPartStart; - - for (int i = 0; i < thePoly->NbSections(); i++) - if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE) - return -1; - - for (int i = 0; i < thePoly->NbSections(); i++) - { - anPartStart.push_back(x.size()); - HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i); - for (int j = 1; j <= aPointList.Size(); j++) - { - x.push_back( aPointList(j).X()); - y.push_back( aPointList(j).Y()); - } - if (thePoly->IsClosedSection(i)) - { - x.push_back( aPointList(1).X()); - y.push_back( aPointList(1).Y()); - } - } - - aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL ); - SHPWriteObject( theShpHandle, -1, aSHPObj ); - SHPDestroyObject( aSHPObj ); - return 1; -} - -int HYDROGUI_ExportFileOp::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly ) -{ - SHPObject *aSHPObj; - std::vector x, y, z; - std::vector anPartStart; - - for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++) - if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE) - return -1; - - for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++) - { - anPartStart.push_back(x.size()); - HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i); - for (int j = 1; j <= aPointList.Size(); j++) - { - x.push_back( aPointList(j).X()); - y.push_back( aPointList(j).Y()); - z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y()))); - } - if ( thePoly->GetPolylineXY()->IsClosedSection(i)) - { - x.push_back( aPointList(1).X()); - y.push_back( aPointList(1).Y()); - z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y()))); - - } - } - - aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL ); - SHPWriteObject( theShpHandle, -1, aSHPObj ); - SHPDestroyObject( aSHPObj ); - return 1; -} - -int HYDROGUI_ExportFileOp::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC ) -{ - TopoDS_Shape aSh = theLC->GetShape(); - if (aSh.IsNull()) - return 0; - TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE); - for (; anEdgeEx.More(); anEdgeEx.Next()) - { - TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current()); - double aFP, aLP; - Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP); - Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur); - if (aLine.IsNull()) - { - Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur); - if (!aTC.IsNull()) - { - Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve()); - if (aLine.IsNull()) - return -1; - } - else - return -1; - } - - } - if (aSh.ShapeType() == TopAbs_FACE) - { - ProcessFace(TopoDS::Face(aSh), theShpHandle); - } - else if (aSh.ShapeType() == TopAbs_COMPOUND) - { - TopExp_Explorer Ex(aSh, TopAbs_FACE); - for (; Ex.More(); Ex.Next()) - { - TopoDS_Face aF = TopoDS::Face(Ex.Current()); - if (aF.IsNull()) - continue; - ProcessFace(aF, theShpHandle); - } - } - else - return 0; - - return 1; - -} - -void HYDROGUI_ExportFileOp::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle) -{ - SHPObject *aSHPObj; - std::vector x, y; - std::vector anPartStart; - if (theFace.ShapeType() == TopAbs_FACE) - { - TopExp_Explorer Ex(theFace, TopAbs_WIRE); - int NbWires = 0; - for (; Ex.More(); Ex.Next()) - { - TopoDS_Wire aW = TopoDS::Wire(Ex.Current()); - if (aW.IsNull()) - continue; - NbWires++; - anPartStart.push_back(x.size()); - TopExp_Explorer aVEx(aW, TopAbs_VERTEX); - NCollection_Sequence aPnts; - for (; aVEx.More(); aVEx.Next()) - { - TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current()); - if (aV.IsNull()) - continue; - gp_Pnt P = BRep_Tool::Pnt(aV); - aPnts.Append(gp_Pnt2d(P.X(), P.Y())); - } - NCollection_Sequence aNPnts; - aNPnts.Append(aPnts.First()); - for (int j = 1; j <= aPnts.Size() - 1; j++) - { - if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) - aNPnts.Append(aPnts(j + 1)); - } - - for (int j = 1; j <= aNPnts.Size(); j++) - { - x.push_back( aNPnts(j).X()); - y.push_back( aNPnts(j).Y()); - } - //x.push_back( aNPnts(1).X()); - //y.push_back( aNPnts(1).Y()); - - } - - aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL ); - SHPWriteObject( theShpHandle, -1, aSHPObj ); - SHPDestroyObject( aSHPObj ); - } - else - return; -} diff --git a/src/HYDROGUI/HYDROGUI_ExportFileOp.h b/src/HYDROGUI/HYDROGUI_ExportFileOp.h index 3fd45f81..d5884f58 100644 --- a/src/HYDROGUI/HYDROGUI_ExportFileOp.h +++ b/src/HYDROGUI/HYDROGUI_ExportFileOp.h @@ -26,10 +26,6 @@ #include "HYDROGUI_Operation.h" #include -//extern "C" { -#include -//}; - class SUIT_FileDlg; class gp_XYZ; class Handle_HYDROData_PolylineXY; @@ -47,14 +43,9 @@ public: protected: virtual void startOperation(); - int WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly ); - int WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly ); - int WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC ); -private: - void ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle); + private: SUIT_FileDlg* myFileDlg; - std::vector mySHPObjects; }; #endif diff --git a/src/HYDROGUI/HYDROGUI_ImportLandCoverOp.cxx b/src/HYDROGUI/HYDROGUI_ImportLandCoverOp.cxx index d072688c..d492a668 100644 --- a/src/HYDROGUI/HYDROGUI_ImportLandCoverOp.cxx +++ b/src/HYDROGUI/HYDROGUI_ImportLandCoverOp.cxx @@ -33,7 +33,7 @@ #include #include - +#include #include #include @@ -62,12 +62,7 @@ #include #include #include -#include -#include -#include -#include -#include -#include + HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule ) @@ -187,67 +182,6 @@ bool HYDROGUI_ImportLandCoverOp::processApply( int& theUpdateFlags, } -void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle) -{ - int aShapeType; - mySHPObjects.clear(); - SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL ); - if (aShapeType == 5) - { - for (int i = 0; i < theHandle->nRecords; i++) - mySHPObjects.push_back(SHPReadObject(theHandle, i)); - } -} - -void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F) -{ - TopoDS_Wire W; - TopoDS_Edge E; - int nParts = anObj->nParts; - gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1)); - BRepBuilderAPI_MakeFace aFBuilder(pln); - - //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape; - //sfs->FixFaceTool()->FixOrientationMode() = 1; - - for ( int i = 0 ; i < nParts ; i++ ) - { - BRepBuilderAPI_MakeWire aBuilder; - int StartIndex = anObj->panPartStart[i]; - int EndIndex; - if (i != nParts - 1) - EndIndex = anObj->panPartStart[i + 1]; - else - EndIndex = anObj->nVertices; - - for ( int k = StartIndex; k < EndIndex - 1 ; k++ ) - { - gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0); - gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0); - if (P1.Distance(P2) < Precision::Confusion()) - continue; - BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2); - aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape())); - } - - aBuilder.Build(); - W = TopoDS::Wire(aBuilder.Shape()); - W.Reverse(); - aFBuilder.Add(W); - } - - aFBuilder.Build(); - TopoDS_Face DF = aFBuilder.Face(); - BRepLib::BuildCurves3d(DF); - bool IsInf = DF.Infinite(); - if(!DF.IsNull()) - { - //sfs->Init ( DF ); - //sfs->Perform(); - F = DF; //TopoDS::Face(sfs->Shape()); - } -} - void HYDROGUI_ImportLandCoverOp::onFileSelected() { HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast( inputPanel() ); @@ -273,15 +207,20 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected() QString anExt = aFileName.split('.', QString::SkipEmptyParts).back(); if (anExt == "shp") - { - SHPHandle aHSHP; - aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" ); - Parse(aHSHP); + { + startDocOperation(); + QApplication::setOverrideCursor(Qt::WaitCursor); - startDocOperation(); QStringList aPolygonsList; - for (int i = 0; i < mySHPObjects.size(); i++) - aPolygonsList.append("polygon_" + QString::number(i + 1)); + TopTools_SequenceOfShape aFaces; + HYDROData_ShapeFile anImporter; + + if (!anImporter.ImportLandCovers(aFileName, aPolygonsList, aFaces)) + { + SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), "Cannot import land cover;\nThe shape type is not polygon" ); + abort(); + } + aPanel->setPolygonNames(aPolygonsList); SalomeApp_Study* aStudy = dynamic_cast( module()->getApp()->activeStudy() ); @@ -307,20 +246,7 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected() } } - QApplication::setOverrideCursor(Qt::WaitCursor); - TopTools_SequenceOfShape aFaces; - TopoDS_Face aF; - if (aHSHP->nShapeType == 5) - { - for (int i = 0; i < mySHPObjects.size(); i++) - { - ProcessSHP(mySHPObjects[i], i, aF); - aFaces.Append(aF); - } - } - else - SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), "Cannot import land cover;\nThe shape type is not polygon" ); QApplication::restoreOverrideCursor(); Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) ); @@ -349,12 +275,9 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected() } commitDocOperation(); - - for (size_t i = 0; i < mySHPObjects.size(); i++ ) - free (mySHPObjects[i]); - mySHPObjects.clear(); - SHPClose(aHSHP); + anImporter.Free(); + } } diff --git a/src/HYDROGUI/HYDROGUI_ImportLandCoverOp.h b/src/HYDROGUI/HYDROGUI_ImportLandCoverOp.h index 62fe7301..6b48f0ef 100644 --- a/src/HYDROGUI/HYDROGUI_ImportLandCoverOp.h +++ b/src/HYDROGUI/HYDROGUI_ImportLandCoverOp.h @@ -27,10 +27,6 @@ #include #include -//extern "C" { -#include -//}; - class SUIT_FileDlg; class HYDROGUI_Shape; class TopoDS_Face; @@ -50,8 +46,7 @@ protected: virtual bool processApply( int& theUpdateFlags, QString& theErrorMsg, QStringList& theBrowseObjectsEntries ); HYDROGUI_InputPanel* createInputPanel() const; - void Parse(SHPHandle theHandle); - void ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F); + void erasePreview(); protected slots: @@ -60,7 +55,6 @@ protected slots: void onViewerSelectionChanged (); private: - std::vector mySHPObjects; QMap myPolygonName2PrsShape; }; -- 2.39.2