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 "HYDROGUI_ImportLandCoverOp.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_ImportLandCoverDlg.h"
31 #include <HYDROGUI_DataObject.h>
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Iterator.h>
35 #include <HYDROData_Profile.h>
37 #include <SUIT_Desktop.h>
38 #include <SUIT_FileDlg.h>
39 #include <LightApp_Application.h>
41 #include <QApplication>
44 #include <SUIT_MessageBox.h>
46 #include <BRep_Builder.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopoDS_Wire.hxx>
50 #include <BRepBuilderAPI_MakeEdge2d.hxx>
51 #include <BRepBuilderAPI_MakeWire.hxx>
52 #include <BRepBuilderAPI_MakeFace.hxx>
53 #include <gp_Pnt2d.hxx>
54 #include <BRepTools.hxx>
59 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
60 : HYDROGUI_Operation( theModule )
62 setName( tr( "IMPORT_LANDCOVER" ) );
65 HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
69 void HYDROGUI_ImportLandCoverOp::startOperation()
71 HYDROGUI_Operation::startOperation();
73 /*myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
74 myFileDlg->setWindowTitle( getName() );
75 myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
76 myFileDlg->setFilter( tr("LANDCOVER_FILTER") );
78 connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
79 connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
85 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
87 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverDlg( module(), getName() );
89 connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
91 connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
96 void HYDROGUI_ImportLandCoverOp::onApply()
98 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
100 QApplication::restoreOverrideCursor();
105 void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
108 mySHPObjects.clear();
109 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
112 for (int i = 0; i < theHandle->nRecords; i++)
113 mySHPObjects.push_back(SHPReadObject(theHandle, i));
117 void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
121 int nParts = anObj->nParts;
122 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
123 BRepBuilderAPI_MakeFace aFBuilder(pln);
124 for ( int i = 0 ; i < nParts ; i++ )
126 BRepBuilderAPI_MakeWire aBuilder;
127 int StartIndex = anObj->panPartStart[i];
130 EndIndex = anObj->panPartStart[i + 1];
132 EndIndex = anObj->nVertices;
134 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
136 gp_Pnt2d P1 (anObj->padfX[k], anObj->padfY[k]);
137 gp_Pnt2d P2 (anObj->padfX[k+1], anObj->padfY[k+1]);
138 BRepBuilderAPI_MakeEdge2d aMakeEdge(P1, P2);
139 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
142 W = TopoDS::Wire(aBuilder.Shape());
147 F = aFBuilder.Face();
149 //TODO build curve 3d
151 /*aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
152 aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
154 aPolylineXY->Update();
156 size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
157 if ( anActiveViewId == 0 )
158 anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
160 module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
162 module()->setIsToUpdate( aPolylineXY );*/
165 void HYDROGUI_ImportLandCoverOp::onFileSelected()
167 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
171 QString anObjectName = aPanel->getObjectName().simplified();
172 anObjectName = aPanel->getFileName();
173 if ( !anObjectName.isEmpty() ) {
174 anObjectName = QFileInfo( anObjectName ).baseName();
177 if ( anObjectName.isEmpty() ) {
178 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
180 aPanel->setObjectName( anObjectName );
182 QString aFileName = aPanel->myFileName->text();
183 if ( aFileName.isEmpty() )
189 QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
194 aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
198 QStringList aPolygonsList;
199 for (int i = 0; i < mySHPObjects.size(); i++)
200 aPolygonsList.append("polygon_" + QString::number(i));
201 aPanel->setPolylineNames(aPolygonsList);
205 BB.MakeCompound(cmp);
207 if (aHSHP->nShapeType == 5)
209 for (int i = 0; i < 10/*mySHPObjects.size()*/; i++) {
210 ProcessSHP(mySHPObjects[i], i, F);
213 ///to hydro_landcover
214 // BRepTools::Write(cmp, "d:/h1.brep");
217 SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
219 commitDocOperation();
221 for (size_t i = 0; i < mySHPObjects.size(); i++ )
222 free (mySHPObjects[i]);
224 mySHPObjects.clear();