Salome HOME
Merge branch 'BR_v14_rc' of ssh://git.salome-platform.org/modules/hydro into BR_v14_rc
[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 #include <HYDROData_LandCover.h>
34
35 #include <HYDROData_Profile.h>
36
37 #include <SUIT_Desktop.h>
38 #include <SUIT_FileDlg.h>
39 #include <LightApp_Application.h>
40
41 #include <QApplication>
42 #include <QFile>
43 #include <QFileInfo>
44 #include <SUIT_MessageBox.h>
45 #include <TopoDS.hxx>
46 #include <TopExp_Explorer.hxx>
47 #include <TopoDS_Wire.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <BRep_Tool.hxx>
50 #include <Precision.hxx>
51
52
53 HYDROGUI_ExportFileOp::HYDROGUI_ExportFileOp( HYDROGUI_Module* theModule )
54 : HYDROGUI_Operation( theModule )
55 {
56   setName( tr( "EXPORT_TO_SHAPE_FILE" ) );
57 }
58
59 HYDROGUI_ExportFileOp::~HYDROGUI_ExportFileOp()
60 {
61 }
62
63 void HYDROGUI_ExportFileOp::startOperation()
64 {
65   HYDROGUI_Operation::startOperation();
66   
67   QString aFilter( tr("SHP_FILTER") ); 
68   
69   Handle(HYDROData_PolylineXY) aPolyXY;
70   Handle(HYDROData_Polyline3D) aPoly3D;
71   Handle(HYDROData_LandCover) aLC;
72   NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq;
73   NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq;
74   NCollection_Sequence<Handle_HYDROData_LandCover> aLCSeq;
75   
76   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
77   for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
78   {
79     aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
80     if (!aPolyXY.IsNull())
81       aPolyXYSeq.Append(aPolyXY);
82
83     aPoly3D = Handle(HYDROData_Polyline3D)::DownCast( aSeq.Value( anIndex ));
84     if (!aPoly3D.IsNull())
85       aPoly3DSeq.Append(aPoly3D);
86
87     aLC = Handle(HYDROData_LandCover)::DownCast( aSeq.Value( anIndex ));
88     if (!aLC.IsNull())
89       aLCSeq.Append(aLC);
90   }
91
92   if (!aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
93     SUIT_MessageBox::warning( module()->getApp()->desktop(), "Export Polyline", "Cannot export polylines of different kind");
94   else
95   {
96     QString aPolyName = "";
97     if (aPolyXYSeq.Size() == 1 && aPoly3DSeq.IsEmpty())
98       aPolyName = aPolyXYSeq(1)->GetName();
99     if (aPoly3DSeq.Size() == 1 && aPolyXYSeq.IsEmpty())
100       aPolyName = aPoly3DSeq(1)->GetName();
101     QString aFileName = SUIT_FileDlg::getFileName( module()->getApp()->desktop(), aPolyName, aFilter, tr( "EXPORT_TO_SHAPE_FILE" ), false );
102     if (!aFileName.isEmpty())
103     {
104       SHPHandle hSHPHandle;
105       if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
106       {
107         hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );
108         for (int i = 1; i <= aPolyXYSeq.Size(); i++)
109           WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i));
110       }
111       else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
112       {
113         hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
114         for (int i = 1; i <= aPoly3DSeq.Size(); i++)
115           WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i));
116       }
117       else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty())
118       {
119         hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
120         for (int i = 1; i <= aLCSeq.Size(); i++)
121           WriteObjectLC(hSHPHandle, aLCSeq(i));
122       }
123       SHPClose( hSHPHandle );
124       commit();
125     }
126     else
127       abort();
128   }
129
130 }
131
132 void HYDROGUI_ExportFileOp::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
133 {
134   SHPObject     *aSHPObj;
135   std::vector<double> x, y;
136   std::vector<int> anPartStart;
137   
138   for (int i = 0; i < thePoly->NbSections(); i++)
139   {
140     anPartStart.push_back(x.size());
141     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
142     for (int j = 1; j <= aPointList.Size(); j++)
143     {
144       x.push_back( aPointList(j).X());
145       y.push_back( aPointList(j).Y()); 
146     }
147     if (thePoly->IsClosedSection(i))
148     {
149       x.push_back( aPointList(1).X());
150       y.push_back( aPointList(1).Y()); 
151     }
152   }
153     
154   aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
155   SHPWriteObject( theShpHandle, -1, aSHPObj );
156   SHPDestroyObject( aSHPObj );
157 }
158
159 void HYDROGUI_ExportFileOp::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
160 {
161   SHPObject     *aSHPObj;
162   std::vector<double> x, y, z;
163   std::vector<int> anPartStart;
164   
165   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
166   {
167     anPartStart.push_back(x.size());
168     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
169     for (int j = 1; j <= aPointList.Size(); j++)
170     {
171       x.push_back( aPointList(j).X());
172       y.push_back( aPointList(j).Y()); 
173       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
174     }
175     if ( thePoly->GetPolylineXY()->IsClosedSection(i))
176     {
177       x.push_back( aPointList(1).X());
178       y.push_back( aPointList(1).Y()); 
179       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
180
181     }
182   }
183   
184   aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
185   SHPWriteObject( theShpHandle, -1, aSHPObj );
186   SHPDestroyObject( aSHPObj );
187 }
188
189 void HYDROGUI_ExportFileOp::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC )
190 {
191
192   TopoDS_Shape aSh = theLC->GetShape();
193   if (aSh.IsNull())
194     return;
195   if (aSh.ShapeType() == TopAbs_FACE)
196   {
197     ProcessFace(TopoDS::Face(aSh), theShpHandle);
198   }
199   else if (aSh.ShapeType() == TopAbs_COMPOUND)
200   {
201     TopExp_Explorer Ex(aSh, TopAbs_FACE);
202     for (; Ex.More(); Ex.Next()) 
203     {
204       TopoDS_Face aF = TopoDS::Face(Ex.Current());   
205       if (aF.IsNull())
206         continue;
207       ProcessFace(aF, theShpHandle);
208     }
209   }
210   else
211     return;
212  
213 }
214
215 void HYDROGUI_ExportFileOp::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
216 {
217   SHPObject     *aSHPObj;
218   std::vector<double> x, y;
219   std::vector<int> anPartStart;
220   if (theFace.ShapeType() == TopAbs_FACE)
221   {
222     TopExp_Explorer Ex(theFace, TopAbs_WIRE);
223     int NbWires = 0;
224     for (; Ex.More(); Ex.Next()) 
225     {
226       TopoDS_Wire aW = TopoDS::Wire(Ex.Current());   
227       if (aW.IsNull())
228         continue;
229       NbWires++;
230       anPartStart.push_back(x.size());
231       TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
232       NCollection_Sequence<gp_Pnt2d> aPnts;
233       for (; aVEx.More(); aVEx.Next())
234       {
235         TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current()); 
236         if (aV.IsNull())
237           continue;
238         gp_Pnt P = BRep_Tool::Pnt(aV);
239         aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
240       }
241       NCollection_Sequence<gp_Pnt2d> aNPnts;
242       aNPnts.Append(aPnts.First());
243       for (int j = 1; j <= aPnts.Size() - 1; j++)
244       {
245         if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) 
246           aNPnts.Append(aPnts(j + 1));
247       }
248
249       for (int j = 1; j <= aNPnts.Size(); j++)
250       { 
251         x.push_back( aNPnts(j).X());
252         y.push_back( aNPnts(j).Y()); 
253       }
254       //x.push_back( aNPnts(1).X());
255       //y.push_back( aNPnts(1).Y()); 
256
257     }
258     
259     aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
260     SHPWriteObject( theShpHandle, -1, aSHPObj );
261     SHPDestroyObject( aSHPObj );
262   }
263   else
264     return;
265 }