Salome HOME
Bug #334: Different behavior in Edit calculation case input panel.
[modules/hydro.git] / src / HYDROData / HYDROData_Profile.cxx
index ec2348e64ea6b0110ebd91d966acf2f2221436bb..76dfad84e4cbe97516c276d931b1c9133e19db9c 100755 (executable)
@@ -4,9 +4,13 @@
 #include "HYDROData_Document.h"
 #include "HYDROData_Iterator.h"
 #include "HYDROData_Tool.h"
+#include "HYDROData_PolylineXY.h"
+
+#include <boost/math/special_functions/fpclassify.hpp>
 
 #include <BRepBuilderAPI_MakeEdge.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
+#include <BRepBuilderAPI_MakePolygon.hxx>
 
 #include <gp_XY.hxx>
 #include <gp_XYZ.hxx>
 #include <OSD_File.hxx>
 #include <OSD_Protection.hxx>
 
+#include <QColor>
 #include <QStringList>
 
-#define PYTHON_PROFILE_ID "KIND_PROFILE"
-
 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
 
@@ -37,12 +40,89 @@ HYDROData_Profile::~HYDROData_Profile()
 {
 }
 
+QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
+{
+  QStringList aResList = dumpObjectCreation( theTreatedObjects );
+  QString aProfileName = GetObjPyName();
+
+  //TCollection_AsciiString aFilePath = GetFilePath();
+  //if ( !aFilePath.IsEmpty() ) 
+  //{
+  //  aResList << QString( "%1.ImportFromFile( \"%2\" );" )
+  //            .arg( aName ).arg( aFilePath.ToCString() );
+  //}
+
+  bool anIsValidProfile = IsValid();
+  
+  QStringList aPntsDefinition;
+  QString aPntsListName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "profile_points" );
+
+  QString aGap = QString().fill( ' ', aPntsListName.length() + 5 );
+  if ( anIsValidProfile )
+  {
+    HYDROData_Profile::ProfilePoints aPointsList = GetProfilePoints();
+    for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
+    {
+      const ProfilePoint& aPoint = aPointsList.Value( k );
+      aPntsDefinition << QString( aGap + "gp_XYZ( %1, %2, %3 )%4" )
+                         .arg( aPoint.X() ).arg( aPoint.Y() ).arg( aPoint.Z() )
+                         .arg( ( k < aNbPoints ? "," : "" ) );
+    }
+  }
+  else
+  {
+    HYDROData_IPolyline::PointsList aPointsList = GetParametricPoints();
+    for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
+    {
+      const HYDROData_IPolyline::Point& aPoint = aPointsList.Value( k );
+      aPntsDefinition << QString( aGap + "gp_XY( %1, %2 )%3" )
+                         .arg( aPoint.X() ).arg( aPoint.Y() )
+                         .arg( ( k < aNbPoints ? "," : "" ) );
+    }
+  }
+
+  if ( !aPntsDefinition.isEmpty() )
+  {
+    QString& aFirstStr = aPntsDefinition.first();
+    aFirstStr = aFirstStr.trimmed();
+    aFirstStr.prepend( QString( "%1 = [ " ).arg( aPntsListName ) );
+    
+    aPntsDefinition.last().append( " ];" );
+
+    aResList << aPntsDefinition;
+    
+    aResList << QString( "%1.%3( %2 );" )
+                .arg( aProfileName ).arg( aPntsListName )
+                .arg( anIsValidProfile ? "SetProfilePoints" : "SetParametricPoints" );
+  
+    aResList << QString( "" );
+  }
+
+  // Set a polyline type if it is not default
+  Handle(HYDROData_ProfileUZ) aPrf = GetProfileUZ( false );
+  if ( !aPrf.IsNull() )
+  {
+    HYDROData_IPolyline::SectionType aSecType = aPrf->GetSectionType( 0 );
+    if ( aSecType != HYDROData_IPolyline::SECTION_POLYLINE )
+    {
+      aResList << QString( "%1.GetProfileUZ().SetSectionType( 0, %2 );" )
+                  .arg( aProfileName ).arg( "HYDROData_IPolyline.SECTION_SPLINE" );
+      aResList << QString( "" );
+    }
+  }
+
+  aResList << QString( "%1.Update();" ).arg( aProfileName );
+  aResList << QString( "" );
+
+  return aResList;
+}
+
 TopoDS_Shape HYDROData_Profile::GetTopShape() const
 {
   TopoDS_Wire aWire;
 
   gp_XY aFirstPoint, aLastPoint;
-  if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
+  if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
     return aWire;
 
   gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
@@ -64,62 +144,51 @@ TopoDS_Shape HYDROData_Profile::GetShape3D() const
 
 void HYDROData_Profile::Update()
 {
-  BRepBuilderAPI_MakeWire aMakeWire;
+  HYDROData_Object::Update();
 
-  ProfilePoints aProfilePoints = GetProfilePoints();
-  for ( int i = 1, n = aProfilePoints.Length(); i < n ; ++i )
+  TopoDS_Wire aWire;
+  Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
+  if ( !aProfile.IsNull() )
   {
-    ProfilePoint aFirstPoint = aProfilePoints.Value( i );
-    ProfilePoint aSecPoint = aProfilePoints.Value( i + 1 );
-
-    gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
-    gp_Pnt aPnt2( aSecPoint.X(),   aSecPoint.Y(),   aSecPoint.Z()   );
-
-    BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
-    TopoDS_Edge anEdge = aMakeEdge;
+    ProfilePoints aProfilePoints = GetProfilePoints();
+    HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
 
-    aMakeWire.Add( anEdge );
+    aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
   }
-
-  TopoDS_Wire aWire;
-  if ( aMakeWire.IsDone() )
-    aWire = aMakeWire;
-
   SetShape3D( aWire );
 }
 
-/**
- * Dump object to Python script representation.
- */
-QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
+QColor HYDROData_Profile::DefaultFillingColor()
 {
-  QStringList aResList;
+  return QColor( Qt::transparent );
+}
 
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
-  if ( aDocument.IsNull() )
-    return aResList;
-                             
-  QString aDocName = aDocument->GetDocPyName();
-  QString aProfileName = GetName();
+QColor HYDROData_Profile::DefaultBorderColor()
+{
+  return QColor( Qt::black );
+}
 
-  aResList << QString( "%1 = %2.CreateObject( %3 );" )
-              .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
-  aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
+QColor HYDROData_Profile::getDefaultFillingColor() const
+{
+  return DefaultFillingColor();
+}
 
-  return aResList;
+QColor HYDROData_Profile::getDefaultBorderColor() const
+{
+  return DefaultBorderColor();
 }
 
 bool HYDROData_Profile::IsValid() const
 {
   gp_XY aFirstPoint, aLastPoint;
-  if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
+  if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
     return false;
 
   int aNbPoints = NbPoints();
   return aNbPoints > 1;
 }
 
-void HYDROData_Profile::SetFirstPoint( const gp_XY& thePoint )
+void HYDROData_Profile::SetLeftPoint( const gp_XY& thePoint )
 {
   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
 
@@ -129,9 +198,11 @@ void HYDROData_Profile::SetFirstPoint( const gp_XY& thePoint )
 
   anArray->SetValue( 0, thePoint.X() );
   anArray->SetValue( 1, thePoint.Y() );
+
+  SetToUpdate( true );
 }
 
-bool HYDROData_Profile::GetFirstPoint( gp_XY& thePoint ) const
+bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint ) const
 {
   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
   if ( aLabel.IsNull() )
@@ -147,7 +218,7 @@ bool HYDROData_Profile::GetFirstPoint( gp_XY& thePoint ) const
   return true;
 }
 
