Salome HOME
merge master
[modules/hydro.git] / src / HYDROData / HYDROData_SinusX.cxx
index 5c0c6f5a9fbecd8fd4359515325d64a7fc2a3699..3927c0a348a1d9e2c7b833d1ffc074d5268298b9 100644 (file)
@@ -1,8 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
-//
-// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
+// Copyright (C) 2014-2015  EDF-R&D
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
@@ -27,6 +23,7 @@
 #include <HYDROData_Entity.h>
 #include <HYDROData_Document.h>
 #include <HYDROData_Profile.h>
+#include <HYDROData_Iterator.h>
 
 #include <gp_XYZ.hxx>
 #include <gp_XY.hxx>
@@ -35,6 +32,8 @@
 #include <QFileInfo>
 #include <QString>
 #include <QStringList>
+#include <QTextStream>
+#include <QColor>
 
 HYDROData_SinusX::HYDROData_SinusX( ) 
 {
@@ -44,6 +43,48 @@ HYDROData_SinusX::~HYDROData_SinusX()
 {
 }
 
+void HYDROData_SinusX::CollectExistingNames(Handle_HYDROData_Document theDocument)
+{
+  HYDROData_Iterator anIter( theDocument );
+  int anInd = 0;
+  myExistingNames.clear();
+  std::vector<int> anAllowedIndexes;
+  for( ; anIter.More(); anIter.Next() )
+    myExistingNames.push_back(anIter.Current()->GetName());
+}
+
+QString HYDROData_SinusX::GetName(const QString& theName)
+{
+  if (myExistingNames.contains(theName))
+  { 
+    QString aName = theName; 
+    while (myExistingNames.contains(aName)) 
+    {
+      QStringList aList = aName.split("_");
+      int aLastInd = aName.lastIndexOf("_");
+      bool IsNum = false; 
+      int anInd = -1;
+      anInd = aList.last().toInt(&IsNum);
+      if (IsNum)
+      {
+        aName = aName.left(aLastInd) + "_";
+        aName+= QString::number(++anInd);
+      }
+      else
+        aName = theName + "_0";
+    }
+
+    myExistingNames.push_back(aName);
+    return aName;
+  }
+  else
+  {
+    myExistingNames.push_back(theName);
+    return theName;
+  }
+
+}
+
 
 bool HYDROData_SinusX::Import(const QString& theFilePath, Handle(HYDROData_Document) theDocument, NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
 {
@@ -62,6 +103,7 @@ bool HYDROData_SinusX::Import(const QString& theFilePath, Handle(HYDROData_Docum
 
     Parse(aFile);
    
+    CollectExistingNames(theDocument);
     SXToHydro(theDocument, theEntities);
     
     aFile.close();
@@ -85,7 +127,7 @@ void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollec
         aAPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[j]));
 
       aBath->SetAltitudePoints(aAPoints);
-      aBath->SetName(myCurveBlocks[i].myName);
+      aBath->SetName(GetName(myCurveBlocks[i].myName + "_bath"));
       theEntities.Append(aBath);
     }
     if (myCurveBlocks[i].myType == 1 || myCurveBlocks[i].myType == 3)
@@ -93,17 +135,20 @@ void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollec
       NCollection_Sequence<gp_XYZ> aPoints;
       for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
         aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[j]));
-      if (myCurveBlocks[i].myIsClosed)
-        aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[0]));
+      /*if (myCurveBlocks[i].myIsClosed)
+        aPoints.Append(gp_XYZ (myCurveBlocks[i].myXYZPoints[0]));*/
       Handle(HYDROData_ProfileUZ) aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( theDocument->CreateObject( KIND_PROFILEUZ ) );
       Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
-      aPolyXY->AddSection( "",  myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE, false ); 
+      aPolyXY->AddSection( "",  
+        myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE, 
+        myCurveBlocks[i].myIsClosed ? true : false); 
       aProfileUZ->CalculateAndAddPoints(aPoints, aPolyXY);
       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( theDocument->CreateObject( KIND_PROFILE ) );
       aProfile->SetParametricPoints(aProfileUZ->GetPoints());
