Salome HOME
Bug #487: dump/load script - problem with obstacle.
[modules/hydro.git] / src / HYDROData / HYDROData_Document.cxx
index 1d51206c5a43a835bcb1c0d5de280d0c05c0dc7d..3568066a333787753c5577adcae0b68ca70ba34c 100644 (file)
@@ -5,9 +5,12 @@
 #include <HYDROData_Tool.h>
 
 #include <TDataStd_Integer.hxx>
+#include <TDataXtd_Position.hxx>
 
 #include <TDF_Delta.hxx>
 
+#include <gp_Pnt.hxx>
+
 #include <QFile>
 #include <QStringList>
 #include <QTextStream>
@@ -23,9 +26,14 @@ static const int TAG_PROPS = 1; // general properties tag
 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
+static const int TAG_LOCAL_CS = 4; // tag of local coordinate system information
+static const gp_Pnt2d DEFAULT_LOCAL_CS( 0, 0 );
 
 using namespace std;
 
+typedef QMap<Standard_Integer,Handle_HYDROData_Entity> MapOfOrdered;
+typedef QMap<QString,Handle_HYDROData_Entity> MapOfUnordered;
+
 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
 {
   Handle(HYDROData_Document) aResult = 
@@ -194,6 +202,13 @@ bool HYDROData_Document::DumpToPython( const QString& theFileName,
 
   bool aRes = true;
 
+  // Dump the local CS data to Python 
+  UpdateLCSFields();
+  QString aLCS = QString( "%1.SetLocalCS( %2, %3 )" ).arg( GetDocPyName() ).arg( myLX ).arg( myLY );
+  if( theIsMultiFile )
+    aLCS.prepend( "  " );
+  HYDROData_Tool::WriteStringsToFile( aFile, QStringList() << aLCS );
+
   // Dump all model objects to Python script
   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMAGE );
   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINEXY );
