summaryrefslogtreecommitdiff
path: root/src/TDocStd/TDocStd_Document.cxx
blob: 36db7fe314967842f661ac86718f2403dfd33b0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
// File:      TDocStd_Document.cxx
// Copyright: Open Cascade 2006

#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 <TCollection_AsciiString.hxx>
#include <TDF_AttributeIterator.hxx>
#include <TDF_ListIteratorOfDeltaList.hxx>
#include <TDF_AttributeList.hxx>
#include <TDF_ListIteratorOfAttributeList.hxx>
#include <TDF_AttributeDelta.hxx>
#include <TDF_AttributeDeltaList.hxx>
#include <TDF_ListIteratorOfAttributeDeltaList.hxx>
#include <TDF_Label.hxx>
#include <TDF_Delta.hxx>
#include <TDocStd_CompoundDelta.hxx>
#include <TDocStd_Owner.hxx>
#include <TDocStd_Modified.hxx>

#include <TDF_IDMap.hxx>
#include <TDocStd_LabelIDMapDataMap.hxx>

#include <CDM_MetaData.hxx>

// List should have a RemoveLast...
#define TDocStd_List_RemoveLast(theList) \
TDF_ListIteratorOfDeltaList it(theList); \
Standard_Integer i,n = theList.Extent(); \
for (i = 1; i < n; i++) it.Next(); \
theList.Remove(it);

#undef DEB_TRANS

#undef DEB_DELTA

#define BUC60836

#define SRN_DELTA_COMPACT

//=======================================================================
//function : Get
//purpose  : 
//=======================================================================

Handle(TDocStd_Document) TDocStd_Document::Get (const TDF_Label& acces)
{
  return TDocStd_Owner::GetDocument(acces.Data());
}

//=======================================================================
//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) : 
myStorageFormat(aStorageFormat),
myData (new TDF_Data()),
myUndoLimit(0),
mySaveTime(0),
myIsNestedTransactionMode(0)
{
  TDF_Transaction* pTr =  new TDF_Transaction (myData,"UNDO");
  myUndoTransaction    = *pTr; delete pTr;
  TDocStd_Owner::SetDocument(myData,this);

#ifdef SRN_DELTA_COMPACT
  myFromUndo.Nullify();  
  myFromRedo.Nullify();
#endif
}


//=======================================================================
//function : IsSaved
//purpose  : 
//=======================================================================

Standard_Boolean TDocStd_Document::IsSaved() const
{
  return CDM_Document::IsStored();
}


//=======================================================================
//function : GetName
//purpose  : 
//=======================================================================

TCollection_ExtendedString TDocStd_Document::GetName () const
{
  return CDM_Document::MetaData()->Name();
}

//=======================================================================
//function : GetPath
//purpose  : 
//=======================================================================

TCollection_ExtendedString TDocStd_Document::GetPath () const
{
  return CDM_Document::MetaData()->Path();
}


//=======================================================================
//function : SetData
//purpose  : 
//=======================================================================

void TDocStd_Document::SetData (const Handle(TDF_Data)& D)
{
  myData = D;
  TDF_Transaction* pTr = new TDF_Transaction(myData,"UNDO");
  myUndoTransaction = *pTr; delete pTr;  
}

//=======================================================================
//function : GetData
//purpose  : 
//=======================================================================

Handle(TDF_Data) TDocStd_Document::GetData () const
{
  return myData;
}

//=======================================================================
//function : Main
//purpose  : 
//=======================================================================

TDF_Label TDocStd_Document::Main () const
{ 
  return  myData->Root().FindChild(1,Standard_True);
}

//=======================================================================
//function : IsEmpty
//purpose  : 
//=======================================================================

Standard_Boolean TDocStd_Document::IsEmpty() const
{
  TDF_AttributeIterator It (Main());
  return !It.More();
}

//=======================================================================
//function : IsValid
//purpose  : 
//=======================================================================

Standard_Boolean TDocStd_Document::IsValid() const
{
  return TDocStd_Modified::IsEmpty(Main());
}

//=======================================================================
//function : SetModified
//purpose  : 
//=======================================================================

void TDocStd_Document::SetModified (const TDF_Label& L)                                  
{  
  TDocStd_Modified::Add(L);
}

