Salome HOME
f1fb2ed34978e715f5cabf16f3b8c64b30321e95
[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     SHPHandle hSHPHandle;
86     if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
87     {
88       hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
89       for (int i = 1; i <= aPolyXYSeq.Size(); i++)
90         WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i));
91     }
92     else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
93     {
94       hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
95       for (int i = 1; i <= aPoly3DSeq.Size(); i++)
96         WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i));
97     }      
98     SHPClose( hSHPHandle );
99   }
100
101   commit();
102 }
103
104 void HYDROGUI_ExportFileOp::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
105 {
106   SHPObject     *aSHPObj;
107   std::vector<double> x, y;
108   std::vector<int> anPartStart;
109   
110   for (int i = 0; i < thePoly->NbSections(); i++)
111   {
112     anPartStart.push_back(x.size());
113     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
114     for (int j = 1; j <= aPointList.Size(); j++)
115     {
116       x.push_back( aPointList(j).X());
117       y.push_back( aPointList(j).Y()); 
118     }
119     if (thePoly->IsClosedSection(i))
120     {
121       x.push_back( aPointList(1).X());
122       y.push_back( aPointList(1).Y()); 
123     }
124   }
125     
126   aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
127   SHPWriteObject( theShpHandle, -1, aSHPObj );
128   SHPDestroyObject( aSHPObj );
129 }
130
131 void HYDROGUI_ExportFileOp::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
132 {
133   SHPObject     *aSHPObj;
134   std::vector<double> x, y, z;
135   std::vector<int> anPartStart;
136   
137   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
138   {
139     anPartStart.push_back(x.size());
140     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
141     for (int j = 1; j <= aPointList.Size(); j++)
142     {
143       x.push_back( aPointList(j).X());
144       y.push_back( aPointList(j).Y()); 
145       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
146     }
147     if ( thePoly->GetPolylineXY()->IsClosedSection(i))
148     {
149       x.push_back( aPointList(1).X());
150       y.push_back( aPointList(1).Y()); 
151       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
152
153     }
154   }
155   
156   aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
157   SHPWriteObject( theShpHandle, -1, aSHPObj );
158   SHPDestroyObject( aSHPObj );
159 }
160
161
162