@@ -252,6 +267,7 @@ QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObj
 
   if ( theIsMultiFile )
   {
+    aResScript << QString( "import salome" );
     aResScript << QString( "" );
     aResScript << QString( "def RebuildData( theStudy ):" );
     aResScript << QString( "  %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
@@ -306,29 +322,184 @@ bool HYDROData_Document::dumpPartitionToPython( QFile&               theFile,
   return aRes;
 }
 
+bool takeLastDigits( QString& theStr, int& aRes )
+{
+  aRes = -1;
+
+  QString anStrNum;
+  for ( int i = theStr.length() - 1; i >= 0; --i )
+  {
+    const QChar& aChar = theStr.at( i );
+    if ( !aChar.isDigit() )
+      break;
+
+    anStrNum.prepend( aChar );
+  }
+
+  if ( anStrNum.isEmpty() )
+    return false;
+
+  theStr.remove( theStr.length() - anStrNum.length(), anStrNum.length() );
+  aRes = anStrNum.toInt();
+
+  return true;
+}
+
+bool isObjectNameLessThan( const QString& theStr1, const QString& theStr2 )
+{
+  QString aStr1 = theStr1, aStr2 = theStr2;
+
+  int aNum1 = -1, aNum2 = -1;
+  if ( takeLastDigits( aStr1, aNum1 ) && takeLastDigits( aStr2, aNum2 ) )
+  {
+    if ( aStr1 == aStr2 )
+      return aNum1 < aNum2;
+  }
+
+  return theStr1 < theStr2;
+}
+
 HYDROData_SequenceOfObjects HYDROData_Document::GetObjectsLayerOrder(
   const Standard_Boolean theIsAll ) const
 {
   HYDROData_SequenceOfObjects anOrder;
 
-  // TODO
+  MapOfOrdered   aMapOfOrdered;
+  MapOfUnordered aMapOfUnordered;
+
+  HYDROData_Iterator anIter( this );
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    Handle(HYDROData_Entity) anObject = anIter.Current();
+    if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
+      continue;
+
+    Standard_Integer anObjZLevel = -1;
+    if ( anObject->GetZLevel( anObjZLevel ) )
+    {
+      aMapOfOrdered.insert( anObjZLevel, anObject );
+    }
+    else
+    {
+      QString anObjName = anObject->GetName();
+      if ( anObjName.isEmpty() )
+        continue;
+
+      aMapOfUnordered.insert( anObjName, anObject );
+    }
+  }
+
+  MapOfOrdered::const_iterator anOrderedMapIt = aMapOfOrdered.constBegin();
+  for ( ; anOrderedMapIt != aMapOfOrdered.constEnd(); anOrderedMapIt++ )
+  {
+    const Handle(HYDROData_Entity)& anObject = anOrderedMapIt.value();
+    anOrder.Prepend( anObject );
+  }
+
+  if ( theIsAll )
+  {
+    QStringList aSortedNames = aMapOfUnordered.keys();
+    qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
+
+    for ( int i = 0; i < aSortedNames.length(); ++i )
+    {
+      QString anObjName = aSortedNames.value( i );
+
+      const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
+      anOrder.Append( anObject );
+    }
+  }
 
   return anOrder;
 }
 
 void HYDROData_Document::SetObjectsLayerOrder( const HYDROData_SequenceOfObjects& theOrder )
 {
-  // TODO
+  // At first we remove previous model order
+  RemoveObjectsLayerOrder();
+
+  // Make new objects order
+  Standard_Integer aLevel = 0;
+  for ( int i = theOrder.Length(), n = 1; i >= n; --i )
+  {
+    const Handle(HYDROData_Entity)& anObject = theOrder.Value( i );
+    if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
+      continue;
+
+    anObject->SetZLevel( aLevel++ );
+  }
 }
 
 void HYDROData_Document::Show( const Handle_HYDROData_Entity& theObject )
 {
-  // TODO
+  HYDROData_SequenceOfObjects anOrder;
+  anOrder.Append( theObject );
+  Show( anOrder );
 }
 
 void HYDROData_Document::Show( const HYDROData_SequenceOfObjects& theObjects )
 {
-  // TODO
+  MapOfUnordered aMapOfUnordered;
+
+  for ( int i = 1, n = theObjects.Length(); i <= n; ++i )
+  {
+    const Handle(HYDROData_Entity)& anObject = theObjects.Value( i );
+    if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
+      continue;
+
+    Standard_Integer anObjZLevel = -1;
+    if ( anObject->GetZLevel( anObjZLevel ) )
+    {
+      continue; // Skip objects that already have the z-level
+    }
+    else
+    {
+      QString anObjName = anObject->GetName();
+      if ( anObjName.isEmpty() )
+        continue;
+
+      aMapOfUnordered.insert( anObjName, anObject );
+    }
+  }
+
+  if ( aMapOfUnordered.isEmpty() )
+    return; // Nothing to show
+
+  Standard_Integer aTopId = 0;
+
+  HYDROData_SequenceOfObjects aModelOrder = GetObjectsLayerOrder( Standard_False );
+  if ( !aModelOrder.IsEmpty() )
+  {
+    const Handle(HYDROData_Entity)& anObject = aModelOrder.First();
+    anObject->GetZLevel( aTopId );
+    aTopId++;
+  }
+
+  aTopId += aMapOfUnordered.size() - 1;
+
+  QStringList aSortedNames = aMapOfUnordered.keys();
+  qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
+
+  for ( int i = 0; i < aSortedNames.length(); ++i )
+  {
+    QString anObjName = aSortedNames.value( i );
+
+    const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
+    anObject->SetZLevel( aTopId-- );
+  }
+}
+
+void HYDROData_Document::RemoveObjectsLayerOrder()
+{
+  HYDROData_Iterator anIter( this );
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    Handle(HYDROData_Entity) anObject = anIter.Current();
+    if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
+      continue;
+
+    anObject->RemoveZLevel();
+  }
 }
 
 void HYDROData_Document::StartOperation()
@@ -474,12 +645,16 @@ HYDROData_Document::HYDROData_Document()
   myDoc->SetUndoLimit(UNDO_LIMIT);
   NewID(); // needed to have at least one attribute in initial document to avoid errors
   myTransactionsAfterSave = 0;