//=======================================================================
//function : IsModified
//purpose  : 
//=======================================================================
//Standard_Boolean TDocStd_Document::IsModified (const TDF_Label& L) const                                 
//{  
//  return TDocStd_Modified::Contains(L);
//}

//=======================================================================
//function : PurgeModified
//purpose  : 
//=======================================================================

void TDocStd_Document::PurgeModified()
{   
  TDocStd_Modified::Clear(Main()); 
}

//=======================================================================
//function : GetModified
//purpose  : 
//=======================================================================

const TDF_LabelMap&  TDocStd_Document::GetModified() const
{  
  return TDocStd_Modified::Get(Main());  
}



//=======================================================================
//function : Update
//purpose  : 
//=======================================================================

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)) {
    TCollection_AsciiString aDocEntry(aReferenceIdentifier);
    UpdateReferences(aDocEntry);
    SetIsUpToDate(aReferenceIdentifier);
  }
}

//=======================================================================
//function : NewCommand
//purpose  : 
//=======================================================================

void TDocStd_Document::NewCommand()
{
#ifdef DEB_TRANS
  if (myUndoTransaction.IsOpen() && myData->Transaction() > 1) {
    Standard_DomainError::Raise ("NewCommand : many open transactions");
  }
#endif

  CommitTransaction();
  OpenTransaction();

#ifdef DEB_TRANS
  cout<<"End NewCommand"<<endl;
#endif
}


//=======================================================================
//function : HasOpenCommand
//purpose  : 
//=======================================================================
Standard_Boolean TDocStd_Document::HasOpenCommand() const
{
  return myUndoTransaction.IsOpen();
}

//=======================================================================
//function : OpenCommand
//purpose  : 
//=======================================================================

void TDocStd_Document::OpenCommand ()
{
  if (!myIsNestedTransactionMode && myUndoTransaction.IsOpen()) {
    Standard_DomainError::Raise("TDocStd_Document::OpenCommand : already open");
  }
  OpenTransaction();
//  if (myUndoLimit != 0) myUndoTransaction.Open();
}

//=======================================================================
//function : CommitCommand
//purpose  : 
//=======================================================================

Standard_Boolean TDocStd_Document::CommitCommand ()
{
  return CommitTransaction();
}


//=======================================================================
//function : AbortCommand
//purpose  : 
//=======================================================================

void TDocStd_Document::AbortCommand ()
{ 
  AbortTransaction();
}


//=======================================================================
//function : CommitTransaction
//purpose  : Private method.
//=======================================================================

Standard_Boolean TDocStd_Document::CommitTransaction()
{
  myData->AllowModification(Standard_True);

  Standard_Boolean isDone = Standard_False;
  // nested transaction mode
  if (myIsNestedTransactionMode && myUndoTransaction.IsOpen()) {

    Handle(TDF_Delta) D = myUndoTransaction.Commit(Standard_True);
    Handle(TDocStd_CompoundDelta) aCompDelta =
      Handle(TDocStd_CompoundDelta)::DownCast(myUndoFILO.First());
    AppendDeltaToTheFirst(aCompDelta, D);
    D = aCompDelta;
    myUndoFILO.RemoveFirst();
    if(myUndoFILO.Extent()) {
      aCompDelta = Handle(TDocStd_CompoundDelta)::DownCast(myUndoFILO.First());
      AppendDeltaToTheFirst(aCompDelta, D);
      myUndoTransaction.Open();
    }
    else {
      if(!D->IsEmpty()) {
        myUndos.Append(D);
        myRedos.Clear(); // if we push an Undo we clear the redos
        isDone = Standard_True;
      }
    }

    // deny modifications if the transaction is not opened
    if(myOnlyTransactionModification) {
      myData->AllowModification(myUndoTransaction.IsOpen() && myUndoLimit
                                ? Standard_True :Standard_False);
    }

  } else {

  // are we undoing...
    if (myUndoLimit != 0 && myUndoTransaction.IsOpen()) {

      Handle(TDF_Delta) D = myUndoTransaction.Commit(Standard_True);
      if (!(D.IsNull() || D->IsEmpty())) {
        isDone = Standard_True;

        myRedos.Clear(); // if we push an Undo we clear the redos
        myUndos.Append(D); // New undos are at the end of the list
        // Check  the limit to remove the oldest one
        if (myUndos.Extent() > myUndoLimit) {
#ifdef SRN_DELTA_COMPACT
          Handle(TDF_Delta) aDelta = myUndos.First();
#endif
          myUndos.RemoveFirst();
#ifdef SRN_DELTA_COMPACT
          if(myFromUndo == aDelta) {
            //The oldest Undo delta coincides with `from` delta
            if(myUndos.Extent() == 1) {   //There is the only Undo
              myFromUndo.Nullify();
              myFromRedo.Nullify();
            }
            else
              myFromUndo = myUndos.First();
          }
#endif
        }
      }

    }

    // deny or allow modifications acording to transaction state
    if(myOnlyTransactionModification) {
      myData->AllowModification (myUndoTransaction.IsOpen() && myUndoLimit
                                 ? Standard_True :Standard_False);
    }
  }
  // Notify CDM_Application of the successful commit
  if (isDone && IsOpened()) {
    const Handle(TDocStd_Application) anAppli =
      Handle(TDocStd_Application)::DownCast(Application());
    if (!anAppli.IsNull())
      anAppli -> OnCommitTransaction (this);
  }
  return isDone;
}


