Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_UseCaseBuilder.cxx
index d629ad14a008e988d3112432fa07016206472290..6c00f01a73e03e2922ae47457f3ac7a8ccb3ce2f 100644 (file)
@@ -1,74 +1,96 @@
-// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-// 
+//
 // 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 
-// version 2.1 of the License.
-// 
-// This library is distributed in the hope that it will be useful 
-// but WITHOUT ANY WARRANTY; without even the implied warranty of 
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  File   : SALOMEDSImpl_UseCaseBuilder.cxx
 //  Author : Sergey RUIN
 //  Module : SALOME
 
-
 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
 #include "SALOMEDSImpl_SObject.hxx"
 #include "SALOMEDSImpl_SComponent.hxx"
 #include "SALOMEDSImpl_Study.hxx"
 #include "SALOMEDSImpl_Attributes.hxx"
 
-#include <TDF_Label.hxx>
-#include <TDF_Tool.hxx>
-#include <TDF_Data.hxx>
-#include <TDF_AttributeList.hxx>
-#include <TDF_ListIteratorOfAttributeList.hxx>
-#include <TCollection_AsciiString.hxx>
-#include <TDF_ChildIterator.hxx>
-
-using namespace std;
-
-IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_UseCaseBuilder, MMgt_TShared )
-IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_UseCaseBuilder, MMgt_TShared )
+#include "DF_ChildIterator.hxx"
 
+#include <list>
 
 #define USE_CASE_LABEL_TAG           2
 #define USE_CASE_GUID                "AA43BB12-D9CD-11d6-945D-0050DA506788"
 
+namespace {
+  // comparator to sort use case nodes in ascending order
+  struct AscSortSOs {
+    bool operator()( SALOMEDSImpl_SObject firstSO, SALOMEDSImpl_SObject secondSO ) const {
+      std::string firstName, secondName;
+      SALOMEDSImpl_SObject refSO;
+      firstSO.ReferencedObject(refSO) ?
+        firstName = refSO.GetName() :
+        firstName = firstSO.GetName();
+      secondSO.ReferencedObject(refSO) ?
+        secondName = refSO.GetName() :
+        secondName = secondSO.GetName();
+      return firstName < secondName;
+    }
+  };
+
+  // comparator to sort use case nodes in descending order
+  struct DescSortSOs {
+    bool operator()( SALOMEDSImpl_SObject firstSO, SALOMEDSImpl_SObject secondSO ) const {
+      std::string firstName, secondName;
+      SALOMEDSImpl_SObject refSO;
+      firstSO.ReferencedObject(refSO) ?
+        firstName = refSO.GetName() :
+        firstName = firstSO.GetName();
+      secondSO.ReferencedObject(refSO) ?
+        secondName = refSO.GetName() :
+        secondName = secondSO.GetName();
+      return firstName > secondName;
+    }
+  };
+}
 
 //============================================================================
 /*! Function : constructor
  *  Purpose  :
  */
 //============================================================================
-SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(const Handle(TDocStd_Document)& theDocument)
-:_doc(theDocument)
+SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(DF_Document* theDocument)
+  :_doc(theDocument), _lastChild(0), _childIndex(-1)
 {
-  if(_doc.IsNull()) return;
+  if(!_doc) return;
   
-  TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
-  if(!aLabel.FindAttribute(Standard_GUID(USE_CASE_GUID), _root)) {
-    _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, Standard_GUID(USE_CASE_GUID));
+  DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
+  if(!(_root = (SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(std::string(USE_CASE_GUID)))) {
+    _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, std::string(USE_CASE_GUID));
   }
  
-  Handle(SALOMEDSImpl_AttributeReference) aRef;
-  if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
+  SALOMEDSImpl_AttributeReference* aRef = NULL;
+  if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
   }  
 
-  Handle(SALOMEDSImpl_AttributeName) aNameAttr;
-  if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aNameAttr)) { 
-    aNameAttr = SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases"); 
+  if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())) { 
+    SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases"); 
   }  
 }
 
@@ -87,67 +109,75 @@ SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
  *  Purpose  : 
  */
 //============================================================================
