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 if (P1.Distance(P2) < Precision::Confusion())
229 BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
230 aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
234 W = TopoDS::Wire(aBuilder.Shape());
240 TopoDS_Face DF = aFBuilder.Face();
241 BRepLib::BuildCurves3d(DF);
242 bool IsInf = DF.Infinite();
247 F = DF; //TopoDS::Face(sfs->Shape());
251 void HYDROGUI_ImportLandCoverOp::onFileSelected()
253 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
257 QString anObjectName = aPanel->getObjectName().simplified();
258 anObjectName = aPanel->getFileName();
259 if ( !anObjectName.isEmpty() )
260 anObjectName = QFileInfo( anObjectName ).baseName();
262 if ( anObjectName.isEmpty() )
263 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVER_NAME" ) );
264 aPanel->setObjectName( anObjectName );
266 QString aFileName = aPanel->getFileName();
267 if ( aFileName.isEmpty() )
273 QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
278 aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
282 QStringList aPolygonsList;
283 for (int i = 0; i < mySHPObjects.size(); i++)
284 aPolygonsList.append("polygon_" + QString::number(i + 1));
285 aPanel->setPolygonNames(aPolygonsList);
287 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
294 Handle(AIS_InteractiveContext) aCtx = NULL;
296 LightApp_Application* anApp = module()->getApp();
297 if ( !getPreviewManager() )
298 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
299 OCCViewer_ViewManager* aViewManager = getPreviewManager();
303 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
305 aCtx = aViewer->getAISContext();
306 connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
310 QApplication::setOverrideCursor(Qt::WaitCursor);
312 TopTools_SequenceOfShape aFaces;
314 if (aHSHP->nShapeType == 5)
316 for (int i = 0; i < mySHPObjects.size(); i++)
318 ProcessSHP(mySHPObjects[i], i, aF);
323 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), "Cannot import land cover;\nThe shape type is not polygon" );
324 QApplication::restoreOverrideCursor();
326 Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
327 for ( int i = 1; i <= aFaces.Length(); i++ )
329 TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
331 aLC->setShape( aFace );
333 if ( aViewManager && !aCtx.IsNull() )
335 HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
337 aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
338 aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
339 if( !aFace.IsNull() )
340 aShape->setShape( aLC->GetShape() );
341 myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
346 if ( !aCtx.IsNull() ) {
347 UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
348 aCtx->UpdateCurrentViewer();
351 commitDocOperation();
353 for (size_t i = 0; i < mySHPObjects.size(); i++ )
354 free (mySHPObjects[i]);
356 mySHPObjects.clear();
362 void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
364 Handle(AIS_InteractiveContext) aCtx = NULL;
366 OCCViewer_ViewManager* aViewManager = getPreviewManager();
367 if ( aViewManager ) {
368 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
369 aCtx = aViewer->getAISContext();
373 if ( !aCtx.IsNull() ) {
374 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
375 Handle(AIS_InteractiveObject) anObject =
376 myPolygonName2PrsShape.value(aName)->getAISObject();
378 bool isSelected = theSelectedNames.contains( aName );
379 if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
380 ( !isSelected && aCtx->IsSelected( anObject) ) ) {
381 aCtx->AddOrRemoveSelected( anObject, Standard_False );
384 aCtx->UpdateCurrentViewer();
389 void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
392 HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
397 OCCViewer_ViewManager* aViewManager = getPreviewManager();
398 Handle(AIS_InteractiveContext) aCtx = NULL;
399 if ( aViewManager ) {
400 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
401 aCtx = aViewer->getAISContext();
405 if ( !aCtx.IsNull() )
407 QStringList aSelectedNames;
408 foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
409 bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
411 aSelectedNames << aName;
414 aPanel->setSelectedPolygonNames( aSelectedNames );
419 void HYDROGUI_ImportLandCoverOp::erasePreview()
421 foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
425 myPolygonName2PrsShape.clear();
429 void HYDROGUI_ImportLandCoverOp::abortOperation()
431 LightApp_Application* anApp = module()->getApp();
433 anApp->disconnect( this );
438 HYDROGUI_Operation::abortOperation();