1 #include <XCAFDoc_ColorTool.ixx>
3 #include <XCAFDoc_Color.hxx>
5 #include <TDF_ChildIDIterator.hxx>
6 #include <TDataStd_TreeNode.hxx>
7 #include <TDataStd_Name.hxx>
8 #include <TNaming_NamedShape.hxx>
9 #include <XCAFDoc_DocumentTool.hxx>
10 #include <XCAFDoc_Color.hxx>
11 #include <TDataStd_UAttribute.hxx>
12 #include <XCAFDoc_GraphNode.hxx>
14 #define AUTONAMING // automatically set names for labels
16 //=======================================================================
17 //function : BaseLabel
19 //=======================================================================
21 TDF_Label XCAFDoc_ColorTool::BaseLabel() const
25 //=======================================================================
26 //function : ShapeTool
28 //=======================================================================
30 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_ColorTool::ShapeTool()
32 if (myShapeTool.IsNull())
33 myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
38 //=======================================================================
41 //=======================================================================
43 Standard_Boolean XCAFDoc_ColorTool::IsColor (const TDF_Label& lab) const
46 return GetColor ( lab, C );
49 //=======================================================================
52 //=======================================================================
54 Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& lab,
55 Quantity_Color& col) const
57 if ( lab.Father() != Label() ) return Standard_False;
59 Handle(XCAFDoc_Color) ColorAttribute;
60 if ( ! lab.FindAttribute ( XCAFDoc_Color::GetID(), ColorAttribute ))
61 return Standard_False;
63 col = ColorAttribute->GetColor();
68 //=======================================================================
69 //function : FindColor
71 //=======================================================================
73 Standard_Boolean XCAFDoc_ColorTool::FindColor (const Quantity_Color& col, TDF_Label& lab) const
75 TDF_ChildIDIterator it(Label(),XCAFDoc_Color::GetID());
76 for (; it.More(); it.Next()) {
77 TDF_Label aLabel = it.Value()->Label();
79 if ( ! GetColor ( aLabel, C ) ) continue;
80 if ( C.IsEqual ( col ) ) {
85 return Standard_False;
88 //=======================================================================
89 //function : FindColor
91 //=======================================================================
93 TDF_Label XCAFDoc_ColorTool::FindColor (const Quantity_Color& col) const
100 //=======================================================================
101 //function : AddColor
103 //=======================================================================
105 TDF_Label XCAFDoc_ColorTool::AddColor (const Quantity_Color& col) const
108 if ( FindColor ( col, L ) ) return L;
110 // create a new color entry
113 L = aTag.NewChild ( Label() );
115 XCAFDoc_Color::Set(L, col);
118 // set name according to color value
119 TCollection_AsciiString str;
120 str += col.StringName ( col.Name() );
122 str += TCollection_AsciiString ( col.Red() );
124 str += TCollection_AsciiString ( col.Green() );
126 str += TCollection_AsciiString ( col.Blue() );
128 TDataStd_Name::Set ( L, str );
134 //=======================================================================
135 //function : RemoveColor
137 //=======================================================================
139 void XCAFDoc_ColorTool::RemoveColor (const TDF_Label& lab) const
141 lab.ForgetAllAttributes (Standard_True);
144 //=======================================================================
145 //function : GetColors
147 //=======================================================================
149 void XCAFDoc_ColorTool::GetColors (TDF_LabelSequence& Labels) const
153 TDF_ChildIDIterator ChildIDIterator(Label(),XCAFDoc_Color::GetID());
154 for (; ChildIDIterator.More(); ChildIDIterator.Next()) {
155 TDF_Label L = ChildIDIterator.Value()->Label();
156 if ( IsColor ( L ) ) Labels.Append ( L );
160 //=======================================================================
161 //function : SetColor
163 //=======================================================================
165 void XCAFDoc_ColorTool::SetColor (const TDF_Label& L,
166 const TDF_Label& colorL,
167 const XCAFDoc_ColorType type) const
170 Handle(TDataStd_TreeNode) refNode, mainNode;
171 mainNode = TDataStd_TreeNode::Set ( colorL, XCAFDoc::ColorRefGUID(type) );
172 refNode = TDataStd_TreeNode::Set ( L, XCAFDoc::ColorRefGUID(type) );
173 refNode->Remove(); // abv: fix against bug in TreeNode::Append()
174 mainNode->Prepend(refNode);
177 //=======================================================================
178 //function : SetColor
180 //=======================================================================
182 void XCAFDoc_ColorTool::SetColor (const TDF_Label& L,
183 const Quantity_Color& Color,
184 const XCAFDoc_ColorType type) const
186 TDF_Label colorL = AddColor ( Color );
187 SetColor ( L, colorL, type );
190 //=======================================================================
191 //function : UnSetColor
193 //=======================================================================
195 void XCAFDoc_ColorTool::UnSetColor (const TDF_Label& L, const XCAFDoc_ColorType type) const
197 L.ForgetAttribute ( XCAFDoc::ColorRefGUID(type) );
200 //=======================================================================
203 //=======================================================================
205 Standard_Boolean XCAFDoc_ColorTool::IsSet (const TDF_Label& L, const XCAFDoc_ColorType type) const
207 Handle(TDataStd_TreeNode) Node;
208 return L.FindAttribute ( XCAFDoc::ColorRefGUID(type), Node) && Node->HasFather();
211 //=======================================================================
212 //function : GetColor
214 //=======================================================================
216 Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& L,
217 const XCAFDoc_ColorType type,
220 Handle(TDataStd_TreeNode) Node;
221 if ( ! L.FindAttribute ( XCAFDoc::ColorRefGUID(type), Node) ||
222 ! Node->HasFather() ) return Standard_False;
223 colorL = Node->Father()->Label();
224 return Standard_True;
227 //=======================================================================
228 //function : GetColor
230 //=======================================================================
232 Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& L,
233 const XCAFDoc_ColorType type,
234 Quantity_Color& color)
237 if ( ! GetColor ( L, type, colorL ) ) return Standard_False;
238 return GetColor ( colorL, color );
241 //=======================================================================
242 //function : SetColor
244 //=======================================================================
246 Standard_Boolean XCAFDoc_ColorTool::SetColor (const TopoDS_Shape& S,
247 const TDF_Label& colorL,
248 const XCAFDoc_ColorType type)
251 if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
252 SetColor ( L, colorL, type );
253 return Standard_True;
256 //=======================================================================
257 //function : SetColor
259 //=======================================================================
261 Standard_Boolean XCAFDoc_ColorTool::SetColor (const TopoDS_Shape& S,
262 const Quantity_Color& Color,
263 const XCAFDoc_ColorType type)
265 TDF_Label colorL = AddColor ( Color );
266 return SetColor ( S, colorL, type );
269 //=======================================================================
270 //function : UnSetColor
272 //=======================================================================
274 Standard_Boolean XCAFDoc_ColorTool::UnSetColor (const TopoDS_Shape& S,
275 const XCAFDoc_ColorType type)
278 if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
279 UnSetColor ( L, type );
280 return Standard_True;
283 //=======================================================================
286 //=======================================================================
288 Standard_Boolean XCAFDoc_ColorTool::IsSet (const TopoDS_Shape& S,
289 const XCAFDoc_ColorType type)
292 if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
293 return IsSet ( L, type );
296 //=======================================================================
297 //function : GetColor
299 //=======================================================================
301 Standard_Boolean XCAFDoc_ColorTool::GetColor (const TopoDS_Shape& S,
302 const XCAFDoc_ColorType type,
306 if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
307 return GetColor ( L, type, colorL );
310 //=======================================================================
311 //function : GetColor
313 //=======================================================================
315 Standard_Boolean XCAFDoc_ColorTool::GetColor (const TopoDS_Shape& S,
316 const XCAFDoc_ColorType type,
317 Quantity_Color& color)
320 if ( ! GetColor ( S, type, colorL ) ) return Standard_False;
321 return GetColor ( colorL, color );
324 //=======================================================================
327 //=======================================================================
329 const Standard_GUID& XCAFDoc_ColorTool::GetID()
331 static Standard_GUID ColorTblID ("efd212ed-6dfd-11d4-b9c8-0060b0ee281b");
335 //=======================================================================
338 //=======================================================================
340 Handle(XCAFDoc_ColorTool) XCAFDoc_ColorTool::Set(const TDF_Label& L)
342 Handle(XCAFDoc_ColorTool) A;
343 if (!L.FindAttribute (XCAFDoc_ColorTool::GetID(), A)) {
344 A = new XCAFDoc_ColorTool ();
346 A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
351 //=======================================================================
354 //=======================================================================
356 const Standard_GUID& XCAFDoc_ColorTool::ID() const
361 //=======================================================================
364 //=======================================================================
366 void XCAFDoc_ColorTool::Restore(const Handle(TDF_Attribute)& /*with*/)
370 //=======================================================================
371 //function : NewEmpty
373 //=======================================================================
375 Handle(TDF_Attribute) XCAFDoc_ColorTool::NewEmpty() const
377 return new XCAFDoc_ColorTool;
380 //=======================================================================
383 //=======================================================================
385 void XCAFDoc_ColorTool::Paste (const Handle(TDF_Attribute)& /*into*/,
386 const Handle(TDF_RelocationTable)& /*RT*/) const
392 //=======================================================================
393 //function : XCAFDoc_ColorTool
395 //=======================================================================
397 XCAFDoc_ColorTool::XCAFDoc_ColorTool()
401 // PTV 23.01.2003 add visibility flag for objects (CAX-IF TRJ11)
402 //=======================================================================
403 //function : IsVisible
405 //=======================================================================
407 Standard_Boolean XCAFDoc_ColorTool::IsVisible (const TDF_Label& L) const
409 Handle(TDataStd_UAttribute) aUAttr;
410 return (!L.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr));
413 //=======================================================================
414 //function : SetVisibility
416 //=======================================================================
418 void XCAFDoc_ColorTool::SetVisibility (const TDF_Label& L,
419 const Standard_Boolean isvisible)
421 Handle(TDataStd_UAttribute) aUAttr;
423 Handle(XCAFDoc_GraphNode) aSHUO;
424 if (ShapeTool()->IsShape(L) || ShapeTool()->GetSHUO( L, aSHUO ) )
425 if (!L.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr))
426 aUAttr->Set( L, XCAFDoc::InvisibleGUID() );
428 else L.ForgetAttribute( XCAFDoc::InvisibleGUID() );
431 //=======================================================================
432 //function : SetInstanceColor
434 //=======================================================================
436 Standard_Boolean XCAFDoc_ColorTool::SetInstanceColor (const TopoDS_Shape& theShape,
437 const XCAFDoc_ColorType type,
438 const Quantity_Color& color,
439 const Standard_Boolean IsCreateSHUO)
441 // find shuo label structure
442 TDF_LabelSequence aLabels;
443 if ( !ShapeTool()->FindComponent( theShape, aLabels ) )
444 return Standard_False;
445 Handle(XCAFDoc_GraphNode) aSHUO;
446 // set the SHUO structure for this component if it is not exist
447 if ( !ShapeTool()->FindSHUO( aLabels, aSHUO ) )
448 if (aLabels.Length() == 1) {
449 // set color directly for component as NAUO
450 SetColor(aLabels.Value(1), color, type);
451 return Standard_True;
453 else if ( !IsCreateSHUO || !ShapeTool()->SetSHUO( aLabels, aSHUO ) )
454 return Standard_False;
455 TDF_Label aSHUOLabel = aSHUO->Label();
456 SetColor( aSHUOLabel, color, type );
457 return Standard_True;
460 //=======================================================================
461 //function : GetInstanceColor
463 //=======================================================================
465 Standard_Boolean XCAFDoc_ColorTool::GetInstanceColor (const TopoDS_Shape& theShape,
466 const XCAFDoc_ColorType type,
467 Quantity_Color& color)
469 // find shuo label structure
470 TDF_LabelSequence aLabels;
471 if ( !ShapeTool()->FindComponent( theShape, aLabels ) )
472 return Standard_False;
473 Handle(XCAFDoc_GraphNode) aSHUO;
474 // get shuo from document by label structure
475 TDF_Label aCompLab = aLabels.Value(aLabels.Length());
476 while (aLabels.Length() > 1) {
477 if ( !ShapeTool()->FindSHUO( aLabels, aSHUO ) ) {
478 // try to find other shuo
479 aLabels.Remove(aLabels.Length());
482 TDF_Label aSHUOLabel = aSHUO->Label();
483 if (GetColor ( aSHUOLabel, type, color ) )
484 return Standard_True;
486 // try to find other shuo
487 aLabels.Remove(aLabels.Length());
490 // attempt to get color exactly of component
491 if (GetColor( aCompLab, type, color ))
492 return Standard_True;
494 // attempt to get color of solid
495 TopLoc_Location aLoc;
496 TopoDS_Shape S0 = theShape;
498 TDF_Label aRefLab = ShapeTool()->FindShape( S0 );
499 if (!aRefLab.IsNull())
500 return GetColor( aRefLab, type, color );
502 return Standard_False;
505 //=======================================================================
506 //function : IsInstanceVisible
508 //=======================================================================
510 Standard_Boolean XCAFDoc_ColorTool::IsInstanceVisible (const TopoDS_Shape& theShape)
512 // check visibility status of top-level solid, cause it is have highest priority
513 TopLoc_Location NullLoc;
514 TopoDS_Shape S0 = theShape;
515 S0.Location( NullLoc );
516 TDF_Label aRefL = ShapeTool()->FindShape( S0 );
517 if (!aRefL.IsNull() && !IsVisible(aRefL))
518 return Standard_False;
519 // find shuo label structure
520 TDF_LabelSequence aLabels;
521 if ( !ShapeTool()->FindComponent( theShape, aLabels ) )
522 return Standard_True;
523 TDF_Label aCompLab = aLabels.Value(aLabels.Length());
524 // visibility status of component withouts SHUO.
525 if (!IsVisible( aCompLab ))
526 return Standard_False;
527 // check by SHUO structure
528 TDF_LabelSequence aCurLabels;
529 aCurLabels.Append(aCompLab);
530 Standard_Integer i = aLabels.Length() - 1;
531 // while (aCurLabels.Length() < aLabels.Length()) {
533 aCurLabels.Prepend( aLabels.Value(i--) );
534 // get shuo from document by label structure
535 Handle(XCAFDoc_GraphNode) aSHUO;
536 if ( !ShapeTool()->FindSHUO( aCurLabels, aSHUO ) )
538 if ( !IsVisible(aSHUO->Label()) )
539 return Standard_False;
541 return Standard_True; //visible, cause cannot find invisibility status
545 //=======================================================================
546 //function : ReverseTreeNodes
548 //=======================================================================
549 static void ReverseTreeNodes(Handle(TDataStd_TreeNode)& mainNode)
551 if(mainNode->HasFirst()) {
552 Handle(TDataStd_TreeNode) tmpNode;
553 Handle(TDataStd_TreeNode) pNode = mainNode->First();
554 Handle(TDataStd_TreeNode) nNode = pNode->Next();
555 while(!nNode.IsNull()) {
556 tmpNode = pNode->Previous();
557 pNode->SetPrevious(nNode);
558 pNode->SetNext(tmpNode);
560 nNode = pNode->Next();
562 tmpNode = pNode->Previous();
563 pNode->SetPrevious(nNode);
564 pNode->SetNext(tmpNode);
565 mainNode->SetFirst(pNode);
570 //=======================================================================
571 //function : ReverseChainsOfTreeNodes
573 //=======================================================================
575 Standard_Boolean XCAFDoc_ColorTool::ReverseChainsOfTreeNodes()
577 TDF_ChildIDIterator it(Label(),XCAFDoc_Color::GetID());
578 for (; it.More(); it.Next()) {
579 TDF_Label aLabel = it.Value()->Label();
580 Handle(TDataStd_TreeNode) mainNode;
581 if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorSurf),mainNode)) {
582 ReverseTreeNodes(mainNode);
584 if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorCurv),mainNode)) {
585 ReverseTreeNodes(mainNode);
587 if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorGen),mainNode)) {
588 ReverseTreeNodes(mainNode);
591 return Standard_True;