0029902: Data Exchange, XCAF - provide extended Material definition for visualization...
[occt.git] / src / XCAFDoc / XCAFDoc_ColorTool.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
7fd59977 14
42cf5bc1 15#include <Quantity_Color.hxx>
16#include <Standard_GUID.hxx>
17#include <Standard_Type.hxx>
7fd59977 18#include <TDataStd_Name.hxx>
42cf5bc1 19#include <TDataStd_TreeNode.hxx>
20#include <TDataStd_UAttribute.hxx>
21#include <TDF_Attribute.hxx>
22#include <TDF_ChildIDIterator.hxx>
23#include <TDF_Label.hxx>
24#include <TDF_RelocationTable.hxx>
7fd59977 25#include <TNaming_NamedShape.hxx>
42cf5bc1 26#include <TopoDS_Shape.hxx>
27#include <XCAFDoc.hxx>
7fd59977 28#include <XCAFDoc_Color.hxx>
42cf5bc1 29#include <XCAFDoc_ColorTool.hxx>
30#include <XCAFDoc_DocumentTool.hxx>
7fd59977 31#include <XCAFDoc_GraphNode.hxx>
42cf5bc1 32#include <XCAFDoc_ShapeTool.hxx>
7fd59977 33
92efcf78 34IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_ColorTool,TDF_Attribute)
35
a4815d55 36static Standard_Boolean XCAFDoc_ColorTool_AutoNaming = Standard_True;
37
38//=======================================================================
39//function : SetAutoNaming
40//purpose :
41//=======================================================================
42void XCAFDoc_ColorTool::SetAutoNaming (Standard_Boolean theIsAutoNaming)
43{
44 XCAFDoc_ColorTool_AutoNaming = theIsAutoNaming;
45}
46
47//=======================================================================
48//function : AutoNaming
49//purpose :
50//=======================================================================
51Standard_Boolean XCAFDoc_ColorTool::AutoNaming()
52{
53 return XCAFDoc_ColorTool_AutoNaming;
54}
7fd59977 55
56//=======================================================================
57//function : BaseLabel
58//purpose :
59//=======================================================================
60
61TDF_Label XCAFDoc_ColorTool::BaseLabel() const
62{
63 return Label();
64}
65//=======================================================================
66//function : ShapeTool
67//purpose :
68//=======================================================================
69
70const Handle(XCAFDoc_ShapeTool)& XCAFDoc_ColorTool::ShapeTool()
71{
72 if (myShapeTool.IsNull())
73 myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
74 return myShapeTool;
75}
76
77
78//=======================================================================
79//function : IsColor
80//purpose :
81//=======================================================================
82
83Standard_Boolean XCAFDoc_ColorTool::IsColor (const TDF_Label& lab) const
84{
85 Quantity_Color C;
86 return GetColor ( lab, C );
87}
88
89//=======================================================================
90//function : GetColor
91//purpose :
92//=======================================================================
93
94Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& lab,
95 Quantity_Color& col) const
96{
973d410e 97 Quantity_ColorRGBA aCol;
98 Standard_Boolean isDone = GetColor(lab, aCol);
99 if (isDone)
100 col = aCol.GetRGB();
101 return isDone;
102}
103
104//=======================================================================
105//function : GetColor
106//purpose :
107//=======================================================================
108
109Standard_Boolean XCAFDoc_ColorTool::GetColor(const TDF_Label& lab,
110 Quantity_ColorRGBA& col) const
111{
112 if (lab.Father() != Label()) return Standard_False;
113
7fd59977 114 Handle(XCAFDoc_Color) ColorAttribute;
973d410e 115 if (!lab.FindAttribute(XCAFDoc_Color::GetID(), ColorAttribute))
7fd59977 116 return Standard_False;
973d410e 117
118 col = ColorAttribute->GetColorRGBA();
119
7fd59977 120 return Standard_True;
121}
122
123//=======================================================================
124//function : FindColor
125//purpose :
126//=======================================================================
127
128Standard_Boolean XCAFDoc_ColorTool::FindColor (const Quantity_Color& col, TDF_Label& lab) const
129{
973d410e 130 Quantity_ColorRGBA aCol;
131 aCol.SetRGB(col);
132 return FindColor(aCol, lab);
133}
134
135//=======================================================================
136//function : FindColor
137//purpose :
138//=======================================================================
139
140Standard_Boolean XCAFDoc_ColorTool::FindColor(const Quantity_ColorRGBA& col, TDF_Label& lab) const
141{
142 TDF_ChildIDIterator it(Label(), XCAFDoc_Color::GetID());
7fd59977 143 for (; it.More(); it.Next()) {
144 TDF_Label aLabel = it.Value()->Label();
973d410e 145 Quantity_ColorRGBA C;
146 if (!GetColor(aLabel, C)) continue;
147 if (C.IsEqual(col)) {
7fd59977 148 lab = aLabel;
149 return Standard_True;
150 }
151 }
152 return Standard_False;
153}
154
155//=======================================================================
156//function : FindColor
157//purpose :
158//=======================================================================
159
973d410e 160TDF_Label XCAFDoc_ColorTool::FindColor (const Quantity_ColorRGBA& col) const
7fd59977 161{
162 TDF_Label L;
163 FindColor ( col, L );
164 return L;
165}
166
973d410e 167//=======================================================================
168//function : FindColor
169//purpose :
170//=======================================================================
171
172TDF_Label XCAFDoc_ColorTool::FindColor(const Quantity_Color& col) const
173{
174 TDF_Label L;
175 FindColor(col, L);
176 return L;
177}
178
7fd59977 179//=======================================================================
180//function : AddColor
181//purpose :
182//=======================================================================
183
184TDF_Label XCAFDoc_ColorTool::AddColor (const Quantity_Color& col) const
973d410e 185{
186 Quantity_ColorRGBA aCol;
187 aCol.SetRGB(col);
188 return AddColor(aCol);
189}
190
191//=======================================================================
192//function : AddColor
193//purpose :
194//=======================================================================
195
a4815d55 196TDF_Label XCAFDoc_ColorTool::AddColor (const Quantity_ColorRGBA& theColor) const
7fd59977 197{
a4815d55 198 TDF_Label aLab;
199 if (FindColor (theColor, aLab))
200 {
201 return aLab;
202 }
7fd59977 203
204 // create a new color entry
7fd59977 205 TDF_TagSource aTag;
a4815d55 206 aLab = aTag.NewChild (Label());
207 XCAFDoc_Color::Set (aLab, theColor);
208
209 if (XCAFDoc_ColorTool_AutoNaming)
210 {
211 // set name according to color value
212 const NCollection_Vec4<float>& anRgbaF = theColor;
213 const NCollection_Vec4<unsigned int> anRgba (anRgbaF * 255.0f);
214 char aColorHex[32];
215 Sprintf (aColorHex, "%02X%02X%02X%02X", anRgba.r(), anRgba.g(), anRgba.b(), anRgba.a());
216 const TCollection_AsciiString aName = TCollection_AsciiString (Quantity_Color::StringName (theColor.GetRGB().Name()))
217 + " (#" + aColorHex + ")";
218 TDataStd_Name::Set (aLab, aName);
219 }
973d410e 220
a4815d55 221 return aLab;
7fd59977 222}
223
224//=======================================================================
225//function : RemoveColor
226//purpose :
227//=======================================================================
228
229void XCAFDoc_ColorTool::RemoveColor (const TDF_Label& lab) const
230{
231 lab.ForgetAllAttributes (Standard_True);
232}
233
234//=======================================================================
235//function : GetColors
236//purpose :
237//=======================================================================
238
239void XCAFDoc_ColorTool::GetColors (TDF_LabelSequence& Labels) const
240{
241 Labels.Clear();
242
243 TDF_ChildIDIterator ChildIDIterator(Label(),XCAFDoc_Color::GetID());
244 for (; ChildIDIterator.More(); ChildIDIterator.Next()) {
245 TDF_Label L = ChildIDIterator.Value()->Label();
246 if ( IsColor ( L ) ) Labels.Append ( L );
247 }
248}
249
250//=======================================================================
251//function : SetColor
252//purpose :
253//=======================================================================
254
255void XCAFDoc_ColorTool::SetColor (const TDF_Label& L,
256 const TDF_Label& colorL,
257 const XCAFDoc_ColorType type) const
258{
259 // set reference
260 Handle(TDataStd_TreeNode) refNode, mainNode;
261 mainNode = TDataStd_TreeNode::Set ( colorL, XCAFDoc::ColorRefGUID(type) );
262 refNode = TDataStd_TreeNode::Set ( L, XCAFDoc::ColorRefGUID(type) );
263 refNode->Remove(); // abv: fix against bug in TreeNode::Append()
264 mainNode->Prepend(refNode);
265}
266
267//=======================================================================
268//function : SetColor
269//purpose :
270//=======================================================================
271
272void XCAFDoc_ColorTool::SetColor (const TDF_Label& L,
273 const Quantity_Color& Color,
274 const XCAFDoc_ColorType type) const
275{
276 TDF_Label colorL = AddColor ( Color );
277 SetColor ( L, colorL, type );
278}
279
973d410e 280//=======================================================================
281//function : SetColor
282//purpose :
283//=======================================================================
284
285void XCAFDoc_ColorTool::SetColor(const TDF_Label& L,
286 const Quantity_ColorRGBA& Color,
287 const XCAFDoc_ColorType type) const
288{
289 TDF_Label colorL = AddColor(Color);
290 SetColor(L, colorL, type);
291}
292
7fd59977 293//=======================================================================
294//function : UnSetColor
295//purpose :
296//=======================================================================
297
298void XCAFDoc_ColorTool::UnSetColor (const TDF_Label& L, const XCAFDoc_ColorType type) const
299{
300 L.ForgetAttribute ( XCAFDoc::ColorRefGUID(type) );
301}
302
303//=======================================================================
304//function : IsSet
305//purpose :
306//=======================================================================
307
308Standard_Boolean XCAFDoc_ColorTool::IsSet (const TDF_Label& L, const XCAFDoc_ColorType type) const
309{
310 Handle(TDataStd_TreeNode) Node;
311 return L.FindAttribute ( XCAFDoc::ColorRefGUID(type), Node) && Node->HasFather();
312}
313
314//=======================================================================
315//function : GetColor
316//purpose :
317//=======================================================================
318
319Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& L,
320 const XCAFDoc_ColorType type,
321 TDF_Label& colorL)
322{
323 Handle(TDataStd_TreeNode) Node;
324 if ( ! L.FindAttribute ( XCAFDoc::ColorRefGUID(type), Node) ||
325 ! Node->HasFather() ) return Standard_False;
326 colorL = Node->Father()->Label();
327 return Standard_True;
328}
329
330//=======================================================================
331//function : GetColor
332//purpose :
333//=======================================================================
334
335Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& L,
336 const XCAFDoc_ColorType type,
337 Quantity_Color& color)
338{
339 TDF_Label colorL;
340 if ( ! GetColor ( L, type, colorL ) ) return Standard_False;
341 return GetColor ( colorL, color );
342}
343
973d410e 344//=======================================================================
345//function : GetColor
346//purpose :
347//=======================================================================
348
349Standard_Boolean XCAFDoc_ColorTool::GetColor(const TDF_Label& L,
350 const XCAFDoc_ColorType type,
351 Quantity_ColorRGBA& color)
352{
353 TDF_Label colorL;
354 if (!GetColor(L, type, colorL)) return Standard_False;
355 return GetColor(colorL, color);
356}
357
7fd59977 358//=======================================================================
359//function : SetColor
360//purpose :
361//=======================================================================
362
363Standard_Boolean XCAFDoc_ColorTool::SetColor (const TopoDS_Shape& S,
364 const TDF_Label& colorL,
365 const XCAFDoc_ColorType type)
366{
367 TDF_Label L;
368 if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
369 SetColor ( L, colorL, type );
370 return Standard_True;
371}
372
373//=======================================================================
374//function : SetColor
375//purpose :
376//=======================================================================
377
378Standard_Boolean XCAFDoc_ColorTool::SetColor (const TopoDS_Shape& S,
379 const Quantity_Color& Color,
380 const XCAFDoc_ColorType type)
381{
382 TDF_Label colorL = AddColor ( Color );
383 return SetColor ( S, colorL, type );
384}
385
973d410e 386//=======================================================================
387//function : SetColor
388//purpose :
389//=======================================================================
390
391Standard_Boolean XCAFDoc_ColorTool::SetColor(const TopoDS_Shape& S,
392 const Quantity_ColorRGBA& Color,
393 const XCAFDoc_ColorType type)
394{
395 TDF_Label colorL = AddColor(Color);
396 return SetColor(S, colorL, type);
397}
398
7fd59977 399//=======================================================================
400//function : UnSetColor
401//purpose :
402//=======================================================================
403
404Standard_Boolean XCAFDoc_ColorTool::UnSetColor (const TopoDS_Shape& S,
405 const XCAFDoc_ColorType type)
406{
407 TDF_Label L;
408 if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
409 UnSetColor ( L, type );
410 return Standard_True;
411}
412
413//=======================================================================
414//function : IsSet
415//purpose :
416//=======================================================================
417
418Standard_Boolean XCAFDoc_ColorTool::IsSet (const TopoDS_Shape& S,
419 const XCAFDoc_ColorType type)
420{
421 TDF_Label L;
422 if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
423 return IsSet ( L, type );
424}
425
426//=======================================================================
427//function : GetColor
428//purpose :
429//=======================================================================
430
431Standard_Boolean XCAFDoc_ColorTool::GetColor (const TopoDS_Shape& S,
432 const XCAFDoc_ColorType type,
433 TDF_Label& colorL)
434{
435 TDF_Label L;
436 if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
437 return GetColor ( L, type, colorL );
438}
439
440//=======================================================================
441//function : GetColor
442//purpose :
443//=======================================================================
444
445Standard_Boolean XCAFDoc_ColorTool::GetColor (const TopoDS_Shape& S,
446 const XCAFDoc_ColorType type,
447 Quantity_Color& color)
448{
449 TDF_Label colorL;
450 if ( ! GetColor ( S, type, colorL ) ) return Standard_False;
451 return GetColor ( colorL, color );
452}
453
973d410e 454//=======================================================================
455//function : GetColor
456//purpose :
457//=======================================================================
458
459Standard_Boolean XCAFDoc_ColorTool::GetColor(const TopoDS_Shape& S,
460 const XCAFDoc_ColorType type,
461 Quantity_ColorRGBA& color)
462{
463 TDF_Label colorL;
464 if (!GetColor(S, type, colorL)) return Standard_False;
465 return GetColor(colorL, color);
466}
467
7fd59977 468//=======================================================================
469//function : GetID
470//purpose :
471//=======================================================================
472
473const Standard_GUID& XCAFDoc_ColorTool::GetID()
474{
475 static Standard_GUID ColorTblID ("efd212ed-6dfd-11d4-b9c8-0060b0ee281b");
476 return ColorTblID;
477}
478
479//=======================================================================
480//function : Set
481//purpose :
482//=======================================================================
483
484Handle(XCAFDoc_ColorTool) XCAFDoc_ColorTool::Set(const TDF_Label& L)
485{
486 Handle(XCAFDoc_ColorTool) A;
487 if (!L.FindAttribute (XCAFDoc_ColorTool::GetID(), A)) {
488 A = new XCAFDoc_ColorTool ();
489 L.AddAttribute(A);
490 A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
491 }
492 return A;
493}
494
495//=======================================================================
496//function : ID
497//purpose :
498//=======================================================================
499
500const Standard_GUID& XCAFDoc_ColorTool::ID() const
501{
502 return GetID();
503}
504
505//=======================================================================
506//function : Restore
507//purpose :
508//=======================================================================
509
510void XCAFDoc_ColorTool::Restore(const Handle(TDF_Attribute)& /*with*/)
511{
512}
513
514//=======================================================================
515//function : NewEmpty
516//purpose :
517//=======================================================================
518
519Handle(TDF_Attribute) XCAFDoc_ColorTool::NewEmpty() const
520{
521 return new XCAFDoc_ColorTool;
522}
523
524//=======================================================================
525//function : Paste
526//purpose :
527//=======================================================================
528
529void XCAFDoc_ColorTool::Paste (const Handle(TDF_Attribute)& /*into*/,
530 const Handle(TDF_RelocationTable)& /*RT*/) const
531{
532}
533
534/**/
535
536//=======================================================================
537//function : XCAFDoc_ColorTool
538//purpose :
539//=======================================================================
540
541XCAFDoc_ColorTool::XCAFDoc_ColorTool()
542{
543}
544
545// PTV 23.01.2003 add visibility flag for objects (CAX-IF TRJ11)
546//=======================================================================
547//function : IsVisible
548//purpose :
549//=======================================================================
550
551Standard_Boolean XCAFDoc_ColorTool::IsVisible (const TDF_Label& L) const
552{
553 Handle(TDataStd_UAttribute) aUAttr;
554 return (!L.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr));
555}
556
557//=======================================================================
558//function : SetVisibility
559//purpose :
560//=======================================================================
561
562void XCAFDoc_ColorTool::SetVisibility (const TDF_Label& L,
563 const Standard_Boolean isvisible)
564{
565 Handle(TDataStd_UAttribute) aUAttr;
566 if (! isvisible ) {
567 Handle(XCAFDoc_GraphNode) aSHUO;
568 if (ShapeTool()->IsShape(L) || ShapeTool()->GetSHUO( L, aSHUO ) )
569 if (!L.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr))
ad67e367 570 TDataStd_UAttribute::Set( L, XCAFDoc::InvisibleGUID() );
7fd59977 571 }
572 else L.ForgetAttribute( XCAFDoc::InvisibleGUID() );
573}
574
08b183fe 575//=======================================================================
576//function : IsColorByLayer
577//purpose :
578//=======================================================================
579
580Standard_Boolean XCAFDoc_ColorTool::IsColorByLayer (const TDF_Label& L) const
581{
582 Handle(TDataStd_UAttribute) aUAttr;
583 return L.FindAttribute(XCAFDoc::ColorByLayerGUID(), aUAttr);
584}
585
586//=======================================================================
587//function : SetColorByLayer
588//purpose :
589//=======================================================================
590
591void XCAFDoc_ColorTool::SetColorByLayer (const TDF_Label& L,
592 const Standard_Boolean isColorByLayer)
593{
594 Handle(TDataStd_UAttribute) aUAttr;
595 if ( isColorByLayer ) {
596 Handle(XCAFDoc_GraphNode) aSHUO;
597 if (ShapeTool()->IsShape(L) || ShapeTool()->GetSHUO( L, aSHUO ) )
598 if (!L.FindAttribute(XCAFDoc::ColorByLayerGUID(), aUAttr))
ad67e367 599 TDataStd_UAttribute::Set( L, XCAFDoc::ColorByLayerGUID() );
08b183fe 600 }
601 else L.ForgetAttribute( XCAFDoc::ColorByLayerGUID() );
602}
603
7fd59977 604//=======================================================================
605//function : SetInstanceColor
606//purpose :
607//=======================================================================
608
609Standard_Boolean XCAFDoc_ColorTool::SetInstanceColor (const TopoDS_Shape& theShape,
610 const XCAFDoc_ColorType type,
611 const Quantity_Color& color,
612 const Standard_Boolean IsCreateSHUO)
973d410e 613{
614 Quantity_ColorRGBA aCol;
615 aCol.SetRGB(color);
616 return SetInstanceColor(theShape, type, aCol, IsCreateSHUO);
617}
618
619//=======================================================================
620//function : SetInstanceColor
621//purpose :
622//=======================================================================
623
624Standard_Boolean XCAFDoc_ColorTool::SetInstanceColor(const TopoDS_Shape& theShape,
625 const XCAFDoc_ColorType type,
626 const Quantity_ColorRGBA& color,
627 const Standard_Boolean IsCreateSHUO)
7fd59977 628{
629 // find shuo label structure
630 TDF_LabelSequence aLabels;
973d410e 631 if (!ShapeTool()->FindComponent(theShape, aLabels))
7fd59977 632 return Standard_False;
633 Handle(XCAFDoc_GraphNode) aSHUO;
634 // set the SHUO structure for this component if it is not exist
973d410e 635 if (!ShapeTool()->FindSHUO(aLabels, aSHUO)) {
7fd59977 636 if (aLabels.Length() == 1) {
637 // set color directly for component as NAUO
638 SetColor(aLabels.Value(1), color, type);
639 return Standard_True;
640 }
973d410e 641 else if (!IsCreateSHUO || !ShapeTool()->SetSHUO(aLabels, aSHUO)) {
7fd59977 642 return Standard_False;
eafb234b 643 }
644 }
7fd59977 645 TDF_Label aSHUOLabel = aSHUO->Label();
973d410e 646 SetColor(aSHUOLabel, color, type);
7fd59977 647 return Standard_True;
648}
649
973d410e 650
7fd59977 651//=======================================================================
652//function : GetInstanceColor
653//purpose :
654//=======================================================================
655
656Standard_Boolean XCAFDoc_ColorTool::GetInstanceColor (const TopoDS_Shape& theShape,
657 const XCAFDoc_ColorType type,
658 Quantity_Color& color)
973d410e 659{
660 Quantity_ColorRGBA aCol;
661 Standard_Boolean isDone = GetInstanceColor(theShape, type, aCol);
662 if (isDone)
663 color = aCol.GetRGB();
664 return isDone;
665}
666
667//=======================================================================
668//function : GetInstanceColor
669//purpose :
670//=======================================================================
671
672Standard_Boolean XCAFDoc_ColorTool::GetInstanceColor(const TopoDS_Shape& theShape,
673 const XCAFDoc_ColorType type,
674 Quantity_ColorRGBA& color)
7fd59977 675{
676 // find shuo label structure
677 TDF_LabelSequence aLabels;
973d410e 678 if (!ShapeTool()->FindComponent(theShape, aLabels))
7fd59977 679 return Standard_False;
680 Handle(XCAFDoc_GraphNode) aSHUO;
681 // get shuo from document by label structure
682 TDF_Label aCompLab = aLabels.Value(aLabels.Length());
683 while (aLabels.Length() > 1) {
973d410e 684 if (!ShapeTool()->FindSHUO(aLabels, aSHUO)) {
7fd59977 685 // try to find other shuo
686 aLabels.Remove(aLabels.Length());
687 continue;
973d410e 688 }
689 else {
7fd59977 690 TDF_Label aSHUOLabel = aSHUO->Label();
973d410e 691 if (GetColor(aSHUOLabel, type, color))
7fd59977 692 return Standard_True;
973d410e 693 else
7fd59977 694 // try to find other shuo
695 aLabels.Remove(aLabels.Length());
696 }
697 }
698 // attempt to get color exactly of component
973d410e 699 if (GetColor(aCompLab, type, color))
7fd59977 700 return Standard_True;
973d410e 701
7fd59977 702 // attempt to get color of solid
703 TopLoc_Location aLoc;
704 TopoDS_Shape S0 = theShape;
973d410e 705 S0.Location(aLoc);
706 TDF_Label aRefLab = ShapeTool()->FindShape(S0);
7fd59977 707 if (!aRefLab.IsNull())
973d410e 708 return GetColor(aRefLab, type, color);
7fd59977 709 // no color assigned
710 return Standard_False;
711}
712
713//=======================================================================
714//function : IsInstanceVisible
715//purpose :
716//=======================================================================
717
718Standard_Boolean XCAFDoc_ColorTool::IsInstanceVisible (const TopoDS_Shape& theShape)
719{
720 // check visibility status of top-level solid, cause it is have highest priority
721 TopLoc_Location NullLoc;
722 TopoDS_Shape S0 = theShape;
723 S0.Location( NullLoc );
724 TDF_Label aRefL = ShapeTool()->FindShape( S0 );
725 if (!aRefL.IsNull() && !IsVisible(aRefL))
726 return Standard_False;
727 // find shuo label structure
728 TDF_LabelSequence aLabels;
729 if ( !ShapeTool()->FindComponent( theShape, aLabels ) )
730 return Standard_True;
731 TDF_Label aCompLab = aLabels.Value(aLabels.Length());
732 // visibility status of component withouts SHUO.
733 if (!IsVisible( aCompLab ))
734 return Standard_False;
735 // check by SHUO structure
736 TDF_LabelSequence aCurLabels;
737 aCurLabels.Append(aCompLab);
738 Standard_Integer i = aLabels.Length() - 1;
739 // while (aCurLabels.Length() < aLabels.Length()) {
740 while (i >= 1) {
741 aCurLabels.Prepend( aLabels.Value(i--) );
742 // get shuo from document by label structure
743 Handle(XCAFDoc_GraphNode) aSHUO;
744 if ( !ShapeTool()->FindSHUO( aCurLabels, aSHUO ) )
745 continue;
746 if ( !IsVisible(aSHUO->Label()) )
747 return Standard_False;
748 }
749 return Standard_True; //visible, cause cannot find invisibility status
750}
751
752
753//=======================================================================
754//function : ReverseTreeNodes
755//purpose : auxilary
756//=======================================================================
757static void ReverseTreeNodes(Handle(TDataStd_TreeNode)& mainNode)
758{
759 if(mainNode->HasFirst()) {
760 Handle(TDataStd_TreeNode) tmpNode;
761 Handle(TDataStd_TreeNode) pNode = mainNode->First();
762 Handle(TDataStd_TreeNode) nNode = pNode->Next();
763 while(!nNode.IsNull()) {
764 tmpNode = pNode->Previous();
765 pNode->SetPrevious(nNode);
766 pNode->SetNext(tmpNode);
767 pNode = nNode;
768 nNode = pNode->Next();
769 }
770 tmpNode = pNode->Previous();
771 pNode->SetPrevious(nNode);
772 pNode->SetNext(tmpNode);
773 mainNode->SetFirst(pNode);
774 }
775}
776
777
778//=======================================================================
779//function : ReverseChainsOfTreeNodes
780//purpose :
781//=======================================================================
782
783Standard_Boolean XCAFDoc_ColorTool::ReverseChainsOfTreeNodes()
784{
785 TDF_ChildIDIterator it(Label(),XCAFDoc_Color::GetID());
786 for (; it.More(); it.Next()) {
787 TDF_Label aLabel = it.Value()->Label();
788 Handle(TDataStd_TreeNode) mainNode;
789 if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorSurf),mainNode)) {
790 ReverseTreeNodes(mainNode);
791 }
792 if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorCurv),mainNode)) {
793 ReverseTreeNodes(mainNode);
794 }
795 if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorGen),mainNode)) {
796 ReverseTreeNodes(mainNode);
797 }
798 }
799 return Standard_True;
800}