0031075: Application Framework - reading STEP file into TDocStd_Document leads to...
[occt.git] / src / TDocStd / TDocStd_Document.cxx
old mode 100755 (executable)
new mode 100644 (file)
index df85eaf..e72d5ca
@@ -1,49 +1,47 @@
-// Copyright (c) 2006-2012 OPEN CASCADE SAS
+// Copyright (c) 2006-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
+#include <TDocStd_Document.hxx>
 
-#include <TDocStd_Document.ixx>
-
-#include <TDocStd.hxx>
-
-#include <TDocStd_XLink.hxx>
-#include <TDocStd_XLinkIterator.hxx>
-#include <TDocStd_Application.hxx>
-
-#include <TDocStd_Context.hxx>
-#include <TCollection_ExtendedString.hxx>
+#include <CDM_Document.hxx>
+#include <CDM_MetaData.hxx>
+#include <Standard_Dump.hxx>
+#include <Standard_Type.hxx>
 #include <TCollection_AsciiString.hxx>
-#include <TDF_AttributeIterator.hxx>
-#include <TDF_ListIteratorOfDeltaList.hxx>
-#include <TDF_AttributeList.hxx>
-#include <TDF_ListIteratorOfAttributeList.hxx>
+#include <TCollection_ExtendedString.hxx>
 #include <TDF_AttributeDelta.hxx>
 #include <TDF_AttributeDeltaList.hxx>
-#include <TDF_ListIteratorOfAttributeDeltaList.hxx>
-#include <TDF_Label.hxx>
+#include <TDF_AttributeIterator.hxx>
+#include <TDF_AttributeList.hxx>
+#include <TDF_Data.hxx>
 #include <TDF_Delta.hxx>
-#include <TDocStd_CompoundDelta.hxx>
-#include <TDocStd_Owner.hxx>
-#include <TDocStd_Modified.hxx>
-
 #include <TDF_IDMap.hxx>
+#include <TDF_Label.hxx>
+#include <TDF_ListIteratorOfAttributeDeltaList.hxx>
+#include <TDF_ListIteratorOfAttributeList.hxx>
+#include <TDF_ListIteratorOfDeltaList.hxx>
+#include <TDF_Reference.hxx>
+#include <TDocStd.hxx>
+#include <TDocStd_Application.hxx>
+#include <TDocStd_CompoundDelta.hxx>
+#include <TDocStd_Context.hxx>
 #include <TDocStd_LabelIDMapDataMap.hxx>
+#include <TDocStd_Modified.hxx>
+#include <TDocStd_Owner.hxx>
+#include <TDocStd_XLink.hxx>
+#include <TDocStd_XLinkIterator.hxx>
 
-#include <CDM_MetaData.hxx>
+IMPLEMENT_STANDARD_RTTIEXT(TDocStd_Document,CDM_Document)
 
 // List should have a RemoveLast...
 #define TDocStd_List_RemoveLast(theList) \
@@ -56,8 +54,6 @@ theList.Remove(it);
 
 #undef DEB_DELTA
 
-#define BUC60836
-
 #define SRN_DELTA_COMPACT
 
 //=======================================================================
@@ -67,38 +63,33 @@ theList.Remove(it);
 
 Handle(TDocStd_Document) TDocStd_Document::Get (const TDF_Label& acces)
 {
-  return TDocStd_Owner::GetDocument(acces.Data());
+  // avoid creation of Handle(TDF_Data) during TDF_Data destruction
+  if (acces.Root().HasAttribute()) {
+    return TDocStd_Owner::GetDocument(acces.Data());
+  }
+  return Handle(TDocStd_Document)();
 }
 
-//=======================================================================
-//function : Destroy
-//purpose  : 
-//=======================================================================
-// void TDocStd_Document::Destroy()
-// {
-//   myUndoTransaction.Commit(); // no needs to store the Undo
-// }
-
-
 //=======================================================================
 //function : TDocStd_Document
 //purpose  : 
 //=======================================================================
 
 
