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