//=======================================================================
//function : AbortTransaction
//purpose  : Private method.
//=======================================================================

void TDocStd_Document::AbortTransaction()
{
  myData->AllowModification(Standard_True);
  
  if (myUndoTransaction.IsOpen())
    if (myUndoLimit != 0)
      myUndoTransaction.Abort();

  if (myIsNestedTransactionMode && myUndoFILO.Extent()) {
    if (!myUndoFILO.First()->IsEmpty())
      myData->Undo(myUndoFILO.First(),Standard_True);
    myUndoFILO.RemoveFirst();
    if (myUndoFILO.Extent())
      myUndoTransaction.Open();
  }
  // deny or allow modifications acording to transaction state
  if (myOnlyTransactionModification) {
    myData->AllowModification (myUndoTransaction.IsOpen() && myUndoLimit
                               ? Standard_True :Standard_False);
  }
  // Notify CDM_Application of the event
  if (IsOpened()) {
    const Handle(TDocStd_Application) anAppli =
      Handle(TDocStd_Application)::DownCast(Application());
    if (!anAppli.IsNull())
      anAppli -> OnAbortTransaction (this);
  }
}


//=======================================================================
//function :OpenTransaction
//purpose  : Private method.
//=======================================================================

void TDocStd_Document::OpenTransaction()
{
  myData->AllowModification(Standard_True);

  // nested transaction mode
  if (myIsNestedTransactionMode) {

    if (myUndoTransaction.IsOpen()) {
      Handle(TDF_Delta) D = myUndoTransaction.Commit(Standard_True);
      Handle(TDocStd_CompoundDelta) aCompDelta =
        Handle(TDocStd_CompoundDelta)::DownCast(myUndoFILO.First());
      AppendDeltaToTheFirst(aCompDelta, D);
    }
    Standard_Integer aLastTime = myData->Time();
    if (myUndoFILO.Extent())
      aLastTime = myUndoFILO.First()->EndTime();
    Handle(TDocStd_CompoundDelta) aCompoundDelta =
      new TDocStd_CompoundDelta;
    aCompoundDelta->Validity(aLastTime, aLastTime);
    myUndoFILO.Prepend(aCompoundDelta);
  } 

  if (myUndoLimit != 0) myUndoTransaction.Open();

  // deny or allow modifications acording to transaction state
  if (myOnlyTransactionModification) {
    myData->AllowModification (myUndoTransaction.IsOpen() && myUndoLimit
                               ? Standard_True :Standard_False);
  }
  // Notify CDM_Application of the event
  if (IsOpened()) {
    const Handle(TDocStd_Application) anAppli =
      Handle(TDocStd_Application)::DownCast(Application());
    if (!anAppli.IsNull())
      anAppli -> OnOpenTransaction (this);
  }
}

//=======================================================================
//function : SetUndoLimit
//purpose  : 
//=======================================================================

