1 // Created on: 2000-09-27
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <XCAFDoc_GraphNode.hxx>
18 #include <Standard_Dump.hxx>
19 #include <Standard_GUID.hxx>
20 #include <Standard_Type.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TDF_DataSet.hxx>
23 #include <TDF_Label.hxx>
24 #include <TDF_RelocationTable.hxx>
26 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_GraphNode,TDF_Attribute)
28 // class methods working on the node:
29 // ===================================
30 //=======================================================================
33 //=======================================================================
34 Standard_Boolean XCAFDoc_GraphNode::Find(const TDF_Label& L,
35 Handle(XCAFDoc_GraphNode)& G)
37 return L.FindAttribute(XCAFDoc_GraphNode::GetDefaultGraphID(), G);
40 //=======================================================================
41 //GraphNode : GetDefaultGraphID
42 //purpose : Static method to get the default ID of a GraphNode
43 //=======================================================================
45 const Standard_GUID& XCAFDoc_GraphNode::GetDefaultGraphID()
47 static Standard_GUID XCAFDoc_GraphNodeID ("efd212f5-6dfd-11d4-b9c8-0060b0ee281b");
48 return XCAFDoc_GraphNodeID;
51 //=======================================================================
53 //purpose : Finds or creates a GraphNode attribute with default ID
54 //=======================================================================
56 Handle(XCAFDoc_GraphNode) XCAFDoc_GraphNode::Set(const TDF_Label& L)
58 Handle(XCAFDoc_GraphNode) GN;
59 if (!L.FindAttribute(XCAFDoc_GraphNode::GetDefaultGraphID(), GN)) {
60 GN = new XCAFDoc_GraphNode();
61 GN->SetGraphID(XCAFDoc_GraphNode::GetDefaultGraphID());
67 //=======================================================================
69 //purpose : Finds or creates a GraphNode attribute with explicit ID
71 //=======================================================================
73 Handle(XCAFDoc_GraphNode) XCAFDoc_GraphNode::Set (const TDF_Label& L,
74 const Standard_GUID& explicitID)
76 Handle(XCAFDoc_GraphNode) GN;
77 if (!L.FindAttribute(explicitID, GN)) {
78 GN = new XCAFDoc_GraphNode ();
79 GN->SetGraphID( explicitID );
88 //=======================================================================
89 //function : XCAFDoc_GraphNode
91 //=======================================================================
93 XCAFDoc_GraphNode::XCAFDoc_GraphNode ()
98 //=======================================================================
99 //function : SetGraphID
101 //=======================================================================
103 void XCAFDoc_GraphNode::SetGraphID (const Standard_GUID& explicitID)
106 myGraphID = explicitID;
111 //=======================================================================
112 //function : SetFather
114 //=======================================================================
116 Standard_Integer XCAFDoc_GraphNode::SetFather(const Handle(XCAFDoc_GraphNode)& F)
119 Standard_Integer Findex = myFathers.Length();
124 //=======================================================================
125 //function : SetChild
127 //=======================================================================
129 Standard_Integer XCAFDoc_GraphNode::SetChild(const Handle(XCAFDoc_GraphNode)& Ch)
132 Standard_Integer Chindex = myChildren.Length();
133 myChildren.Append(Ch);
137 //=======================================================================
138 //function : UnSetFather
140 //=======================================================================
142 void XCAFDoc_GraphNode::UnSetFather(const Handle(XCAFDoc_GraphNode)& F)
144 Standard_Integer Findex = FatherIndex(F);
147 F->UnSetChildlink (this);
153 //=======================================================================
154 //function : UnSetFather
156 //=======================================================================
158 void XCAFDoc_GraphNode::UnSetFather(const Standard_Integer Findex)
162 UnSetFather (GetFather (Findex));
167 //=======================================================================
168 //function : UnSetFatherlink
169 //purpose : Remove link finily
170 //=======================================================================
172 void XCAFDoc_GraphNode::UnSetFatherlink(const Handle(XCAFDoc_GraphNode)& F)
175 Standard_Integer Findex = FatherIndex (F);
178 myFathers.Remove( Findex );
182 //=======================================================================
183 //function : UnSetChild
185 //=======================================================================
187 void XCAFDoc_GraphNode::UnSetChild(const Handle(XCAFDoc_GraphNode)& Ch)
189 Standard_Integer Chindex = ChildIndex(Ch);
192 Ch->UnSetFatherlink (this);
198 //=======================================================================
199 //function : UnSetChild
201 //=======================================================================
203 void XCAFDoc_GraphNode::UnSetChild(const Standard_Integer Chindex)
207 UnSetChild (GetChild (Chindex));
212 //=======================================================================
213 //function : UnSetChildlink
214 //purpose : Remove link finily
215 //=======================================================================
217 void XCAFDoc_GraphNode::UnSetChildlink(const Handle(XCAFDoc_GraphNode)& Ch)
220 Standard_Integer Chindex = ChildIndex (Ch);
223 myChildren.Remove (Chindex);
227 //=======================================================================
228 //function : GetFather
230 //=======================================================================
232 Handle(XCAFDoc_GraphNode) XCAFDoc_GraphNode::GetFather(const Standard_Integer Findex) const
234 Handle(XCAFDoc_GraphNode) F = myFathers.Value(Findex);
238 //=======================================================================
239 //function : GetChild
241 //=======================================================================
243 Handle(XCAFDoc_GraphNode) XCAFDoc_GraphNode::GetChild(const Standard_Integer Chindex) const
245 Handle(XCAFDoc_GraphNode) Ch = myChildren.Value(Chindex);
249 //=======================================================================
250 //function : FatherIndex
252 //=======================================================================
254 Standard_Integer XCAFDoc_GraphNode::FatherIndex(const Handle(XCAFDoc_GraphNode)& F) const
256 Standard_Integer Findex = 0;
257 if (NbFathers() != 0)
259 for (Findex = 1 ; Findex <= NbFathers(); Findex++)
261 if (F == myFathers.Value (Findex))
270 //=======================================================================
271 //function : ChildIndex
273 //=======================================================================
275 Standard_Integer XCAFDoc_GraphNode::ChildIndex(const Handle(XCAFDoc_GraphNode)& Ch) const
277 Standard_Integer Chindex;
278 if (NbChildren() != 0)
280 for (Chindex = 1; Chindex <= NbChildren(); Chindex++)
282 if (Ch == myChildren.Value (Chindex))
291 //=======================================================================
292 //function : IsFather
294 //=======================================================================
296 Standard_Boolean XCAFDoc_GraphNode::IsFather(const Handle(XCAFDoc_GraphNode)& Ch) const
298 if ( ChildIndex(Ch) ) return Standard_True;
299 return Standard_False;
302 //=======================================================================
305 //=======================================================================
307 Standard_Boolean XCAFDoc_GraphNode::IsChild(const Handle(XCAFDoc_GraphNode)& F) const
309 if ( FatherIndex(F) ) return Standard_True;
310 return Standard_False;
313 //=======================================================================
314 //function : NbFathers
316 //=======================================================================
318 Standard_Integer XCAFDoc_GraphNode::NbFathers() const
320 return myFathers.Length();
323 //=======================================================================
324 //function : NbChildren
326 //=======================================================================
328 Standard_Integer XCAFDoc_GraphNode::NbChildren() const
330 return myChildren.Length();
335 // Implementation of Attribute methods:
336 // ===================================
338 //=======================================================================
341 //=======================================================================
343 const Standard_GUID& XCAFDoc_GraphNode::ID() const
349 //=======================================================================
352 //=======================================================================
354 void XCAFDoc_GraphNode::Restore(const Handle(TDF_Attribute)& other)
356 Handle(XCAFDoc_GraphNode) F = Handle(XCAFDoc_GraphNode)::DownCast(other);
357 myFathers = F->myFathers;
358 myChildren = F->myChildren;
359 myGraphID = F->myGraphID;
363 //=======================================================================
366 //=======================================================================
368 void XCAFDoc_GraphNode::Paste(const Handle(TDF_Attribute)& into,
369 const Handle(TDF_RelocationTable)& RT) const
371 Handle(XCAFDoc_GraphNode) intof = Handle(XCAFDoc_GraphNode)::DownCast(into);
372 Handle(XCAFDoc_GraphNode) func;
373 Standard_Integer i = 1;
374 for (; i <= NbFathers(); i++) {
375 if (!RT->HasRelocation(myFathers(i), func) && RT->AfterRelocate())
381 intof->SetFather(func);
386 for (; i <= NbChildren(); i++)
388 if (!RT->HasRelocation(myChildren(i), func) && RT->AfterRelocate())
394 intof->SetChild(func);
397 intof->SetGraphID(myGraphID);
401 //=======================================================================
402 //function : NewEmpty
404 //=======================================================================
406 Handle(TDF_Attribute) XCAFDoc_GraphNode::NewEmpty() const
408 Handle(XCAFDoc_GraphNode) G = new XCAFDoc_GraphNode();
409 G->SetGraphID(myGraphID);
414 //=======================================================================
415 //function : References
417 //=======================================================================
419 void XCAFDoc_GraphNode::References(const Handle(TDF_DataSet)& aDataSet) const
422 Handle(XCAFDoc_GraphNode) fct;
423 for ( i = 1; i <= NbChildren(); i++ ) {
426 aDataSet->AddAttribute(fct);
429 for ( i = 1; i <= NbFathers(); i++ ) {
431 if ( !fct.IsNull()) {
432 aDataSet->AddAttribute(fct);
438 //=======================================================================
441 //=======================================================================
443 Standard_OStream& XCAFDoc_GraphNode::Dump (Standard_OStream& anOS) const
445 TDF_Attribute::Dump (anOS);
446 Standard_Integer i = 1;
447 if ( myFathers.Length()!= 0 ) {
449 for (; i <= NbFathers(); i++) {
450 if ( !myFathers(i)->Label().IsNull() )
451 myFathers(i)->Label().EntryDump(anOS);
456 if ( myChildren.Length()!= 0 ) {
458 for (; i <= NbChildren(); i++) {
459 if ( !myChildren(i)->Label().IsNull() )
460 myChildren(i)->Label().EntryDump(anOS);
468 //=======================================================================
469 //function : BeforeForget
471 //=======================================================================
473 void XCAFDoc_GraphNode::BeforeForget()
475 while (myFathers.Length () > 0)
479 while (myChildren.Length () > 0)
485 //=======================================================================
486 //function : DumpJson
488 //=======================================================================
489 void XCAFDoc_GraphNode::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
491 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
493 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
495 for (XCAFDoc_GraphNodeSequence::Iterator anIteratorFather (myFathers); anIteratorFather.More(); anIteratorFather.Next())
497 const Handle(XCAFDoc_GraphNode)& aFather = anIteratorFather.Value();
498 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aFather.get())
501 for (XCAFDoc_GraphNodeSequence::Iterator anIteratorChild (myChildren); anIteratorChild.More(); anIteratorChild.Next())
503 const Handle(XCAFDoc_GraphNode)& aChild = anIteratorChild.Value();
504 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aChild.get())
507 OCCT_DUMP_FIELD_VALUE_GUID (theOStream, myGraphID)