Salome HOME
unified access to default colors
[modules/hydro.git] / src / HYDROData / HYDROData_ShapeFile.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 <HYDROData_ShapeFile.h>
24 #include <HYDROData_PolylineXY.h>
25 #include <HYDROData_Polyline3D.h>
26 #include <HYDROData_Bathymetry.h>
27 #include <HYDROData_LandCover.h>
28 #include <HYDROData_Profile.h>
29 #include <HYDROData_Iterator.h>
30
31 #include <QFile>
32 #include <QFileInfo>
33 #include <TopoDS.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopoDS_Wire.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <BRep_Tool.hxx>
40 #include <Precision.hxx>
41 #include <Handle_Geom_Curve.hxx>
42 #include <Handle_Geom_Line.hxx>
43 #include <Handle_Geom_TrimmedCurve.hxx>
44 #include <Geom_TrimmedCurve.hxx>
45 #include <BRepBuilderAPI_MakeEdge.hxx>
46 #include <BRepBuilderAPI_MakeWire.hxx>
47 #include <BRepBuilderAPI_MakeFace.hxx>
48 #include <gp_Pln.hxx>
49 #include <BRepLib.hxx>
50 #include <ShapeFix_Shape.hxx>
51 #include <TopTools_SequenceOfShape.hxx>
52 #include <QColor>
53 #include <BRepTopAdaptor_FClass2d.hxx>
54
55 HYDROData_ShapeFile::HYDROData_ShapeFile() : myHSHP(NULL)
56
57 }
58
59 HYDROData_ShapeFile::~HYDROData_ShapeFile()
60 {
61   Free();
62 }
63
64 void HYDROData_ShapeFile::Export(const QString& aFileName, 
65   NCollection_Sequence<Handle_HYDROData_PolylineXY> aPolyXYSeq,
66   NCollection_Sequence<Handle_HYDROData_Polyline3D> aPoly3DSeq,
67   NCollection_Sequence<Handle_HYDROData_LandCover> aLCSeq,
68   QStringList& aNonExpList)
69 {
70   SHPHandle hSHPHandle;
71   if (!aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty())
72   {
73     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARC );        
74     for (int i = 1; i <= aPolyXYSeq.Size(); i++)
75       if (WriteObjectPolyXY(hSHPHandle, aPolyXYSeq(i)) != 1)
76         aNonExpList.append(aPolyXYSeq(i)->GetName());
77
78   }
79   else if (aPolyXYSeq.IsEmpty() && !aPoly3DSeq.IsEmpty())
80   {
81     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_ARCZ );
82     for (int i = 1; i <= aPoly3DSeq.Size(); i++)
83       if (WriteObjectPoly3D(hSHPHandle, aPoly3DSeq(i)) != 1)
84         aNonExpList.append(aPoly3DSeq(i)->GetName());
85   }
86   else if (aPolyXYSeq.IsEmpty() && aPoly3DSeq.IsEmpty() && !aLCSeq.IsEmpty())
87   {
88     hSHPHandle = SHPCreate( aFileName.toAscii().data(), SHPT_POLYGON );
89     for (int i = 1; i <= aLCSeq.Size(); i++)
90       if (WriteObjectLC(hSHPHandle, aLCSeq(i)) != 1)
91         aNonExpList.append(aLCSeq(i)->GetName());
92   }
93   if (hSHPHandle->nRecords > 0)
94     SHPClose( hSHPHandle );
95   else
96   {
97     SHPClose( hSHPHandle );
98     QString aFN = aFileName.simplified();
99     remove (aFN.toStdString().c_str());
100     remove (aFN.replace( ".shp", ".shx", Qt::CaseInsensitive).toStdString().c_str());
101   }
102 }
103
104 int HYDROData_ShapeFile::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     if (thePoly->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
112       return -1;
113
114   for (int i = 0; i < thePoly->NbSections(); i++)
115   {
116     anPartStart.push_back(x.size());
117     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPoints(i);
118     for (int j = 1; j <= aPointList.Size(); j++)
119     {
120       x.push_back( aPointList(j).X());
121       y.push_back( aPointList(j).Y()); 
122     }
123     if (thePoly->IsClosedSection(i))
124     {
125       x.push_back( aPointList(1).X());
126       y.push_back( aPointList(1).Y()); 
127     }
128   }
129     
130   aSHPObj = SHPCreateObject( SHPT_ARC, -1, thePoly->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
131   SHPWriteObject( theShpHandle, -1, aSHPObj );
132   SHPDestroyObject( aSHPObj );
133   return 1;
134 }
135
136 int HYDROData_ShapeFile::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     if (thePoly->GetPolylineXY()->GetSectionType(i) == HYDROData_IPolyline::SECTION_SPLINE)
144       return -1;
145   
146   for (int i = 0; i < thePoly->GetPolylineXY()->NbSections(); i++)
147   {
148     anPartStart.push_back(x.size());
149     HYDROData_PolylineXY::PointsList aPointList = thePoly->GetPolylineXY()->GetPoints(i);
150     for (int j = 1; j <= aPointList.Size(); j++)
151     {
152       x.push_back( aPointList(j).X());
153       y.push_back( aPointList(j).Y()); 
154       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(j).X(), aPointList(j).Y())));
155     }
156     if ( thePoly->GetPolylineXY()->IsClosedSection(i))
157     {
158       x.push_back( aPointList(1).X());
159       y.push_back( aPointList(1).Y()); 
160       z.push_back(thePoly->GetAltitudeObject()->GetAltitudeForPoint(gp_XY (aPointList(1).X(), aPointList(1).Y())));
161
162     }
163   }
164   
165   aSHPObj = SHPCreateObject( SHPT_ARCZ, -1, thePoly->GetPolylineXY()->NbSections(), &anPartStart[0], NULL, x.size(), &x[0], &y[0], &z[0], NULL );
166   SHPWriteObject( theShpHandle, -1, aSHPObj );
167   SHPDestroyObject( aSHPObj );
168   return 1;
169 }
170
171 int HYDROData_ShapeFile::WriteObjectLC(SHPHandle theShpHandle, Handle_HYDROData_LandCover theLC )
172 {
173   TopoDS_Shape aSh = theLC->GetShape();
174   if (aSh.IsNull())
175     return 0;
176   TopExp_Explorer anEdgeEx(aSh, TopAbs_EDGE);
177   for (; anEdgeEx.More(); anEdgeEx.Next()) 
178   {
179     TopoDS_Edge E = TopoDS::Edge(anEdgeEx.Current());
180     double aFP, aLP;
181     Handle_Geom_Curve aCur = BRep_Tool::Curve(E, aFP, aLP);
182     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCur);
183     if (aLine.IsNull())
184     {
185       Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
186       if (!aTC.IsNull())
187       {
188         Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aTC->BasisCurve());
189         if (aLine.IsNull())
190           return -1;
191       }
192       else
193         return -1;
194     }
195
196   }
197   if (aSh.ShapeType() == TopAbs_FACE)
198   {
199     ProcessFace(TopoDS::Face(aSh), theShpHandle);
200   }
201   else if (aSh.ShapeType() == TopAbs_COMPOUND)
202   {
203     TopExp_Explorer Ex(aSh, TopAbs_FACE);
204     for (; Ex.More(); Ex.Next()) 
205     {
206       TopoDS_Face aF = TopoDS::Face(Ex.Current());   
207       if (aF.IsNull())
208         continue;
209       ProcessFace(aF, theShpHandle);
210     }
211   }
212   else
213     return 0;
214
215   return 1;
216  
217 }
218
219 void HYDROData_ShapeFile::ProcessFace(TopoDS_Face theFace, SHPHandle theShpHandle)
220 {
221   SHPObject     *aSHPObj;
222   std::vector<double> x, y;
223   std::vector<int> anPartStart;
224   if (theFace.ShapeType() == TopAbs_FACE)
225   {
226     TopExp_Explorer Ex(theFace, TopAbs_WIRE);
227     int NbWires = 0;
228     for (; Ex.More(); Ex.Next()) 
229     {
230       TopoDS_Wire aW = TopoDS::Wire(Ex.Current());   
231       if (aW.IsNull())
232         continue;
233       NbWires++;
234       anPartStart.push_back(x.size());
235       TopExp_Explorer aVEx(aW, TopAbs_VERTEX);
236       NCollection_Sequence<gp_Pnt2d> aPnts;
237       for (; aVEx.More(); aVEx.Next())
238       {
239         TopoDS_Vertex aV = TopoDS::Vertex(aVEx.Current()); 
240         if (aV.IsNull())
241           continue;
242         gp_Pnt P = BRep_Tool::Pnt(aV);
243         aPnts.Append(gp_Pnt2d(P.X(), P.Y()));
244       }
245       NCollection_Sequence<gp_Pnt2d> aNPnts;
246       aNPnts.Append(aPnts.First());
247       for (int j = 1; j <= aPnts.Size() - 1; j++)
248       {
249         if (!aPnts(j).IsEqual(aPnts(j + 1), Precision::Confusion())) 
250           aNPnts.Append(aPnts(j + 1));
251       }
252
253       for (int j = 1; j <= aNPnts.Size(); j++)
254       { 
255         x.push_back( aNPnts(j).X());
256         y.push_back( aNPnts(j).Y()); 
257       }
258       //x.push_back( aNPnts(1).X());
259       //y.push_back( aNPnts(1).Y()); 
260
261     }
262     
263     aSHPObj = SHPCreateObject( SHPT_POLYGON, -1, NbWires, &anPartStart[0], NULL, x.size(), &x[0], &y[0], NULL, NULL );
264     SHPWriteObject( theShpHandle, -1, aSHPObj );
265     SHPDestroyObject( aSHPObj );
266   }
267   else
268     return;
269 }
270
271 bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& theShapeTypeOfFile)
272 {
273   int aShapeType;
274   mySHPObjects.clear();
275   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
276   theShapeTypeOfFile = aShapeType;
277   bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
278    (theType == ShapeType_Polygon && aShapeType == 5);
279   if (ToRead) 
280   {
281     for (int i = 0; i < theHandle->nRecords; i++) 
282       mySHPObjects.push_back(SHPReadObject(theHandle, i));
283     return true;
284   }
285   else
286     return false;
287 }
288
289 void HYDROData_ShapeFile::ReadSHPPolygon(SHPObject* anObj, int i, TopoDS_Face& F)
290 {
291   TopoDS_Wire W;
292   TopoDS_Edge E; 
293   int nParts = anObj->nParts;
294   gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
295
296   //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
297   //sfs->FixFaceTool()->FixOrientationMode() = 1;
298   TopTools_SequenceOfShape aWires;
299   for ( int i = 0 ; i < nParts ; i++ )
300   { 
301     BRepBuilderAPI_MakeWire aBuilder;
302     int StartIndex = anObj->panPartStart[i];
303     int EndIndex;
304     if (i != nParts - 1)
305       EndIndex = anObj->panPartStart[i + 1];
306     else
307       EndIndex = anObj->nVertices;
308
309     for ( int k = StartIndex; k < EndIndex - 1  ; k++ )
310     {
311       gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
312       gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
313       if (P1.Distance(P2) < Precision::Confusion())
314         continue;
315       BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
316       aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
317     }
318     
319     aBuilder.Build();
320     W = TopoDS::Wire(aBuilder.Shape());
321     W.Orientation(TopAbs_FORWARD);
322     BRepBuilderAPI_MakeFace aDB(pln, W);
323     TopoDS_Face aDummyFace = TopoDS::Face(aDB.Shape());
324     BRepTopAdaptor_FClass2d FClass(aDummyFace, Precision::PConfusion());
325     if ( i == 0 && FClass.PerformInfinitePoint() == TopAbs_OUT) 
326       W.Reverse();
327     if ( i > 0 && FClass.PerformInfinitePoint() != TopAbs_IN) 
328       W.Reverse();
329    
330     aWires.Append(W);
331   }
332   
333   BRepBuilderAPI_MakeFace aFBuilder(pln, TopoDS::Wire(aWires(1)));
334   for (int i = 2; i <= aWires.Length(); i++)
335     aFBuilder.Add(TopoDS::Wire(aWires(i)));
336   TopoDS_Face DF = TopoDS::Face(aFBuilder.Shape());
337
338   BRepLib::BuildCurves3d(DF);  
339   if(!DF.IsNull()) 
340   {
341     //sfs->Init ( DF );
342     //sfs->Perform();
343     F = DF; //TopoDS::Face(sfs->Shape());
344   }
345 }
346
347 int HYDROData_ShapeFile::ImportLandCovers(const QString theFileName, QStringList& thePolygonsList, TopTools_SequenceOfShape& theFaces, int& theShapeTypeOfFile)
348 {
349   Free();
350   int Stat = TryOpenShapeFile(theFileName);
351   if (Stat != 0)
352     return Stat;
353   myHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
354   if (!Parse(myHSHP, HYDROData_ShapeFile::ShapeType_Polygon, theShapeTypeOfFile))
355     return 0;
356   for (size_t i = 0; i < mySHPObjects.size(); i++)
357     thePolygonsList.append("polygon_" + QString::number(i + 1));
358    
359   TopoDS_Face aF;
360   if (myHSHP->nShapeType == 5)
361   {
362     for (size_t i = 0; i < mySHPObjects.size(); i++) 
363     {
364        ReadSHPPolygon(mySHPObjects[i], i, aF);
365        theFaces.Append(aF);
366     }
367     return 1;
368   }
369   else
370     return 0;
371 }
372
373 void HYDROData_ShapeFile::Free()
374 {  
375   for (size_t i = 0; i < mySHPObjects.size(); i++ )
376     free (mySHPObjects[i]);
377
378   mySHPObjects.clear();
379   if (myHSHP != NULL)
380   {
381     SHPClose(myHSHP);
382     myHSHP = NULL; 
383   }
384 }
385
386
387 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
388   int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
389 {
390
391   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
392   
393   int nParts = anObj->nParts;
394   for ( int i = 0 ; i < nParts ; i++ )
395   {
396     int StartIndex = anObj->panPartStart[i];
397     int EndIndex;
398     if (i != nParts - 1)
399       EndIndex = anObj->panPartStart[i + 1];
400     else
401       EndIndex = anObj->nVertices;
402
403     bool IsClosed = false;
404     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
405     if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
406         anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
407     {
408       IsClosed = true;
409       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
410     }
411     else
412       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
413     
414     if (IsClosed)
415       EndIndex--;
416     for ( int k = StartIndex; k < EndIndex ; k++ )
417     {
418       HYDROData_PolylineXY::Point aSectPoint;
419       aSectPoint.SetX( anObj->padfX[k] );
420       aSectPoint.SetY( anObj->padfY[k] );
421       aPolylineXY->AddPoint( i, aSectPoint );
422     }
423
424   }
425   
426   aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
427   aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
428   
429   aPolylineXY->Update();
430   theEntities.Append(aPolylineXY);
431
432 }
433
434 void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
435   int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
436 {
437   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
438
439   Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
440
441   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
442   HYDROData_Bathymetry::AltitudePoints aAPoints;
443
444   int nParts = anObj->nParts;
445   for ( int i = 0 ; i < nParts ; i++ )
446   {
447     //bool aSectClosure = true;
448     int StartIndex = anObj->panPartStart[i];
449     int EndIndex;
450     if (i != nParts - 1)
451       EndIndex = anObj->panPartStart[i + 1];
452     else
453       EndIndex = anObj->nVertices;
454
455     bool IsClosed = false;
456     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
457     if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
458         anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] &&
459         anObj->padfZ[StartIndex] == anObj->padfZ[EndIndex - 1])
460     {
461       IsClosed = true;
462       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true );
463     }
464     else
465       aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false  );
466     
467     if (IsClosed)
468       EndIndex--;
469     for ( int k = StartIndex ; k < EndIndex ; k++ )
470     {
471       HYDROData_PolylineXY::Point aSectPoint;
472       aSectPoint.SetX( anObj->padfX[k] );
473       aSectPoint.SetY( anObj->padfY[k] );
474       aPolylineXY->AddPoint( i, aSectPoint );
475       aAPoints.Append(gp_XYZ (anObj->padfX[k], anObj->padfY[k], anObj->padfZ[k]));
476     }
477   }
478
479
480   QString aBathName = theFileName + "_bath_" + QString::number(theInd);
481   QString aPolyXYName = theFileName + "_polyXY_" + QString::number(theInd);
482   QString aPoly3DName = theFileName + "_poly3D_" + QString::number(theInd);
483
484   aPolylineXY->SetName( aPolyXYName );
485   aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
486   aPolylineXY->Update();
487   
488   aBath->SetAltitudePoints(aAPoints);
489   aBath->SetName( aBathName );
490
491   aPolylineObj->SetPolylineXY (aPolylineXY, false);
492   aPolylineObj->SetAltitudeObject(aBath);
493
494   aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
495   aPolylineObj->SetName( aPoly3DName );
496   
497   aPolylineObj->Update();
498   theEntities.Append(aPolylineXY);
499   theEntities.Append(aPolylineObj);
500
501 }
502
503
504
505 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName, 
506   NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
507 {
508   //Free();
509   int aStat = TryOpenShapeFile(theFileName);
510   if (aStat != 0)
511     return aStat;
512
513   HYDROData_Iterator anIter( theDocument );
514   int anInd = 0;
515   QStringList anExistingNames;
516   std::vector<int> anAllowedIndexes;
517   for( ; anIter.More(); anIter.Next() )
518     anExistingNames.push_back(anIter.Current()->GetName());
519
520   SHPHandle aHSHP;
521   aHSHP = SHPOpen( theFileName.toAscii().data(), "rb" );
522   
523   QFileInfo aFileInfo(theFileName);
524   QString aBaseFileName = aFileInfo.baseName();
525
526   if (!Parse(aHSHP, HYDROData_ShapeFile::ShapeType_Polyline, theShapeTypeOfFile))
527     return 0;
528   if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
529   {
530     anInd = 0;
531     for (;anAllowedIndexes.size() < mySHPObjects.size();)
532     {
533       if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
534       {
535         anAllowedIndexes.push_back(anInd);
536         anInd++;
537       }
538       else
539         anInd++;
540     }
541     
542     for (size_t i = 0; i < mySHPObjects.size(); i++ )
543     {
544       ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
545     }
546     aStat = 1;
547   }
548   else if (aHSHP->nShapeType == 13)
549   {
550     anInd = 0;
551     for (;anAllowedIndexes.size() < mySHPObjects.size();)
552     {
553       if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)) &&
554           !anExistingNames.contains(aBaseFileName + "_Poly3D_" + QString::number(anInd)) &&
555           !anExistingNames.contains(aBaseFileName + "_Bath_" + QString::number(anInd)))
556       {
557         anAllowedIndexes.push_back(anInd);
558         anInd++;
559       }
560       else
561         anInd++;
562     }
563     for (size_t i = 0; i < mySHPObjects.size(); i++ )
564       ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
565     aStat = 1;
566   }
567   else  
568   {
569     aStat = 0;
570   }
571   
572   for (size_t i = 0; i < mySHPObjects.size(); i++ )
573     free (mySHPObjects[i]);
574
575   mySHPObjects.clear();
576   SHPClose(aHSHP);
577   return aStat;
578 }
579
580 QString HYDROData_ShapeFile::GetShapeTypeName(int theType)
581 {
582   switch (theType)
583   {
584     case 0:
585       return "null shape";
586     case 1:
587       return "point (unsupported by HYDRO)";
588     case 3:
589       return "arc/polyline (supported by HYDRO)";
590     case 5:
591       return "polygon (supported by HYDRO)";
592     case 8:
593       return "multipoint (unsupported by HYDRO)";
594     case 11:
595       return "pointZ (unsupported by HYDRO)";
596     case 13:
597       return "arcZ/polyline (supported by HYDRO)";
598     case 15:
599       return "polygonZ (unsupported by HYDRO)";
600     case 18:
601       return "multipointZ (unsupported by HYDRO)";
602     case 21:
603       return "pointM (unsupported by HYDRO)";
604     case 23:
605       return "arcM/polyline (supported by HYDRO)";
606     case 25:
607       return "polygonM (unsupported by HYDRO)";
608     case 28:
609       return "multipointM (unsupported by HYDRO)";
610     case 31:
611       return "multipatch (unsupported by HYDRO)";
612     default:
613       return "unknown";
614   }
615 }
616
617 int HYDROData_ShapeFile::TryOpenShapeFile(QString theFileName)
618 {
619   QString aSHPfile = theFileName.simplified();
620   QString aSHXfile = theFileName.simplified().replace( ".shp", ".shx", Qt::CaseInsensitive);
621   FILE* pFileSHP = NULL;
622   pFileSHP = fopen (aSHPfile.toAscii().data(), "r");
623   FILE* pFileSHX = NULL;
624   pFileSHX = fopen (aSHXfile.toAscii().data(), "r");
625
626   if (pFileSHP == NULL || pFileSHX == NULL)
627   {
628     if (pFileSHP == NULL)
629       return -1;
630     if (pFileSHX == NULL)
631       return -2;
632   }
633   
634   fclose (pFileSHP);
635   fclose (pFileSHX);
636   return 0;
637 }