1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImportLandCoverOp.h"
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>
34 #include <HYDROGUI_DataObject.h>
35 #include <HYDROData_Bathymetry.h>
36 #include <HYDROData_Iterator.h>
38 #include <HYDROData_Profile.h>
40 #include <SUIT_Desktop.h>
41 #include <SUIT_FileDlg.h>
42 #include <LightApp_Application.h>
44 #include <QApplication>
47 #include <SUIT_MessageBox.h>
49 #include <OCCViewer_ViewManager.h>
50 #include <OCCViewer_ViewModel.h>
52 #include <SalomeApp_Study.h>
54 #include <LightApp_Application.h>
55 #include <LightApp_DataOwner.h>
56 #include <LightApp_Displayer.h>
57 #include <LightApp_SelectionMgr.h>
59 #include <SUIT_Desktop.h>
60 #include <SUIT_ViewManager.h>
62 #include <BRep_Builder.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>
70 #include <BRepLib.hxx>
71 #include <ShapeFix_Shape.hxx>
74 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
75 : HYDROGUI_Operation( theModule )
77 setName( tr( "IMPORT_LANDCOVER" ) );
80 HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
85 void HYDROGUI_ImportLandCoverOp::startOperation()
87 HYDROGUI_Operation::startOperation();
91 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
93 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverDlg( module(), getName() );
95 connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
97 connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
102 void HYDROGUI_ImportLandCoverOp::onApply()
105 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
110 QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
111 aPanel->removePolygonNames( aSelectedtPolygons );
113 if (!aSelectedtPolygons.empty())
115 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
116 TopoDS_Shape aResShape;
117 if (aSelectedtPolygons.size() > 1)
121 BB.MakeCompound(cmp);
123 foreach ( QString aName, aSelectedtPolygons ) {
124 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
125 if ( aShape.IsNull() )
128 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
129 delete aShapeToDelete;
135 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
136 if ( !aShape.IsNull() )
138 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
139 delete aShapeToDelete;
145 aLC->SetName( aPanel->getFileName() + "_polygon_" );
146 aLC->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
147 aLC->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
149 aLC->setShape(aResShape);
154 module()->setIsToUpdate( aLC );
159 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
161 QApplication::restoreOverrideCursor();
166 void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
169 mySHPObjects.clear();
170 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
173 for (int i = 0; i < theHandle->nRecords; i++)
174 mySHPObjects.push_back(SHPReadObject(theHandle, i));
178 void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
182 int nParts = anObj->nParts;
183 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
184 BRepBuilderAPI_MakeFace aFBuilder(pln);
186 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
187 sfs->FixFaceTool()->FixOrientationMode() = 1;
189 for ( int i = 0 ; i < nParts ; i++ )
191 BRepBuilderAPI_MakeWire aBuilder;
192 int StartIndex = anObj->panPartStart[i];
195 EndIndex = anObj->panPartStart[i + 1];
197 EndIndex = anObj->nVertices;
199 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
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()));
208 W = TopoDS::Wire(aBuilder.Shape());
214 TopoDS_Face DF = aFBuilder.Face();
215 BRepLib::BuildCurves3d(DF);
220 F = TopoDS::Face(sfs->Shape());
224 void HYDROGUI_ImportLandCoverOp::onFileSelected()
226 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
230 QString anObjectName = aPanel->getObjectName().simplified();
231 anObjectName = aPanel->getFileName();
232 if ( !anObjectName.isEmpty() ) {
233 anObjectName = QFileInfo( anObjectName ).baseName();
236 if ( anObjectName.isEmpty() ) {
237 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
239 aPanel->setObjectName( anObjectName );
241 QString aFileName = aPanel->getFileName();
242 if ( aFileName.isEmpty() )
248 QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
253 aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
257 QStringList aPolygonsList;
258 for (int i = 1; i < mySHPObjects.size(); i++)
259 aPolygonsList.append("polygon_" + QString::number(i));
260 aPanel->setPolygonNames(aPolygonsList);
262 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
269 Handle(AIS_InteractiveContext) aCtx = NULL;
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();
278 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
280 aCtx = aViewer->getAISContext();
281 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
285 TopTools_SequenceOfShape aFaces;
287 if (aHSHP->nShapeType == 5)
289 for (int i = 1; i < 4 /*mySHPObjects.size()*/; i++) { ///TODO temp
290 ProcessSHP(mySHPObjects[i], i, aF);
295 SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
298 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
299 for ( int i = 1; i <= aFaces.Length(); i++ )
301 TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
303 aLC->setShape( aFace );
305 if ( aViewManager && !aCtx.IsNull() )
307 HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
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);
320 if ( !aCtx.IsNull() ) {
321 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
322 aCtx->UpdateCurrentViewer();
325 commitDocOperation();
327 for (size_t i = 0; i < mySHPObjects.size(); i++ )
328 free (mySHPObjects[i]);
330 mySHPObjects.clear();
336 void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
338 Handle(AIS_InteractiveContext) aCtx = NULL;
340 OCCViewer_ViewManager* aViewManager = getPreviewManager();
341 if ( aViewManager ) {
342 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
343 aCtx = aViewer->getAISContext();
347 if ( !aCtx.IsNull() ) {
348 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
349 Handle(AIS_InteractiveObject) anObject =
350 myPolygonName2PrsShape.value(aName)->getAISObject();
352 bool isSelected = theSelectedNames.contains( aName );
353 if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
354 ( !isSelected && aCtx->IsSelected( anObject) ) ) {
355 aCtx->AddOrRemoveSelected( anObject, Standard_False );
358 aCtx->UpdateCurrentViewer();
363 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
366 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
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();
379 if ( !aCtx.IsNull() ) {
380 QStringList aSelectedNames;
382 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
383 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
385 aSelectedNames << aName;
389 aPanel->setSelectedPolygonNames( aSelectedNames );
394 void HYDROGUI_ImportLandCoverOp::erasePreview()
396 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
400 myPolygonName2PrsShape.clear();
404 void HYDROGUI_ImportLandCoverOp::abortOperation()
406 LightApp_Application* anApp = module()->getApp();
408 anApp->disconnect( this );
413 HYDROGUI_Operation::abortOperation();