Salome HOME
refs #585: polylines operations (split/merge)
[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       BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
228       aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
229     }
230     
231     aBuilder.Build();
232     W = TopoDS::Wire(aBuilder.Shape());
233     W.Reverse();
234     aFBuilder.Add(W);
235   }
236
237   aFBuilder.Build();
238   TopoDS_Face DF = aFBuilder.Face();
239   BRepLib::BuildCurves3d(DF);
240   if(!DF.IsNull()) 
241   {
242     //sfs->Init ( DF );
243     //sfs->Perform();
244     F = DF; //TopoDS::Face(sfs->Shape());
245   }
246 }
247
248 void HYDROGUI_ImportLandCoverOp::onFileSelected()
249 {
250   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
251   if ( !aPanel )
252     return;
253   
254   QString anObjectName = aPanel->getObjectName().simplified();
255   anObjectName = aPanel->getFileName();
256   if ( !anObjectName.isEmpty() ) 
257       anObjectName = QFileInfo( anObjectName ).baseName();
258
259   if ( anObjectName.isEmpty() ) 
260     anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVER_NAME" ) );
261   aPanel->setObjectName( anObjectName );
262
263   QString aFileName = aPanel->getFileName();
264   if ( aFileName.isEmpty() )
265   {
266     abort();
267     return;
268   }
269
270   QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
271
272   if (anExt == "shp")
273   {
274     SHPHandle aHSHP;
275     aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
276     Parse(aHSHP);
277     
278     startDocOperation();
279     QStringList aPolygonsList;
280     for (int i = 1; i < mySHPObjects.size(); i++)
281       aPolygonsList.append("polygon_" + QString::number(i));
282     aPanel->setPolygonNames(aPolygonsList);
283
284     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
285     if ( !aStudy ) {
286       return;
287     }
288
289     erasePreview();
290
291     Handle(AIS_InteractiveContext) aCtx = NULL;
292
293     LightApp_Application* anApp = module()->getApp();
294     if ( !getPreviewManager() )
295       setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
296     OCCViewer_ViewManager* aViewManager = getPreviewManager();
297
298     if ( aViewManager )
299     {
300       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
301       {
302         aCtx = aViewer->getAISContext();
303         connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
304       }
305     }
306
307     QApplication::setOverrideCursor(Qt::WaitCursor);
308
309     TopTools_SequenceOfShape aFaces;
310     TopoDS_Face aF;
311     if (aHSHP->nShapeType == 5)
312     {
313       for (int i = 0; i < mySHPObjects.size(); i++) 
314       {
315          ProcessSHP(mySHPObjects[i], i, aF);
316          aFaces.Append(aF);
317       }
318     }
319     else
320       SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
321     QApplication::restoreOverrideCursor();
322
323     Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
324     for ( int i = 1; i <= aFaces.Length(); i++ ) 
325     {
326       TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
327
328       aLC->setShape( aFace );      
329       
330       if ( aViewManager && !aCtx.IsNull() )
331       {
332         HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
333
334         aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
335         aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
336         if( !aFace.IsNull() )
337           aShape->setShape( aLC->GetShape() );
338         myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
339       }
340     }
341     aLC->Remove();
342
343     if ( !aCtx.IsNull() ) {
344       UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
345       aCtx->UpdateCurrentViewer();
346     }
347
348     commitDocOperation();
349     
350     for (size_t i = 0; i < mySHPObjects.size(); i++ )
351       free (mySHPObjects[i]);
352
353     mySHPObjects.clear();
354     SHPClose(aHSHP);
355   }
356   
357 }
358
359 void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
360 {
361   Handle(AIS_InteractiveContext) aCtx = NULL;
362
363   OCCViewer_ViewManager* aViewManager = getPreviewManager();
364   if ( aViewManager ) {
365     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
366       aCtx = aViewer->getAISContext();
367     }
368   }
369
370   if ( !aCtx.IsNull() ) {
371     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
372       Handle(AIS_InteractiveObject) anObject = 
373         myPolygonName2PrsShape.value(aName)->getAISObject();
374
375       bool isSelected = theSelectedNames.contains( aName );
376       if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
377            ( !isSelected && aCtx->IsSelected( anObject) ) ) {
378         aCtx->AddOrRemoveSelected( anObject, Standard_False );
379       }
380     }
381     aCtx->UpdateCurrentViewer();
382   }
383 }
384
385
386 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
387 {
388   // Get panel
389   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
390   if ( !aPanel ) {
391     return;
392   }
393
394   OCCViewer_ViewManager* aViewManager = getPreviewManager();
395   Handle(AIS_InteractiveContext) aCtx = NULL;
396   if ( aViewManager ) {
397     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
398       aCtx = aViewer->getAISContext();
399     }
400   }
401   
402   if ( !aCtx.IsNull() )
403   {
404     QStringList aSelectedNames;
405     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
406       bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
407       if ( isSelected ) {
408         aSelectedNames << aName;
409       }
410     }
411     aPanel->setSelectedPolygonNames( aSelectedNames );
412   }
413 }
414
415
416 void HYDROGUI_ImportLandCoverOp::erasePreview()
417 {
418   foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
419     delete aShape;
420   }
421
422   myPolygonName2PrsShape.clear();
423 }
424
425
426 void HYDROGUI_ImportLandCoverOp::abortOperation()
427 {
428   LightApp_Application* anApp = module()->getApp();
429   if ( anApp ) {
430     anApp->disconnect( this );
431   }
432
433   erasePreview();
434
435   HYDROGUI_Operation::abortOperation();
436 }
437
438