Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' 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 <TopoDS_Edge.hxx>
50 #include <BRep_Tool.hxx>
51 #include <Precision.hxx>
52 #include <Handle_Geom_Curve.hxx>
53 #include <Handle_Geom_Line.hxx>
54 #include <Handle_Geom_TrimmedCurve.hxx>
55 #include <Geom_TrimmedCurve.hxx>
56
57 HYDROGUI_ExportFileOp::HYDROGUI_ExportFileOp( HYDROGUI_Module* theModule )
58 : HYDROGUI_Operation( theModule )
59 {
60   setName( tr( "EXPORT_TO_SHAPE_FILE" ) );
61 }
62
63 HYDROGUI_ExportFileOp::~HYDROGUI_ExportFileOp()
64 {
65 }
66
67 void HYDROGUI_ExportFileOp::startOperation()
68 {
69   HYDROGUI_Operation::startOperation();
70   
71   QString aFilter( tr("SHP_FILTER") ); 
72   
73   Handle(HYDROData_PolylineXY) aPolyXY;
74   Handle(HYDROData_Polyline3D) aPoly3D;
75   Handle(HYDROData_LandCover) aLC;
76   NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq;
77   NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq;
78   NCollection_Sequence<Handle_HYDROData_LandCover> aLCSeq;
79   
80   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
81   for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
82   {
83     aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( aSeq.Value( anIndex ));
84     if (!aPolyXY.IsNull())
85       aPolyXYSeq.Append(aPolyXY);
86
87     aPoly3D = Handle(HYDROData_Polyline3D)::DownCast( aSeq.Value( anIndex ));
88     if (!aPoly3D.IsNull())
89       aPoly3DSeq.Append(aPoly3D);
90
91     aLC = Handle(HYDROData_LandCover)::DownCast( aSeq.Value( anIndex ));
92     if (!aLC.IsNull())
93       aLCSeq.Append(aLC);
94   }
95
96   if (!aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
97     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "EXPORT_TO_SHAPE_FILE" ), tr("ERROR_POLYLINES_OF_DIFF_KIND"));
98   else
99   {
100     QString aName = "";
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     if (aLCSeq.Size() == 1 && aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
106       aName = aLCSeq(1)->GetName();
107     QString aFileName = SUIT_FileDlg::getFileName( module()->getApp()->desktop(), aName, aFilter, tr( "EXPORT_TO_SHAPE_FILE" ), false );
108     if (!aFileName.isEmpty())
109     {
110       SHPHandle hSHPHandle;
111       QStringList aNonExpList;
112       if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
113       {
114         hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );        
115         for (int i = 1; i <= aPolyXYSeq.Size(); i++)
116           if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
117             aNonExpList.append(aPolyXYSeq(i)->GetName());
118
119       }
120       else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
121       {
122         hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
123         for (int i = 1; i <= aPoly3DSeq.Size(); i++)
124           if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
125             aNonExpList.append(aPoly3DSeq(i)->GetName());
126       }
127       else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty())
128       {
129         hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
130         for (int i = 1; i <= aLCSeq.Size(); i++)
131           if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
132             aNonExpList.append(aLCSeq(i)->GetName());
133       }
134       SHPClose( hSHPHandle );
135       if (!aNonExpList.empty())
136       {
137         QString aMessage = tr("CANNOT_BE_EXPORTED") + "\n";
138         foreach (QString anNonExpEntName, aNonExpList)
139           aMessage += anNonExpEntName + "\n"; 
140         SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "EXPORT_TO_SHAPE_FILE" ), aMessage);
141       }
142       commit();
143     }
144     else
145       abort();
146   }
147
148 }
149
150 int HYDROGUI_ExportFileOp::WriteObjectPolyXY(SHPHandle theShpHandle, Handle_HYDROData_PolylineXY thePoly )
151 {
152   SHPObject     *aSHPObj;
153   std::vector<double> x, y;
154   std::vector<int> anPartStart;
155   
156   for (int i = 0; i < thePoly->NbSections(); i++)    
157     if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
158       return -1;
159
160   for (int i = 0; i < thePoly->NbSections(); i++)
161   {
162     anPartStart.push_back(x.size());
163     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
164     for (int j = 1; j <= aPointList.Size(); j++)
165     {
166       x.push_back( aPointList(j).X());
167       y.push_back( aPointList(j).Y()); 
168     }
169     if (thePoly->IsClosedSection(i))
170     {
171       x.push_back( aPointList(1).X());
172       y.push_back( aPointList(1).Y()); 
173     }
174   }
175     
176   aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
177   SHPWriteObject( theShpHandle, -1, aSHPObj );
178   SHPDestroyObject( aSHPObj );
179   return 1;
180 }
181
182 int HYDROGUI_ExportFileOp::WriteObjectPoly3D(SHPHandle theShpHandle, Handle_HYDROData_Polyline3D thePoly )
183 {
184   SHPObject     *aSHPObj;
185   std::vector<double> x, y, z;
186   std::vector<int> anPartStart;
187
188   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)    
189     if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
190       return -1;
191   
192   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
193   {
194     anPartStart.push_back(x.size());
195     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
196     for (int j = 1; j <= aPointList.Size(); j++)
197     {
198       x.push_back( aPointList(j).X());
199       y.push_back( aPointList(j).Y()); 
200       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
201     }
202     if ( thePoly->GetPolylineXY()->IsClosedSection(i))
203     {
204       x.push_back( aPointList(1).X());
205       y.push_back( aPointList(1).Y()); 
206       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
207
208     }
209   }
210   
211   aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
212   SHPWriteObject( theShpHandle, -1, aSHPObj );
213   SHPDestroyObject( aSHPObj );
214   return 1;
215 }
216
217 int HYDROGUI_ExportFileOp::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC )
218 {
219   TopoDS_Shape aSh = theLC->GetShape();
220   if (aSh.IsNull())
221     return 0;
222   TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE);
223   for (; anEdgeEx.More(); anEdgeEx.Next()) 
224   {
225     TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
226     double aFP, aLP;
227     Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
228     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
229     if (aLine.IsNull())
230     {
231       Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
232       if (!aTC.IsNull())
233       {
234         Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
235         if (aLine.IsNull())
236           return -1;
237       }
238       else
239         return -1;
240     }
241
242   }
243   if (aSh.ShapeType() == TopAbs_FACE)
244   {
245     ProcessFace(TopoDS::Face(aSh), theShpHandle);
246   }
247   else if (aSh.ShapeType() == TopAbs_COMPOUND)
248   {
249     TopExp_Explorer Ex(aSh, TopAbs_FACE);
250     for (; Ex.More(); Ex.Next()) 
251     {
252       TopoDS_Face aF = TopoDS::Face(Ex.Current());   
253       if (aF.IsNull())
254         continue;
255       ProcessFace(aF, theShpHandle);
256     }
257   }
258   else
259     return 0;
260
261   return 1;
262  
263 }
264
265 void HYDROGUI_ExportFileOp::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
266 {
267   SHPObject     *aSHPObj;
268   std::vector<double> x, y;
269   std::vector<int> anPartStart;
270   if (theFace.ShapeType() == TopAbs_FACE)
271   {
272     TopExp_Explorer Ex(theFace, TopAbs_WIRE);
273     int NbWires = 0;
274     for (; Ex.More(); Ex.Next()) 
275     {
276       TopoDS_Wire aW = TopoDS::Wire(Ex.Current());   
277       if (aW.IsNull())
278         continue;
279       NbWires++;
280       anPartStart.push_back(x.size());
281       TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
282       NCollection_Sequence<gp_Pnt2d> aPnts;
283       for (; aVEx.More(); aVEx.Next())
284       {
285         TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current()); 
286         if (aV.IsNull())
287           continue;
288         gp_Pnt P = BRep_Tool::Pnt(aV);
289         aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
290       }
291       NCollection_Sequence<gp_Pnt2d> aNPnts;
292       aNPnts.Append(aPnts.First());
293       for (int j = 1; j <= aPnts.Size() - 1; j++)
294       {
295         if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) 
296           aNPnts.Append(aPnts(j + 1));
297       }
298
299       for (int j = 1; j <= aNPnts.Size(); j++)
300       { 
301         x.push_back( aNPnts(j).X());
302         y.push_back( aNPnts(j).Y()); 
303       }
304       //x.push_back( aNPnts(1).X());
305       //y.push_back( aNPnts(1).Y()); 
306
307     }
308     
309     aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
310     SHPWriteObject( theShpHandle, -1, aSHPObj );
311     SHPDestroyObject( aSHPObj );
312   }
313   else
314     return;
315 }