summaryrefslogtreecommitdiff
path: root/src/TObj/TObj_Model.cxx
blob: f3ee1a1fd93ab3f0085beb59c2afa5735c9bf84e (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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
// File:        TObj_Model.cxx
// Created:     Tue Nov  23 09:38:21 2004
// Author:      Pavel TELKOV
// Copyright:   Open CASCADE  2007
// The original implementation Copyright: (C) RINA S.p.A

#include <TObj_Model.hxx>

#include <OSD_File.hxx>
#include <Precision.hxx>
#include <Standard_ErrorHandler.hxx>
#include <TCollection_ExtendedString.hxx>
#include <TCollection_HAsciiString.hxx>
#include <TDataStd_Integer.hxx>
#include <TDataStd_Real.hxx>
#include <TDF_Tool.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDocStd_Document.hxx>
#include <TDocStd_Owner.hxx>

#include <TObj_Assistant.hxx>
#include <TObj_Application.hxx>
#include <TObj_CheckModel.hxx>
#include <TObj_HiddenPartition.hxx>
#include <TObj_LabelIterator.hxx>
#include <TObj_ModelIterator.hxx>
#include <TObj_Object.hxx>
#include <TObj_Partition.hxx>
#include <TObj_TObject.hxx>
#include <TObj_TModel.hxx>
#include <TObj_TNameContainer.hxx>
#include <Message_Msg.hxx>

#ifdef WNT
  #include <io.h>
#else
  #include <unistd.h>
#endif

#include <stdio.h>

IMPLEMENT_STANDARD_HANDLE(TObj_Model,MMgt_TShared)
IMPLEMENT_STANDARD_RTTIEXT(TObj_Model,MMgt_TShared)

//=======================================================================
//function : TObj_Model
//purpose  :
//=======================================================================

TObj_Model::TObj_Model ()
{
  myMessenger = GetApplication()->Messenger();
}

//=======================================================================
//function : GetApplication
//purpose  :
//=======================================================================

const Handle(TObj_Application) TObj_Model::GetApplication()
{
  return TObj_Application::GetInstance();
}

//=======================================================================
//function : Destructor
//purpose  :
//=======================================================================

TObj_Model::~TObj_Model ()
{
  Close();
}

//=======================================================================
//function : CloseDocument
//purpose  : free OCAF document
//=======================================================================

void TObj_Model::CloseDocument (const Handle(TDocStd_Document)& theDoc)
{
  // prevent Abort of the following modifs at document destruction if
  // a transaction is open: see theDoc->myUndoTransaction.~()
  if ( theDoc->HasOpenCommand() )
    theDoc->AbortCommand();
 
  // Application
  const Handle(TObj_Application) anApplication = GetApplication();

  // cleanup CAF data
  // PTV 21.11.2006:
  //WARNING: It could be better to remove all attributes in OCAF document structure
  // except TDocStd_Owner attribute.
  // After all other is dead set to empty document to it and remove it from label
  // ...
  // But we still have previous implementation:
  // - owner
  Handle(TDocStd_Owner) owner;
  if (theDoc->Main().Root().FindAttribute(TDocStd_Owner::GetID(), owner))
  {
    Handle_TDocStd_Document empty;
    owner->SetDocument(empty);
  }

  // just all other attributes
  theDoc->Main().Root().ForgetAllAttributes(Standard_True);
  anApplication->Close( theDoc );
}

//=======================================================================
//function : Load
//purpose  : Loads the model from the file
//=======================================================================

Standard_Boolean TObj_Model::Load (const char* theFile)
{
  // Return status
  Standard_Boolean aStatus = Standard_True;

  // Document
  Handle(TDocStd_Document) aDoc;

  // Application
  const Handle(TObj_Application) anApplication = GetApplication();

  // Current model
  const Handle(TObj_Model) me = this;
  TObj_Assistant::SetCurrentModel( me );
  TObj_Assistant::ClearTypeMap();

  Standard_Boolean isFileEmpty = checkDocumentEmpty( theFile );
  if ( isFileEmpty )
  {
    // theFile is empty, create new TDocStd_Document for this model
    aStatus = anApplication->CreateNewDocument(aDoc, GetFormat());

    if ( aStatus == Standard_True )
    {
      // Put model in a new attribute on root label
      TDF_Label aLabel = aDoc->Main();
      Handle(TObj_TModel) anAtr = new TObj_TModel;
      aLabel.AddAttribute(anAtr);
      anAtr->Set( me );
      // Record that label in the model object, and initialise the new model
      SetLabel(aLabel);
    }
  }
  else
  {
    // retrieve TDocStd_Document from <theFile>
    Messenger()->Send(Message_Msg("TObj_M_LoadDocument") << (Standard_CString)theFile,
			     Message_Info);
    aStatus = anApplication->LoadDocument(theFile,aDoc);

    if ( aStatus == Standard_True )
    {
      // Check for validity of the model read:
      // if it had wrong type, it has not been not properly restored
      TDF_Label aLabel = GetLabel();
      Standard_Boolean isValid = !aLabel.IsNull() && !aDoc.IsNull();
      {
        try
        {
          isValid = isValid && aLabel.Data() == aDoc->GetData();
        }
        catch (Standard_Failure)
        {
          isValid = Standard_False;
        }
      }
      if (!isValid)
      {
        if (!aDoc.IsNull()) CloseDocument (aDoc);
        myLabel.Nullify();
        Messenger()->Send(Message_Msg("TObj_M_WrongFile") << (Standard_CString)theFile,
				 Message_Alarm);
        aStatus = Standard_False;
      }
    }
    else
    {
      // release document from session
      // no message is needed as it has been put in anApplication->LoadDocument()
      if (!aDoc.IsNull()) CloseDocument (aDoc);
      myLabel.Nullify();
    }
  }
  //    initialise the new model
  if ( aStatus == Standard_True )
  {
    Standard_Boolean isInitOk = Standard_False;
    {
      try
      {
        isInitOk = initNewModel(isFileEmpty);
      }
      catch (Standard_Failure)
      {
#if defined(_DEBUG) || defined(DEB)
        Handle(Standard_Failure) anExc = Standard_Failure::Caught();
        TCollection_ExtendedString aString(anExc->DynamicType()->Name());
        aString = aString + ": " + anExc->GetMessageString();
        Messenger()->Send(Message_Msg("TObj_Appl_Exception") << aString);
#endif
        Messenger()->Send(Message_Msg("TObj_M_WrongFile") << (Standard_CString)theFile,
				 Message_Alarm);
      }
    }
    if (!isInitOk )
    {
      if (!aDoc.IsNull()) CloseDocument (aDoc);
      myLabel.Nullify();
      aStatus = Standard_False;
    }
  }
  TObj_Assistant::UnSetCurrentModel();
  TObj_Assistant::ClearTypeMap();
  return aStatus;
}

//=======================================================================
//function : GetFile
//purpose  : Returns the full file name this model is to be saved to, 
//           or null if the model was not saved yet
//=======================================================================

Handle(TCollection_HAsciiString) TObj_Model::GetFile() const
{
  Handle(TDocStd_Document) aDoc = TDocStd_Document::Get(GetLabel());
  if ( !aDoc.IsNull() ) {
    TCollection_AsciiString anOldPath( aDoc->GetPath() );
    if ( !anOldPath.IsEmpty() )
      return new TCollection_HAsciiString( anOldPath );
  }
  return 0;
}

//=======================================================================
//function : Save
//purpose  : Save the model to the same file
//=======================================================================

Standard_Boolean TObj_Model::Save ()
{
  Handle(TDocStd_Document) aDoc = TDocStd_Document::Get(GetLabel());
  if ( aDoc.IsNull() )
    return Standard_False;

  TCollection_AsciiString anOldPath( aDoc->GetPath() );
  if ( !anOldPath.IsEmpty() )
    return SaveAs( anOldPath.ToCString() );
  return Standard_True;
}

//=======================================================================
//function : SaveAs
//purpose  : Save the model to a file
//=======================================================================

Standard_Boolean TObj_Model::SaveAs (const char* theFile)
{
  TObj_Assistant::ClearTypeMap();
  // OCAF document
  Handle(TDocStd_Document) aDoc = TDocStd_Document::Get(GetLabel());
  if ( aDoc.IsNull() )
    return Standard_False;

  // checking that file is present on disk
  /* do not check, because could try to save as new document to existent file 
  if(!access(theFile, 0))
  {
    // checking that document has not been changed from last save
    if(!aDoc->IsChanged())
      return Standard_True;
  }
  */
  // checking write access permission
  FILE *aF = fopen (theFile, "w");
  if (aF == NULL) {
    Messenger()->Send (Message_Msg("TObj_M_NoWriteAccess") << (Standard_CString)theFile, 
			      Message_Alarm);
    return Standard_False;
  }
  else
    fclose (aF);

  // store transaction mode
  Standard_Boolean aTrMode = aDoc->ModificationMode();
  aDoc->SetModificationMode( Standard_False );
  // store all trancienmt fields of object in OCAF document if any
  Handle(TObj_ObjectIterator) anIterator;
  for(anIterator = GetObjects(); anIterator->More(); anIterator->Next())
  {
    Handle(TObj_Object) anOCAFObj = anIterator->Value();
    if (anOCAFObj.IsNull())
      continue;
    anOCAFObj->BeforeStoring();
  } // end of for(anIterator = ...)
  // set transaction mode back
  aDoc->SetModificationMode( aTrMode );

  // Application
  const Handle(TObj_Application) anApplication = GetApplication();

  // call Application->SaveAs()
  Standard_Boolean aStatus = anApplication->SaveDocument (aDoc, theFile);

  TObj_Assistant::ClearTypeMap();
  return aStatus;
}

//=======================================================================
//function : Close
//purpose  : Close the model and free related OCAF document
//=======================================================================

Standard_Boolean TObj_Model::Close()
{
  // OCAF document
  TDF_Label aLabel = GetLabel();
  if ( aLabel.IsNull() )
    return Standard_False;
  Handle(TDocStd_Document) aDoc = TDocStd_Document::Get(aLabel);
  if ( aDoc.IsNull() )
    return Standard_False;

  CloseDocument (aDoc);

  myLabel.Nullify();
  return Standard_True;
}

//=======================================================================
//function : GetDocumentModel
//purpose  : returns model which contains a document with the label
//           returns NULL handle if label is NULL
//=======================================================================

Handle(TObj_Model) TObj_Model::GetDocumentModel
                         (const TDF_Label& theLabel)
{
  Handle(TObj_Model) aModel;
  if(theLabel.IsNull())
    return aModel;

  Handle(TDocStd_Document) aDoc;
  Handle(TDF_Data) aData = theLabel.Data();
  TDF_Label aRootL = aData->Root();
  if ( aRootL.IsNull())
    return aModel;
  Handle(TDocStd_Owner) aDocOwnerAtt;
  if (aRootL.FindAttribute (TDocStd_Owner::GetID(), aDocOwnerAtt))
    aDoc = aDocOwnerAtt->GetDocument();
  
  if ( aDoc.IsNull() )
    return aModel;

  TDF_Label aLabel = aDoc->Main();
  Handle(TObj_TModel) anAttr;
  if(aLabel.FindAttribute(TObj_TModel::GetID(), anAttr))
    aModel = anAttr->Model();

  return aModel;
}

//=======================================================================
//function : GetObjects
//purpose  :
//=======================================================================

Handle(TObj_ObjectIterator) TObj_Model::GetObjects() const
{
  Handle(TObj_Model) me = this;
  return new TObj_ModelIterator(me);
}

//=======================================================================
//function : GetChildren
//purpose  :
//=======================================================================

Handle(TObj_ObjectIterator) TObj_Model::GetChildren() const
{
  Handle(TObj_Partition) aMainPartition = GetMainPartition();
  if(aMainPartition.IsNull())
    return 0;
  return aMainPartition->GetChildren();
}

//=======================================================================
//function : FindObject
//purpose  :
//=======================================================================

Handle(TObj_Object) TObj_Model::FindObject
       (const Handle(TCollection_HExtendedString)& theName,
        const Handle(TObj_TNameContainer)& theDictionary ) const
{
  Handle(TObj_TNameContainer) aDictionary = theDictionary;
  if ( aDictionary.IsNull() )
    aDictionary = GetDictionary();
  Handle(TObj_Object) aResult;
  //Check is object with given name is present in model
  if( IsRegisteredName( theName, aDictionary ) )
  {
    TDF_Label aLabel = aDictionary->Get().Find( theName );
    TObj_Object::GetObj( aLabel, aResult );
  }

  return aResult;
}

//=======================================================================
//function : GetRoot
//purpose  :
//=======================================================================

Handle(TObj_Object) TObj_Model::GetRoot() const
{
  return getPartition(GetLabel());
}

//=======================================================================
//function : GetMainPartition
//purpose  :
//=======================================================================

Handle(TObj_Partition) TObj_Model::GetMainPartition() const
{
  return getPartition( GetLabel() );
}

//=======================================================================
//function : SetNewName
//purpose  :
//=======================================================================

void TObj_Model::SetNewName(const Handle(TObj_Object)& theObject)
{
  Handle(TObj_Partition) aPartition = TObj_Partition::GetPartition(theObject);

  //sets name if partition is found
  if(aPartition.IsNull()) return;

  Handle(TCollection_HExtendedString) name = aPartition->GetNewName();
  if ( ! name.IsNull() ) theObject->SetName(name);
}

//=======================================================================
//function : IsRegisteredName
//purpose  :
//=======================================================================

Standard_Boolean TObj_Model::IsRegisteredName(const Handle(TCollection_HExtendedString)& theName,
                                                  const Handle(TObj_TNameContainer)& theDictionary ) const
{
  Handle(TObj_TNameContainer) aDictionary = theDictionary;
  if ( aDictionary.IsNull() )
    aDictionary = GetDictionary();

  if ( aDictionary.IsNull() )
    return Standard_False;
  return aDictionary->IsRegistered( theName );
}

//=======================================================================
//function : RegisterName
//purpose  :
//=======================================================================

void TObj_Model::RegisterName(const Handle(TCollection_HExtendedString)& theName,
                                  const TDF_Label& theLabel,
                                  const Handle(TObj_TNameContainer)& theDictionary ) const
{
  Handle(TObj_TNameContainer) aDictionary = theDictionary;
  if ( aDictionary.IsNull() )
    aDictionary = GetDictionary();

  if ( !aDictionary.IsNull() )
    aDictionary->RecordName( theName, theLabel );
}

//=======================================================================
//function : UnRegisterName
//purpose  :
//=======================================================================

void TObj_Model::UnRegisterName(const Handle(TCollection_HExtendedString)& theName,
                                    const Handle(TObj_TNameContainer)& theDictionary ) const
{
  Handle(TObj_TNameContainer) aDictionary = theDictionary;
  if ( aDictionary.IsNull() )
    aDictionary = GetDictionary();

  if ( !aDictionary.IsNull() )
    aDictionary->RemoveName( theName );
}

//=======================================================================
//function : GetDictionary
//purpose  :
//=======================================================================

Handle(TObj_TNameContainer) TObj_Model::GetDictionary() const
{
  Handle(TObj_TNameContainer) A;
  TDF_Label aLabel = GetLabel();
  if (!aLabel.IsNull())
    aLabel.FindAttribute(TObj_TNameContainer::GetID(),A);
  return A;
}

//=======================================================================
//function : getPartition
//purpose  :
//=======================================================================

Handle(TObj_Partition) TObj_Model::getPartition
                         (const TDF_Label&       theLabel,
                          const Standard_Boolean theHidden) const
{
  Handle(TObj_Partition) aPartition;
  if(theLabel.IsNull()) return aPartition;
  Handle(TObj_TObject) A;

  if (!theLabel.FindAttribute (TObj_TObject::GetID(), A))
  {
    if (theHidden)
      aPartition = new TObj_HiddenPartition(theLabel);
    else
      aPartition = TObj_Partition::Create(theLabel);
  }
  else
    aPartition = Handle(TObj_Partition)::DownCast(A->Get());

  return aPartition;
}

//=======================================================================
//function : getPartition
//purpose  :
//=======================================================================

Handle(TObj_Partition) TObj_Model::getPartition
                         (const TDF_Label&                  theLabel,
                          const Standard_Integer            theIndex,
                          const TCollection_ExtendedString& theName,
                          const Standard_Boolean            theHidden) const
{
  Handle(TObj_Partition) aPartition;
  if(theLabel.IsNull()) return aPartition;

  TDF_Label aLabel = theLabel.FindChild(theIndex,Standard_False);
  Standard_Boolean isNew = Standard_False;
  // defining is partition new
  if ( aLabel.IsNull() )
  {
    aLabel = theLabel.FindChild(theIndex,Standard_True);
    isNew = Standard_True;
  }
  // obtaining the partition
  aPartition = getPartition( aLabel, theHidden );

  //setting name to new partition
  if(isNew)
    aPartition->SetName(new TCollection_HExtendedString(theName));
  return aPartition;
}


//=======================================================================
//function : getPartition
//purpose  :
//=======================================================================

Handle(TObj_Partition) TObj_Model::getPartition
                         (const Standard_Integer            theIndex,
                          const TCollection_ExtendedString& theName,
                          const Standard_Boolean            theHidden) const
{
  return getPartition (GetMainPartition()->GetChildLabel(),
                       theIndex, theName, theHidden);
}

//=======================================================================
//function : initNewModel
//purpose  :
//=======================================================================

Standard_Boolean TObj_Model::initNewModel (const Standard_Boolean IsNew)
{
  // set names map
  TObj_TNameContainer::Set(GetLabel());

  // do something for loaded model.
  if (!IsNew)
  {
    // Register names of model in names map.
    Handle(TObj_ObjectIterator) anIterator;
    for(anIterator = GetObjects(); anIterator->More(); anIterator->Next())
    {
      Handle(TObj_Object) anOCAFObj = anIterator->Value();
      if (anOCAFObj.IsNull())
        continue;
      anOCAFObj->AfterRetrieval();
    } // end of for(anIterator = ...)
    // update back references for loaded model by references
    updateBackReferences( GetMainPartition() );

    if ( isToCheck() )
    {
      // check model consistency
      Handle(TObj_CheckModel) aCheck = GetChecker();
      aCheck->Perform();
      aCheck->SendMessages();
      // tell that the model has been modified
      SetModified(Standard_True);
    }
  }
  return Standard_True;
}

//=======================================================================
//function : updateBackReferences
//purpose  :
//=======================================================================

void TObj_Model::updateBackReferences (const Handle(TObj_Object)& theObject)
{
  // recursive update back references
  if ( theObject.IsNull() )
    return;
  Handle(TObj_ObjectIterator) aChildren = theObject->GetChildren();
  for(;aChildren->More() && aChildren->More(); aChildren->Next())
  {
    Handle(TObj_Object) aChild = aChildren->Value();
    updateBackReferences( aChild );
  }
  // update back references of reference objects
  Handle(TObj_LabelIterator) anIter =
    Handle(TObj_LabelIterator)::DownCast(theObject->GetReferences());

  if(anIter.IsNull()) // to avoid exception
    return;

  // LH3D15722. Remove all back references to make sure there will be no unnecessary
  // duplicates, since some back references may already exist after model upgrading.
  // (do not take care that object can be from other document, because 
  // we do not modify document, all modifications are made in transient fields)
  for( ; anIter->More() ; anIter->Next())
  {
    Handle(TObj_Object) anObject = anIter->Value();
    if ( !anObject.IsNull() )
      anObject->RemoveBackReference( theObject, Standard_False );
  }
  // and at last create back references
  anIter = Handle(TObj_LabelIterator)::DownCast(theObject->GetReferences());
  if(!anIter.IsNull())
    for( ; anIter->More() ; anIter->Next())
    {
      Handle(TObj_Object) anObject = anIter->Value();
      if ( !anObject.IsNull() )
        anObject->AddBackReference( theObject );
    }
}

//=======================================================================
//function : GetDocument
//purpose  :
//=======================================================================

Handle(TDocStd_Document) TObj_Model::GetDocument() const
{
  Handle(TDocStd_Document) D;
  TDF_Label aLabel = GetLabel();
  if (!aLabel.IsNull())
    D = TDocStd_Document::Get(aLabel);
  return D;
}

//=======================================================================
//function : HasOpenCommand
//purpose  :
//=======================================================================

Standard_Boolean TObj_Model::HasOpenCommand() const
{
  return GetDocument()->HasOpenCommand();
}

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

void TObj_Model::OpenCommand() const
{
  GetDocument()->OpenCommand();
}

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

void TObj_Model::CommitCommand() const
{
  GetDocument()->CommitCommand();
}

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

void TObj_Model::AbortCommand() const
{
  GetDocument()->AbortCommand();
}

//=======================================================================
//function : IsModified
//purpose  : Status of modification
//=======================================================================

Standard_Boolean TObj_Model::IsModified () const
{
  Handle(TDocStd_Document) aDoc = GetDocument();
  return aDoc.IsNull() ? Standard_False : aDoc->IsChanged();
}

//=======================================================================
//function : SetModified
//purpose  : Status of modification
//=======================================================================

void TObj_Model::SetModified (const Standard_Boolean theModified)
{
  Handle(TDocStd_Document) aDoc = GetDocument();
  if (!aDoc.IsNull())
  {
    Standard_Integer aSavedTime = aDoc->GetData()->Time();
    if (theModified)
      --aSavedTime;
    aDoc->SetSavedTime (aSavedTime);
  }
}

//=======================================================================
//function : checkDocumentEmpty
//purpose  : Check whether the document contains the Ocaf data
//=======================================================================

Standard_Boolean TObj_Model::checkDocumentEmpty (const char* theFile)
{
  if (!theFile)
    return Standard_True;

  TCollection_AsciiString aFile ((Standard_CString) theFile);
  if (aFile.IsEmpty())
    return Standard_True;

  OSD_Path aPath (aFile);
  OSD_File osdfile (aPath);
  if ( !osdfile.Exists() )
    return Standard_True;
  
  FILE* f = fopen( theFile, "r" );
  if ( f )
  {
    Standard_Boolean isZeroLengh = Standard_False;
    fseek( f, 0, SEEK_END );
    if ( ftell( f ) == 0 )
      isZeroLengh = Standard_True;

    fclose( f );
    return isZeroLengh;
  }
  return Standard_False;
}

//=======================================================================
//function : GetGUID
//purpose  :
//=======================================================================

Standard_GUID TObj_Model::GetGUID() const
{
  Standard_GUID aGUID("3bbefb49-e618-11d4-ba38-0060b0ee18ea");
  return aGUID;
}

//=======================================================================
//function : GetFormat
//purpose  :
//=======================================================================

TCollection_ExtendedString TObj_Model::GetFormat() const
{
  return TCollection_ExtendedString ("TObjBin");
}

//=======================================================================
//function : GetFormatVersion
//purpose  :
//=======================================================================

Standard_Integer TObj_Model::GetFormatVersion() const
{
  TDF_Label aLabel = GetDataLabel().FindChild(DataTag_FormatVersion,Standard_False);
  if(aLabel.IsNull())
    return -1;

  Handle(TDataStd_Integer) aNum;
  if(!aLabel.FindAttribute ( TDataStd_Integer::GetID(), aNum ))
    return -1;
  else
    return aNum->Get();
}

//=======================================================================
//function : SetFormatVersion
//purpose  :
//=======================================================================

void TObj_Model::SetFormatVersion(const Standard_Integer theVersion)
{
  TDF_Label aLabel = GetDataLabel().FindChild(DataTag_FormatVersion,Standard_True);
  TDataStd_Integer::Set(aLabel,theVersion);
}


//=======================================================================
//function : GetDataLabel
//purpose  :
//=======================================================================

TDF_Label TObj_Model::GetDataLabel() const
{
  return GetMainPartition()->GetDataLabel();
}

//=======================================================================
//function : Paste
//purpose  :
//=======================================================================

Standard_Boolean TObj_Model::Paste (Handle(TObj_Model)      theModel,
                                        Handle(TDF_RelocationTable) theRelocTable)
{
  if(theModel.IsNull()) return Standard_False;
  // clearing dictionary of objects names
//  theModel->GetDictionary()->NewEmpty()->Paste(theModel->GetDictionary(),
//                                               new TDF_RelocationTable);
//  theModel->GetLabel().ForgetAllAttributes(Standard_True);
  TObj_TNameContainer::Set(theModel->GetLabel());
  GetMainPartition()->Clone(theModel->GetLabel(), theRelocTable);
  return Standard_True;
}

//=======================================================================
//function : CopyReferences
//purpose  :
//=======================================================================

void TObj_Model::CopyReferences(const Handle(TObj_Model)& theTarget,
                                    const Handle(TDF_RelocationTable)& theRelocTable)
{
  Handle(TObj_Object) aMyRoot = GetMainPartition();
  Handle(TObj_Object) aTargetRoot = theTarget->GetMainPartition();
  aMyRoot->CopyReferences(aTargetRoot, theRelocTable);
}

//=======================================================================
//function : GetModelName
//purpose  : Returns the name of the model
//           by default returns TObj
//=======================================================================

Handle(TCollection_HExtendedString) TObj_Model::GetModelName() const
{
  Handle(TCollection_HExtendedString) aName =
    new TCollection_HExtendedString("TObj");
  return aName;
}

//=======================================================================
//function : Update
//purpose  : default implementation is empty
//=======================================================================

Standard_Boolean TObj_Model::Update ()
{
  return Standard_True;
}

//=======================================================================
//function : GetChecker
//purpose  :
//=======================================================================

Handle(TObj_CheckModel) TObj_Model::GetChecker() const
{
  return new TObj_CheckModel (this);
}