Salome HOME
add python method HYDROData_PolylineXY.ImportShapesFromFile('name') BR_PY3
authorPaul RASCLE <paul.rascle@edf.fr>
Sat, 15 Feb 2020 14:14:02 +0000 (15:14 +0100)
committerPaul RASCLE <paul.rascle@edf.fr>
Sat, 15 Feb 2020 14:15:07 +0000 (15:15 +0100)
python example:
from HYDROPy import *
HYDROData_PolylineXY.ImportShapesFromFile("/tmp/myshape.shp")
myShape = hydro_doc.FindObjectByName('myshape_PolyXY_0')

src/HYDROData/HYDROData_PolylineXY.cxx
src/HYDROData/HYDROData_PolylineXY.h
src/HYDROPy/HYDROData_PolylineXY.sip

index 1e27572f2e1adeb6326e811a31dd6990cd166604..9da8cddb93792858a8a0d49ca421f214d36e72de 100644 (file)
 
 #include "HYDROData_PolylineXY.h"
 
+#include <HYDROData_Polyline3D.h>
+#include <HYDROData_Bathymetry.h>
+#include <HYDROData_Iterator.h>
+#include <HYDROData_ShapeFile.h>
+#include <HYDROData_Profile.h>
+
 #include "HYDROData_BSplineOperation.h"
 #include "HYDROData_Document.h"
 #include "HYDROData_ShapesTool.h"
@@ -84,7 +90,7 @@
 #include <QPainterPath>
 #include <QVariant>
 
-//#define _DEVDEBUG_
+#define _DEVDEBUG_
 #include "HYDRO_trace.hxx"
 #include <BRepTools.hxx>
 #include <sstream>
@@ -1647,3 +1653,185 @@ bool HYDROData_PolylineXY::GetDBFInfo(QStringList& theDBFTable) const
     return false;
   return true;
 }
