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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ExportFileOp.h"
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>
32 #include <HYDROData_StricklerTable.h>
34 #include <HYDROData_Profile.h>
36 #include <SUIT_Desktop.h>
37 #include <SUIT_FileDlg.h>
38 #include <LightApp_Application.h>
40 #include <QApplication>
43 #include <SUIT_MessageBox.h>
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>
60 #include <LightApp_Application.h>
63 HYDROGUI_ExportFileOp::HYDROGUI_ExportFileOp( HYDROGUI_Module* theModule )
64 : HYDROGUI_Operation( theModule )
66 setName( tr( "EXPORT_TO_SHAPE_FILE" ) );
69 HYDROGUI_ExportFileOp::~HYDROGUI_ExportFileOp()
73 void HYDROGUI_ExportFileOp::startOperation()
75 HYDROGUI_Operation::startOperation();
77 QString aFilter( tr("SHP_FILTER") );
79 Handle(HYDROData_PolylineXY) aPolyXY;
80 Handle(HYDROData_Polyline3D) aPoly3D;
81 NCollection_Sequence<Handle(HYDROData_PolylineXY)> aPolyXYSeq;
82 NCollection_Sequence<Handle(HYDROData_Polyline3D)> aPoly3DSeq;
84 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
85 for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
87 aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
88 if (!aPolyXY.IsNull())
89 aPolyXYSeq.Append(aPolyXY);
91 aPoly3D = Handle(HYDROData_Polyline3D)::DownCast( aSeq.Value( anIndex ));
92 if (!aPoly3D.IsNull())
93 aPoly3DSeq.Append(aPoly3D);
96 if (!aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
97 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "EXPORT_TO_SHAPE_FILE" ), tr("ERROR_POLYLINES_OF_DIFF_KIND"));
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())
108 QStringList aNonExpList;
109 HYDROData_ShapeFile anExporter;
110 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
111 if (!aPolyXYSeq.IsEmpty() || !aPolyXYSeq.IsEmpty())
113 anExporter.Export(aDoc, aFileName, aPolyXYSeq, aPoly3DSeq, aNonExpList);
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
123 HYDROData_Iterator anIt( aDoc, KIND_STRICKLER_TABLE );
124 for( ; anIt.More(); anIt.Next() )
126 Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIt.Current() );
127 if ( !aStricklerTableObj.IsNull())
128 anAttrNames << aStricklerTableObj->GetAttrName();
134 QStringList SortedListOfAttr = anAttrNames.toList();
135 SortedListOfAttr.sort();
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 )
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;
147 HYDROData_Iterator anIt( aDoc, KIND_STRICKLER_TABLE );
148 for( ; anIt.More(); anIt.Next() )
150 aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( anIt.Current() );
151 if ( !aStricklerTableObj.IsNull() && aStricklerTableObj->GetAttrName() == aCItem)
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();
165 //export attribute info
166 QStringList anAttrValues;
167 QStringList aStricklerTypes;
168 if (aNonExpList.empty())
170 HYDROData_LandCoverMap::Explorer aLCMIt( aLCM );
171 for( ; aLCMIt.More(); aLCMIt.Next() )
173 QString aST = aLCMIt.StricklerType();
174 anAttrValues << aStricklerTableObj->GetAttrValue(aST);
175 aStricklerTypes << aST;
178 aLCM->ExportDBF(aDBFFileName, aCItem, anAttrValues, aStricklerTypes);
189 if (!aNonExpList.empty())
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);