From: isn Date: Tue, 23 Jun 2015 12:17:54 +0000 (+0300) Subject: Import of SHP polygons (draft) X-Git-Tag: v1.4.2~34^2~3 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=da492f56876a0e2403d68828c1082cf259102a91;p=modules%2Fhydro.git Import of SHP polygons (draft) --- diff --git a/src/HYDROData/HYDROData_LandCover.h b/src/HYDROData/HYDROData_LandCover.h index 398d6cdd..289e4bff 100644 --- a/src/HYDROData/HYDROData_LandCover.h +++ b/src/HYDROData/HYDROData_LandCover.h @@ -132,12 +132,13 @@ public: HYDRODATA_EXPORT static TopoDS_Shape buildShape( const HYDROData_SequenceOfObjects& thePolylines, TCollection_AsciiString& theErrorMsg ); -protected: /** * Sets the shape presentation of the land cover. */ HYDRODATA_EXPORT virtual void setShape( const TopoDS_Shape& theShape ); +protected: + /** * Removes the shape from data label of the land cover object. */ diff --git a/src/HYDROGUI/HYDROGUI_ImportLandCoverDlg.cxx b/src/HYDROGUI/HYDROGUI_ImportLandCoverDlg.cxx index b997bd4f..ed68774e 100644 --- a/src/HYDROGUI/HYDROGUI_ImportLandCoverDlg.cxx +++ b/src/HYDROGUI/HYDROGUI_ImportLandCoverDlg.cxx @@ -34,8 +34,7 @@ HYDROGUI_ImportLandCoverDlg::HYDROGUI_ImportLandCoverDlg( HYDROGUI_Module* theMo { SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr(); - // Import bathymetry from file - myFileNameGroup = new QGroupBox( tr( "IMPORT_POLYGON_FROM_FILE" ) ); + myFileNameGroup = new QGroupBox( tr( "IMPORT_LANDCOVER_FROM_FILE" ) ); QLabel* aFileNameLabel = new QLabel( tr( "LANCOVER_NAME" ), myFileNameGroup ); @@ -52,7 +51,6 @@ HYDROGUI_ImportLandCoverDlg::HYDROGUI_ImportLandCoverDlg( HYDROGUI_Module* theMo aFileNameLayout->addWidget( myFileName ); aFileNameLayout->addWidget( aBrowseBtn ); - // Bathymetry name myObjectNameGroup = new QGroupBox( tr( "FILE_NAME" ) ); QLabel* aBathymetryNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup ); @@ -64,25 +62,24 @@ HYDROGUI_ImportLandCoverDlg::HYDROGUI_ImportLandCoverDlg( HYDROGUI_Module* theMo aBathymetryNameLayout->addWidget( aBathymetryNameLabel ); aBathymetryNameLayout->addWidget( myObjectName ); - // List of recognized polylines - QGroupBox* aPolylinesGroup = new QGroupBox( tr( "FOUND_POLYGONS_OF_SHP_FILE" ), mainFrame() ); - myPolylines = new QListWidget( aPolylinesGroup ); - myPolylines->setSelectionMode( QListWidget::ExtendedSelection ); - myPolylines->setEditTriggers( QListWidget::NoEditTriggers ); - myPolylines->setViewMode( QListWidget::ListMode ); - myPolylines->setSortingEnabled( false ); - - QBoxLayout* aPolylinesLayout = new QVBoxLayout; - aPolylinesLayout->addWidget( myPolylines ); - aPolylinesGroup->setLayout( aPolylinesLayout ); + QGroupBox* aPolygonsGroup = new QGroupBox( tr( "FOUND_POLYGONS_OF_SHP_FILE" ), mainFrame() ); + myPolygons = new QListWidget( aPolygonsGroup ); + myPolygons->setSelectionMode( QListWidget::ExtendedSelection ); + myPolygons->setEditTriggers( QListWidget::NoEditTriggers ); + myPolygons->setViewMode( QListWidget::ListMode ); + myPolygons->setSortingEnabled( false ); + + QBoxLayout* aPolygonsLayout = new QVBoxLayout; + aPolygonsLayout->addWidget( myPolygons ); + aPolygonsGroup->setLayout( aPolygonsLayout ); // Layout addWidget( myFileNameGroup ); addWidget( myObjectNameGroup ); - addWidget( aPolylinesGroup ); + addWidget( aPolygonsGroup ); // Conections - connect( myPolylines, SIGNAL( itemSelectionChanged() ), this, SLOT( onItemSelectionChanged() ) ); + connect( myPolygons, SIGNAL( itemSelectionChanged() ), this, SLOT( onItemSelectionChanged() ) ); connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) ); } @@ -90,42 +87,36 @@ HYDROGUI_ImportLandCoverDlg::~HYDROGUI_ImportLandCoverDlg() { } -void HYDROGUI_ImportLandCoverDlg::setImageName( const QString& theName ) -{ - myImageName->setText( theName ); -} - void HYDROGUI_ImportLandCoverDlg::reset() { - myImageName->clear(); - myPolylines->clear(); + myPolygons->clear(); } -void HYDROGUI_ImportLandCoverDlg::setPolylineNames( const QStringList& theNames ) +void HYDROGUI_ImportLandCoverDlg::setPolygonNames( const QStringList& theNames ) { - myPolylines->clear(); - myPolylines->addItems( theNames ); + myPolygons->clear(); + myPolygons->addItems( theNames ); } -void HYDROGUI_ImportLandCoverDlg::removePolylineNames( const QStringList& theNames ) +void HYDROGUI_ImportLandCoverDlg::removePolygonNames( const QStringList& theNames ) { QList aFoundItems; foreach ( const QString& aName, theNames ) { - aFoundItems = myPolylines->findItems( aName, Qt::MatchExactly ); + aFoundItems = myPolygons->findItems( aName, Qt::MatchExactly ); foreach ( QListWidgetItem* anItem, aFoundItems ) { - anItem = myPolylines->takeItem( myPolylines->row( anItem ) ); + anItem = myPolygons->takeItem( myPolygons->row( anItem ) ); delete anItem; } } } -void HYDROGUI_ImportLandCoverDlg::setSelectedPolylineNames( const QStringList& theNames ) +void HYDROGUI_ImportLandCoverDlg::setSelectedPolygonNames( const QStringList& theNames ) { - myPolylines->clearSelection(); + myPolygons->clearSelection(); foreach( const QString aName, theNames ) { - QList anItems = myPolylines->findItems( aName, Qt::MatchExactly ); + QList anItems = myPolygons->findItems( aName, Qt::MatchExactly ); if ( anItems.count() == 1 ) { anItems.first()->setSelected( true ); } @@ -134,14 +125,14 @@ void HYDROGUI_ImportLandCoverDlg::setSelectedPolylineNames( const QStringList& t void HYDROGUI_ImportLandCoverDlg::onItemSelectionChanged() { - emit selectionChanged( getSelectedtPolylineNames() ); + emit selectionChanged( getSelectedPolygonNames() ); } -QStringList HYDROGUI_ImportLandCoverDlg::getSelectedtPolylineNames() const +QStringList HYDROGUI_ImportLandCoverDlg::getSelectedPolygonNames() const { QStringList aSelectedNames; - QList aSelectedItems = myPolylines->selectedItems(); + QList aSelectedItems = myPolygons->selectedItems(); foreach( const QListWidgetItem* anItem, aSelectedItems ) { aSelectedNames << anItem->text(); } diff --git a/src/HYDROGUI/HYDROGUI_ImportLandCoverDlg.h b/src/HYDROGUI/HYDROGUI_ImportLandCoverDlg.h index c9df0432..67482a90 100644 --- a/src/HYDROGUI/HYDROGUI_ImportLandCoverDlg.h +++ b/src/HYDROGUI/HYDROGUI_ImportLandCoverDlg.h @@ -33,16 +33,15 @@ class HYDROGUI_ImportLandCoverDlg : public HYDROGUI_InputPanel public: HYDROGUI_ImportLandCoverDlg( HYDROGUI_Module* theModule, const QString& theTitle ); virtual ~HYDROGUI_ImportLandCoverDlg(); - void setImageName( const QString& theName ); void reset(); - void setPolylineNames( const QStringList& theNames ); - void removePolylineNames( const QStringList& theNames ); + void setPolygonNames( const QStringList& theNames ); + void removePolygonNames( const QStringList& theNames ); - void setSelectedPolylineNames( const QStringList& theNames ); + void setSelectedPolygonNames( const QStringList& theNames ); - QStringList getSelectedtPolylineNames() const; + QStringList getSelectedPolygonNames() const; void setObjectName( const QString& theName ); QString getObjectName() const; @@ -62,20 +61,14 @@ signals: public slots: void onItemSelectionChanged(); -public: - - QLineEdit* myFileName; //temp private: - QLineEdit* myImageName; + QLineEdit* myFileName; QGroupBox* myFileNameGroup; -// QLineEdit* myFileName; - - QListWidget* myPolylines; + QListWidget* myPolygons; QGroupBox* myObjectNameGroup; QLineEdit* myObjectName; - }; #endif diff --git a/src/HYDROGUI/HYDROGUI_ImportLandcoverOp.cxx b/src/HYDROGUI/HYDROGUI_ImportLandcoverOp.cxx index 56ca8395..c67bd1ad 100644 --- a/src/HYDROGUI/HYDROGUI_ImportLandcoverOp.cxx +++ b/src/HYDROGUI/HYDROGUI_ImportLandcoverOp.cxx @@ -27,6 +27,9 @@ #include "HYDROGUI_UpdateFlags.h" #include "HYDROGUI_Tool.h" #include "HYDROGUI_ImportLandCoverDlg.h" +#include "HYDROGUI_Shape.h" +#include +#include #include #include @@ -43,17 +46,29 @@ #include #include +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include + #include #include #include #include -#include +#include #include #include -#include -#include #include -#include +#include +#include HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule ) @@ -64,21 +79,12 @@ HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModu HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp() { + } void HYDROGUI_ImportLandCoverOp::startOperation() { HYDROGUI_Operation::startOperation(); - - /*myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true ); - myFileDlg->setWindowTitle( getName() ); - myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles ); - myFileDlg->setFilter( tr("LANDCOVER_FILTER") ); - - connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) ); - connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) ); - - myFileDlg->exec();*/ } @@ -95,6 +101,61 @@ HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const void HYDROGUI_ImportLandCoverOp::onApply() { + + HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast( inputPanel() ); + if ( !aPanel ) { + return; + } + + 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() ) + { + aLC->SetName( aPanel->getFileName() + "_polygon_" ); + 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 ); QApplication::restoreOverrideCursor(); @@ -121,6 +182,10 @@ void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face 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; @@ -133,33 +198,27 @@ void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face for ( int k = StartIndex; k < EndIndex - 1 ; k++ ) { - gp_Pnt2d P1 (anObj->padfX[k], anObj->padfY[k]); - gp_Pnt2d P2 (anObj->padfX[k+1], anObj->padfY[k+1]); - BRepBuilderAPI_MakeEdge2d aMakeEdge(P1, P2); + gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0); + gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0); + 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(); - F = aFBuilder.Face(); - - //TODO build curve 3d - - /*aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() ); - aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) ); - - aPolylineXY->Update(); - - size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() ); - if ( anActiveViewId == 0 ) - anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() ); - - module()->setObjectVisible( anActiveViewId, aPolylineXY, true ); - - module()->setIsToUpdate( aPolylineXY );*/ + TopoDS_Face DF = aFBuilder.Face(); + BRepLib::BuildCurves3d(DF); + if(!DF.IsNull()) + { + sfs->Init ( DF ); + sfs->Perform(); + F = TopoDS::Face(sfs->Shape()); + } } void HYDROGUI_ImportLandCoverOp::onFileSelected() @@ -179,7 +238,7 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected() } aPanel->setObjectName( anObjectName ); - QString aFileName = aPanel->myFileName->text(); + QString aFileName = aPanel->getFileName(); if ( aFileName.isEmpty() ) { abort(); @@ -196,26 +255,73 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected() startDocOperation(); QStringList aPolygonsList; - for (int i = 0; i < mySHPObjects.size(); i++) + for (int i = 1; i < mySHPObjects.size(); i++) aPolygonsList.append("polygon_" + QString::number(i)); - aPanel->setPolylineNames(aPolygonsList); - - TopoDS_Compound cmp; - BRep_Builder BB; - BB.MakeCompound(cmp); - TopoDS_Face F; + aPanel->setPolygonNames(aPolygonsList); + + SalomeApp_Study* aStudy = dynamic_cast( module()->getApp()->activeStudy() ); + if ( !aStudy ) { + return; + } + + erasePreview(); + + Handle(AIS_InteractiveContext) aCtx = NULL; + + LightApp_Application* anApp = module()->getApp(); + if ( !getPreviewManager() ) + setPreviewManager( ::qobject_cast( 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() ) ); + } + } + + TopTools_SequenceOfShape aFaces; + TopoDS_Face aF; if (aHSHP->nShapeType == 5) { - for (int i = 0; i < 10/*mySHPObjects.size()*/; i++) { - ProcessSHP(mySHPObjects[i], i, F); - BB.Add(cmp, F); + for (int i = 1; i < 4 /*mySHPObjects.size()*/; i++) { ///TODO temp + ProcessSHP(mySHPObjects[i], i, aF); + aFaces.Append(aF); } - ///to hydro_landcover - // BRepTools::Write(cmp, "d:/h1.brep"); } else SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" ); + + 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++ ) @@ -227,5 +333,84 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected() } +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( 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(); +} diff --git a/src/HYDROGUI/HYDROGUI_ImportLandcoverOp.h b/src/HYDROGUI/HYDROGUI_ImportLandcoverOp.h index f8b95c5e..181948a9 100644 --- a/src/HYDROGUI/HYDROGUI_ImportLandcoverOp.h +++ b/src/HYDROGUI/HYDROGUI_ImportLandcoverOp.h @@ -25,12 +25,14 @@ #include "HYDROGUI_Operation.h" #include +#include //extern "C" { #include //}; class SUIT_FileDlg; +class HYDROGUI_Shape; class TopoDS_Face; @@ -44,17 +46,22 @@ public: protected: virtual void startOperation(); + virtual void abortOperation(); + virtual void onApply(); HYDROGUI_InputPanel* createInputPanel() const; void Parse(SHPHandle theHandle); void ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F); + void erasePreview(); protected slots: - void onFileSelected(); + void onFileSelected(); + void onSelectionChanged( const QStringList& theSelectedNames ); + void onViewerSelectionChanged (); private: - //SUIT_FileDlg* myFileDlg; std::vector mySHPObjects; + QMap myPolygonName2PrsShape; }; #endif