+  myLX = -1;
+  myLY = -1;
 }
 
 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
 {
   myDoc = theDoc;
   myTransactionsAfterSave = 0;
+  myLX = -1;
+  myLY = -1;
 }
 
 HYDROData_Document::~HYDROData_Document()
@@ -503,3 +678,106 @@ TDF_Label HYDROData_Document::LabelOfObjects()
 {
   return myDoc->Main().FindChild(TAG_OBJECTS);
 }
+
+TDF_Label HYDROData_Document::LabelOfLocalCS() const
+{
+  return myDoc->Main().FindChild(TAG_LOCAL_CS);
+}
+
+void HYDROData_Document::GetLocalCS( double& theLX, double& theLY ) const
+{
+  TDF_Label aLocalCSLab = LabelOfLocalCS();
+
+  Handle( TDataXtd_Position ) aLocalCS;
+  if( aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
+  {
+    gp_Pnt aLocalCS3d = aLocalCS->GetPosition();
+    theLX = aLocalCS3d.X();
+    theLY = aLocalCS3d.Y();
+  }
+  else
+  {
+    theLX = DEFAULT_LOCAL_CS.X();
+    theLY = DEFAULT_LOCAL_CS.Y();
+  }
+}
+
+void HYDROData_Document::SetLocalCS( double theLX, double theLY )
+{
+  UpdateLCSFields();
+
+  // update the local CS data in attribute
+  TDF_Label aLocalCSLab = LabelOfLocalCS();
+  Handle( TDataXtd_Position ) aLocalCS;
+  if( !aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
+    aLocalCS = TDataXtd_Position::Set( aLocalCSLab );
+
+  gp_Pnt aLocalCS3d( theLX, theLY, 0 );
+  aLocalCS->SetPosition( aLocalCS3d );
+
+  // calculate delta for coordinates
+  double aDX = myLX - theLX;
+  double aDY = myLY - theLY;
+
+  // update the local CS data in internal fields
+  myLX = theLX;
+  myLY = theLY;
+
+  //update all objects in the document
+  HYDROData_Iterator anIterator( this, KIND_UNKNOWN );
+  for( ; anIterator.More(); anIterator.Next() )
+    anIterator.Current()->UpdateLocalCS( aDX, aDY );
+}
+
+void HYDROData_Document::UpdateLCSFields() const
+{
+  if( myLX >= 0 && myLY >= 0 )
+    return;
+
+  double aLX, aLY;
+  GetLocalCS( aLX, aLY );
+  HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
+  aThat->myLX = aLX;
+  aThat->myLY = aLY;
+}
+
+void HYDROData_Document::Transform( double& X, double& Y, bool IsToLocalCS ) const
+{
+  UpdateLCSFields();
+  if( IsToLocalCS )
+  {
+    X -= myLX;
+    Y -= myLY;
+  }
+  else
+  {
+    X += myLX;
+    Y += myLY;
+  }
+}
+
+void HYDROData_Document::Transform( gp_Pnt& thePnt, bool IsToLocalCS ) const
+{
+  double X = thePnt.X();
+  double Y = thePnt.Y();
+  double Z = thePnt.Z();
+  Transform( X, Y, IsToLocalCS );
+  thePnt = gp_Pnt( X, Y, Z ); 
+}
+
+void HYDROData_Document::Transform( gp_XYZ& thePnt, bool IsToLocalCS ) const
+{
+  double X = thePnt.X();
+  double Y = thePnt.Y();
+  double Z = thePnt.Z();
+  Transform( X, Y, IsToLocalCS );
+  thePnt = gp_XYZ( X, Y, Z ); 
+}
+
+void HYDROData_Document::Transform( gp_XY& thePnt, bool IsToLocalCS ) const
+{
+  double X = thePnt.X();
+  double Y = thePnt.Y();
+  Transform( X, Y, IsToLocalCS );
+  thePnt = gp_XY( X, Y ); 
+}