+
+
+bool HYDROData_PolylineXY::ImportShapesFromFile(const QString& aFileName)
+{
+  NCollection_Sequence<Handle(HYDROData_Entity)> importedEntities;
+  if (aFileName.isEmpty())
+    return false;
+
+  QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
+  anExt.toLower();
+  bool importXY = false;
+  Handle(HYDROData_Document) theDocument = HYDROData_Document::Document();
+
+//     if (anExt == "xyz")
+//     {
+//       importXY = SUIT_MessageBox::question( module->getApp()->desktop(),
+//             tr( "IMPORT_POLYLINE" ),
+//             tr( "IMPORT_POLYLINE_XY_PART_ONLY" ),
+//             QMessageBox::Yes | QMessageBox::No,
+//             SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
+//     }
+  importXY = true;
+  if (anExt == "shp")
+    {
+      HYDROData_ShapeFile anImporter;
+      NCollection_IndexedDataMap<Handle(HYDROData_Entity), SHPObject*> theEntitiesToSHPObj;
+      NCollection_Sequence<Handle(HYDROData_Entity)> PolyEnt;
+      int aShapeTypeOfFile = -1;
+      int aStat = anImporter.OpenAndParse(aFileName);
+      if (aStat == 1)
+        {
+          aStat = anImporter.ImportPolylines(theDocument, aFileName, theEntitiesToSHPObj,
+                                             HYDROData_ShapeFile::ImportShapeType_All);
+          for (int k = 1; k <= theEntitiesToSHPObj.Extent(); k++)
+            {
+              PolyEnt.Append(theEntitiesToSHPObj.FindKey(k));
+            }
+          if (aStat == 1 || aStat == 2)
+            {
+              //try to import DBF
+              QString aDBFFileName;
+              aDBFFileName = aFileName.simplified().replace(aFileName.simplified().size() - 4, 4, ".dbf");
+              bool DBF_Stat = anImporter.DBF_OpenDBF(aDBFFileName);
+              if (DBF_Stat)
+                {
+                  QStringList aFieldList = anImporter.DBF_GetFieldList();
+                  int nbRecords = anImporter.DBF_GetNbRecords();
+                  //assert(theEntitiesToSHPObj.Extent() == nbRecords);
+                  if (theEntitiesToSHPObj.Extent() == nbRecords)
+                    {
+                      int indNameAttrFound = -1;
+                      int k = 0;
+                      bool bUseNameAttrFound = false;
+                      for (QStringList::iterator it = aFieldList.begin(); it != aFieldList.end(); it++, k++)
+                        if (QString::compare(*it, "name", Qt::CaseInsensitive) == 0)
+                          {
+                            indNameAttrFound = k;
+                            break;
+                          }
+
+                      if (indNameAttrFound != -1)
+                        bUseNameAttrFound = true;
+
+//                       if (indNameAttrFound != -1)
+//                             bUseNameAttrFound = SUIT_MessageBox::question( module->getApp()->desktop(),
+//                                                        tr( "IMPORT_POLYLINE" ),
+//                                                        tr( "IMPORT_POLYLINE_USE_NAME_ATTR" ),
+//                                                        QMessageBox::Yes | QMessageBox::No,
+//                                                        SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
+
+                      std::vector<std::vector<HYDROData_ShapeFile::DBF_AttrValue>> anAttrVV;
+                      for (int i = 0; i < aFieldList.size(); i++)
+                        {
+                          std::vector<HYDROData_ShapeFile::DBF_AttrValue> anAttrV;
+                          anAttrVV.push_back(anAttrV);
+                          anImporter.DBF_GetAttributeList(i, anAttrVV[i]);
+                        }
+
+                      int indNULL = 1;
+                      for (int i = 1; i <= theEntitiesToSHPObj.Extent(); i++)
+                        {
+                          Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast(
+                              theEntitiesToSHPObj.FindKey(i));
+                          QStringList aDBFinfo;
+                          aDBFinfo << aFieldList; //first, the table header
+                          for (int j = 0; j < aFieldList.size(); j++)
+                            {
+                              QString attr;
+                              const HYDROData_ShapeFile::DBF_AttrValue &attrV = anAttrVV[j][i - 1];
+                              if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_String)
+                                aDBFinfo << attrV.myStrVal;
+                              else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Integer)
+                                aDBFinfo << QString::number(attrV.myIntVal);
+                              else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Double)
+                                aDBFinfo << QString::number(attrV.myDoubleVal);
+                              else
+                                aDBFinfo << "";
+                            }
+                          //assert(aDBFinfo.size() / 2 == aFieldList.size());
+                          aPolylineXY->SetDBFInfo(aDBFinfo);
+                          if (bUseNameAttrFound)
+                            {
+                              QString nameOfPoly = aDBFinfo[aDBFinfo.size() / 2 + indNameAttrFound];
+                              if (!nameOfPoly.isEmpty())
+                                aPolylineXY->SetName(nameOfPoly);
+                              else
+                                {
+                                  aPolylineXY->SetName("null_name_" + QString::number(indNULL));
+                                  indNULL++;
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+          if (anImporter.GetShapeType() == 13) //Polyline 3D
+            {
+//               HYDROGUI_ImportPolylineDlg* aDLG = new HYDROGUI_ImportPolylineDlg( module->getApp()->desktop(), PolyEnt );
+//               aDLG->setModal( true );
+//               aDLG->setWindowTitle(tr("IMPORT_POLYLINE"));
+//               //QApplication::restoreOverrideCursor();
+//               if( aDLG->exec()==QDialog::Accepted )
+
+              NCollection_Sequence<Handle(HYDROData_Entity)> CheckedPolylines = PolyEnt; //aDLG->GetCheckedPolylines();
+              NCollection_IndexedDataMap<Handle(HYDROData_Entity), SHPObject*> CheckedEntitiesToSHPObj;
+              for (int k = 1; k <= CheckedPolylines.Size(); k++)
+                {
+                  SHPObject *const*SHPObkP = theEntitiesToSHPObj.Seek(CheckedPolylines(k));
+                  if (SHPObkP != NULL)
+                    CheckedEntitiesToSHPObj.Add(CheckedPolylines(k), *SHPObkP);
+                }
+              NCollection_Sequence<Handle(HYDROData_Entity)> theEntitiesPoly3D;
+              anImporter.ImportPolylines3D(theDocument, CheckedEntitiesToSHPObj, theEntitiesPoly3D,
+                                           HYDROData_ShapeFile::ImportShapeType_All);
+              PolyEnt.Append(theEntitiesPoly3D);
+
+            }
+          anImporter.Free();
+        }
+//      if (aStat == 1)
+//        UpdateView(module, PolyEnt);
+//      else if (aStat == 2)
+//        {
+//          UpdateView(module, PolyEnt);
+//          if (theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType_All) //if other flag = > no need to show this messagebox
+//            SUIT_MessageBox::information(module->getApp()->desktop(), tr("IMPORT_POLYLINE"),
+//                                         tr("POLYGON_IMPORTED_AS_POLYLINE"));
+//        }
+//      else
+//        {
+//          QString aMess;
+//          if (theShapeTypesToImport == HYDROData_ShapeFile::ImportShapeType::ImportShapeType_Polygon)
+//            aMess += tr("POLYLINE_IMPORT_FAILED_AS_POLYGON") + ";\n";
+//          else
+//            aMess += tr("POLYLINE_IMPORT_FAILED_AS_POLYLINE") + ";\n";
+//
+//          if (aStat == -1)
+//            aMess += tr("CANT_OPEN_SHP");
+//          else if (aStat == -2)
+//            aMess += tr("CANT_OPEN_SHX");
+//          else
+//            aMess += tr("SHAPE_TYPE_IS") + anImporter.GetShapeTypeName(aShapeTypeOfFile);
+//          SUIT_MessageBox::warning(module->getApp()->desktop(), tr("IMPORT_POLYLINE"), aMess);
+//        }
+      importedEntities.Append(PolyEnt);
+    }
+  else if (anExt == "xy" || (importXY && anExt == "xyz"))
+    {
+      if (!HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, true, importedEntities))
+        DEBTRACE("no polyline imported");
+        //SUIT_MessageBox::warning(module->getApp()->desktop(), tr("IMPORT_POLYLINE"), tr("NO_ONE_POLYLINE_IMPORTED"));
+    }
+  else if (anExt == "xyz")
+    {
+      if (!HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, false, importedEntities))
+        DEBTRACE("no polyline imported");
+        //SUIT_MessageBox::warning(module->getApp()->desktop(), tr("IMPORT_POLYLINE"), tr("NO_ONE_POLYLINE_IMPORTED"));
+    }
+  return true;
+}
+
index 14aeaff95a085f7f126ec9b1523fc7980c4b2e41..3dc7e1886f5f4eb05499f8d33e368d607e1d0a5d 100644 (file)
@@ -305,6 +305,8 @@ public:
 
   HYDRODATA_EXPORT bool GetDBFInfo(QStringList& theDBFTable) const;
 
+  HYDRODATA_EXPORT static bool ImportShapesFromFile( const QString& theFileName );
+
 protected:
 
   /**
index a32e3f180e48180cd71b26e1a66545975a37630a..11c10909eb083065c1a5fa07a6c33369623daf45 100644 (file)
@@ -125,6 +125,19 @@ public:
    */
   virtual QPainterPath GetPainterPath() const;
 
+  /**
+   * import polylines from .shp or .xyz files
+   */
+  static bool ImportShapesFromFile( const QString& theFileName /In/ )
+  [bool ( const QString& )];
+  %MethodCode
+    QString aString(*a0);
+    Py_BEGIN_ALLOW_THREADS
+    sipRes = HYDROData_PolylineXY::ImportShapesFromFile( aString );
+    Py_END_ALLOW_THREADS
+  %End
+
+
 protected:
 
   /**