0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / TObj / TObj_TReference.cxx
CommitLineData
b311480e 1// Created on: 2004-11-23
2// Created by: Pavel TELKOV
973c2be1 3// Copyright (c) 2004-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16// The original implementation Copyright: (C) RINA S.p.A
17
18#include <TObj_Object.hxx>
19#include <TObj_TReference.hxx>
20#include <TObj_TObject.hxx>
21
22#include <Standard_GUID.hxx>
23#include <TDF_RelocationTable.hxx>
24#include <TDF_DeltaOnAddition.hxx>
ec357c5c 25#include <TDF_DeltaOnRemoval.hxx>
7fd59977 26
7fd59977 27
92efcf78 28IMPLEMENT_STANDARD_RTTIEXT(TObj_TReference,TDF_Attribute)
29
7fd59977 30//=======================================================================
31//function : TObj_TReference
32//purpose :
33//=======================================================================
34
35TObj_TReference::TObj_TReference()
36{
37}
38
39//=======================================================================
40//function : GetID
41//purpose :
42//=======================================================================
43
44const Standard_GUID& TObj_TReference::GetID()
45{
46 static Standard_GUID theGUID ("3bbefb44-e618-11d4-ba38-0060b0ee18ea");
47 return theGUID;
48}
49
50//=======================================================================
51//function : ID
52//purpose :
53//=======================================================================
54
55const Standard_GUID& TObj_TReference::ID() const
56{
57 return GetID();
58}
59
60//=======================================================================
61//function : Set
62//purpose :
63//=======================================================================
64
65Handle(TObj_TReference) TObj_TReference::Set
66 (const TDF_Label& theLabel,
67 const Handle(TObj_Object)& theObject,
68 const Handle(TObj_Object)& theMaster)
69{
70 Handle(TObj_TReference) A;
71 if (!theLabel.FindAttribute(TObj_TReference::GetID(), A))
72 {
73 A = new TObj_TReference;
74 theLabel.AddAttribute(A);
75 }
76 else
77 {
78 Handle(TObj_Object) anObj = A->Get();
79 if ( ! anObj.IsNull() ) anObj->RemoveBackReference(theMaster);
80 }
81 A->Set(theObject, theMaster->GetLabel());
82 if ( ! theObject.IsNull() )
83 theObject->AddBackReference(theMaster);
84 return A;
85}
86
87//=======================================================================
88//function : Set
89//purpose :
90//=======================================================================
91
92void TObj_TReference::Set(const Handle(TObj_Object)& theElem,
93 const TDF_Label& theMasterLabel)
94{
95 Backup();
96 if ( theElem.IsNull() )
97 myLabel.Nullify();
98 else
99 myLabel = theElem->GetLabel();
100
101 myMasterLabel = theMasterLabel;
102}
103
104//=======================================================================
105//function : Set
106//purpose : for persistent only.
107//=======================================================================
108
109void TObj_TReference::Set(const TDF_Label& theLabel,
110 const TDF_Label& theMasterLabel)
111{
112 Backup();
113 myLabel = theLabel;
114 myMasterLabel = theMasterLabel;
115}
116
117//=======================================================================
118//function : Get
119//purpose :
120//=======================================================================
121
122Handle(TObj_Object) TObj_TReference::Get() const
123{
124 Handle(TObj_TObject) aTObject;
125 Handle(TObj_Object) anObject;
126 // Take TObj_TObject from label and get from it TObj_Object
127 if ( myLabel.IsNull() || ! myLabel.FindAttribute(TObj_TObject::GetID(), aTObject) )
128 {
129 return anObject;
130 }
a9dde4a3 131 anObject = aTObject->Get();
7fd59977 132 return anObject;
133}
134
135//=======================================================================
136//function : NewEmpty
137//purpose :
138//=======================================================================
139
140Handle(TDF_Attribute) TObj_TReference::NewEmpty () const
141{
142 return new TObj_TReference();
143}
144
145//=======================================================================
146//function : Restore
147//purpose :
148//=======================================================================
149
150void TObj_TReference::Restore(const Handle(TDF_Attribute)& theWith)
151{
152 Handle(TObj_TReference) aReference = Handle(TObj_TReference)::DownCast (theWith);
153 myLabel = aReference->myLabel;
154 myMasterLabel = aReference->myMasterLabel;
155}
156
157//=======================================================================
158//function : Paste
159//purpose :
160//=======================================================================
161
162void TObj_TReference::Paste (const Handle(TDF_Attribute)& theInto,
163 const Handle(TDF_RelocationTable)& RT) const
164{
165 Handle(TObj_TReference) aReference = Handle(TObj_TReference)::DownCast (theInto);
166 Handle(TObj_TObject) aObject, aMasterTObj;
167 if (myLabel.IsNull())
168 {
169 // unvalidity if it neccessary
170 aReference->myLabel.Nullify();
171 return;
172 }
173
174 // get new referenced object
175 TDF_Label aRefLabel = myLabel;
176 if(!RT->HasRelocation(myLabel, aRefLabel))
177 aRefLabel = myLabel;
178 aRefLabel.FindAttribute(TObj_TObject::GetID(), aObject);
179 Handle(TObj_Object) anIObject;
180 if ( ! aObject.IsNull() )
181 anIObject = aObject->Get();
182
183 // find correct master label
184 Handle(TObj_Object) aMasterObj;
185 TObj_Object::GetObj(aReference->Label(), aMasterObj, Standard_True);
186 TDF_Label aMasterLabel;
187 if ( ! aMasterObj.IsNull() ) aMasterLabel = aMasterObj->GetLabel();
188 if ( aMasterLabel.IsNull() ||
189 ! aMasterLabel.FindAttribute(TObj_TObject::GetID(), aMasterTObj))
190 return;
191
192 // set master and referenced label
193 aReference->Set(anIObject, aMasterLabel);
194
195 // update back references
196 if ( !anIObject.IsNull() )
197 anIObject->AddBackReference(aMasterTObj->Get());
198}
199
200//=======================================================================
201//function : BeforeForget
202//purpose : for correct tranzaction mechanism.
203//=======================================================================
204
205void TObj_TReference::BeforeForget()
206{
207 // check if master object exist
208 if(myMasterLabel.IsNull())
209 return;
210
211 // removing back reference
212 Handle(TObj_Object) aMasterObject;
213 Handle(TObj_TObject) aTObject;
214 if (!myMasterLabel.FindAttribute(TObj_TObject::GetID(), aTObject ))
215 return;
216 aMasterObject = aTObject->Get();
217
218 Handle(TObj_Object) anObj = Get();
219 if(anObj.IsNull())
220 return;
221
222 aMasterObject->BeforeForgetReference( GetLabel() );
223 anObj->RemoveBackReference( aMasterObject );
224}
225
226//=======================================================================
227//function : BeforeUndo
228//purpose :
229//=======================================================================
230
231Standard_Boolean TObj_TReference::BeforeUndo(const Handle(TDF_AttributeDelta)& theDelta,
232 const Standard_Boolean /*isForced*/)
233{
234 if (!theDelta->IsKind(STANDARD_TYPE(TDF_DeltaOnAddition)))
235 return Standard_True;
236
237 if(myMasterLabel.IsNull())
238 return Standard_True;
239
240 Handle(TObj_Object) anObject = Get();
241 if( anObject.IsNull() )
242 return Standard_True;
243
244 Handle(TObj_Object) aMasterObject;
245 Handle(TObj_TObject) aTObject;
246 if (!myMasterLabel.FindAttribute(TObj_TObject::GetID(), aTObject ))
247 return Standard_True;
248 aMasterObject = aTObject->Get();
249
250 if( !anObject.IsNull() )
251 anObject->RemoveBackReference( aMasterObject );
252
253 return Standard_True;
254}
255
256//=======================================================================
257//function : AfterUndo
258//purpose :
259//=======================================================================
260
261Standard_Boolean TObj_TReference::AfterUndo(const Handle(TDF_AttributeDelta)& theDelta,
262 const Standard_Boolean /*isForced*/)
263{
264 if (!theDelta->IsKind(STANDARD_TYPE(TDF_DeltaOnRemoval)))
265 return Standard_True;
266
267 if(myMasterLabel.IsNull())
268 return Standard_True;
269
270 Handle(TObj_Object) anObject = Get();
271 if( anObject.IsNull() )
272 return Standard_True;
273
274 Handle(TObj_Object) aMasterObject;
275 Handle(TObj_TObject) aTObject;
276
277 if (!myMasterLabel.FindAttribute(TObj_TObject::GetID(), aTObject ))
278 return Standard_True;
279 aMasterObject = aTObject->Get();
280
281 if( !anObject.IsNull() )
282 anObject->AddBackReference( aMasterObject );
283
284 return Standard_True;
285}
286
287
288//=======================================================================
289//function : AfterResume
290//purpose :
291//=======================================================================
292
293void TObj_TReference::AfterResume()
294{
295 if(myMasterLabel.IsNull())
296 return;
297
298 Handle(TObj_Object) aMasterObject;
299 Handle(TObj_TObject) aTObject;
300 if (!myMasterLabel.FindAttribute(TObj_TObject::GetID(), aTObject ))
301 return;
302 aMasterObject = aTObject->Get();
303 Handle(TObj_Object) anObject = Get();
304
305 if ( !anObject.IsNull() )
306 anObject->AddBackReference( aMasterObject );
307}
308
309//=======================================================================
310//function : AfterRetrieval
311//purpose :
312//=======================================================================
313
314Standard_Boolean TObj_TReference::AfterRetrieval(const Standard_Boolean /*forceIt*/)
315{
316 if(myMasterLabel.IsNull())
317 return Standard_True;
318
319 Handle(TObj_Object) anObject = Get();
320 Handle(TObj_Object) aMasterObject;
321 Handle(TObj_TObject) aTObject;
322 if (!myMasterLabel.FindAttribute(TObj_TObject::GetID(), aTObject ))
323 return Standard_False;
324
325 aMasterObject = aTObject->Get();
326 if ( !anObject.IsNull() )
327 anObject->AddBackReference( aMasterObject );
328
329 return Standard_True;
330}