0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[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 IMPLEMENT_STANDARD_RTTIEXT(BinTObjDrivers_ReferenceDriver,BinMDF_ADriver)
32
33 //=======================================================================
34 //function : BinTObjDrivers_ReferenceDriver
35 //purpose  : constructor
36 //=======================================================================
37
38 BinTObjDrivers_ReferenceDriver::BinTObjDrivers_ReferenceDriver
39                          (const Handle(CDM_MessageDriver)& theMessageDriver)
40 : BinMDF_ADriver( theMessageDriver, NULL)
41 {
42 }
43
44 //=======================================================================
45 //function : NewEmpty
46 //purpose  : Creates a new attribute
47 //=======================================================================
48
49 Handle(TDF_Attribute) BinTObjDrivers_ReferenceDriver::NewEmpty() const
50 {
51   return new TObj_TReference;
52 }
53
54 //=======================================================================
55 //function : Paste
56 //purpose  : Translate the contents of <theSource> and put it
57 //           into <theTarget>.
58 //=======================================================================
59
60 Standard_Boolean BinTObjDrivers_ReferenceDriver::Paste
61                          (const BinObjMgt_Persistent&  theSource,
62                           const Handle(TDF_Attribute)& theTarget,
63                           BinObjMgt_RRelocationTable&) const
64 {
65   // master label
66   TDF_Label aMasterLabel;
67   Handle(TDF_Data) aDS = theTarget->Label().Data();
68   if (! theSource.GetLabel (aDS, aMasterLabel)) return Standard_False;
69
70   // isSameDoc flag
71   Standard_Boolean isSameDoc = Standard_False;
72   if (! (theSource >> isSameDoc)) return Standard_False;
73
74   // DS for referred label
75   if (!isSameDoc) 
76   {
77     TCollection_AsciiString aName;
78     if (! (theSource >> aName)) return Standard_False;
79     Handle(TObj_Model) aModel = Handle(TObj_Model)::DownCast
80       ( TObj_Assistant::FindModel( aName.ToCString() ));
81     if (aModel.IsNull())
82     {
83       TCollection_AsciiString anEntry;
84       TDF_Tool::Entry (theTarget->Label(), anEntry);
85       WriteMessage (TCollection_ExtendedString ("TObj_TReference retrieval: ")
86                     + "wrong model ID " + aName + ", entry " + anEntry);
87       return Standard_False;
88     }
89     aDS = aModel->GetLabel().Data();
90   }
91   // reffered label
92   TDF_Label aLabel;
93   if (! theSource.GetLabel (aDS, aLabel)) return Standard_False;
94
95   // set reference attribute fields
96   Handle(TObj_TReference) aTarget =
97     Handle(TObj_TReference)::DownCast (theTarget);
98   aTarget->Set ( aLabel, aMasterLabel );
99
100   return !aLabel.IsNull() && !aMasterLabel.IsNull();
101 }
102
103 //=======================================================================
104 //function : Paste
105 //purpose  : Translate the contents of <theSource> and put it
106 //           into <theTarget>.
107 //           Store master and referred labels as entry, the other model referred
108 //           as entry in model-container
109 //=======================================================================
110
111 void BinTObjDrivers_ReferenceDriver::Paste
112                          (const Handle(TDF_Attribute)& theSource,
113                           BinObjMgt_Persistent&        theTarget,
114                           BinObjMgt_SRelocationTable&) const
115 {
116   Handle(TObj_TReference) aSource =
117     Handle(TObj_TReference)::DownCast (theSource);
118
119   Handle(TObj_Object) aLObject = aSource->Get();
120   if (aLObject.IsNull())
121     return;
122
123   // labels
124   TDF_Label aLabel = aLObject->GetLabel();
125   TDF_Label aMasterLabel = aSource->GetMasterLabel();
126   Standard_Boolean isSameDoc = (aLabel.Root() == aMasterLabel.Root());
127
128   // store data
129   // 1 - the master label;
130   theTarget << aMasterLabel;
131   // 2 - isSameDoc flag plus may be a Model ID
132   theTarget << isSameDoc;
133   if (! isSameDoc)
134   {
135     TCollection_AsciiString aName;
136     Handle(TObj_Model) aModel =
137       Handle(TObj_Model)::DownCast( aLObject->GetModel() );
138     aName = TCollection_AsciiString( aModel->GetModelName()->String() );
139     theTarget << aName;
140   }
141   // 3 - referred label;
142   theTarget << aLabel;
143 }