--- /dev/null
- SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
+ // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
+ //
+ // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+ // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+ //
+ // This library is free software; you can redistribute it and/or
+ // modify it under the terms of the GNU Lesser General Public
+ // License as published by the Free Software Foundation; either
+ // version 2.1 of the License, or (at your option) any later version.
+ //
+ // This library is distributed in the hope that it will be useful,
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ // Lesser General Public License for more details.
+ //
+ // You should have received a copy of the GNU Lesser General Public
+ // License along with this library; if not, write to the Free Software
+ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ //
+ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+ //
+
+ #include "HYDROGUI_ImportLandCoverOp.h"
+
+ #include "HYDROGUI_DataModel.h"
+ #include "HYDROGUI_Module.h"
+ #include "HYDROGUI_UpdateFlags.h"
+ #include "HYDROGUI_Tool.h"
+ #include "HYDROGUI_ImportLandCoverDlg.h"
+ #include "HYDROGUI_Shape.h"
+ #include <HYDROData_LandCover.h>
+ #include <HYDROGUI_ZLayers.h>
+
+ #include <HYDROGUI_DataObject.h>
+ #include <HYDROData_Iterator.h>
+
+ #include <HYDROData_Profile.h>
+
+ #include <SUIT_Desktop.h>
+ #include <SUIT_FileDlg.h>
+ #include <LightApp_Application.h>
+
+ #include <QApplication>
+ #include <QFile>
+ #include <QFileInfo>
+ #include <SUIT_MessageBox.h>
+
+ #include <OCCViewer_ViewManager.h>
+ #include <OCCViewer_ViewModel.h>
+
+ #include <SalomeApp_Study.h>
+
+ #include <LightApp_Application.h>
+ #include <LightApp_DataOwner.h>
+ #include <LightApp_Displayer.h>
+ #include <LightApp_SelectionMgr.h>
+
+ #include <SUIT_Desktop.h>
+ #include <SUIT_ViewManager.h>
+
+ #include <BRep_Builder.hxx>
+ #include <TopoDS.hxx>
+ #include <TopoDS_Shape.hxx>
+ #include <TopoDS_Wire.hxx>
+ #include <BRepBuilderAPI_MakeEdge.hxx>
+ #include <BRepBuilderAPI_MakeWire.hxx>
+ #include <BRepBuilderAPI_MakeFace.hxx>
+ #include <gp_Pln.hxx>
+ #include <BRepLib.hxx>
+ #include <ShapeFix_Shape.hxx>
+
+
+ HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
+ : HYDROGUI_Operation( theModule )
+ {
+ setName( tr( "IMPORT_LANDCOVER" ) );
+ }
+
+ HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
+ {
+ erasePreview();
+ }
+
+ void HYDROGUI_ImportLandCoverOp::startOperation()
+ {
+ HYDROGUI_Operation::startOperation();
+
+ if ( !getPreviewManager() ) {
+ setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
+ module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
+ }
+
+ if ( !isApplyAndClose() ) {
+ return;
+ }
+
+ HYDROGUI_ImportLandCoverDlg* aPanel =
+ ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
+ if ( !aPanel ) {
+ return;
+ }
+
+ aPanel->reset();
+ }
+
+
+ HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
+ {
+ HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverDlg( module(), getName() );
+
+ connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
+
+ connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
+
+ return aPanel;
+ }
+
+ bool HYDROGUI_ImportLandCoverOp::processApply( int& theUpdateFlags,
+ QString& theErrorMsg,
+ QStringList& theBrowseObjectsEntries )
+ {
+
+ HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
+ if ( !aPanel ) {
+ return false;
+ }
+
+ QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
+ aPanel->removePolygonNames( aSelectedtPolygons );
+
+ if (!aSelectedtPolygons.empty())
+ {
+ Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
+ TopoDS_Shape aResShape;
+ if (aSelectedtPolygons.size() > 1)
+ {
+ TopoDS_Compound cmp;
+ BRep_Builder BB;
+ BB.MakeCompound(cmp);
+
+ foreach ( QString aName, aSelectedtPolygons ) {
+ TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
+ if ( aShape.IsNull() )
+ continue;
+ BB.Add(cmp, aShape);
+ HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
+ delete aShapeToDelete;
+ }
+ aResShape = cmp;
+ }
+ else
+ {
+ TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
+ if ( !aShape.IsNull() )
+ {
+ HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
+ delete aShapeToDelete;
+ aResShape = aShape;
+ }
+ }
+ if( !aLC.IsNull() )
+ {
+ QString aLCName = aPanel->getObjectName() + "_polygon";
+ int i = 0;
+ for( ;HYDROGUI_Tool::FindObjectByName(module(), aLCName); i++)
+ aLCName = aPanel->getObjectName() + "_polygon_" + QString::number(i);
+ aLC->SetName( aLCName );
+ aLC->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
+ aLC->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
+
+ aLC->setShape(aResShape);
+ aLC->Show();
+
+ //erasePreview();
+
+ module()->setIsToUpdate( aLC );
+
+ }
+ }
+
+ module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
+
+ if ( isApplyAndClose() )
+ erasePreview();
+
+ return true;
+ }
+
+
+ void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
+ {
+ int aShapeType;
+ mySHPObjects.clear();
+ SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
+ if (aShapeType == 5)
+ {
+ for (int i = 0; i < theHandle->nRecords; i++)
+ mySHPObjects.push_back(SHPReadObject(theHandle, i));
+ }
+ }
+
+ void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
+ {
+ TopoDS_Wire W;
+ TopoDS_Edge E;
+ int nParts = anObj->nParts;
+ gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
+ BRepBuilderAPI_MakeFace aFBuilder(pln);
+
+ //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
+ //sfs->FixFaceTool()->FixOrientationMode() = 1;
+
+ for ( int i = 0 ; i < nParts ; i++ )
+ {
+ BRepBuilderAPI_MakeWire aBuilder;
+ int StartIndex = anObj->panPartStart[i];
+ int EndIndex;
+ if (i != nParts - 1)
+ EndIndex = anObj->panPartStart[i + 1];
+ else
+ EndIndex = anObj->nVertices;
+
+ for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
+ {
+ gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
+ gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
+ if (P1.Distance(P2) < Precision::Confusion())
+ continue;
+ BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
+ aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
+ }
+
+ aBuilder.Build();
+ W = TopoDS::Wire(aBuilder.Shape());
+ W.Reverse();
+ aFBuilder.Add(W);
+ }
+
+ aFBuilder.Build();
+ TopoDS_Face DF = aFBuilder.Face();
+ BRepLib::BuildCurves3d(DF);
+ bool IsInf = DF.Infinite();
+ if(!DF.IsNull())
+ {
+ //sfs->Init ( DF );
+ //sfs->Perform();
+ F = DF; //TopoDS::Face(sfs->Shape());
+ }
+ }
+
+ void HYDROGUI_ImportLandCoverOp::onFileSelected()
+ {
+ HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
+ if ( !aPanel )
+ return;
+
+ QString anObjectName = aPanel->getObjectName().simplified();
+ anObjectName = aPanel->getFileName();
+ if ( !anObjectName.isEmpty() )
+ anObjectName = QFileInfo( anObjectName ).baseName();
+
+ if ( anObjectName.isEmpty() )
+ anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVER_NAME" ) );
+ aPanel->setObjectName( anObjectName );
+
+ QString aFileName = aPanel->getFileName();
+ if ( aFileName.isEmpty() )
+ {
+ abort();
+ return;
+ }
+
+ QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
+
+ if (anExt == "shp")
+ {
+ SHPHandle aHSHP;
+ aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
+ Parse(aHSHP);
+
+ startDocOperation();
+ QStringList aPolygonsList;
+ for (int i = 0; i < mySHPObjects.size(); i++)
+ aPolygonsList.append("polygon_" + QString::number(i + 1));
+ aPanel->setPolygonNames(aPolygonsList);
+
+ SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
+ if ( !aStudy ) {
+ return;
+ }
+
+ erasePreview();
+
+ Handle(AIS_InteractiveContext) aCtx = NULL;
+
+ LightApp_Application* anApp = module()->getApp();
+ if ( !getPreviewManager() )
+ setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
+ OCCViewer_ViewManager* aViewManager = getPreviewManager();
+
+ if ( aViewManager )
+ {
+ if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
+ {
+ aCtx = aViewer->getAISContext();
+ connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
+ }
+ }
+
+ QApplication::setOverrideCursor(Qt::WaitCursor);
+
+ TopTools_SequenceOfShape aFaces;
+ TopoDS_Face aF;
+ if (aHSHP->nShapeType == 5)
+ {
+ for (int i = 0; i < mySHPObjects.size(); i++)
+ {
+ ProcessSHP(mySHPObjects[i], i, aF);
+ aFaces.Append(aF);
+ }
+ }
+ else
++ SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), "Cannot import land cover;\nThe shape type is not polygon" );
+ QApplication::restoreOverrideCursor();
+
+ Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
+ for ( int i = 1; i <= aFaces.Length(); i++ )
+ {
+ TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
+
+ aLC->setShape( aFace );
+
+ if ( aViewManager && !aCtx.IsNull() )
+ {
+ HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
+
+ aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
+ aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
+ if( !aFace.IsNull() )
+ aShape->setShape( aLC->GetShape() );
+ myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
+ }
+ }
+ aLC->Remove();
+
+ if ( !aCtx.IsNull() ) {
+ UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
+ aCtx->UpdateCurrentViewer();
+ }
+
+ commitDocOperation();
+
+ for (size_t i = 0; i < mySHPObjects.size(); i++ )
+ free (mySHPObjects[i]);
+
+ mySHPObjects.clear();
+ SHPClose(aHSHP);
+ }
+
+ }
+
+ void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
+ {
+ Handle(AIS_InteractiveContext) aCtx = NULL;
+
+ OCCViewer_ViewManager* aViewManager = getPreviewManager();
+ if ( aViewManager ) {
+ if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
+ aCtx = aViewer->getAISContext();
+ }
+ }
+
+ if ( !aCtx.IsNull() ) {
+ foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
+ Handle(AIS_InteractiveObject) anObject =
+ myPolygonName2PrsShape.value(aName)->getAISObject();
+
+ bool isSelected = theSelectedNames.contains( aName );
+ if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
+ ( !isSelected && aCtx->IsSelected( anObject) ) ) {
+ aCtx->AddOrRemoveSelected( anObject, Standard_False );
+ }
+ }
+ aCtx->UpdateCurrentViewer();
+ }
+ }
+
+
+ void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
+ {
+ // Get panel
+ HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
+ if ( !aPanel ) {
+ return;
+ }
+
+ OCCViewer_ViewManager* aViewManager = getPreviewManager();
+ Handle(AIS_InteractiveContext) aCtx = NULL;
+ if ( aViewManager ) {
+ if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
+ aCtx = aViewer->getAISContext();
+ }
+ }
+
+ if ( !aCtx.IsNull() )
+ {
+ QStringList aSelectedNames;
+ foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
+ bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
+ if ( isSelected ) {
+ aSelectedNames << aName;
+ }
+ }
+ aPanel->setSelectedPolygonNames( aSelectedNames );
+ }
+ }
+
+
+ void HYDROGUI_ImportLandCoverOp::erasePreview()
+ {
+ foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
+ delete aShape;
+ }
+
+ myPolygonName2PrsShape.clear();
+ }
+
+
+ void HYDROGUI_ImportLandCoverOp::abortOperation()
+ {
+ LightApp_Application* anApp = module()->getApp();
+ if ( anApp ) {
+ anApp->disconnect( this );
+ }
+
+ erasePreview();
+
+ HYDROGUI_Operation::abortOperation();
+ }
+
+