0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / TDF / TDF_Attribute.cxx
CommitLineData
b311480e 1// Created by: DAUTRY Philippe
2// Copyright (c) 1997-1999 Matra Datavision
973c2be1 3// Copyright (c) 1999-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// ------------------
7fd59977 17// Version: 0.0
b311480e 18//Version Date Purpose
7fd59977 19// 0.0 Feb 7 1997 Creation
20
42cf5bc1 21#include <Standard_DomainError.hxx>
bc73b006 22#include <Standard_Dump.hxx>
42cf5bc1 23#include <Standard_GUID.hxx>
24#include <Standard_ImmutableObject.hxx>
25#include <Standard_Type.hxx>
7fd59977 26#include <TCollection_AsciiString.hxx>
42cf5bc1 27#include <TDF_Attribute.hxx>
28#include <TDF_AttributeDelta.hxx>
29#include <TDF_AttributeIterator.hxx>
30#include <TDF_Data.hxx>
31#include <TDF_DataSet.hxx>
7fd59977 32#include <TDF_DefaultDeltaOnModification.hxx>
33#include <TDF_DefaultDeltaOnRemoval.hxx>
42cf5bc1 34#include <TDF_DeltaOnAddition.hxx>
7fd59977 35#include <TDF_DeltaOnForget.hxx>
42cf5bc1 36#include <TDF_DeltaOnModification.hxx>
37#include <TDF_DeltaOnRemoval.hxx>
7fd59977 38#include <TDF_DeltaOnResume.hxx>
42cf5bc1 39#include <TDF_IDFilter.hxx>
7fd59977 40#include <TDF_Label.hxx>
41#include <TDF_LabelNode.hxx>
42cf5bc1 42#include <TDF_RelocationTable.hxx>
7fd59977 43#include <TDF_Tool.hxx>
44
25e59720 45IMPLEMENT_STANDARD_RTTIEXT(TDF_Attribute,Standard_Transient)
92efcf78 46
7fd59977 47#undef TDF_DATA_COMMIT_OPTIMIZED
48
7fd59977 49//=======================================================================
50//function : TDF_Attribute
51//purpose : Initializes fields.
52//=======================================================================
53
54TDF_Attribute::TDF_Attribute() :
55myLabelNode(NULL),
56myTransaction(0),
57mySavedTransaction(0),
58myFlags(1)
59
60{}
61
62
63//=======================================================================
64//function : Label
65//purpose :
66//=======================================================================
67
68const TDF_Label TDF_Attribute::Label() const
69{ return TDF_Label(myLabelNode); }
70
71
72//=======================================================================
73//function : Find
74//purpose :
75//=======================================================================
76
77Standard_Boolean TDF_Attribute::FindAttribute (const Standard_GUID& anID,
78 Handle(TDF_Attribute)& anAttribute) const
79{
80 return Label().FindAttribute(anID,anAttribute);
81}
82
83//=======================================================================
84//function : IsAttribute
85//purpose :
86//=======================================================================
87
88Standard_Boolean TDF_Attribute::IsAttribute (const Standard_GUID& anID) const
89{
90 return Label().IsAttribute(anID);
91}
92
93//=======================================================================
94//function : AddAttribute
95//purpose :
96//=======================================================================
97
98void TDF_Attribute::AddAttribute (const Handle(TDF_Attribute)& otherAttribute) const
99{ Label().AddAttribute (otherAttribute); }
100
101
102
103//=======================================================================
104//function : ForgetAttribute
105//purpose :
106//=======================================================================
107
108Standard_Boolean TDF_Attribute::ForgetAttribute (const Standard_GUID& anID) const
109{ return Label().ForgetAttribute (anID); }
110
111
112
113//=======================================================================
114//function : ForgetAllAttributes
115//purpose :
116//=======================================================================
117
118void TDF_Attribute::ForgetAllAttributes (const Standard_Boolean clearChildren) const
119{ Label().ForgetAllAttributes (clearChildren); }
120
121
122
123
124//=======================================================================
125//function : Forget
126//purpose :
127//=======================================================================
128
129void TDF_Attribute::Forget (const Standard_Integer aTransaction)
130{
131 mySavedTransaction = myTransaction;
132 myTransaction = aTransaction;
133 myFlags = (myFlags | TDF_AttributeForgottenMsk);
134 Validate(Standard_False);
135}
136
137
138//=======================================================================
139//function : Resume
140//purpose :
141//=======================================================================
142
143void TDF_Attribute::Resume()
144{
145 myTransaction = mySavedTransaction;
146 mySavedTransaction = -1; // To say "just resumed"!
147 myFlags = (myFlags & ~TDF_AttributeForgottenMsk);
148 Validate(Standard_True);
149}
150
151
152//=======================================================================
153//function : UntilTransaction
154//purpose :
155//=======================================================================
156
157Standard_Integer TDF_Attribute::UntilTransaction() const
158{
159 if (IsForgotten()) return myTransaction;
160 else if (IsBackuped()) return myNext->myTransaction - 1;
161 else if (IsValid()) return myLabelNode->Data()->Transaction();
9775fa61 162 throw Standard_DomainError("The attribute structure is wrong.");
7fd59977 163}
164
165
166//=======================================================================
167//function : AfterAddition
168//purpose :
169//=======================================================================
170
171void TDF_Attribute::AfterAddition()
172{}
173
174
175//=======================================================================
176//function : BeforeRemoval
177//purpose :
178//=======================================================================
179
180void TDF_Attribute::BeforeRemoval()
181{}
182
183//=======================================================================
184//function : BeforeForget
185//purpose :
186//=======================================================================
187
188void TDF_Attribute::BeforeForget()
189{}
190
191
192//=======================================================================
193//function : AfterResume
194//purpose :
195//=======================================================================
196
197void TDF_Attribute::AfterResume()
198{}
199
200
201//=======================================================================
202//function : BeforeUndo
203//purpose : Before application of a TDF_Delta.
204//=======================================================================
205
206Standard_Boolean TDF_Attribute::BeforeUndo
35e08fe8 207(const Handle(TDF_AttributeDelta)& /*anAttDelta*/,
208 const Standard_Boolean /*forceIt*/)
7fd59977 209{ return Standard_True; }
210
211
212//=======================================================================
213//function : AfterUndo
214//purpose : After application of a TDF_Delta.
215//=======================================================================
216
217Standard_Boolean TDF_Attribute::AfterUndo
35e08fe8 218(const Handle(TDF_AttributeDelta)& /*anAttDelta*/,
219 const Standard_Boolean /*forceIt*/)
7fd59977 220{ return Standard_True; }
221
222
223//=======================================================================
224//function : AfterRetrieval
225//purpose :
226//=======================================================================
227
228Standard_Boolean TDF_Attribute::AfterRetrieval
35e08fe8 229(const Standard_Boolean /*forceIt*/)
7fd59977 230{ return Standard_True; }
231
7fd59977 232//=======================================================================
233//function : BeforeCommitTransaction
234//purpose :
235//=======================================================================
236
237void TDF_Attribute::BeforeCommitTransaction() {}
7fd59977 238
239//=======================================================================
240//function : Backup
241//purpose : Backups the attribute.
242//=======================================================================
243
244void TDF_Attribute::Backup()
245{
246 // The attribute must be valid and attached to a label.
247 if (IsValid() && (myLabelNode != NULL)) {
248 Handle(TDF_Data) aData = myLabelNode->Data();
249
250 // check that modification is allowed
251 if ( !aData->IsModificationAllowed() ) {
252 TCollection_AsciiString aMess;
253 aMess = "Attribute \"";
254 aMess += DynamicType()->Name();
255 aMess += "\" is changed outside transaction";
9775fa61 256 throw Standard_ImmutableObject(aMess.ToCString());
7fd59977 257 }
258
259 const Standard_Integer currentTransaction =
260 aData->Transaction();
261 if (myTransaction < currentTransaction) {//"!=" is less secure.
262 Handle(TDF_Attribute) backup = BackupCopy();
263#ifdef TDF_DATA_COMMIT_OPTIMIZED
264 myLabelNode->AttributesModified(Standard_True);
265#endif
266 backup->myLabelNode = myLabelNode;
267 backup->myNext = this; // Back reference;
268 backup->myBackup = myBackup;
269 backup->myTransaction = myTransaction;
270 backup->Backup(Standard_True);
271
272 myBackup = backup;
273 myTransaction = currentTransaction;
274 }
275 }
276}
277
278
279//=======================================================================
280//function : BackupCopy
281//purpose : Standard implementation of BackupCopy.
282//=======================================================================
283
284Handle(TDF_Attribute) TDF_Attribute::BackupCopy() const
285{
286 Handle(TDF_Attribute) copy = NewEmpty(); copy->Restore(this);
287 return copy;
288}
289
290
291//=======================================================================
292//function : RemoveBackup
293//purpose :
294//=======================================================================
295
296void TDF_Attribute::RemoveBackup()
297{
0797d9d3 298#ifdef OCCT_DEBUG
7fd59977 299 if (myBackup.IsNull())
9775fa61 300 throw Standard_DomainError("Impossible to remove a nonexistent backup.");
7fd59977 301#endif
302 myBackup->BeforeRemoval();
303 myBackup->myLabelNode = NULL; // Absolutly necessary!
304 myBackup->myNext.Nullify(); // Absolutly necessary!
305 myBackup = myBackup->myBackup;
306 if (!myBackup.IsNull()) myBackup->myNext = this; // New back reference.
307}
308
309//=======================================================================
310//function : References
311//purpose : Adds the referenced attributes or labels.
312//=======================================================================
313
302f96fb 314void TDF_Attribute::References(const Handle(TDF_DataSet)& /*aDataSet*/) const
7fd59977 315{}
316
317
318//=======================================================================
319//function : DeltaOnAddition
320//purpose : Creates such a delta.
321//=======================================================================
322
323Handle(TDF_DeltaOnAddition) TDF_Attribute::DeltaOnAddition() const
324{ return new TDF_DeltaOnAddition(this); }
325
326
327//=======================================================================
328//function : DeltaOnForget
329//purpose : Creates such a delta.
330//=======================================================================
331
332Handle(TDF_DeltaOnForget) TDF_Attribute::DeltaOnForget() const
333{ return new TDF_DeltaOnForget(this); }
334
335
336//=======================================================================
337//function : DeltaOnResume
338//purpose : Creates such a delta.
339//=======================================================================
340
341Handle(TDF_DeltaOnResume) TDF_Attribute::DeltaOnResume() const
342{ return new TDF_DeltaOnResume(this); }
343
344
345//=======================================================================
346//function : DeltaOnModification
347//purpose : Creates such a delta.
348//=======================================================================
349
350Handle(TDF_DeltaOnModification) TDF_Attribute::DeltaOnModification
351(const Handle(TDF_Attribute)& anOldAttribute) const
352{ return new TDF_DefaultDeltaOnModification(anOldAttribute); }
353
354
355//=======================================================================
356//function : DeltaOnModification
357//purpose : Applies such a delta.
358//=======================================================================
359
360void TDF_Attribute::DeltaOnModification
361(const Handle(TDF_DeltaOnModification)& aDelta)
362{
363 Backup();
364 Restore(aDelta->Attribute());
365}
366
367
368//=======================================================================
369//function : DeltaOnRemoval
370//purpose : Creates such a delta.
371//=======================================================================
372
373Handle(TDF_DeltaOnRemoval) TDF_Attribute::DeltaOnRemoval() const
374{ return new TDF_DefaultDeltaOnRemoval(this); } // myBackup
375
376
377//=======================================================================
378//function : Dump
379//purpose : This method is equivalent to operator <<
380//=======================================================================
381
382Standard_OStream& TDF_Attribute::Dump(Standard_OStream& anOS) const
383{
384 anOS<<"\t"<<DynamicType()->Name()<<"\tTrans. "<<myTransaction<<";";
385 if (IsValid()) anOS<<" Valid";
386 if (IsBackuped()) anOS<<" Backuped";
387 if (IsForgotten()) anOS<<" Forgotten";
388 char toto[45];
389 Standard_PCharacter pStr;
390 //
391 pStr=toto;
2cb44241 392 ID().ToCString(pStr);
04232180 393 anOS<<";\tID = "<<toto<<std::endl;
7fd59977 394 return anOS;
395}
396
397
398//=======================================================================
399//function :ExtendedDump
400//purpose :
401//=======================================================================
402
403void TDF_Attribute::ExtendedDump
404(Standard_OStream& anOS,
35e08fe8 405 const TDF_IDFilter& /*aFilter*/,
406 TDF_AttributeIndexedMap& /*aMap*/) const
7fd59977 407{ Dump(anOS); }
408
bc73b006 409//=======================================================================
410//function : DumpJson
411//purpose :
412//=======================================================================
413void TDF_Attribute::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
414{
415 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
416
417 TCollection_AsciiString aLabel;
418 TDF_Tool::Entry (Label(), aLabel);
419 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aLabel)
420
421 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTransaction)
422 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, mySavedTransaction)
423 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myFlags)
424
425 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myNext.get())
426 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myBackup.get())
427}