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