-      aPolyXY->SetName(myCurveBlocks[i].myName + "_polyXY");
-      aProfileUZ->SetName(myCurveBlocks[i].myName + "_profileUZ");
-      aProfile->SetName(myCurveBlocks[i].myName + "_profile");
+      aPolyXY->SetName(GetName(myCurveBlocks[i].myName + "_polyXY"));
+      aProfileUZ->SetName(GetName(myCurveBlocks[i].myName + "_profileUZ"));
+      aProfile->SetName(GetName(myCurveBlocks[i].myName + "_profile"));
+      aPolyXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
       theEntities.Append(aPolyXY);
       theEntities.Append(aProfileUZ);
       theEntities.Append(aProfile);
@@ -118,7 +163,8 @@ void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollec
           HYDROData_ProfileUZ::PointsList aPointList;
           for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
             aPointList.Append(gp_XY (myCurveBlocks[i].myXYZPoints[j].X(), myCurveBlocks[i].myXYZPoints[j].Z()));
-           
+          aProfile->GetProfileUZ()->SetSectionType(0,  myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
+          aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
           aProfile->SetParametricPoints(aPointList);
           if ( ! (myCurveBlocks[i].myAdditionalCurveInfo[0] == 0 &&  myCurveBlocks[i].myAdditionalCurveInfo[1] == 0 && 
             myCurveBlocks[i].myAdditionalCurveInfo[2] == 0 && myCurveBlocks[i].myAdditionalCurveInfo[3] == 0) )
@@ -127,7 +173,7 @@ void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollec
             aProfile->SetRightPoint(gp_XY(myCurveBlocks[i].myAdditionalCurveInfo[2], myCurveBlocks[i].myAdditionalCurveInfo[3]));
             aProfile->Update();
           }
-          aProfile->SetName(myCurveBlocks[i].myName + "_profile");
+          aProfile->SetName(GetName(myCurveBlocks[i].myName + "_profile"));
           theEntities.Append(aProfile);
         }
       }
@@ -137,8 +183,10 @@ void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollec
         HYDROData_Profile::ProfilePoints aPointList;
         for (size_t j = 0; j < myCurveBlocks[i].myXYZPoints.size(); j++)
           aPointList.Append(myCurveBlocks[i].myXYZPoints[j]);
+        aProfile->GetProfileUZ()->SetSectionType(0,  myCurveBlocks[i].myIsSpline ? HYDROData_PolylineXY::SECTION_SPLINE : HYDROData_PolylineXY::SECTION_POLYLINE);
+        aProfile->GetProfileUZ()->SetSectionClosed(0, myCurveBlocks[i].myIsClosed ? true : false);
         aProfile->SetProfilePoints(aPointList);
-        aProfile->SetName(myCurveBlocks[i].myName + "_profile");
+        aProfile->SetName(GetName(myCurveBlocks[i].myName + "_profile"));
         theEntities.Append(aProfile);
       }
     }
@@ -147,8 +195,6 @@ void HYDROData_SinusX::SXToHydro(Handle(HYDROData_Document) theDocument, NCollec
 }
  
 
-
-
 bool HYDROData_SinusX::Parse(QFile& theFile)
 {
   if ( !theFile.isOpen() )
@@ -256,3 +302,107 @@ bool HYDROData_SinusX::Parse(QFile& theFile)
 
 }
 
