Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_v14_rc' into BR_SHP_FORMAT
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ExportFileOp.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ExportFileOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_UpdateFlags.h"
28 #include "HYDROGUI_Tool.h"
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Polyline3D.h>
31 #include <HYDROGUI_DataObject.h>
32 #include <HYDROData_Bathymetry.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
45
46 HYDROGUI_ExportFileOp::HYDROGUI_ExportFileOp( HYDROGUI_Module* theModule )
47 : HYDROGUI_Operation( theModule )
48 {
49   setName( tr( "EXPORT_POLYLINE" ) );
50 }
51
52 HYDROGUI_ExportFileOp::~HYDROGUI_ExportFileOp()
53 {
54 }
55
56 void HYDROGUI_ExportFileOp::startOperation()
57 {
58   HYDROGUI_Operation::startOperation();
59   
60   QString aFilter( "*.shp" ); //temp  ext-n; replace with filter; TODO
61   
62   Handle(HYDROData_PolylineXY) aPolyXY;
63   Handle(HYDROData_Polyline3D) aPoly3D;
64   NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq;
65   NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq;
66
67   
68   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
69   for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
70   {
71     aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
72     if (!aPolyXY.IsNull())
73       aPolyXYSeq.Append(aPolyXY);
74
75     aPoly3D = Handle(HYDROData_Polyline3D)::DownCast( aSeq.Value( anIndex ));
76     if (!aPoly3D.IsNull())
77       aPoly3DSeq.Append(aPoly3D);
78   }
79
80   if (!aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
81     SUIT_MessageBox::warning( module()->getApp()->desktop(), "Export Polyline", "Cannot export polylines of different kind");
82   else
83   {
84     QString aFileName = SUIT_FileDlg::getFileName( module()->getApp()->desktop(), "", aFilter, tr( "EXPORT_POLYLINE" ), false );
85     if (!aFileName.isEmpty())
86     {
87       SHPHandle hSHPHandle;
88       if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
89       {
90         hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
91         for (int i = 1; i <= aPolyXYSeq.Size(); i++)
92           WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i));
93       }
94       else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
95       {
96         hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
97         for (int i = 1; i <= aPoly3DSeq.Size(); i++)
98           WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i));
99       }      
100       SHPClose( hSHPHandle );
101       commit();
102     }
103     else
104       abort();
105   }
106
107 }
108
109 void HYDROGUI_ExportFileOp::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
110 {
111   SHPObject     *aSHPObj;
112   std::vector<double> x, y;
113   std::vector<int> anPartStart;
114   
115   for (int i = 0; i < thePoly->NbSections(); i++)
116   {
117     anPartStart.push_back(x.size());
118     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
119     for (int j = 1; j <= aPointList.Size(); j++)
120     {
121       x.push_back( aPointList(j).X());
122       y.push_back( aPointList(j).Y()); 
123     }
124     if (thePoly->IsClosedSection(i))
125     {
126       x.push_back( aPointList(1).X());
127       y.push_back( aPointList(1).Y()); 
128     }
129   }
130     
131   aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
132   SHPWriteObject( theShpHandle, -1, aSHPObj );
133   SHPDestroyObject( aSHPObj );
134 }
135
136 void HYDROGUI_ExportFileOp::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
137 {
138   SHPObject     *aSHPObj;
139   std::vector<double> x, y, z;
140   std::vector<int> anPartStart;
141   
142   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
143   {
144     anPartStart.push_back(x.size());
145     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
146     for (int j = 1; j <= aPointList.Size(); j++)
147     {
148       x.push_back( aPointList(j).X());
149       y.push_back( aPointList(j).Y()); 
150       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
151     }
152     if ( thePoly->GetPolylineXY()->IsClosedSection(i))
153     {
154       x.push_back( aPointList(1).X());
155       y.push_back( aPointList(1).Y()); 
156       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
157
158     }
159   }
160   
161   aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
162   SHPWriteObject( theShpHandle, -1, aSHPObj );
163   SHPDestroyObject( aSHPObj );
164 }
165
166
167