]> SALOME platform Git repositories - modules/hydro.git/commitdiff
Salome HOME
Import of SHP files : import the contour of polygon as polyline.
authorisn <isn@opencascade.com>
Tue, 30 Aug 2016 13:44:46 +0000 (16:44 +0300)
committerisn <isn@opencascade.com>
Tue, 30 Aug 2016 13:44:46 +0000 (16:44 +0300)
info message will appear after successful importing of polygon => just to warn user that the file contained polygon(s) data not the actual polylines

src/HYDROData/HYDROData_ShapeFile.cxx
src/HYDROData/HYDROData_ShapeFile.h
src/HYDROGUI/HYDROGUI_ImportPolylineOp.cxx

index 37f759f0d29425d55fa6173b2d82ffc4a14739b2..21076836fe65c289e1bcfbe90d141456fe62de97 100644 (file)
@@ -379,7 +379,8 @@ bool HYDROData_ShapeFile::Parse(SHPHandle theHandle, ShapeType theType, int& the
   mySHPObjects.clear();
   SHPGetInfo( theHandle, NULL, &aShapeType, NULL, NULL );
   theShapeTypeOfFile = aShapeType;
-  bool ToRead = (theType == ShapeType_Polyline && (aShapeType == 3 || aShapeType == 13 || aShapeType == 23)) ||
+  bool ToRead = (theType == ShapeType_Polyline && 
+    (aShapeType == 3 || aShapeType == 13 || aShapeType == 23 || aShapeType == 5)) ||
    (theType == ShapeType_Polygon && aShapeType == 5);
   if (ToRead) 
   {
@@ -562,7 +563,7 @@ void HYDROData_ShapeFile::Free()
 
 
 void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
-  int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
+  int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, bool bReadAsPolyline)
 {
 
   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
@@ -577,19 +578,28 @@ void HYDROData_ShapeFile::ReadSHPPolyXY(Handle(HYDROData_Document) theDocument,
     else
       EndIndex = anObj->nVertices;
 
-    bool IsClosed = false;
     HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; 
-    if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
-        anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
+    if (!bReadAsPolyline)
     {
-      IsClosed = true;
-      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
+      bool IsClosed = false;
+      if (anObj->padfX[StartIndex] == anObj->padfX[EndIndex - 1] &&
+        anObj->padfY[StartIndex] == anObj->padfY[EndIndex - 1] )
+      {
+        IsClosed = true;
+        aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
+      }
+      else
+        aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
+
+      if (IsClosed)
+        EndIndex--;
     }
     else
-      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, false);
-    
-    if (IsClosed)
+    {
+      //polygon; contours always closed
+      aPolylineXY->AddSection( TCollection_AsciiString( ("poly_section_" + QString::number(i)).data()->toAscii()), aSectType, true);
       EndIndex--;
+    }
     for ( int k = StartIndex; k < EndIndex ; k++ )
     {
       HYDROData_PolylineXY::Point aSectPoint = gp_XY(anObj->padfX[k], anObj->padfY[k]);
@@ -675,7 +685,21 @@ void HYDROData_ShapeFile::ReadSHPPoly3D(Handle(HYDROData_Document) theDocument,
 
 }
 
-
+void HYDROData_ShapeFile::GetFreeIndices(std::vector<int>& theAllowedIndexes, QString strName, size_t theObjsSize,
+                                         QStringList theExistingNames, QString theBaseFileName)
+{
+  int anInd = 0;
+  for (;theAllowedIndexes.size() < theObjsSize;)
+  {
+    if (!theExistingNames.contains(theBaseFileName + strName + QString::number(anInd)))
+    {
+      theAllowedIndexes.push_back(anInd);
+      anInd++;
+    }
+    else
+      anInd++;
+  }
+}
 
 int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument, const QString& theFileName, 
   NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, int& theShapeTypeOfFile)
@@ -702,21 +726,12 @@ int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument,
     return 0;
   if (aHSHP->nShapeType == 3 || aHSHP->nShapeType == 23)
   {
-    anInd = 0;
-    for (;anAllowedIndexes.size() < mySHPObjects.size();)
-    {
-      if (!anExistingNames.contains(aBaseFileName + "_PolyXY_" + QString::number(anInd)))
-      {
-        anAllowedIndexes.push_back(anInd);
-        anInd++;
-      }
-      else
-        anInd++;
-    }
-    
+    size_t anObjsSize = mySHPObjects.size();
+    GetFreeIndices(anAllowedIndexes, "_PolyXY_", anObjsSize, anExistingNames, aBaseFileName);
     for (size_t i = 0; i < mySHPObjects.size(); i++ )
     {
-      ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
+      ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities, false);
     }
     aStat = 1;
   }
@@ -736,9 +751,23 @@ int HYDROData_ShapeFile::ImportPolylines(Handle(HYDROData_Document) theDocument,
         anInd++;
     }
     for (size_t i = 0; i < mySHPObjects.size(); i++ )
+    {
       ReadSHPPoly3D(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities);
+    }
     aStat = 1;
   }
+  else if (aHSHP->nShapeType == 5)
+  {
+    //import polygon's contours as polylines
+    size_t anObjsSize = mySHPObjects.size();
+    GetFreeIndices(anAllowedIndexes, "_PolyXY_", anObjsSize, anExistingNames, aBaseFileName);
+    for (size_t i = 0; i < mySHPObjects.size(); i++ )
+    {
+      ReadSHPPolyXY(theDocument, mySHPObjects[i], aBaseFileName, anAllowedIndexes[i], theEntities, true);
+    }
+    aStat = 2;
+  }
   else  
   {
     aStat = 0;
index fbeeb64358ec0bb0a78641f47ef4ff79dcbee1e9..d74e63d5fa8ccd971d490f8f1912e3f64dd5826f 100644 (file)
@@ -118,7 +118,7 @@ public:
 
   //Import Polyline
   void ReadSHPPolyXY(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
-                     int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities);
+                     int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities, bool bReadAsPolyline = false);
 
   void ReadSHPPoly3D(Handle(HYDROData_Document) theDocument, SHPObject* anObj, QString theFileName, 
                      int theInd, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities);
@@ -149,6 +149,9 @@ private:
 
   int TryOpenShapeFile(QString theFileName);
 
+  void GetFreeIndices(std::vector<int>& theAllowedIndexes, QString strName, size_t theObjsSize,
+                      QStringList theExistingNames, QString theBaseFileName);
+
 private:
   std::vector<SHPObject*> mySHPObjects;
   SHPHandle myHSHP;
index 7cdbd1ed7decc6f854da5299b6d5e828aec373b8..e130caa1d0586b19c4228b5eca98a84f0a1ee88c 100644 (file)
@@ -94,6 +94,12 @@ void HYDROGUI_ImportPolylineOp::onApply()
       int aStat = anImporter.ImportPolylines(doc(), aFileName, theEntities, aShapeTypeOfFile ); 
       if (aStat == 1)
         UpdateView(theEntities);
+      else if (aStat == 2)
+      {
+        UpdateView(theEntities);
+        SUIT_MessageBox::information(module()->getApp()->desktop(), 
+          tr( "IMPORT_POLYLINE" ), "Contour of the polygon(s) have been imported as polyline(s)");
+      }
       else
       {
         QString aMess = "Cannot import content of this file as polyline;\n";