]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ExportFileOp.cxx
Salome HOME
Use discretization to write non-linear borders of LCM to SHP file // p.4
[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         Handle_HYDROData_LandCoverMap aLCM = Handle(HYDROData_LandCoverMap)::DownCast( aSeq(1) );
136         bool IsLinear = aLCM->CheckLinear();
137         HYDROGUI_ExportLandCoverMapDlg aDlg( module()->getApp()->desktop(), IsLinear, anAttrNames.toList());
138         if ( aDlg.exec() == HYDROGUI_ExportLandCoverMapDlg::Accepted )
139         { 
140           //In our case :  aSeq.Size() == 1
141           //Export of multiple landcover maps into the one shp-file is disallowed.
142           QString aCItem = aDlg.getCurrentItem();
143           Handle(HYDROData_StricklerTable) aStricklerTableObj;
144           {
145             HYDROData_Iterator anIt( aDoc, KIND_STRICKLER_TABLE );
146             for( ; anIt.More(); anIt.Next() )
147             {
148               aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIt.Current() );
149               if ( !aStricklerTableObj.IsNull() && aStricklerTableObj->GetAttrName() == aCItem)
150                 break;
151             }
152           }
153
154           //export shape-data
155           anExporter.Export(aFileName, aLCM, aNonExpList, false, !IsLinear, aDlg.getDeflValue());
156           QString aDBFFileName = aFileName.replace( ".shp", ".dbf", Qt::CaseInsensitive);
157           //Even if attribute-checkbox is unchecked, the .dbf-file should be removed. 
158           //otherwise it may be used with wrong .shp-file. This is an incorrect behaivor.
159           remove (aDBFFileName.toStdString().c_str());
160           bool bToSaveAttrInfo = aDlg.getAttrCheckBoxState();
161           if (bToSaveAttrInfo)
162           {
163             //export attribute info
164             QStringList anAttrValues;
165             QStringList aStricklerTypes;
166             if (aNonExpList.empty())
167             {
168               HYDROData_LandCoverMap::Explorer aLCMIt( aLCM );
169               for( ; aLCMIt.More(); aLCMIt.Next() )
170               {
171                 QString aST = aLCMIt.StricklerType();
172                 anAttrValues << aStricklerTableObj->GetAttrValue(aST);
173                 aStricklerTypes << aST;
174               }
175             }
176             aLCM->ExportDBF(aDBFFileName, aCItem, anAttrValues, aStricklerTypes);
177           }
178         }
179         else
180         {
181           abort();
182           return;
183         }
184
185       }
186
187       if (!aNonExpList.empty())
188       {
189         QString aMessage = tr("CANNOT_BE_EXPORTED") + "\n";
190         foreach (QString anNonExpEntName, aNonExpList)
191           aMessage += anNonExpEntName + "\n"; 
192         SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "EXPORT_TO_SHAPE_FILE" ), aMessage);
193       }
194       commit();
195     }
196     else
197       abort();
198   }
199
200 }
201