+bool HYDROData_SinusX::Export(const QString& theFilePath, const NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
+{
+  if ( theFilePath.isEmpty() )
+  { 
+    return false;
+  }
+
+  QString anExt = theFilePath.split('.', QString::SkipEmptyParts).back();
+
+  if (anExt == "sx")
+  {
+    QFile aFile (theFilePath);
+    
+    aFile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
+
+    HydroToSX(aFile, theEntities);
+    
+    aFile.close();
+    
+    return true;
+  }
+  else
+    return false;
+
+}
+
+void HYDROData_SinusX::HydroToSX(QFile& theFile, const NCollection_Sequence<Handle_HYDROData_Entity>& theEntities)
+{
+  QTextStream aTextStream(&theFile);
+  aTextStream << "C  Generated by HYDRO Module\n";
+  aTextStream << "C\n";
+
+  for (int i = 1; i <= theEntities.Size(); i++)
+  {
+    Handle_HYDROData_Entity anEnt = theEntities.Value(i);
+    if (anEnt->IsKind( STANDARD_TYPE(HYDROData_Bathymetry) ))
+    {
+      Handle(HYDROData_Bathymetry) aBathy = Handle(HYDROData_Bathymetry)::DownCast( anEnt );
+      HYDROData_Bathymetry::AltitudePoints anXYZPoints = aBathy->GetAltitudePoints(true);
+      //Write to stream
+      aTextStream << "B S\n";
+      aTextStream << "CN " << aBathy->GetName() << "\n";
+      aTextStream << "CP 0 0\n";
+      aTextStream << "CP 0\n";
+      for (int j = anXYZPoints.Lower(); j <= anXYZPoints.Upper(); j++)
+        aTextStream << " " << QString::number(anXYZPoints(j).X(), 'f', 3)  
+                    << " " << QString::number(anXYZPoints(j).Y(), 'f', 3)  
+                    << " " << QString::number(anXYZPoints(j).Z(), 'f', 3) << "\n"; 
+    }
+    else if (anEnt->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ))
+    {
+      Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( anEnt );
+      for (int j = 0; j < aPolyXY->NbSections(); j++)
+      { 
+        //Collect data
+        bool IsClosed = aPolyXY->IsClosedSection(j);
+        bool IsSpline = false;
+        if (aPolyXY->GetSectionType(j) == HYDROData_PolylineXY::SECTION_SPLINE)
+          IsSpline = true;
+        HYDROData_PolylineXY::PointsList anXYPoints = aPolyXY->GetPoints(j, true);
+        //Write to stream
+        aTextStream << "B N\n";
+        aTextStream << "CN " << aPolyXY->GetName() << "\n";
+        aTextStream << "CP " << IsClosed << " " << IsSpline << "\n";
+        aTextStream << "CP 0.0\n";
+        aTextStream << "CP 0\n";
+        if (aPolyXY->NbSections() > 1)
+          aTextStream << "C " << aPolyXY->GetName() << "_section_" << QString::number(j) << "\n";
+        for (int k = anXYPoints.Lower(); k <= anXYPoints.Upper(); k++)
+         aTextStream << " " << QString::number(anXYPoints(k).X(), 'f', 3)  
+                     << " " << QString::number(anXYPoints(k).Y(), 'f', 3)  
+                     << " 0.000\n"; 
+      }
+    }
+    else if (anEnt->IsKind( STANDARD_TYPE(HYDROData_Profile) ))
+    {
+      //Collect data
+      Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( anEnt );
+      HYDROData_ProfileUZ::PointsList aPointList = aProfile->GetParametricPoints();
+      bool IsClosed = aProfile->GetProfileUZ(false)->IsClosedSection(0);;
+      bool IsSpline = false;
+      if (aProfile->GetProfileUZ(false)->GetSectionType(0) == HYDROData_PolylineXY::SECTION_SPLINE)
+        IsSpline = true;
+      //Write to stream
+      aTextStream << "B P\n";
+      aTextStream << "CN " << aProfile->GetName() << "\n";
+      aTextStream << "CP " << IsClosed << " " << IsSpline << "\n";
+      gp_XY aLeftPoint(0.0, 0.0);
+      gp_XY aRightPoint(0.0, 0.0);
+      if (aProfile->GetLeftPoint(aLeftPoint, true) && aProfile->GetRightPoint(aRightPoint, true))
+        aTextStream << "CP " << QString::number(aLeftPoint.X(), 'f', 3) << " " <<
+                                QString::number(aLeftPoint.Y(), 'f', 3) << " " <<
+                                QString::number(aRightPoint.X(), 'f', 3) << " " <<
+                                QString::number(aRightPoint.Y(), 'f', 3) << "\n";
+      else
+        aTextStream << "CP 0.0 0.0 0.0 0.0\n";
+      aTextStream << "CP 2\n";
+      for (int k = aPointList.Lower(); k <= aPointList.Upper(); k++)
+         aTextStream << " " << QString::number(aPointList(k).X(), 'f', 3)  
+                     << " 0.000 "
+                     << QString::number(aPointList(k).Y(), 'f', 3) << "\n";  
+    }
+  }
+}
\ No newline at end of file