-TDocStd_Document::TDocStd_Document(const TCollection_ExtendedString& aStorageFormat) : 
+TDocStd_Document::TDocStd_Document(const TCollection_ExtendedString& aStorageFormat) :
 myStorageFormat(aStorageFormat),
 myData (new TDF_Data()),
 myUndoLimit(0),
+myUndoTransaction ("UNDO"),
 mySaveTime(0),
-myIsNestedTransactionMode(0)
+myIsNestedTransactionMode(0),
+mySaveEmptyLabels(Standard_False)
 {
-  TDF_Transaction* pTr =  new TDF_Transaction (myData,"UNDO");
-  myUndoTransaction    = *pTr; delete pTr;
+  myUndoTransaction.Initialize (myData);
   TDocStd_Owner::SetDocument(myData,this);
 
 #ifdef SRN_DELTA_COMPACT
-  myFromUndo.Nullify();  
+  myFromUndo.Nullify();
   myFromRedo.Nullify();
 #endif
 }
@@ -144,8 +135,7 @@ TCollection_ExtendedString TDocStd_Document::GetPath () const
 void TDocStd_Document::SetData (const Handle(TDF_Data)& D)
 {
   myData = D;
-  TDF_Transaction* pTr = new TDF_Transaction(myData,"UNDO");
-  myUndoTransaction = *pTr; delete pTr;  
+  myUndoTransaction.Initialize (myData);
 }
 
 //=======================================================================