-void HYDROData_Profile::SetLastPoint( const gp_XY& thePoint )
+void HYDROData_Profile::SetRightPoint( const gp_XY& thePoint )
 {
   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
 
@@ -157,9 +228,11 @@ void HYDROData_Profile::SetLastPoint( const gp_XY& thePoint )
 
   anArray->SetValue( 0, thePoint.X() );
   anArray->SetValue( 1, thePoint.Y() );
+
+  SetToUpdate( true );
 }
 
-bool HYDROData_Profile::GetLastPoint( gp_XY& thePoint ) const
+bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint ) const
 {
   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
   if ( aLabel.IsNull() )
@@ -184,6 +257,8 @@ void HYDROData_Profile::Invalidate()
   TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
   if ( !aLastLabel.IsNull() )
     aLastLabel.ForgetAllAttributes();
+
+  SetToUpdate( true );
 }
 
 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
@@ -214,7 +289,10 @@ void HYDROData_Profile::RemovePoints()
 {
   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
   if ( !aProfileUZ.IsNull() )
+  {
     aProfileUZ->RemoveSections();
+    SetToUpdate( true );
+  }
 }
 
 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
@@ -227,6 +305,8 @@ void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsLi
     const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
     aProfileUZ->AddPoint( 0, aPoint );
   }
+
+  SetToUpdate( true );
 }
 
 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
@@ -260,8 +340,8 @@ void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
     aProfileUZ->AddPoint( 0, aParPoint );
   }
 
