Salome HOME
corrections
[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_Tool.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 <Handle_Geom_Curve.hxx>
52 #include <Handle_Geom_Line.hxx>
53 #include <Handle_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       if (!aPolyXYSeq.IsEmpty() || !aPolyXYSeq.IsEmpty())
111         //Export polylines
112         anExporter.Export(aFileName, aPolyXYSeq, aPoly3DSeq, aNonExpList);
113       else
114       {
115         //Export polygons
116         //Extract all attribute names from all strickler tables
117         Handle_HYDROData_Document aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
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         //
135         HYDROGUI_ExportLandCoverMapDlg aDlg( module()->getApp()->desktop(), anAttrNames.toList());
136         if ( aDlg.exec() == HYDROGUI_ExportLandCoverMapDlg::Accepted )
137         { 
138           //In our case :  aSeq.Size() == 1
139           //Export of multiple landcover maps into the one shp-file is disallowed.
140           Handle_HYDROData_LandCoverMap aLCM = Handle(HYDROData_LandCoverMap)::DownCast( aSeq(1) );
141           QString aCItem = aDlg.getCurrentItem();
142           Handle(HYDROData_StricklerTable) aStricklerTableObj;
143           {
144             HYDROData_Iterator anIt( aDoc, KIND_STRICKLER_TABLE );
145             for( ; anIt.More(); anIt.Next() )
146             {
147               aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIt.Current() );
148               if ( !aStricklerTableObj.IsNull() && aStricklerTableObj->GetAttrName() == aCItem)
149                 break;
150             }
151           }
152
153           //export shape-data
154           anExporter.Export(aFileName, aLCM, aNonExpList);
155           QString aDBFFileName = aFileName.replace( ".shp", ".dbf", Qt::CaseInsensitive);
156           //Even if attribute-checkbox is unchecked, the .dbf-file should be removed. 
157           //otherwise it may be used with wrong .shp-file. This is an incorrect behaivor.
158           remove (aDBFFileName.toStdString().c_str());
159           bool bToSaveAttrInfo = aDlg.getAttrCheckBoxState();
160           if (bToSaveAttrInfo)
161           {
162             //export attribute info
163             QStringList anAttrValues;
164             QStringList aStricklerTypes;
165             if (aNonExpList.empty())
166             {
167               HYDROData_LandCoverMap::Explorer aLCMIt( aLCM );
168               for( ; aLCMIt.More(); aLCMIt.Next() )
169               {
170                 QString aST = aLCMIt.StricklerType();
171                 anAttrValues << aStricklerTableObj->GetAttrValue(aST);
172                 aStricklerTypes << aST;
173               }
174             }
175             aLCM->ExportDBF(aDBFFileName, aCItem, anAttrValues, aStricklerTypes);
176           }
177         }
178         else
179         {
180           abort();
181           return;
182         }
183
184       }
185
186       if (!aNonExpList.empty())
187       {
188         QString aMessage = tr("CANNOT_BE_EXPORTED") + "\n";
189         foreach (QString anNonExpEntName, aNonExpList)
190           aMessage += anNonExpEntName + "\n"; 
191         SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "EXPORT_TO_SHAPE_FILE" ), aMessage);
192       }
193       commit();
194     }
195     else
196       abort();
197   }
198
199 }
200