void TDocStd_Document::SetUndoLimit(const Standard_Integer L)
{  
#ifdef SRN_DELTA_COMPACT
  myFromUndo.Nullify();  //Compaction has to aborted
  myFromRedo.Nullify();
#endif

  CommitTransaction ();
  myUndoLimit = (L > 0) ? L : 0;
  Standard_Integer n = myUndos.Extent() - myUndoLimit;
  while (n > 0) {
    myUndos.RemoveFirst();
    --n;
  }
  // deny or allow modifications acording to transaction state
  if(myOnlyTransactionModification) {
    myData->AllowModification(myUndoTransaction.IsOpen() && myUndoLimit
                              ? Standard_True :Standard_False);
  }
  //OpenTransaction(); dp 15/10/99
}

//=======================================================================
//function : GetUndoLimit
//purpose  : 
//=======================================================================

Standard_Integer TDocStd_Document::GetUndoLimit() const
{
  return myUndoLimit;
}

//=======================================================================
//function : Undos
//purpose  : 
//=======================================================================

Standard_Integer TDocStd_Document::GetAvailableUndos() const
{
  return myUndos.Extent();
}

//=======================================================================
//function : ClearUndos
//purpose  : 
//=======================================================================

void TDocStd_Document::ClearUndos()
{
  myUndos.Clear();
  myRedos.Clear();
#ifdef SRN_DELTA_COMPACT
  myFromRedo.Nullify();
  myFromUndo.Nullify();
#endif
}

//=======================================================================
//function : ClearRedos
//purpose  : 
//=======================================================================

void TDocStd_Document::ClearRedos()
{
  myRedos.Clear();
#ifdef SRN_DELTA_COMPACT
  myFromRedo.Nullify();
#endif
}

//=======================================================================
//function : Undo
//purpose  : 
// Some importante notice:
// 1) The most recent undo delta is at the end of the list.
// 2) Removing the LAST item of a list is tedious, but it is done only on
//    Undo. Remove first is done at each command if the limit is reached!
// 3) To make fun, the redos are not like the undos: the most recent delta
//    is at the beginning! Like this, it is easier to remove it after use.
//=======================================================================
Standard_Boolean TDocStd_Document::Undo() 
{
  // Don't call NewCommand(), because it may commit Interactive Attributes
  // and generate a undesirable Delta!

  Standard_Boolean isOpened = myUndoTransaction.IsOpen();
  Standard_Boolean undoDone = Standard_False;
  //TDF_Label currentObjectLabel = CurrentLabel (); //Sauve pour usage ulterieur.

  if (!myUndos.IsEmpty()) {
    // Reset the transaction
    AbortTransaction();

    // only for nested transaction mode
    while(myIsNestedTransactionMode && myUndoFILO.Extent())
      AbortTransaction();

    // allow modifications
    myData->AllowModification(Standard_True);

    // Apply the Undo
    // should test the applicability before.
#ifdef DEB_DELTA
    cout<<"DF before Undo =================================="<<endl; TDF_Tool::DeepDump(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);
#endif
    // Push the redo
    myRedos.Prepend(D);
    // Remove the last Undo
    TDocStd_List_RemoveLast(myUndos);
    undoDone = Standard_True;
  }

  if (isOpened && undoDone) OpenTransaction();

  // deny or allow modifications acording to transaction state
  if(myOnlyTransactionModification) {
    myData->AllowModification(myUndoTransaction.IsOpen() && myUndoLimit
                              ? Standard_True :Standard_False);
  }
  
  return undoDone;
}

//=======================================================================
//function : GetAvailableRedos
//purpose  : 
//=======================================================================

Standard_Integer TDocStd_Document:: GetAvailableRedos() const
{
  // should test the applicability before.
  return myRedos.Extent();
}

