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