-bool SALOMEDSImpl_UseCaseBuilder::Append(const Handle(SALOMEDSImpl_SObject)& theObject)
+bool SALOMEDSImpl_UseCaseBuilder::Append(const SALOMEDSImpl_SObject& theObject)
 {
-  if(_root.IsNull() || theObject.IsNull()) return false;
+  if(!_root || !theObject) return false;
 
-  TDF_Label aLabel = theObject->GetLabel();
+  DF_Label aLabel = theObject.GetLabel();
   if(aLabel.IsNull()) return false;
 
-  Handle(SALOMEDSImpl_AttributeTreeNode) aNode, aCurrentNode;
+  SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
+  SALOMEDSImpl_AttributeTreeNode* aCurrentNode = NULL;
   aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
   aNode->Remove();
 
-  Handle(SALOMEDSImpl_AttributeReference) aRef;
-  if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
+  SALOMEDSImpl_AttributeReference* aRef;
+  if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
   }  
 
-  TDF_Label aCurrent = aRef->Get();
-  if(aCurrent.IsNull() || !aCurrent.FindAttribute(_root->ID(), aCurrentNode)) 
+  DF_Label aCurrent = aRef->Get();
+  if(aCurrent.IsNull() || !(aCurrentNode=(SALOMEDSImpl_AttributeTreeNode*)aCurrent.FindAttribute(_root->ID())))
     aCurrentNode = _root;
 
-  aCurrentNode->Append(aNode);
+  aCurrentNode->Append(aNode, &_childIndex);
+  _lastChild = aNode;
+
+  // Mantis issue 0020136: Drag&Drop in OB
+  SALOMEDSImpl_Study::GetStudyImpl(theObject.GetLabel())->addSO_Notification(theObject);
 
   return true;
 }
 
- //============================================================================
+//============================================================================
 /*! Function : Remove
  *  Purpose  :
  */
 //============================================================================
-bool SALOMEDSImpl_UseCaseBuilder::Remove(const Handle(SALOMEDSImpl_SObject)& theObject)
+bool SALOMEDSImpl_UseCaseBuilder::Remove(const SALOMEDSImpl_SObject& theObject)
 {
-  if(_root.IsNull() || theObject.IsNull()) return false;
+  if(!_root || !theObject) return false;
 
-  TDF_Label aLabel = theObject->GetLabel();   
+  DF_Label aLabel = theObject.GetLabel();
   if(aLabel.IsNull()) return false;
 
-  Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
-  if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
+  SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
+  if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
+
+  if ( _lastChild && aNode->GetFather() == _lastChild->GetFather() )
+    _lastChild = 0;
 
   aNode->Remove();
 
-  TDF_AttributeList aList;
-  aList.Append(aNode);
+  std::vector<DF_Attribute*> aList;
+  aList.push_back(aNode);
 
-  Handle(SALOMEDSImpl_AttributeReference) aRef;
-  if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
-    aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
-  }  
-  TDF_Label aCurrent = aRef->Get();
+  SALOMEDSImpl_AttributeReference* aRef = NULL;
+  if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
+    aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
+  }
+
+  DF_Label aCurrent = aRef->Get();
 
-  SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, Standard_True);
-  for(; aChildItr.More(); aChildItr.Next()) 
-    aList.Append(aChildItr.Value());
+  SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, true);
+  for(; aChildItr.More(); aChildItr.Next())
+    aList.push_back(aChildItr.Value());
 
