1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ExportFileOp.h"
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>
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>
46 HYDROGUI_ExportFileOp::HYDROGUI_ExportFileOp( HYDROGUI_Module* theModule )
47 : HYDROGUI_Operation( theModule )
49 setName( tr( "EXPORT_POLYLINE" ) );
52 HYDROGUI_ExportFileOp::~HYDROGUI_ExportFileOp()
56 void HYDROGUI_ExportFileOp::startOperation()
58 HYDROGUI_Operation::startOperation();
60 QString aFilter( "*.shp" ); //temp ext-n; replace with filter; TODO
62 Handle(HYDROData_PolylineXY) aPolyXY;
63 Handle(HYDROData_Polyline3D) aPoly3D;
64 NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq;
65 NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq;
68 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
69 for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
71 aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
72 if (!aPolyXY.IsNull())
73 aPolyXYSeq.Append(aPolyXY);
75 aPoly3D = Handle(HYDROData_Polyline3D)::DownCast( aSeq.Value( anIndex ));
76 if (!aPoly3D.IsNull())
77 aPoly3DSeq.Append(aPoly3D);
80 if (!aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
81 SUIT_MessageBox::warning( module()->getApp()->desktop(), "Export Polyline", "Cannot export polylines of different kind");
84 QString aFileName = SUIT_FileDlg::getFileName( module()->getApp()->desktop(), "", aFilter, tr( "EXPORT_POLYLINE" ), false );
86 if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
88 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
89 for (int i = 1; i <= aPolyXYSeq.Size(); i++)
90 WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i));
92 else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
94 hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
95 for (int i = 1; i <= aPoly3DSeq.Size(); i++)
96 WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i));
98 SHPClose( hSHPHandle );
104 void HYDROGUI_ExportFileOp::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
107 std::vector<double> x, y;
108 std::vector<int> anPartStart;
110 for (int i = 0; i < thePoly->NbSections(); i++)
112 anPartStart.push_back(x.size());
113 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
114 for (int j = 1; j <= aPointList.Size(); j++)
116 x.push_back( aPointList(j).X());
117 y.push_back( aPointList(j).Y());
119 if (thePoly->IsClosedSection(i))
121 x.push_back( aPointList(1).X());
122 y.push_back( aPointList(1).Y());
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 );
131 void HYDROGUI_ExportFileOp::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
134 std::vector<double> x, y, z;
135 std::vector<int> anPartStart;
137 for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
139 anPartStart.push_back(x.size());
140 HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
141 for (int j = 1; j <= aPointList.Size(); j++)
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())));
147 if ( thePoly->GetPolylineXY()->IsClosedSection(i))
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())));
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 );