//=======================================================================
//function : Redo
//purpose  : 
//=======================================================================
Standard_Boolean TDocStd_Document::Redo() 
{
  Standard_Boolean isOpened = myUndoTransaction.IsOpen();
  Standard_Boolean undoDone = Standard_False;
  // TDF_Label currentObjectLabel = CurrentLabel();//Sauve pour usage ulterieur.
  if (!myRedos.IsEmpty()) {
    // should test the applicability before.
    // Reset the transaction
    AbortTransaction();

    // only for nested transaction mode
    while(myIsNestedTransactionMode && myUndoFILO.Extent())
      AbortTransaction();

    // allow modifications
    myData->AllowModification(Standard_True);

    // Apply the Redo
#ifdef DEB_DELTA
    cout<<"DF before Redo =================================="<<endl; TDF_Tool::DeepDump(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);
#endif
    // Push the redo of the redo as an undo (got it !)
    myUndos.Append(D);
    // remove the Redo from the head
    myRedos.RemoveFirst();
    undoDone = Standard_True;
  }
  
  if (isOpened && undoDone) OpenTransaction();

  // deny or allow modifications acording to transaction state
  if(myOnlyTransactionModification) {
    myData->AllowModification(myUndoTransaction.IsOpen() && myUndoLimit
                              ? Standard_True :Standard_False);
  }

  return undoDone;
}

//=======================================================================
//function : UpdateReferences
//purpose  : 
//=======================================================================

void TDocStd_Document::UpdateReferences(const TCollection_AsciiString& aDocEntry) 
{

  TDF_AttributeList aRefList;
  TDocStd_XLink* xRefPtr;
  for (TDocStd_XLinkIterator xItr (this); xItr.More(); xItr.Next()) {
    xRefPtr = xItr.Value();
    if (xRefPtr->DocumentEntry() == aDocEntry) {
      aRefList.Append(xRefPtr->Update());
    }
  }
  TDF_ListIteratorOfAttributeList It(aRefList);
  for (;It.More();It.Next()) {
    //     // mise a jour import
    SetModified(It.Value()->Label());
  }
}


//=======================================================================
//function : GetUndos
//purpose  : 
//=======================================================================

const TDF_DeltaList& TDocStd_Document::GetUndos() const 
{
  return myUndos;
}


//=======================================================================
//function : GetRedos
//purpose  : 
//=======================================================================

const TDF_DeltaList& TDocStd_Document::GetRedos() const 
{
  return myRedos;
}

//=======================================================================
//function : InitDeltaCompaction
//purpose  : 
//=======================================================================

Standard_Boolean TDocStd_Document::InitDeltaCompaction()
{
#ifdef SRN_DELTA_COMPACT
  if (myUndoLimit == 0 || myUndos.Extent() == 0) {
    myFromRedo.Nullify();
    myFromUndo.Nullify();
    return Standard_False; //No Undos to compact
  }

  myFromRedo.Nullify();

  myFromUndo = myUndos.Last();
  if(myRedos.Extent() > 0) myFromRedo = myRedos.First();
#endif
  return Standard_True;
}

//=======================================================================
//function : PerformDeltaCompaction
//purpose  : 
//=======================================================================

Standard_Boolean TDocStd_Document::PerformDeltaCompaction()  
{ 
#ifdef SRN_DELTA_COMPACT
  if(myFromUndo.IsNull()) return Standard_False;  //Redo can be Null for this operation 

  TDF_DeltaList aList; 
  Handle(TDocStd_CompoundDelta) aCompoundDelta = new TDocStd_CompoundDelta; 
  TDF_ListIteratorOfDeltaList anIterator(myUndos); 
  TDF_ListIteratorOfAttributeDeltaList aDeltasIterator;
  TDocStd_LabelIDMapDataMap aMap; 
  Standard_Boolean isFound = Standard_False, isTimeSet = Standard_False; 

  //Process Undos

  for(; anIterator.More(); anIterator.Next()) { 
    if(!isFound) { 
      if(myFromUndo == anIterator.Value()) isFound = Standard_True; 
      aList.Append(anIterator.Value());  //Fill the list of deltas that precede compound delta 
      continue;
    } 

    if(!isTimeSet) {  //Set begin and end time when the compound delta is valid
      aCompoundDelta->Validity(anIterator.Value()->BeginTime(), myUndos.Last()->EndTime());
      isTimeSet = Standard_True;
    } 
    
    aDeltasIterator.Initialize(anIterator.Value()->AttributeDeltas());
    for(; aDeltasIterator.More(); aDeltasIterator.Next()) {   
      if(!aMap.IsBound(aDeltasIterator.Value()->Label())) {
	TDF_IDMap* pIDMap = new TDF_IDMap();
	aMap.Bind(aDeltasIterator.Value()->Label(), *pIDMap);
	delete pIDMap;
	}
      if(aMap(aDeltasIterator.Value()->Label()).Add(aDeltasIterator.Value()->ID())) //The attribute is not 
	aCompoundDelta->AddAttributeDelta(aDeltasIterator.Value());                 //already in the delta
    }
  } 

  myUndos.Clear(); 
  myUndos.Assign(aList); 
  myUndos.Append(aCompoundDelta); 

  //Process Redos

  if(myFromRedo.IsNull()) {
    myRedos.Clear();
    return Standard_True;
  }

  aList.Clear();

  for(anIterator.Initialize(myRedos); anIterator.More(); anIterator.Next()) { 
    aList.Append(anIterator.Value()); 
    if(anIterator.Value() == myFromRedo) break;
  }

  myRedos.Clear();
  myRedos.Assign(aList); 
#endif
  return Standard_True; 
} 


