#include <HYDROData_Bathymetry.h>
#include <HYDROData_Iterator.h>
#include <HYDROData_ShapeFile.h>
+#include <HYDROData_Tool.h>
#include <HYDROData_Profile.h>
#include <QFile>
#include <QFileInfo>
#include <SUIT_MessageBox.h>
+#include <gp_XY.hxx>
HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
myFileDlg->setWindowTitle( getName() );
myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
- myFileDlg->setFilter( tr("POLYLINE_FILTER") );
+ myFileDlg->setNameFilter( tr("POLYLINE_FILTER") );
connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
myFileDlg->exec();
}
-void HYDROGUI_ImportPolylineOp::onApply()
+NCollection_Sequence<Handle(HYDROData_Entity)> HYDROGUI_ImportPolylineOp::ImportPolyOp(
+ const QStringList& aFileNames, Handle(HYDROData_Document) theDocument,
+ HYDROGUI_Module* module, HYDROData_ShapeFile::ImportShapeType theShapeTypesToImport)
{
- if ( !myFileDlg )
- {
- abort();
- return;
- }
-
- QStringList aFileNames = myFileDlg->selectedFiles();
-
- QApplication::setOverrideCursor( Qt::WaitCursor );
- startDocOperation();
-
+ NCollection_Sequence<Handle(HYDROData_Entity)> importedEntities;
foreach (QString aFileName, aFileNames)
{
if ( aFileName.isEmpty() )
continue;
QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
-
+ anExt.toLower();
+ bool importXY = false;
+ 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;
+ }
if (anExt == "shp")
{
HYDROData_ShapeFile anImporter;
- NCollection_Sequence<Handle_HYDROData_Entity> theEntities;
+ NCollection_Sequence<Handle(HYDROData_Entity)> theEntities;
int aShapeTypeOfFile = -1;
- int aStat = anImporter.ImportPolylines(doc(), aFileName, theEntities, aShapeTypeOfFile );
+ int aStat = anImporter.ImportPolylines(theDocument, aFileName, theEntities,
+ aShapeTypeOfFile, theShapeTypesToImport );
+ 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 (theEntities.Length() == nbRecords);
+ if (theEntities.Length() == 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 = 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 <= theEntities.Length(); i++)
+ {
+ Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theEntities(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 (aStat == 1)
- UpdateView(theEntities);
+ UpdateView(module, 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)");
+ UpdateView(module, theEntities);
+ 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 = "Cannot import content of this file as polyline;\n";
+ 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 += "Cannot open SHP file";
+ aMess += tr ("CANT_OPEN_SHP");
else if (aStat == -2)
- aMess += "Cannot open SHX file";
+ aMess += tr ("CANT_OPEN_SHX");
else
- aMess += "The shape type of file is " + anImporter.GetShapeTypeName(aShapeTypeOfFile);
- SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_POLYLINE" ), aMess);
+ aMess += tr ("SHAPE_TYPE_IS") + anImporter.GetShapeTypeName(aShapeTypeOfFile);
+ SUIT_MessageBox::warning( module->getApp()->desktop(), tr( "IMPORT_POLYLINE" ), aMess);
}
+ importedEntities.Append(theEntities);
+ }
+ else if ( anExt == "xy" || (importXY && anExt == "xyz"))
+ {
+ if (!HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, true, importedEntities))
+ 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))
+ SUIT_MessageBox::warning( module->getApp()->desktop(), tr( "IMPORT_POLYLINE" ), tr( "NO_ONE_POLYLINE_IMPORTED" ));
}
}
+ return importedEntities;
+}
+
+void HYDROGUI_ImportPolylineOp::onApply()
+{
+ if ( !myFileDlg )
+ {
+ abort();
+ return;
+ }
+
+ QStringList aFileNames = myFileDlg->selectedFiles();
+
+ QApplication::setOverrideCursor( Qt::WaitCursor );
+ startDocOperation();
+
+ ImportPolyOp(aFileNames, doc(), module(), HYDROData_ShapeFile::ImportShapeType_All);
+
if (!aFileNames.empty())
{
commitDocOperation();
commit();
- module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
+ module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init | UF_OCCViewer );
}
else
abort();
QApplication::restoreOverrideCursor();
}
-void HYDROGUI_ImportPolylineOp::UpdateView( NCollection_Sequence<Handle_HYDROData_Entity>& anEntities)
+void HYDROGUI_ImportPolylineOp::UpdateView( HYDROGUI_Module* module, NCollection_Sequence<Handle(HYDROData_Entity)>& anEntities)
{
- size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
+ size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module );
if ( anActiveViewId == 0 )
- anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
+ anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module );
for (int i = 1; i <= anEntities.Size() ; i++)
{
anEntities(i)->Update();
- module()->setObjectVisible( anActiveViewId, anEntities(i), true );
- module()->setIsToUpdate( anEntities(i) );
+ module->setObjectVisible( anActiveViewId, anEntities(i), true );
+ module->setIsToUpdate( anEntities(i) );
}
}