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_Iterator.h>
37 #include <HYDROData_Profile.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_FileDlg.h>
41 #include <LightApp_Application.h>
43 #include <QApplication>
46 #include <SUIT_MessageBox.h>
48 #include <OCCViewer_ViewManager.h>
49 #include <OCCViewer_ViewModel.h>
51 #include <SalomeApp_Study.h>
53 #include <LightApp_Application.h>
54 #include <LightApp_DataOwner.h>
55 #include <LightApp_Displayer.h>
56 #include <LightApp_SelectionMgr.h>
58 #include <SUIT_Desktop.h>
59 #include <SUIT_ViewManager.h>
61 #include <BRep_Builder.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>
69 #include <BRepLib.hxx>
70 #include <ShapeFix_Shape.hxx>
73 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
74 : HYDROGUI_Operation( theModule )
76 setName( tr( "IMPORT_LANDCOVER" ) );
79 HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
84 void HYDROGUI_ImportLandCoverOp::startOperation()
86 HYDROGUI_Operation::startOperation();
90 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
92 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverDlg( module(), getName() );
94 connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
96 connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
101 bool HYDROGUI_ImportLandCoverOp::processApply( int& theUpdateFlags,
102 QString& theErrorMsg,
103 QStringList& theBrowseObjectsEntries )
106 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
111 QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
112 aPanel->removePolygonNames( aSelectedtPolygons );
114 if (!aSelectedtPolygons.empty())
116 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
117 TopoDS_Shape aResShape;
118 if (aSelectedtPolygons.size() > 1)
122 BB.MakeCompound(cmp);
124 foreach ( QString aName, aSelectedtPolygons ) {
125 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
126 if ( aShape.IsNull() )
129 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
130 delete aShapeToDelete;
136 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
137 if ( !aShape.IsNull() )
139 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
140 delete aShapeToDelete;
146 QString aLCName = aPanel->getObjectName() + "_polygon";
148 for( ;HYDROGUI_Tool::FindObjectByName(module(), aLCName); i++)
149 aLCName = aPanel->getObjectName() + "_polygon_" + QString::number(i);
150 aLC->SetName( aLCName );
151 aLC->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
152 aLC->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
154 aLC->setShape(aResShape);
159 module()->setIsToUpdate( aLC );
164 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
166 if ( isApplyAndClose() )
173 void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
176 mySHPObjects.clear();
177 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
180 for (int i = 0; i < theHandle->nRecords; i++)
181 mySHPObjects.push_back(SHPReadObject(theHandle, i));
185 void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
189 int nParts = anObj->nParts;
190 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
191 BRepBuilderAPI_MakeFace aFBuilder(pln);
193 //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
194 //sfs->FixFaceTool()->FixOrientationMode() = 1;
196 for ( int i = 0 ; i < nParts ; i++ )
198 BRepBuilderAPI_MakeWire aBuilder;
199 int StartIndex = anObj->panPartStart[i];
202 EndIndex = anObj->panPartStart[i + 1];
204 EndIndex = anObj->nVertices;
206 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
208 gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
209 gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
210 BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
211 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
215 W = TopoDS::Wire(aBuilder.Shape());
221 TopoDS_Face DF = aFBuilder.Face();
222 BRepLib::BuildCurves3d(DF);
227 F = DF; //TopoDS::Face(sfs->Shape());
231 void HYDROGUI_ImportLandCoverOp::onFileSelected()
233 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
237 QString anObjectName = aPanel->getObjectName().simplified();
238 anObjectName = aPanel->getFileName();
239 if ( !anObjectName.isEmpty() )
240 anObjectName = QFileInfo( anObjectName ).baseName();
242 if ( anObjectName.isEmpty() )
243 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVER_NAME" ) );
244 aPanel->setObjectName( anObjectName );
246 QString aFileName = aPanel->getFileName();
247 if ( aFileName.isEmpty() )
253 QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
258 aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
262 QStringList aPolygonsList;
263 for (int i = 1; i < mySHPObjects.size(); i++)
264 aPolygonsList.append("polygon_" + QString::number(i));
265 aPanel->setPolygonNames(aPolygonsList);
267 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
274 Handle(AIS_InteractiveContext) aCtx = NULL;
276 LightApp_Application* anApp = module()->getApp();
277 if ( !getPreviewManager() )
278 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
279 OCCViewer_ViewManager* aViewManager = getPreviewManager();
283 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
285 aCtx = aViewer->getAISContext();
286 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
290 QApplication::setOverrideCursor(Qt::WaitCursor);
292 TopTools_SequenceOfShape aFaces;
294 if (aHSHP->nShapeType == 5)
296 for (int i = 0; i < mySHPObjects.size(); i++)
298 ProcessSHP(mySHPObjects[i], i, aF);
303 SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
304 QApplication::restoreOverrideCursor();
306 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
307 for ( int i = 1; i <= aFaces.Length(); i++ )
309 TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
311 aLC->setShape( aFace );
313 if ( aViewManager && !aCtx.IsNull() )
315 HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
317 aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
318 aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
319 if( !aFace.IsNull() )
320 aShape->setShape( aLC->GetShape() );
321 myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
326 if ( !aCtx.IsNull() ) {
327 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
328 aCtx->UpdateCurrentViewer();
331 commitDocOperation();
333 for (size_t i = 0; i < mySHPObjects.size(); i++ )
334 free (mySHPObjects[i]);
336 mySHPObjects.clear();
342 void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
344 Handle(AIS_InteractiveContext) aCtx = NULL;
346 OCCViewer_ViewManager* aViewManager = getPreviewManager();
347 if ( aViewManager ) {
348 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
349 aCtx = aViewer->getAISContext();
353 if ( !aCtx.IsNull() ) {
354 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
355 Handle(AIS_InteractiveObject) anObject =
356 myPolygonName2PrsShape.value(aName)->getAISObject();
358 bool isSelected = theSelectedNames.contains( aName );
359 if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
360 ( !isSelected && aCtx->IsSelected( anObject) ) ) {
361 aCtx->AddOrRemoveSelected( anObject, Standard_False );
364 aCtx->UpdateCurrentViewer();
369 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
372 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
377 OCCViewer_ViewManager* aViewManager = getPreviewManager();
378 Handle(AIS_InteractiveContext) aCtx = NULL;
379 if ( aViewManager ) {
380 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
381 aCtx = aViewer->getAISContext();
385 if ( !aCtx.IsNull() )
387 QStringList aSelectedNames;
388 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
389 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
391 aSelectedNames << aName;
394 aPanel->setSelectedPolygonNames( aSelectedNames );
399 void HYDROGUI_ImportLandCoverOp::erasePreview()
401 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
405 myPolygonName2PrsShape.clear();
409 void HYDROGUI_ImportLandCoverOp::abortOperation()
411 LightApp_Application* anApp = module()->getApp();
413 anApp->disconnect( this );
418 HYDROGUI_Operation::abortOperation();