@@ -239,8 +229,8 @@ void TDocStd_Document::Update(const Handle(CDM_Document)& /*aToDocument*/,
                               const Standard_Integer aReferenceIdentifier,
                               const Standard_Address aModifContext) 
 {
-  const TDocStd_Context CC = *((TDocStd_Context *)&aModifContext);
-  if (CC.ModifiedReferences() || !IsUpToDate(aReferenceIdentifier)) {
+  const TDocStd_Context* CC = static_cast<TDocStd_Context*> (aModifContext);
+  if (CC->ModifiedReferences() || !IsUpToDate(aReferenceIdentifier)) {
     TCollection_AsciiString aDocEntry(aReferenceIdentifier);
     UpdateReferences(aDocEntry);
     SetIsUpToDate(aReferenceIdentifier);
@@ -254,17 +244,17 @@ void TDocStd_Document::Update(const Handle(CDM_Document)& /*aToDocument*/,
 
 void TDocStd_Document::NewCommand()
 {
-#ifdef DEB_TRANS
+#ifdef OCCT_DEBUG_TRANS
   if (myUndoTransaction.IsOpen() && myData->Transaction() > 1) {
-    Standard_DomainError::Raise ("NewCommand : many open transactions");
+    throw Standard_DomainError("NewCommand : many open transactions");
   }
 #endif
 
   CommitTransaction();
   OpenTransaction();
 
-#ifdef DEB_TRANS
-  cout<<"End NewCommand"<<endl;
+#ifdef OCCT_DEBUG_TRANS
+  std::cout<<"End NewCommand"<<std::endl;
 #endif
 }
 
@@ -286,7 +276,7 @@ Standard_Boolean TDocStd_Document::HasOpenCommand() const
 void TDocStd_Document::OpenCommand ()
 {
   if (!myIsNestedTransactionMode && myUndoTransaction.IsOpen()) {
-    Standard_DomainError::Raise("TDocStd_Document::OpenCommand : already open");
+    throw Standard_DomainError("TDocStd_Document::OpenCommand : already open");
   }
   OpenTransaction();
 //  if (myUndoLimit != 0) myUndoTransaction.Open();
@@ -587,15 +577,13 @@ Standard_Boolean TDocStd_Document::Undo()
 
     // Apply the Undo
     // should test the applicability before.
-#ifdef DEB_DELTA
-    cout<<"DF before Undo =================================="<<endl; TDF_Tool::DeepDump(cout,myData);
+#ifdef OCCT_DEBUG_DELTA
+    std::cout<<"DF before Undo =================================="<<std::endl; TDF_Tool::DeepDump(std::cout,myData);
 #endif
     Handle(TDF_Delta) D = myData->Undo(myUndos.Last(),Standard_True);
-#ifdef BUC60836 
     D->SetName(myUndos.Last()->Name());
-#endif
-#ifdef DEB_DELTA
-    cout<<"DF after Undo =================================="<<endl; TDF_Tool::DeepDump(cout,myData);
+#ifdef OCCT_DEBUG_DELTA
+    std::cout<<"DF after Undo =================================="<<std::endl; TDF_Tool::DeepDump(std::cout,myData);
 #endif
     // Push the redo
     myRedos.Prepend(D);
@@ -648,15 +636,13 @@ Standard_Boolean TDocStd_Document::Redo()
     myData->AllowModification(Standard_True);
 
     // Apply the Redo
-#ifdef DEB_DELTA
-    cout<<"DF before Redo =================================="<<endl; TDF_Tool::DeepDump(cout,myData);
+#ifdef OCCT_DEBUG_DELTA
+    std::cout<<"DF before Redo =================================="<<std::endl; TDF_Tool::DeepDump(std::cout,myData);
 #endif
     Handle(TDF_Delta) D = myData->Undo(myRedos.First(),Standard_True);
-#ifdef BUC60836
     D->SetName(myRedos.First()->Name());
-#endif
-#ifdef DEB_DELTA
-    cout<<"DF after Redo =================================="<<endl; TDF_Tool::DeepDump(cout,myData);
+#ifdef OCCT_DEBUG_DELTA
+    std::cout<<"DF after Redo =================================="<<std::endl; TDF_Tool::DeepDump(std::cout,myData);
 #endif
     // Push the redo of the redo as an undo (got it !)
     myUndos.Append(D);
@@ -904,3 +890,57 @@ void TDocStd_Document::RemoveFirstUndo() {
   myUndos.RemoveFirst();
 }
 
+//=======================================================================
+//function : BeforeClose
+//purpose  : 
+//=======================================================================
+void TDocStd_Document::BeforeClose() 
+{
+  SetModificationMode(Standard_False);
+  AbortTransaction();
+  if(myIsNestedTransactionMode)
+        myUndoFILO.Clear();
+  ClearUndos();
+}
+
+//=======================================================================
+//function : DumpJson
+//purpose  : 
+//=======================================================================
+void TDocStd_Document::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
+{
+  OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
+
+  OCCT_DUMP_BASE_CLASS (theOStream, theDepth, CDM_Document)
+  
+  OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myStorageFormat)
+
+  for (TDF_DeltaList::Iterator anUndoIt (myUndos); anUndoIt.More(); anUndoIt.Next())
+  {
+    const Handle(TDF_Delta)& anUndo = anUndoIt.Value();
+    OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, anUndo.get())
+  }
+
+  for (TDF_DeltaList::Iterator aRedoIt (myRedos); aRedoIt.More(); aRedoIt.Next())
+  {
+    const Handle(TDF_Delta)& aRedo = aRedoIt.Value();
+    OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aRedo.get())
+  }
+
+  OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myData.get())
+  OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myUndoLimit)
+  OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myUndoTransaction)
+  OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myFromUndo.get())
+  OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myFromRedo.get())
+  OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, mySaveTime)
+  OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsNestedTransactionMode)
+
+  for (TDF_DeltaList::Iterator anUndoFILOIt (myUndoFILO); anUndoFILOIt.More(); anUndoFILOIt.Next())
+  {
+    const Handle(TDF_Delta)& anUndoFILO = anUndoFILOIt.Value();
+    OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, anUndoFILO.get())
+  }
+
+  OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myOnlyTransactionModification)
+  OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, mySaveEmptyLabels)
+}