-  SetFirstPoint( aFirstPoint );
-  SetLastPoint( aLastPoint );
+  SetLeftPoint( aFirstPoint );
+  SetRightPoint( aLastPoint );
 }
 
 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
@@ -269,7 +349,7 @@ HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
   ProfilePoints aResPoints;
 
   gp_XY aFirstPoint, aLastPoint;
-  if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
+  if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
     return aResPoints;
 
   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
@@ -325,58 +405,69 @@ TCollection_AsciiString HYDROData_Profile::GetFilePath() const
   return aRes;
 }
 
-bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
-                                        const TCollection_AsciiString&    theFileName )
+int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
+                                       const TCollection_AsciiString&    theFileName,
+                                       NCollection_Sequence<int>&        theBadProfilesIds )
 {
   if ( theDoc.IsNull() || theFileName.IsEmpty() )
-    return false;
+    return 0;
 
   OSD_File aFile( theFileName );
   if ( !aFile.IsReadable() )
-    return false;
+    return 0;
 
   aFile.Open( OSD_ReadOnly, OSD_Protection() );
   if ( !aFile.IsOpen() )
-    return false;
-
-  bool aRes = true;
+    return 0;
 
   NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
 
-  while ( aRes && !aFile.IsAtEnd() )
+  int aProfileId = 1;
+  Handle(HYDROData_Profile) aNewProfile;
+  for ( ; !aFile.IsAtEnd(); ++aProfileId )
   {
-    Handle(HYDROData_Profile) aNewProfile = 
-      Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
+    if ( aNewProfile.IsNull() )
+      aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
     
-    aRes = aNewProfile->ImportFromFile( aFile );
-
-    aCreatedProfiles.Append( aNewProfile );
+    bool anIsRead = false;
+    if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
+    {
+      aCreatedProfiles.Append( aNewProfile );
+      aNewProfile.Nullify();
+    }
+    else if ( anIsRead )
+    {
+      theBadProfilesIds.Append( aProfileId );
+    }
   }
 
+  if ( !aNewProfile.IsNull() )
+    aNewProfile->Remove();
+
   // Close the file
   aFile.Close();
 
   for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
   {
     Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
-    if ( aRes )
-    {
-      QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
-      aProfile->SetName( aProfileName );
 
-      aProfile->SetFilePath( theFileName );
-    }
-    else
-    {
-      aProfile->Remove();
-    }
+    QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
+    aProfile->SetName( aProfileName );
+
+    aProfile->SetFilePath( theFileName );
+
+    aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
   }
 
-  return aRes;
+  return aCreatedProfiles.Length();
 }
 
-bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
+bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
+                                        bool*                          theIsRead )
 {
+  if ( theIsRead )
+    *theIsRead = false;
+
   // Try to open the file
   OSD_File aFile( theFileName );
   if ( !aFile.IsReadable() )
@@ -386,7 +477,7 @@ bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileNa
   if ( !aFile.IsOpen() )
     return false;
 
-  bool aRes = ImportFromFile( aFile );
+  bool aRes = ImportFromFile( aFile, theIsRead );
 
   // Close the file
   aFile.Close();
@@ -400,8 +491,12 @@ bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileNa
   return aRes;
 }
 
-bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
+bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
+                                        bool*     theIsRead )
 {
+  if ( theIsRead )
+    *theIsRead = false;
+
   if ( !theFile.IsOpen() )
     return false;
 
@@ -422,7 +517,16 @@ bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
 
     aLine.LeftAdjust(); aLine.RightAdjust();
     if ( aLine.IsEmpty() )
-      continue;
+    {
+      if ( !anIsParametric && !anIsGeoref )
+        continue; // Definition is not started yet
+
+      break; // Next profile started
+    }
+
+    // Set flag of read status to true
+    if ( theIsRead )
+      *theIsRead = true;
 
     TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
     TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
@@ -444,6 +548,10 @@ bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
     double aCoordX = aValX.RealValue();
     double aCoordY = aValY.RealValue();
 
+    if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
+         boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
+      aRes = false;
+
     if ( anIsParametric )
     {
       if ( aCoordX < aPrevVal )
@@ -467,6 +575,8 @@ bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
       }
 
       double aCoordZ = aValZ.RealValue();
+      if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
+        aRes = false;
 
       ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
       aPointsXYZ.Append( aPoint );
@@ -485,8 +595,9 @@ bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
     else if ( anIsGeoref )
     {
       SetProfilePoints( aPointsXYZ );
-      Update();
     }
+
+    Update();
   }
 
   return aRes;