0024023: Revamp the OCCT Handle -- plugin
[occt.git] / src / BinTObjDrivers / BinTObjDrivers_ReferenceDriver.cxx
1 // Created on: 2004-11-24
2 // Created by: Edward AGAPOV
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 // The original implementation Copyright: (C) RINA S.p.A
17
18
19 #include <BinTObjDrivers_ReferenceDriver.hxx>
20 #include <CDM_MessageDriver.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TObj_TReference.hxx>
23 #include <BinObjMgt_Persistent.hxx>
24 #include <TObj_Object.hxx>
25 #include <TObj_Model.hxx>
26 #include <TObj_Assistant.hxx>
27 #include <TDF_ChildIterator.hxx>
28 #include <TDF_Tool.hxx>
29
30
31 //=======================================================================
32 //function : BinTObjDrivers_ReferenceDriver
33 //purpose  : constructor
34 //=======================================================================
35
36 BinTObjDrivers_ReferenceDriver::BinTObjDrivers_ReferenceDriver
37                          (const Handle(CDM_MessageDriver)& theMessageDriver)
38 : BinMDF_ADriver( theMessageDriver, NULL)
39 {
40 }
41
42 //=======================================================================
43 //function : NewEmpty
44 //purpose  : Creates a new attribute
45 //=======================================================================
46
47 Handle(TDF_Attribute) BinTObjDrivers_ReferenceDriver::NewEmpty() const
48 {
49   return new TObj_TReference;
50 }
51
52 //=======================================================================
53 //function : Paste
54 //purpose  : Translate the contents of <theSource> and put it
55 //           into <theTarget>.
56 //=======================================================================
57
58 Standard_Boolean BinTObjDrivers_ReferenceDriver::Paste
59                          (const BinObjMgt_Persistent&  theSource,
60                           const Handle(TDF_Attribute)& theTarget,
61                           BinObjMgt_RRelocationTable&) const
62 {
63   // master label
64   TDF_Label aMasterLabel;
65   Handle(TDF_Data) aDS = theTarget->Label().Data();
66   if (! theSource.GetLabel (aDS, aMasterLabel)) return Standard_False;
67
68   // isSameDoc flag
69   Standard_Boolean isSameDoc = Standard_False;
70   if (! (theSource >> isSameDoc)) return Standard_False;
71
72   // DS for referred label
73   if (!isSameDoc) 
74   {
75     TCollection_AsciiString aName;
76     if (! (theSource >> aName)) return Standard_False;
77     Handle(TObj_Model) aModel = Handle(TObj_Model)::DownCast
78       ( TObj_Assistant::FindModel( aName.ToCString() ));
79     if (aModel.IsNull())
80     {
81       TCollection_AsciiString anEntry;
82       TDF_Tool::Entry (theTarget->Label(), anEntry);
83       WriteMessage (TCollection_ExtendedString ("TObj_TReference retrieval: ")
84                     + "wrong model ID " + aName + ", entry " + anEntry);
85       return Standard_False;
86     }
87     aDS = aModel->GetLabel().Data();
88   }
89   // reffered label
90   TDF_Label aLabel;
91   if (! theSource.GetLabel (aDS, aLabel)) return Standard_False;
92
93   // set reference attribute fields
94   Handle(TObj_TReference) aTarget =
95     Handle(TObj_TReference)::DownCast (theTarget);
96   aTarget->Set ( aLabel, aMasterLabel );
97
98   return !aLabel.IsNull() && !aMasterLabel.IsNull();
99 }
100
101 //=======================================================================
102 //function : Paste
103 //purpose  : Translate the contents of <theSource> and put it
104 //           into <theTarget>.
105 //           Store master and referred labels as entry, the other model referred
106 //           as entry in model-container
107 //=======================================================================
108
109 void BinTObjDrivers_ReferenceDriver::Paste
110                          (const Handle(TDF_Attribute)& theSource,
111                           BinObjMgt_Persistent&        theTarget,
112                           BinObjMgt_SRelocationTable&) const
113 {
114   Handle(TObj_TReference) aSource =
115     Handle(TObj_TReference)::DownCast (theSource);
116
117   Handle(TObj_Object) aLObject = aSource->Get();
118   if (aLObject.IsNull())
119     return;
120
121   // labels
122   TDF_Label aLabel = aLObject->GetLabel();
123   TDF_Label aMasterLabel = aSource->GetMasterLabel();
124   Standard_Boolean isSameDoc = (aLabel.Root() == aMasterLabel.Root());
125
126   // store data
127   // 1 - the master label;
128   theTarget << aMasterLabel;
129   // 2 - isSameDoc flag plus may be a Model ID
130   theTarget << isSameDoc;
131   if (! isSameDoc)
132   {
133     TCollection_AsciiString aName;
134     Handle(TObj_Model) aModel =
135       Handle(TObj_Model)::DownCast( aLObject->GetModel() );
136     aName = TCollection_AsciiString( aModel->GetModelName()->String() );
137     theTarget << aName;
138   }
139   // 3 - referred label;
140   theTarget << aLabel;
141 }