//=======================================================================
//function : StorageFormat
//purpose  : 
//=======================================================================

TCollection_ExtendedString TDocStd_Document::StorageFormat() const 
{
  return myStorageFormat;
}


//=======================================================================
//function : ChangeStorageFormat
//purpose  : 
//=======================================================================

void TDocStd_Document::ChangeStorageFormat (const TCollection_ExtendedString& newStorageFormat) 
{
  if (newStorageFormat != myStorageFormat) {
    myStorageFormat = newStorageFormat;
    myResourcesAreLoaded = Standard_False;
    CDM_Document::LoadResources ();
  }
}




//=======================================================================
//function : Recompute
//purpose  : 
//=======================================================================

void TDocStd_Document::Recompute ()
{
  if (IsValid()) return;
  // find the top function and execute it
  //  Handle(TDesign_Function) F;
  //  if (Main().FindAttribute(TDesign_Function::GetID(),F)) {
  // TFunction_Solver slv;
  // slv.SetTouched(GetModified());
  // slv.ExecuteFrom(F);
  PurgeModified();
}

//=======================================================================
//function : AppendDeltaToTheFirst
//purpose  : Appends delta to the first delta in the myUndoFILO
//=======================================================================

void TDocStd_Document::AppendDeltaToTheFirst
  (const Handle(TDocStd_CompoundDelta)& theDelta1,
   const Handle(TDF_Delta)& theDelta2)
{
  if(theDelta2->IsEmpty()) return;
  TDocStd_LabelIDMapDataMap aMap; 
    
  TDF_ListIteratorOfAttributeDeltaList aDeltasIterator1
    (theDelta1->AttributeDeltas());
  for(; aDeltasIterator1.More(); aDeltasIterator1.Next()) {   
    TDF_Label aLabel = aDeltasIterator1.Value()->Label();
    if(!aMap.IsBound(aLabel)) {
      TDF_IDMap aTmpIDMap;
      aMap.Bind(aLabel, aTmpIDMap);
    }
    Standard_GUID anID = aDeltasIterator1.Value()->ID();
    TDF_IDMap& anIDMap = aMap.ChangeFind(aLabel);
    anIDMap.Add(anID);
  }
  
  theDelta1->Validity(theDelta1->BeginTime(), theDelta2->EndTime());
  TDF_ListIteratorOfAttributeDeltaList aDeltasIterator2
    (theDelta2->AttributeDeltas());
  for(; aDeltasIterator2.More(); aDeltasIterator2.Next()) {   
    TDF_Label aLabel = aDeltasIterator2.Value()->Label();
    Standard_GUID anID = aDeltasIterator2.Value()->ID();
    if(aMap.IsBound(aLabel)) {
      const TDF_IDMap& anIDMap = aMap.Find(aLabel);
      if(anIDMap.Contains(anID)) continue;
    }
    theDelta1->AddAttributeDelta(aDeltasIterator2.Value());
  }
}

//=======================================================================
//function : RemoveFirstUndo
//purpose  : 
//=======================================================================
void TDocStd_Document::RemoveFirstUndo() {
  if (myUndos.IsEmpty()) return;
  myUndos.RemoveFirst();
}