-  TDF_ListIteratorOfAttributeList anIterator(aList);
-  for(; anIterator.More(); anIterator.Next()) {
-    if(anIterator.Value()->Label() ==  aCurrent) { //The current node is removed
+  for(size_t i = 0, len = aList.size(); i<len; i++) {
+    if(aList[i]->Label() ==  aCurrent) { //The current node is removed
       aRef->Set(_root->Label()); //Reset the current node to the root
     }
-    anIterator.Value()->Label().ForgetAttribute(_root->ID());
+    aList[i]->Label().ForgetAttribute(_root->ID());
   }
 
   return true;
@@ -159,54 +189,147 @@ bool SALOMEDSImpl_UseCaseBuilder::Remove(const Handle(SALOMEDSImpl_SObject)& the
  *  Purpose  :
  */
 //============================================================================
-bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const Handle(SALOMEDSImpl_SObject)& theFather, 
-                                          const Handle(SALOMEDSImpl_SObject)& theObject)
+bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const SALOMEDSImpl_SObject& theFather, 
+                                           const SALOMEDSImpl_SObject& theObject)
 {
-  if(_root.IsNull() || theFather.IsNull() || theObject.IsNull()) return false;
+  if(!_root || !theFather || !theObject) return false;
 
-  TDF_Label aFatherLabel = theFather->GetLabel(), aLabel = theObject->GetLabel();
-  Handle(SALOMEDSImpl_AttributeTreeNode) aFather, aNode;
+  DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
+  if(aFatherLabel == aLabel) return false;
+
+  SALOMEDSImpl_AttributeTreeNode *aFather = NULL, *aNode = NULL;
   
   if(aFatherLabel.IsNull()) return false;
-  if(!aFatherLabel.FindAttribute(_root->ID(), aFather)) return false;
+  if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return false;
 
   if(aLabel.IsNull()) return false;
-  if(!aLabel.FindAttribute(_root->ID(), aNode)) {
+  if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
     aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
   }
 
+  if ( aNode == _lastChild && !_lastChild->HasNext() && _lastChild->GetFather() == aFather )
+    return true; // aNode is already the last child
+
   aNode->Remove();
 
-  return aFather->Append(aNode);
+  bool ret = true;
+  if ( _lastChild && _lastChild->GetFather() == aFather &&
+       !_lastChild->HasNext() ) // _lastChild is the last under aFather
+  {
+    _lastChild->InsertAfter( aNode );
+    _lastChild = aNode;
+    ++_childIndex;
+  }
+  else
+  {
+    ret = aFather->Append(aNode, &_childIndex);
+    _lastChild  = aNode;
+  }
+
+  // Mantis issue 0020136: Drag&Drop in OB
+  SALOMEDSImpl_Study::GetStudyImpl(theObject.GetLabel())->addSO_Notification(theObject);
+
+  return ret;
 }
 
 //============================================================================
-/*! Function : InsertBefore
+/*! Function : GetIndexInFather
  *  Purpose  :
  */
 //============================================================================
-bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const Handle(SALOMEDSImpl_SObject)& theFirst
-                                              const Handle(SALOMEDSImpl_SObject)& theNext)
+int SALOMEDSImpl_UseCaseBuilder::GetIndexInFather(const SALOMEDSImpl_SObject& theFather
+                                                  const SALOMEDSImpl_SObject& theObject)
 {
-  if(_root.IsNull() || theFirst.IsNull() || theNext.IsNull()) return false;
+  int index = -1;
+  if(!_root || !theFather || !theObject) return index;
+
+  DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
+  if(aFatherLabel == aLabel) return index;
 
-  TDF_Label aFirstLabel = theFirst->GetLabel(), aLabel= theNext->GetLabel();
-  Handle(SALOMEDSImpl_AttributeTreeNode) aFirstNode, aNode;
+  SALOMEDSImpl_AttributeTreeNode *aFather = NULL, *aNode = NULL;
   
+  if(aFatherLabel.IsNull()) return index;
+  if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return index;
+
+  if(aLabel.IsNull()) return index;
+  if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
+    aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
+  }
+
+  if ( _lastChild && _lastChild->GetFather() == aFather )
+  {
+    if ( aNode == _lastChild )
+      index = _childIndex;
+    else if ( aNode == _lastChild->GetPrevious())
+      index = _childIndex - 1;
+    else if ( aNode == _lastChild->GetNext())
+    {
+      index = ++_childIndex;
+      _lastChild = aNode;
+    }
+  }
+
+  if ( index < 0 )
+  {
+    SALOMEDSImpl_AttributeTreeNode* Last = aFather->GetFirst();
+    for ( index = 0; Last && aNode != Last && Last->HasNext(); ++index )
+    {
+      Last = Last->GetNext();
+    }
+    if ( Last != aNode )
+      index = -1;
+    else if ( !Last->HasNext() )
+    {
+      _lastChild = Last;
+      _childIndex = index;
+    }
+  }
+
+  return index;
+}
+
+//============================================================================
+/*! Function : InsertBefore
+ *  Purpose  :
+ */
+//============================================================================
+bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const SALOMEDSImpl_SObject& theFirst,
+                                               const SALOMEDSImpl_SObject& theNext)
+{
+  if(!_root || !theFirst || !theNext) return false;
+
+  DF_Label aFirstLabel = theFirst.GetLabel(), aLabel= theNext.GetLabel();
+  if(aFirstLabel == aLabel) return false;
+
+  SALOMEDSImpl_AttributeTreeNode *aFirstNode = NULL, *aNode = NULL;
+
   if(aFirstLabel.IsNull()) return false;
-  if(aFirstLabel.FindAttribute(_root->ID(), aFirstNode)) {
+  if((aFirstNode=(SALOMEDSImpl_AttributeTreeNode*)aFirstLabel.FindAttribute(_root->ID()))) {
     aFirstNode->Remove();
     aFirstLabel.ForgetAttribute(aFirstNode->ID());
   }
 
   aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
-  
+
   if(aLabel.IsNull()) return false;
-  if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
+  if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
 
   aFirstNode->Remove();
 
-  return aNode->InsertBefore(aFirstNode);
+  bool ret = aNode->InsertBefore(aFirstNode);
+
+  if ( _lastChild && _lastChild->GetFather() == aNode->GetFather() )
+  {
+    if ( aNode == _lastChild )
+      ++_childIndex;
+    else
+      _lastChild = 0;
+  }
+
+  // Mantis issue 0020136: Drag&Drop in OB
+  SALOMEDSImpl_Study::GetStudyImpl(theFirst.GetLabel())->addSO_Notification(theFirst);
+
+  return ret;
 }
 
 
@@ -215,18 +338,17 @@ bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const Handle(SALOMEDSImpl_SObject
  *  Purpose  :
  */
 //============================================================================
-bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const Handle(SALOMEDSImpl_SObject)& theObject)
+bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const SALOMEDSImpl_SObject& theObject)
 {
-  if(_root.IsNull() || theObject.IsNull()) return false;
+  if(!_root || !theObject) return false;
 
-  TDF_Label aLabel = theObject->GetLabel();
-  Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
+  DF_Label aLabel = theObject.GetLabel();
+  SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
   if(aLabel.IsNull()) return false;
-  if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
+  if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
 
-
-  Handle(SALOMEDSImpl_AttributeReference) aRef;
-  if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
+  SALOMEDSImpl_AttributeReference* aRef = NULL;
+  if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());  
   }
   
