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