Salome HOME
Import of SHP polygons (draft)
[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 #include "HYDROGUI_Shape.h"
31 #include <HYDROData_LandCover.h>
32 #include <HYDROGUI_ZLayers.h>
33
34 #include <HYDROGUI_DataObject.h>
35 #include <HYDROData_Bathymetry.h>
36 #include <HYDROData_Iterator.h>
37
38 #include <HYDROData_Profile.h>
39
40 #include <SUIT_Desktop.h>
41 #include <SUIT_FileDlg.h>
42 #include <LightApp_Application.h>
43
44 #include <QApplication>
45 #include <QFile>
46 #include <QFileInfo>
47 #include <SUIT_MessageBox.h>
48
49 #include <OCCViewer_ViewManager.h>
50 #include <OCCViewer_ViewModel.h>
51
52 #include <SalomeApp_Study.h>
53
54 #include <LightApp_Application.h>
55 #include <LightApp_DataOwner.h>
56 #include <LightApp_Displayer.h>
57 #include <LightApp_SelectionMgr.h>
58
59 #include <SUIT_Desktop.h>
60 #include <SUIT_ViewManager.h>
61
62 #include <BRep_Builder.hxx>
63 #include <TopoDS.hxx>
64 #include <TopoDS_Shape.hxx>
65 #include <TopoDS_Wire.hxx>
66 #include <BRepBuilderAPI_MakeEdge.hxx>
67 #include <BRepBuilderAPI_MakeWire.hxx>
68 #include <BRepBuilderAPI_MakeFace.hxx>
69 #include <gp_Pln.hxx>
70 #include <BRepLib.hxx>
71 #include <ShapeFix_Shape.hxx>
72
73
74 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
75 : HYDROGUI_Operation( theModule )
76 {
77   setName( tr( "IMPORT_LANDCOVER" ) );
78 }
79
80 HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
81 {
82
83 }
84
85 void HYDROGUI_ImportLandCoverOp::startOperation()
86 {
87   HYDROGUI_Operation::startOperation();
88 }
89
90
91 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
92 {
93   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverDlg( module(), getName() );
94
95   connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
96
97   connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
98
99   return aPanel;
100 }
101
102 void HYDROGUI_ImportLandCoverOp::onApply()
103 {
104
105   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
106   if ( !aPanel ) {
107     return;
108   }
109   
110   QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
111   aPanel->removePolygonNames( aSelectedtPolygons );
112     
113   if (!aSelectedtPolygons.empty())
114   {
115     Handle(HYDROData_LandCover) aLC =  Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
116     TopoDS_Shape aResShape;
117     if (aSelectedtPolygons.size() > 1) 
118     {
119       TopoDS_Compound cmp;
120       BRep_Builder BB;
121       BB.MakeCompound(cmp);
122
123       foreach ( QString aName, aSelectedtPolygons ) {
124         TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
125         if ( aShape.IsNull() ) 
126           continue;
127         BB.Add(cmp, aShape);
128         HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
129         delete aShapeToDelete;
130       }
131       aResShape = cmp;
132     }
133     else
134     {         
135       TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
136       if ( !aShape.IsNull() ) 
137       {
138         HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
139         delete aShapeToDelete;
140        aResShape = aShape;
141       }
142     }
143     if( !aLC.IsNull() ) 
144     {
145       aLC->SetName( aPanel->getFileName() + "_polygon_" );
146       aLC->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
147       aLC->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
148       
149       aLC->setShape(aResShape);
150       aLC->Show();
151             
152       //erasePreview();
153       
154       module()->setIsToUpdate( aLC );
155       
156     }
157   }
158  
159   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
160   
161   QApplication::restoreOverrideCursor();
162 }
163
164
165
166 void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
167 {
168   int aShapeType;
169   mySHPObjects.clear();
170   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
171   if (aShapeType == 5) 
172   {
173     for (int i = 0; i < theHandle->nRecords; i++) 
174       mySHPObjects.push_back(SHPReadObject(theHandle, i));
175   }
176 }
177
178 void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
179 {
180   TopoDS_Wire W;
181   TopoDS_Edge E; 
182   int nParts = anObj->nParts;
183   gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
184   BRepBuilderAPI_MakeFace aFBuilder(pln);
185
186   Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
187   sfs->FixFaceTool()->FixOrientationMode() = 1;
188  
189   for ( int i = 0 ; i < nParts ; i++ )
190   { 
191     BRepBuilderAPI_MakeWire aBuilder;
192     int StartIndex = anObj->panPartStart[i];
193     int EndIndex;
194     if (i != nParts - 1)
195       EndIndex = anObj->panPartStart[i + 1];
196     else
197       EndIndex = anObj->nVertices;
198
199     for ( int k = StartIndex; k < EndIndex - 1  ; k++ )
200     {
201       gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
202       gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
203       BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
204       aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
205     }
206     
207     aBuilder.Build();
208     W = TopoDS::Wire(aBuilder.Shape());
209     W.Reverse();
210     aFBuilder.Add(W);
211   }
212
213   aFBuilder.Build();
214   TopoDS_Face DF = aFBuilder.Face();
215   BRepLib::BuildCurves3d(DF);
216   if(!DF.IsNull()) 
217   {
218     sfs->Init ( DF );
219     sfs->Perform();
220     F = TopoDS::Face(sfs->Shape());
221   }
222 }
223
224 void HYDROGUI_ImportLandCoverOp::onFileSelected()
225 {
226   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
227   if ( !aPanel )
228     return;
229
230   QString anObjectName = aPanel->getObjectName().simplified();
231    anObjectName = aPanel->getFileName();
232    if ( !anObjectName.isEmpty() ) {
233        anObjectName = QFileInfo( anObjectName ).baseName();
234    }
235
236    if ( anObjectName.isEmpty() ) {
237      anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
238    }
239    aPanel->setObjectName( anObjectName );
240
241    QString aFileName = aPanel->getFileName();
242   if ( aFileName.isEmpty() )
243   {
244     abort();
245     return;
246   }
247
248   QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
249
250   if (anExt == "shp")
251   {
252     SHPHandle aHSHP;
253     aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
254     Parse(aHSHP);
255   
256     startDocOperation();
257     QStringList aPolygonsList;
258     for (int i = 1; i < mySHPObjects.size(); i++)
259       aPolygonsList.append("polygon_" + QString::number(i));
260     aPanel->setPolygonNames(aPolygonsList);
261
262     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
263     if ( !aStudy ) {
264       return;
265     }
266
267     erasePreview();
268
269     Handle(AIS_InteractiveContext) aCtx = NULL;
270
271     LightApp_Application* anApp = module()->getApp();
272     if ( !getPreviewManager() )
273       setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
274     OCCViewer_ViewManager* aViewManager = getPreviewManager();
275
276     if ( aViewManager )
277     {
278       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
279       {
280         aCtx = aViewer->getAISContext();
281         connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
282       }
283     }
284
285     TopTools_SequenceOfShape aFaces;
286     TopoDS_Face aF;
287     if (aHSHP->nShapeType == 5)
288     {
289       for (int i = 1; i < 4 /*mySHPObjects.size()*/; i++) {   ///TODO temp
290          ProcessSHP(mySHPObjects[i], i, aF);
291          aFaces.Append(aF);
292       }
293     }
294     else
295       SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
296     
297
298     Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
299     for ( int i = 1; i <= aFaces.Length(); i++ ) 
300     {
301       TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
302
303       aLC->setShape( aFace );      
304       
305       if ( aViewManager && !aCtx.IsNull() )
306       {
307         HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
308
309         aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
310         aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
311         if( !aFace.IsNull() )
312           aShape->setShape( aLC->GetShape() );
313         myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
314
315       }
316
317     }
318     aLC->Remove();
319
320     if ( !aCtx.IsNull() ) {
321       UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
322       aCtx->UpdateCurrentViewer();
323     }
324
325     commitDocOperation();
326     
327     for (size_t i = 0; i < mySHPObjects.size(); i++ )
328       free (mySHPObjects[i]);
329
330     mySHPObjects.clear();
331     SHPClose(aHSHP);
332   }
333   
334 }
335
336 void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
337 {
338   Handle(AIS_InteractiveContext) aCtx = NULL;
339
340   OCCViewer_ViewManager* aViewManager = getPreviewManager();
341   if ( aViewManager ) {
342     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
343       aCtx = aViewer->getAISContext();
344     }
345   }
346
347   if ( !aCtx.IsNull() ) {
348     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
349       Handle(AIS_InteractiveObject) anObject = 
350         myPolygonName2PrsShape.value(aName)->getAISObject();
351
352       bool isSelected = theSelectedNames.contains( aName );
353       if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
354            ( !isSelected && aCtx->IsSelected( anObject) ) ) {
355         aCtx->AddOrRemoveSelected( anObject, Standard_False );
356       }
357     }
358     aCtx->UpdateCurrentViewer();
359   }
360 }
361
362
363 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
364 {
365   // Get panel
366   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
367   if ( !aPanel ) {
368     return;
369   }
370
371   OCCViewer_ViewManager* aViewManager = getPreviewManager();
372   Handle(AIS_InteractiveContext) aCtx = NULL;
373   if ( aViewManager ) {
374     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
375       aCtx = aViewer->getAISContext();
376     }
377   }
378   
379   if ( !aCtx.IsNull() ) {
380     QStringList aSelectedNames;
381
382     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
383       bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
384       if ( isSelected ) {
385         aSelectedNames << aName;
386       }
387     }
388
389     aPanel->setSelectedPolygonNames( aSelectedNames );
390   }
391 }
392
393
394 void HYDROGUI_ImportLandCoverOp::erasePreview()
395 {
396   foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
397     delete aShape;
398   }
399
400   myPolygonName2PrsShape.clear();
401 }
402
403
404 void HYDROGUI_ImportLandCoverOp::abortOperation()
405 {
406   LightApp_Application* anApp = module()->getApp();
407   if ( anApp ) {
408     anApp->disconnect( this );
409   }
410
411   erasePreview();
412
413   HYDROGUI_Operation::abortOperation();
414 }
415
416