@@ -242,10 +364,10 @@ bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const Handle(SALOMEDSImpl_SOb
 //============================================================================
 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
 {
-  if(_root.IsNull()) return false;
+  if(!_root) return false;
    
-  Handle(SALOMEDSImpl_AttributeReference) aRef;
-  if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) 
+  SALOMEDSImpl_AttributeReference* aRef = NULL;
+  if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) 
     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
 
   aRef->Set(_root->Label());
@@ -257,20 +379,91 @@ bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
  *  Purpose  :
  */
 //============================================================================
-bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const Handle(SALOMEDSImpl_SObject)& theObject)
+bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObject)
 {
-  if(_root.IsNull()) return false;
+  if(!_root) return false;
 
-  TDF_Label aLabel;
-  if (theObject.IsNull()) aLabel = _root->Label();
+  DF_Label aLabel;
+  if (!theObject) aLabel = _root->Label();
   else 
-    aLabel = theObject->GetLabel(); 
+    aLabel = theObject.GetLabel(); 
   if(aLabel.IsNull()) return false;
 
-  Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
-  if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
+  SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
+  if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false; 
+  
+  return (aNode->GetFirst());
+}
+
+//============================================================================
+/*! Function : SortChildren
+ *  Purpose  :
+ */
+//============================================================================
+bool SALOMEDSImpl_UseCaseBuilder::SortChildren(const SALOMEDSImpl_SObject& theObject, bool theAscendingOrder)
+{
+  if(!_root) return false;
+
+  DF_Label aLabel;
+  if (!theObject) aLabel = _root->Label();
+  else 
+    aLabel = theObject.GetLabel(); 
+  if(aLabel.IsNull()) return false;
+
+  SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
+  if (!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
+
+  std::list<SALOMEDSImpl_SObject> aRefSOs;
+  std::list<SALOMEDSImpl_SObject> aNodeSOs;
+  for ( SALOMEDSImpl_AttributeTreeNode* aChildNode=aNode->GetFirst(); aChildNode; aChildNode=aChildNode->GetNext() ) {
+    if ( SALOMEDSImpl_SObject aSO = SALOMEDSImpl_Study::SObject( aChildNode->Label() ) ) {
+      if ( aChildNode->FindAttribute( SALOMEDSImpl_AttributeReference::GetID() ) )
+        aRefSOs.push_back( aSO );      
+      else
+        aNodeSOs.push_back( aSO );
+    }
+  }
+  if ( aRefSOs.empty() && aNodeSOs.empty() ) return false;
+
+  //sort items by names in ascending/descending order
+  _lastChild = 0;
+  std::list<SALOMEDSImpl_SObject>::iterator it;  
+  if ( !aRefSOs.empty() ) {
+    theAscendingOrder ? aRefSOs.sort( AscSortSOs() ) : aRefSOs.sort( DescSortSOs() );
+    for ( it = aRefSOs.begin(); it != aRefSOs.end(); ++it ) {
+      AppendTo( theObject, *it );
+    }
+  }  
+  if ( !aNodeSOs.empty() ) {
+    theAscendingOrder ? aNodeSOs.sort( AscSortSOs() ) : aNodeSOs.sort( DescSortSOs() );
+    for ( it = aNodeSOs.begin(); it != aNodeSOs.end(); ++it ) {
+      AppendTo( theObject, *it );
+    }
+  }
+
+  return true;
+}
+
+//============================================================================
+/*! Function : GetFather
+ *  Purpose  :
+ */
+//============================================================================
+SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetFather(const SALOMEDSImpl_SObject& theObject)
+{
+  SALOMEDSImpl_SObject so;
+  if (!_root || !theObject) return so;
+
+  DF_Label aLabel = theObject.GetLabel(); 
+  if (aLabel.IsNull()) return so;
+
+  SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
+  if (!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return so; 
 
-  return !(aNode->GetFirst().IsNull());
+  SALOMEDSImpl_AttributeTreeNode* aFatherNode = aNode->GetFather();
+  if (!aFatherNode) return so;
+
+  return aFatherNode->GetSObject();
 }
 
 //============================================================================
@@ -278,15 +471,15 @@ bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const Handle(SALOMEDSImpl_SObject)
  *  Purpose  :
  */
 //============================================================================
-bool SALOMEDSImpl_UseCaseBuilder::SetName(const TCollection_AsciiString& theName) {
-  if(_root.IsNull()) return false;
+bool SALOMEDSImpl_UseCaseBuilder::SetName(const std::string& theName) {
+  if(!_root) return false;
 
-  Handle(SALOMEDSImpl_AttributeName) aNameAttrib;
+  SALOMEDSImpl_AttributeName* aNameAttrib = NULL;
 
-  if (!_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aNameAttrib))
+  if (!(aNameAttrib=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
     aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
-  else    
-    aNameAttrib->SetValue(theName);
+     
+  aNameAttrib->SetValue(theName);
     
   return true;
 }
@@ -297,16 +490,18 @@ bool SALOMEDSImpl_UseCaseBuilder::SetName(const TCollection_AsciiString& theName
  *  Purpose  :
  */
 //============================================================================
-Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
+SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
 {
-  if(_root.IsNull()) return NULL;
+  SALOMEDSImpl_SObject so;
+  if(!_root) return so;
 
-  Handle(SALOMEDSImpl_AttributeReference) aRef;
-  if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
+  SALOMEDSImpl_AttributeReference* aRef = NULL;
+  if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
   }  
-  TDF_Label aCurrent = aRef->Get();  
-  if(aCurrent.IsNull()) return NULL;
+  
+  DF_Label aCurrent = aRef->Get();  
+  if(aCurrent.IsNull()) return so;
 
   return SALOMEDSImpl_Study::SObject(aCurrent);
 }
@@ -316,15 +511,14 @@ Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
  *  Purpose  :
  */
 //============================================================================
-TCollection_AsciiString SALOMEDSImpl_UseCaseBuilder::GetName() 
+std::string SALOMEDSImpl_UseCaseBuilder::GetName() 
 {
-  TCollection_AsciiString aString;
-  if(_root.IsNull()) return aString;
-
-  Handle(SALOMEDSImpl_AttributeName) aName;
-  if (!_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aName)) return aString;
-  aString = TCollection_AsciiString(aName->Value());
-  return aString;
+  std::string aString;
+  if(!_root) return aString;
+  
+  SALOMEDSImpl_AttributeName* aName = NULL;
+  if (!(aName=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) return aString;
+  return aName->Value();
 }
 
 //============================================================================ 
@@ -332,47 +526,68 @@ TCollection_AsciiString SALOMEDSImpl_UseCaseBuilder::GetName()
  *  Purpose  :  
  */ 
 //============================================================================ 
-bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const Handle(SALOMEDSImpl_SObject)& theObject)
+bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const SALOMEDSImpl_SObject& theObject)
 {
-  if(theObject.IsNull()) return false;
-  TDF_Label aFather, aLabel = theObject->GetLabel(); 
+  if(!theObject) return false;
+  DF_Label aFather, aLabel = theObject.GetLabel(); 
   aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
   if(aLabel.Father() == aFather) return true;
   return false;
 }
 
+//============================================================================ 
+/*! Function :  IsUseCaseNode
+ *  Purpose  :  
+ */ 
+//============================================================================ 
+bool SALOMEDSImpl_UseCaseBuilder::IsUseCaseNode(const SALOMEDSImpl_SObject& theObject)
+{
+  if(!_root) return false;
+
+  DF_Label aLabel;
+  if (!theObject) aLabel = _root->Label();
+  else 
+    aLabel = theObject.GetLabel(); 
+  if(aLabel.IsNull()) return false;
+
+  SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
+  if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false; 
+  
+  return true;
+}
+
 //============================================================================ 
 /*! Function : NewUseCase 
  *  Purpose  :  
  */ 
 //============================================================================ 
-Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::AddUseCase(const TCollection_AsciiString& theName)
+SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::AddUseCase(const std::string& theName)
 {
-  Standard_GUID aBasicGUID(USE_CASE_GUID);
+  std::string aBasicGUID(USE_CASE_GUID);
 
   //Create a use cases structure if it not exists 
 
-  Handle(SALOMEDSImpl_AttributeTreeNode) aFatherNode, aNode;
-  Handle(SALOMEDSImpl_AttributeInteger) anInteger;
-  Handle(SALOMEDSImpl_AttributeReference) aRef;
+  SALOMEDSImpl_AttributeTreeNode *aFatherNode = NULL, *aNode = NULL;
+  SALOMEDSImpl_AttributeInteger* anInteger = NULL;
+  SALOMEDSImpl_AttributeReference* aRef = NULL;
 
-  TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
+  DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
 
-  if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
+  if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
     aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
   }
  
-  if(!aRef->Get().FindAttribute(aBasicGUID, aFatherNode)) {
+  if(!(aFatherNode=(SALOMEDSImpl_AttributeTreeNode*)aRef->Get().FindAttribute(aBasicGUID))) {
     aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
   }
 
-  if(!_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID(), anInteger)) {
+  if(!(anInteger=(SALOMEDSImpl_AttributeInteger*)_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
     anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
-  }
+  }    
 
   //Create a new use case
   anInteger->SetValue(anInteger->Value()+1);
-  TDF_Label aChild = aLabel.FindChild(anInteger->Value());
+  DF_Label aChild = aLabel.FindChild(anInteger->Value());
   aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
   aNode->Remove();
   aFatherNode->Append(aNode);
@@ -386,25 +601,24 @@ Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::AddUseCase(const TColl
  *  Purpose  : Creates a new UseCase iterator, if anObject is null all use cases are iterated 
  */
 //============================================================================
-Handle(SALOMEDSImpl_UseCaseIterator) 
-SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const Handle(SALOMEDSImpl_SObject)& theObject) 
+SALOMEDSImpl_UseCaseIterator
+SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const SALOMEDSImpl_SObject& theObject) 
 {
-  TDF_Label aLabel;
+  DF_Label aLabel;
 
-  if(!theObject.IsNull()) {
-    aLabel = theObject->GetLabel(); //Iterate only sub tree in the use case
+  if(theObject) {
+    aLabel = theObject.GetLabel(); //Iterate only sub tree in the use case
   }
   else {
     aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
   }
 
-  return new SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false); 
+  return SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false); 
 }
 
 
-Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::GetSObject(const TCollection_AsciiString& theEntry)
+SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetSObject(const std::string& theEntry)
 {
-   TDF_Label aLabel;    
-   TDF_Tool::Label(_doc->GetData(), theEntry, aLabel);
-   return SALOMEDSImpl_Study::SObject(aLabel);    
+  DF_Label L = DF_Label::Label(_root->Label(), theEntry);
+  return SALOMEDSImpl_Study::SObject(L);    
 }