0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / XmlTObjDrivers / XmlTObjDrivers_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 "XmlTObjDrivers_ReferenceDriver.hxx"
20
21 #include <CDM_MessageDriver.hxx>
22 #include <TDF_Tool.hxx>
23 #include <TDF_Attribute.hxx>
24
25 #include <XmlObjMgt.hxx>
26 #include <XmlObjMgt_Persistent.hxx>
27 #include <XmlObjMgt_RRelocationTable.hxx>
28
29 #include <TObj_TReference.hxx>
30 #include <TObj_Model.hxx>
31 #include <TObj_Object.hxx>
32 #include <TObj_Assistant.hxx>
33 #include <TDF_ChildIterator.hxx>
34
35
36 IMPLEMENT_STANDARD_RTTIEXT(XmlTObjDrivers_ReferenceDriver,XmlMDF_ADriver)
37 IMPLEMENT_DOMSTRING (MasterEntry,        "master")
38 IMPLEMENT_DOMSTRING (ReferredEntry,      "entry")
39 IMPLEMENT_DOMSTRING (ReferredModelEntry, "modelentry")
40
41 //=======================================================================
42 //function : XmlTObjDrivers_ReferenceDriver
43 //purpose  : constructor
44 //=======================================================================
45
46 XmlTObjDrivers_ReferenceDriver::XmlTObjDrivers_ReferenceDriver
47                          (const Handle(CDM_MessageDriver)& theMessageDriver)
48 : XmlMDF_ADriver( theMessageDriver, NULL)
49 {
50 }
51
52 //=======================================================================
53 //function : NewEmpty
54 //purpose  : Creates a new attribute
55 //=======================================================================
56
57 Handle(TDF_Attribute) XmlTObjDrivers_ReferenceDriver::NewEmpty() const
58 {
59   return new TObj_TReference;
60 }
61
62 //=======================================================================
63 //function : Paste
64 //purpose  : Translate the contents of <aSource> and put it
65 //           into <aTarget>, using the relocation table
66 //           <aRelocTable> to keep the sharings.
67 //=======================================================================
68
69 Standard_Boolean XmlTObjDrivers_ReferenceDriver::Paste
70                          (const XmlObjMgt_Persistent&  Source,
71                           const Handle(TDF_Attribute)& Target,
72                           XmlObjMgt_RRelocationTable&  /*RelocTable*/) const
73 {
74   const XmlObjMgt_Element& anElement = Source;
75   
76   // get entries
77   TCollection_AsciiString RefEntry    = anElement.getAttribute(::ReferredEntry());
78   TCollection_AsciiString MasterEntry = anElement.getAttribute(::MasterEntry());
79   // entry in model holder
80   TCollection_AsciiString InHolderEntry =
81     anElement.getAttribute(::ReferredModelEntry());
82
83   // master label
84   TDF_Label aLabel, aMasterLabel;
85   TDF_Tool::Label (Target->Label().Data(), MasterEntry, aMasterLabel);
86   // referred label
87   if (InHolderEntry.IsEmpty())
88     TDF_Tool::Label (Target->Label().Data(), RefEntry, aLabel, Standard_True);
89   else
90   {
91     Handle(TObj_Model) aModel = TObj_Assistant::FindModel (InHolderEntry.ToCString());
92     TDF_Tool::Label (aModel->GetLabel().Data(), RefEntry, aLabel, Standard_True);
93   }
94   Handle(TObj_TReference) aTarget =
95     Handle(TObj_TReference)::DownCast (Target);
96   aTarget->Set ( aLabel, aMasterLabel );
97
98   return !aLabel.IsNull() && !aMasterLabel.IsNull();
99 }
100
101 //=======================================================================
102 //function : Paste
103 //purpose  : Translate the contents of <aSource> and put it
104 //           into <aTarget>, using the relocation table
105 //           <aRelocTable> to keep the sharings.
106 //           Store master and referred labels as entry, the other model referred
107 //           as entry in model-container
108 //=======================================================================
109
110 void XmlTObjDrivers_ReferenceDriver::Paste
111                          (const Handle(TDF_Attribute)& Source,
112                           XmlObjMgt_Persistent&        Target,
113                           XmlObjMgt_SRelocationTable&  /*RelocTable*/) const
114 {
115   Handle(TObj_TReference) aSource =
116     Handle(TObj_TReference)::DownCast (Source);
117
118   Handle(TObj_Object) aLObject = aSource->Get();
119   if (aLObject.IsNull())
120     return;
121
122   // referred entry
123   TCollection_AsciiString entry;
124   TDF_Label aLabel = aLObject->GetLabel();
125   TDF_Tool::Entry( aLabel, entry );
126   Target.Element().setAttribute(::ReferredEntry(), entry.ToCString());
127
128   // master entry
129   entry.Clear();
130   TDF_Label aMasterLabel = aSource->GetMasterLabel();
131   TDF_Tool::Entry( aMasterLabel, entry );
132   Target.Element().setAttribute(::MasterEntry(), entry.ToCString());
133
134   // is reference to other document 
135   if (aLabel.Root() == aMasterLabel.Root()) return;
136
137   Handle(TObj_Model) aModel = aLObject->GetModel();
138   TCollection_AsciiString aModelName( aModel->GetModelName()->String() );
139   Target.Element().setAttribute(::ReferredModelEntry(), aModelName.ToCString());
140 }