Salome HOME
lot 10 - warnings for DTM - untested
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportBCPolygonOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ImportBCPolygonOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_UpdateFlags.h"
24 #include "HYDROGUI_Tool2.h"
25 #include <HYDROData_PolylineXY.h>
26
27 #include <HYDROData_BCPolygon.h>
28 #include <HYDROGUI_DataObject.h>
29
30 #include <HYDROGUI_ImportPolylineOp.h>
31
32 #include <SUIT_Desktop.h>
33 #include <SUIT_FileDlg.h>
34 #include <LightApp_Application.h>
35
36 #include <QApplication>
37 #include <QFile>
38 #include <QFileInfo>
39 #include <SUIT_MessageBox.h>
40
41
42 HYDROGUI_ImportBCPolygonOp::HYDROGUI_ImportBCPolygonOp( HYDROGUI_Module* theModule )
43 : HYDROGUI_Operation( theModule )
44 {
45   setName( tr( "IMPORT_BC_POLYGON" ) );
46 }
47
48 HYDROGUI_ImportBCPolygonOp::~HYDROGUI_ImportBCPolygonOp()
49 {
50 }
51
52 void HYDROGUI_ImportBCPolygonOp::startOperation()
53 {
54   HYDROGUI_Operation::startOperation();
55
56   myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
57   myFileDlg->setWindowTitle( getName() );
58   myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
59   myFileDlg->setNameFilter( tr("BC_POLYGON_FILTER") );
60
61   connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
62   connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
63
64   myFileDlg->exec();
65 }
66
67
68 bool HYDROGUI_ImportBCPolygonOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
69                                            QStringList& theBrowseObjectsEntries )
70 {
71   if ( !myFileDlg )
72   {
73     //abort();
74     return false;
75   }
76
77   QStringList aFileNames = myFileDlg->selectedFiles();
78   
79   QApplication::setOverrideCursor( Qt::WaitCursor );  
80   //startDocOperation();
81
82    NCollection_Sequence<Handle(HYDROData_Entity)> importedEnts = 
83      HYDROGUI_ImportPolylineOp::ImportPolyOp(aFileNames, doc(), module(), 
84      HYDROData_ShapeFile::ImportShapeType_Polygon);
85
86    for (int i=1;i<=importedEnts.Size();i++ )
87    {
88       Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast(importedEnts(i));
89       if (aPolylineXY.IsNull())
90         continue;
91       TopoDS_Shape aTopSh = HYDROData_BCPolygon::generateTopShape(aPolylineXY);
92       if (aTopSh.IsNull())
93         continue;
94
95       //get boundary type from DBF
96       QStringList aDBFTableList;
97       aPolylineXY->GetDBFInfo(aDBFTableList);
98       QVector<QString> aDBFTable = aDBFTableList.toVector();
99       int nbs = aDBFTable.size();
100       QString aBTypeStr;
101       for (int i = 0; i < nbs / 2; i++)
102         if (aDBFTable[i] == "type")
103         {
104           aBTypeStr = aDBFTable[nbs / 2 + i];
105           break;
106         }
107
108       int aBTypeVal = 0;
109       bool Ok = false;
110       aBTypeVal = aBTypeStr.toInt(&Ok);
111       if (!Ok)
112         aBTypeVal = 0;
113
114       QString aPolyXYName = aPolylineXY->GetName();
115       Handle(HYDROData_BCPolygon) aBCObj = Handle(HYDROData_BCPolygon)::DownCast( doc()->CreateObject( KIND_BC_POLYGON ) );
116       aBCObj->SetName( "BP_" + aPolyXYName );
117       aBCObj->SetBoundaryType(aBTypeVal);
118   
119       aBCObj->SetFillingColor( aBCObj->DefaultFillingColor() );
120       aBCObj->SetBorderColor( aBCObj->DefaultBorderColor() );
121       aBCObj->SetPolyline( aPolylineXY );
122       aBCObj->Update(aTopSh);
123
124       module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aBCObj, true );
125       QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aBCObj );
126       theBrowseObjectsEntries.append( anEntry );
127       module()->setIsToUpdate( aBCObj );
128    }
129  
130   if (!aFileNames.empty())
131   {
132     //commitDocOperation();
133     //commit();
134     //module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init | UF_OCCViewer );
135     theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
136   }
137   //else
138   //  abort();
139   
140   QApplication::restoreOverrideCursor();
141   return true;
142 }