Salome HOME
SHP poly 2
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportLandcoverOp.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ImportLandCoverOp.h"
24
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"
30
31 #include <HYDROGUI_DataObject.h>
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Iterator.h>
34
35 #include <HYDROData_Profile.h>
36
37 #include <SUIT_Desktop.h>
38 #include <SUIT_FileDlg.h>
39 #include <LightApp_Application.h>
40
41 #include <QApplication>
42 #include <QFile>
43 #include <QFileInfo>
44 #include <SUIT_MessageBox.h>
45
46 #include <BRep_Builder.hxx>
47 #include <TopoDS.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>
55 #include <gp_Pln.hxx>
56 #include <QLineEdit>
57
58
59 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
60 : HYDROGUI_Operation( theModule )
61 {
62   setName( tr( "IMPORT_LANDCOVER" ) );
63 }
64
65 HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
66 {
67 }
68
69 void HYDROGUI_ImportLandCoverOp::startOperation()
70 {
71   HYDROGUI_Operation::startOperation();
72
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") );
77
78   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
79   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
80
81   myFileDlg->exec();*/
82 }
83
84
85 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
86 {
87   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverDlg( module(), getName() );
88
89   connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
90
91   connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
92
93   return aPanel;
94 }
95
96 void HYDROGUI_ImportLandCoverOp::onApply()
97 {
98   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
99   
100   QApplication::restoreOverrideCursor();
101 }
102
103
104
105 void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
106 {
107   int aShapeType;
108   mySHPObjects.clear();
109   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
110   if (aShapeType == 5) 
111   {
112     for (int i = 0; i < theHandle->nRecords; i++) 
113       mySHPObjects.push_back(SHPReadObject(theHandle, i));
114   }
115 }
116
117 void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
118 {
119   TopoDS_Wire W;
120   TopoDS_Edge E; 
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++ )
125   { 
126     BRepBuilderAPI_MakeWire aBuilder;
127     int StartIndex = anObj->panPartStart[i];
128     int EndIndex;
129     if (i != nParts - 1)
130       EndIndex = anObj->panPartStart[i + 1];
131     else
132       EndIndex = anObj->nVertices;
133
134     for ( int k = StartIndex; k < EndIndex - 1  ; k++ )
135     {
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()));
140     }
141     aBuilder.Build();
142     W = TopoDS::Wire(aBuilder.Shape());
143     aFBuilder.Add(W);
144   }
145
146   aFBuilder.Build();
147   F = aFBuilder.Face();
148
149   //TODO build curve 3d
150
151   /*aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
152   aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
153   
154   aPolylineXY->Update();
155   
156   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
157   if ( anActiveViewId == 0 )
158     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
159   
160   module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
161   
162   module()->setIsToUpdate( aPolylineXY );*/
163 }
164
165 void HYDROGUI_ImportLandCoverOp::onFileSelected()
166 {
167   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
168   if ( !aPanel )
169     return;
170
171   QString anObjectName = aPanel->getObjectName().simplified();
172    anObjectName = aPanel->getFileName();
173    if ( !anObjectName.isEmpty() ) {
174        anObjectName = QFileInfo( anObjectName ).baseName();
175    }
176
177    if ( anObjectName.isEmpty() ) {
178      anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
179    }
180    aPanel->setObjectName( anObjectName );
181
182   QString aFileName = aPanel->myFileName->text();
183   if ( aFileName.isEmpty() )
184   {
185     abort();
186     return;
187   }
188
189   QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
190
191   if (anExt == "shp")
192   {
193     SHPHandle aHSHP;
194     aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
195     Parse(aHSHP);
196   
197     startDocOperation();
198     QStringList aPolygonsList;
199     for (int i = 0; i < mySHPObjects.size(); i++)
200       aPolygonsList.append("polygon_" + QString::number(i));
201     aPanel->setPolylineNames(aPolygonsList);
202  
203     TopoDS_Compound cmp;
204     BRep_Builder BB;
205     BB.MakeCompound(cmp);
206     TopoDS_Face F;
207     if (aHSHP->nShapeType == 5)
208     {
209       for (int i = 0; i < 10/*mySHPObjects.size()*/; i++) {
210          ProcessSHP(mySHPObjects[i], i, F);
211          BB.Add(cmp, F);
212       }
213       ///to hydro_landcover
214       // BRepTools::Write(cmp, "d:/h1.brep");
215     }
216     else
217       SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
218     
219     commitDocOperation();
220     
221     for (size_t i = 0; i < mySHPObjects.size(); i++ )
222       free (mySHPObjects[i]);
223
224     mySHPObjects.clear();
225     SHPClose(aHSHP);
226   }
227   
228 }
229
230
231