Salome HOME
SHP Poly 1
[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
57
58 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
59 : HYDROGUI_Operation( theModule )
60 {
61   setName( tr( "IMPORT_LANDCOVER" ) );
62 }
63
64 HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
65 {
66 }
67
68 void HYDROGUI_ImportLandCoverOp::startOperation()
69 {
70   HYDROGUI_Operation::startOperation();
71
72   myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
73   myFileDlg->setWindowTitle( getName() );
74   myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
75   myFileDlg->setFilter( tr("LANDCOVER_FILTER") );
76
77   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
78   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
79
80   myFileDlg->exec();
81 }
82
83 void HYDROGUI_ImportLandCoverOp::onApply()
84 {
85   if ( !myFileDlg )
86   {
87     abort();
88     return;
89   }
90
91   QString aFileName = myFileDlg->selectedFile();
92   if ( aFileName.isEmpty() )
93   {
94     abort();
95     return;
96   }
97
98   QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
99
100   if (anExt == "shp")
101   {
102     SHPHandle aHSHP;
103     aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
104     Parse(aHSHP);
105     std::map<QListWidgetItem*, SHPObject*> aWItemToSObjectMap;
106     HYDROGUI_ImportLandCoverDlg anImportLandCoverDlg( mySHPObjects, aWItemToSObjectMap, module()->getApp()->desktop() );
107     if ( anImportLandCoverDlg.exec() == HYDROGUI_ImportLandCoverDlg::Accepted )
108     {
109       QApplication::setOverrideCursor( Qt::WaitCursor );
110       
111       startDocOperation();
112
113       int T = anImportLandCoverDlg.GetSelectedItems().size();
114  
115       TopoDS_Compound cmp;
116       BRep_Builder BB;
117       BB.MakeCompound(cmp);
118       TopoDS_Face F;
119       if (aHSHP->nShapeType == 5)
120       {
121         for (int i = 0; i < this->mySHPObjects.size(); i++) {
122            ProcessSHP(mySHPObjects[i], i, F);
123            BB.Add(cmp, F);
124         }
125         ///to hydro_landcover
126         // BRepTools::Write(cmp, "d:/h1.brep");
127       }
128       else
129         SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
130       commitDocOperation();
131       commit();
132     }
133     
134     for (size_t i = 0; i < mySHPObjects.size(); i++ )
135       free (mySHPObjects[i]);
136
137     mySHPObjects.clear();
138     SHPClose(aHSHP);
139   }
140
141   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
142   
143   QApplication::restoreOverrideCursor();
144 }
145
146
147
148 void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
149 {
150   int aShapeType;
151   mySHPObjects.clear();
152   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
153   if (aShapeType == 5) 
154   {
155     for (int i = 0; i < theHandle->nRecords; i++) 
156       mySHPObjects.push_back(SHPReadObject(theHandle, i));
157   }
158 }
159
160 void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
161 {
162   TopoDS_Wire W;
163   TopoDS_Edge E; 
164   int nParts = anObj->nParts;
165   gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
166   BRepBuilderAPI_MakeFace aFBuilder(pln);
167   for ( int i = 0 ; i < nParts ; i++ )
168   { 
169     BRepBuilderAPI_MakeWire aBuilder;
170     int StartIndex = anObj->panPartStart[i];
171     int EndIndex;
172     if (i != nParts - 1)
173       EndIndex = anObj->panPartStart[i + 1];
174     else
175       EndIndex = anObj->nVertices;
176
177     for ( int k = StartIndex; k < EndIndex - 1  ; k++ )
178     {
179       gp_Pnt2d P1 (anObj->padfX[k], anObj->padfY[k]);
180       gp_Pnt2d P2 (anObj->padfX[k+1], anObj->padfY[k+1]);
181       BRepBuilderAPI_MakeEdge2d aMakeEdge(P1, P2);
182       aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
183     }
184     aBuilder.Build();
185     W = TopoDS::Wire(aBuilder.Shape());
186     aFBuilder.Add(W);
187   }
188
189   aFBuilder.Build();
190   F = aFBuilder.Face();
191
192   //TODO build curve 3d
193
194   /*aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
195   aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
196   
197   aPolylineXY->Update();
198   
199   size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
200   if ( anActiveViewId == 0 )
201     anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
202   
203   module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
204   
205   module()->setIsToUpdate( aPolylineXY );*/
206 }