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();
88 if ( !getPreviewManager() ) {
89 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
90 module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
93 if ( !isApplyAndClose() ) {
97 HYDROGUI_ImportLandCoverDlg* aPanel =
98 ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
107 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
109 HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverDlg( module(), getName() );
111 connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
113 connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
118 bool HYDROGUI_ImportLandCoverOp::processApply( int& theUpdateFlags,
119 QString& theErrorMsg,
120 QStringList& theBrowseObjectsEntries )
123 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
128 QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
129 aPanel->removePolygonNames( aSelectedtPolygons );
131 if (!aSelectedtPolygons.empty())
133 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
134 TopoDS_Shape aResShape;
135 if (aSelectedtPolygons.size() > 1)
139 BB.MakeCompound(cmp);
141 foreach ( QString aName, aSelectedtPolygons ) {
142 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
143 if ( aShape.IsNull() )
146 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
147 delete aShapeToDelete;
153 TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
154 if ( !aShape.IsNull() )
156 HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
157 delete aShapeToDelete;
163 QString aLCName = aPanel->getObjectName() + "_polygon";
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() );
171 aLC->setShape(aResShape);
176 module()->setIsToUpdate( aLC );
181 module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
183 if ( isApplyAndClose() )
190 void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
193 mySHPObjects.clear();
194 SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
197 for (int i = 0; i < theHandle->nRecords; i++)
198 mySHPObjects.push_back(SHPReadObject(theHandle, i));
202 void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face& F)
206 int nParts = anObj->nParts;
207 gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
208 BRepBuilderAPI_MakeFace aFBuilder(pln);
210 //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
211 //sfs->FixFaceTool()->FixOrientationMode() = 1;
213 for ( int i = 0 ; i < nParts ; i++ )
215 BRepBuilderAPI_MakeWire aBuilder;
216 int StartIndex = anObj->panPartStart[i];
219 EndIndex = anObj->panPartStart[i + 1];
221 EndIndex = anObj->nVertices;
223 for ( int k = StartIndex; k < EndIndex - 1 ; k++ )
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()));
232 W = TopoDS::Wire(aBuilder.Shape());
238 TopoDS_Face DF = aFBuilder.Face();
239 BRepLib::BuildCurves3d(DF);
244 F = DF; //TopoDS::Face(sfs->Shape());
248 void HYDROGUI_ImportLandCoverOp::onFileSelected()
250 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
254 QString anObjectName = aPanel->getObjectName().simplified();
255 anObjectName = aPanel->getFileName();
256 if ( !anObjectName.isEmpty() )
257 anObjectName = QFileInfo( anObjectName ).baseName();
259 if ( anObjectName.isEmpty() )
260 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVER_NAME" ) );
261 aPanel->setObjectName( anObjectName );
263 QString aFileName = aPanel->getFileName();
264 if ( aFileName.isEmpty() )
270 QString anExt = aFileName.split('.', QString::SplitBehavior::SkipEmptyParts).back();
275 aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
279 QStringList aPolygonsList;
280 for (int i = 1; i < mySHPObjects.size(); i++)
281 aPolygonsList.append("polygon_" + QString::number(i));
282 aPanel->setPolygonNames(aPolygonsList);
284 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
291 Handle(AIS_InteractiveContext) aCtx = NULL;
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();
300 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
302 aCtx = aViewer->getAISContext();
303 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
307 QApplication::setOverrideCursor(Qt::WaitCursor);
309 TopTools_SequenceOfShape aFaces;
311 if (aHSHP->nShapeType == 5)
313 for (int i = 0; i < mySHPObjects.size(); i++)
315 ProcessSHP(mySHPObjects[i], i, aF);
320 SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
321 QApplication::restoreOverrideCursor();
323 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
324 for ( int i = 1; i <= aFaces.Length(); i++ )
326 TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
328 aLC->setShape( aFace );
330 if ( aViewManager && !aCtx.IsNull() )
332 HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
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);
343 if ( !aCtx.IsNull() ) {
344 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
345 aCtx->UpdateCurrentViewer();
348 commitDocOperation();
350 for (size_t i = 0; i < mySHPObjects.size(); i++ )
351 free (mySHPObjects[i]);
353 mySHPObjects.clear();
359 void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
361 Handle(AIS_InteractiveContext) aCtx = NULL;
363 OCCViewer_ViewManager* aViewManager = getPreviewManager();
364 if ( aViewManager ) {
365 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
366 aCtx = aViewer->getAISContext();
370 if ( !aCtx.IsNull() ) {
371 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
372 Handle(AIS_InteractiveObject) anObject =
373 myPolygonName2PrsShape.value(aName)->getAISObject();
375 bool isSelected = theSelectedNames.contains( aName );
376 if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
377 ( !isSelected && aCtx->IsSelected( anObject) ) ) {
378 aCtx->AddOrRemoveSelected( anObject, Standard_False );
381 aCtx->UpdateCurrentViewer();
386 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
389 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
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();
402 if ( !aCtx.IsNull() )
404 QStringList aSelectedNames;
405 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
406 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
408 aSelectedNames << aName;
411 aPanel->setSelectedPolygonNames( aSelectedNames );
416 void HYDROGUI_ImportLandCoverOp::erasePreview()
418 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
422 myPolygonName2PrsShape.clear();
426 void HYDROGUI_ImportLandCoverOp::abortOperation()
428 LightApp_Application* anApp = module()->getApp();
430 anApp->disconnect( this );
435 HYDROGUI_Operation::abortOperation();