Salome HOME
0d9027d2253c70e5f8d80370026e873ed41c0b34
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ExportFileOp.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_ExportFileOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_UpdateFlags.h"
24 #include "HYDROGUI_Tool2.h"
25 #include "HYDROGUI_ExportLandCoverMapDlg.h"
26 #include <HYDROData_PolylineXY.h>
27 #include <HYDROData_Polyline3D.h>
28 #include <HYDROGUI_DataObject.h>
29 #include <HYDROData_Bathymetry.h>
30 #include <HYDROData_LandCoverMap.h>
31
32 #include <HYDROData_StricklerTable.h>
33
34 #include <HYDROData_Profile.h>
35
36 #include <SUIT_Desktop.h>
37 #include <SUIT_FileDlg.h>
38 #include <LightApp_Application.h>
39
40 #include <QApplication>
41 #include <QFile>
42 #include <QFileInfo>
43 #include <SUIT_MessageBox.h>
44 #include <TopoDS.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <TopoDS_Wire.hxx>
47 #include <TopoDS_Vertex.hxx>
48 #include <TopoDS_Edge.hxx>
49 #include <BRep_Tool.hxx>
50 #include <Precision.hxx>
51 #include <Geom_Curve.hxx>
52 #include <Geom_Line.hxx>
53 #include <Geom_TrimmedCurve.hxx>
54 #include <Geom_TrimmedCurve.hxx>
55 #include <HYDROData_ShapeFile.h>
56 #include <SUIT_Desktop.h>
57 #include <SalomeApp_Study.h>
58 #include <HYDROData_Iterator.h>
59
60 #include <LightApp_Application.h>
61 #include <QSet>
62
63 HYDROGUI_ExportFileOp::HYDROGUI_ExportFileOp( HYDROGUI_Module* theModule )
64 : HYDROGUI_Operation( theModule )
65 {
66   setName( tr( "EXPORT_TO_SHAPE_FILE" ) );
67 }
68
69 HYDROGUI_ExportFileOp::~HYDROGUI_ExportFileOp()
70 {
71 }
72
73 void HYDROGUI_ExportFileOp::startOperation()
74 {
75   HYDROGUI_Operation::startOperation();
76   
77   QString aFilter( tr("SHP_FILTER") ); 
78   
79   Handle(HYDROData_PolylineXY) aPolyXY;
80   Handle(HYDROData_Polyline3D) aPoly3D;
81   NCollection_Sequence<Handle(HYDROData_PolylineXY)> aPolyXYSeq;
82   NCollection_Sequence<Handle(HYDROData_Polyline3D)> aPoly3DSeq;
83   //
84   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
85   for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
86   {
87     aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
88     if (!aPolyXY.IsNull())
89       aPolyXYSeq.Append(aPolyXY);
90
91     aPoly3D = Handle(HYDROData_Polyline3D)::DownCast( aSeq.Value( anIndex ));
92     if (!aPoly3D.IsNull())
93       aPoly3DSeq.Append(aPoly3D);
94   }
95
96   if (!aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
97     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "EXPORT_TO_SHAPE_FILE" ), tr("ERROR_POLYLINES_OF_DIFF_KIND"));
98   else
99   {
100     QString aName = "";
101     if (aPolyXYSeq.Size() == 1 && aPoly3DSeq.IsEmpty())
102       aName = aPolyXYSeq(1)->GetName();
103     if (aPoly3DSeq.Size() == 1 && aPolyXYSeq.IsEmpty())
104       aName = aPoly3DSeq(1)->GetName();
105     QString aFileName = SUIT_FileDlg::getFileName( module()->getApp()->desktop(), aName, aFilter, tr( "EXPORT_TO_SHAPE_FILE" ), false );
106     if (!aFileName.isEmpty())
107     {
108       QStringList aNonExpList;
109       HYDROData_ShapeFile anExporter;
110       Handle(HYDROData_Document) aDoc = HYDROData_Document::Document();
111       if (!aPolyXYSeq.IsEmpty() || !aPoly3DSeq.IsEmpty())
112         //Export polylines
113         anExporter.Export(aDoc, aFileName, aPolyXYSeq, aPoly3DSeq, aNonExpList);
114       else
115       {
116         //Export polygons
117         //Extract all attribute names from all strickler tables
118         QSet<QString> anAttrNames;
119         //use QSet to store attribute names. Yet it's not so good if the document contains two strickler types
120         //with the same name
121         if (aDoc)
122         {
123           HYDROData_Iterator anIt( aDoc, KIND_STRICKLER_TABLE );
124           for( ; anIt.More(); anIt.Next() )
125           {
126             Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIt.Current() );
127             if ( !aStricklerTableObj.IsNull())
128               anAttrNames << aStricklerTableObj->GetAttrName();
129           }
130         }
131         else
132           return; 
133
134         QStringList SortedListOfAttr = anAttrNames.toList();
135         SortedListOfAttr.sort();
136         //
137         Handle(HYDROData_LandCoverMap) aLCM = Handle(HYDROData_LandCoverMap)::DownCast( aSeq(1) );
138         bool IsLinear = aLCM->CheckLinear();
139         HYDROGUI_ExportLandCoverMapDlg aDlg( module()->getApp()->desktop(), IsLinear, SortedListOfAttr);
140         if ( aDlg.exec() == HYDROGUI_ExportLandCoverMapDlg::Accepted )
141         { 
142           //In our case :  aSeq.Size() == 1
143           //Export of multiple landcover maps into the one shp-file is disallowed.
144           QString aCItem = aDlg.getCurrentItem();
145           Handle(HYDROData_StricklerTable) aStricklerTableObj;
146           {
147             HYDROData_Iterator anIt( aDoc, KIND_STRICKLER_TABLE );
148             for( ; anIt.More(); anIt.Next() )
149             {
150               aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIt.Current() );
151               if ( !aStricklerTableObj.IsNull() && aStricklerTableObj->GetAttrName() == aCItem)
152                 break;
153             }
154           }
155
156           //export shape-data
157           anExporter.Export(aDoc, aFileName, aLCM, aNonExpList, false, !IsLinear, aDlg.getDeflValue());
158           QString aDBFFileName = aFileName.replace( ".shp", ".dbf", Qt::CaseInsensitive);
159           //Even if attribute-checkbox is unchecked, the .dbf-file should be removed. 
160           //otherwise it may be used with wrong .shp-file. This is an incorrect behaivor.
161           remove (aDBFFileName.toStdString().c_str());
162           bool bToSaveAttrInfo = aDlg.getAttrCheckBoxState() && !aDlg.getCurrentItem().isEmpty();
163           if (bToSaveAttrInfo)
164           {
165             //export attribute info
166             QStringList anAttrValues;
167             QStringList aStricklerTypes;
168             if (aNonExpList.empty())
169             {
170               HYDROData_LandCoverMap::Explorer aLCMIt( aLCM );
171               for( ; aLCMIt.More(); aLCMIt.Next() )
172               {
173                 QString aST = aLCMIt.StricklerType();
174                 anAttrValues << aStricklerTableObj->GetAttrValue(aST);
175                 aStricklerTypes << aST;
176               }
177             }
178             aLCM->ExportDBF(aDBFFileName, aCItem, anAttrValues, aStricklerTypes);
179           }
180         }
181         else
182         {
183           abort();
184           return;
185         }
186
187       }
188
189       if (!aNonExpList.empty())
190       {
191         QString aMessage = tr("CANNOT_BE_EXPORTED") + "\n";
192         foreach (QString anNonExpEntName, aNonExpList)
193           aMessage += anNonExpEntName + "\n"; 
194         SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "EXPORT_TO_SHAPE_FILE" ), aMessage);
195       }
196       commit();
197     }
198     else
199       abort();
200   }
201
202 }
203