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 | #include <XCAFDoc_ColorTool.ixx> |
15 | #include <XCAFDoc.hxx> |
16 | #include <XCAFDoc_Color.hxx> |
17 | |
18 | #include <TDF_ChildIDIterator.hxx> |
19 | #include <TDataStd_TreeNode.hxx> |
20 | #include <TDataStd_Name.hxx> |
21 | #include <TNaming_NamedShape.hxx> |
22 | #include <XCAFDoc_DocumentTool.hxx> |
23 | #include <XCAFDoc_Color.hxx> |
24 | #include <TDataStd_UAttribute.hxx> |
25 | #include <XCAFDoc_GraphNode.hxx> |
26 | |
27 | #define AUTONAMING // automatically set names for labels |
28 | |
29 | //======================================================================= |
30 | //function : BaseLabel |
31 | //purpose : |
32 | //======================================================================= |
33 | |
34 | TDF_Label XCAFDoc_ColorTool::BaseLabel() const |
35 | { |
36 | return Label(); |
37 | } |
38 | //======================================================================= |
39 | //function : ShapeTool |
40 | //purpose : |
41 | //======================================================================= |
42 | |
43 | const Handle(XCAFDoc_ShapeTool)& XCAFDoc_ColorTool::ShapeTool() |
44 | { |
45 | if (myShapeTool.IsNull()) |
46 | myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label()); |
47 | return myShapeTool; |
48 | } |
49 | |
50 | |
51 | //======================================================================= |
52 | //function : IsColor |
53 | //purpose : |
54 | //======================================================================= |
55 | |
56 | Standard_Boolean XCAFDoc_ColorTool::IsColor (const TDF_Label& lab) const |
57 | { |
58 | Quantity_Color C; |
59 | return GetColor ( lab, C ); |
60 | } |
61 | |
62 | //======================================================================= |
63 | //function : GetColor |
64 | //purpose : |
65 | //======================================================================= |
66 | |
67 | Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& lab, |
68 | Quantity_Color& col) const |
69 | { |
70 | if ( lab.Father() != Label() ) return Standard_False; |
71 | |
72 | Handle(XCAFDoc_Color) ColorAttribute; |
73 | if ( ! lab.FindAttribute ( XCAFDoc_Color::GetID(), ColorAttribute )) |
74 | return Standard_False; |
75 | |
76 | col = ColorAttribute->GetColor(); |
77 | |
78 | return Standard_True; |
79 | } |
80 | |
81 | //======================================================================= |
82 | //function : FindColor |
83 | //purpose : |
84 | //======================================================================= |
85 | |
86 | Standard_Boolean XCAFDoc_ColorTool::FindColor (const Quantity_Color& col, TDF_Label& lab) const |
87 | { |
88 | TDF_ChildIDIterator it(Label(),XCAFDoc_Color::GetID()); |
89 | for (; it.More(); it.Next()) { |
90 | TDF_Label aLabel = it.Value()->Label(); |
91 | Quantity_Color C; |
92 | if ( ! GetColor ( aLabel, C ) ) continue; |
93 | if ( C.IsEqual ( col ) ) { |
94 | lab = aLabel; |
95 | return Standard_True; |
96 | } |
97 | } |
98 | return Standard_False; |
99 | } |
100 | |
101 | //======================================================================= |
102 | //function : FindColor |
103 | //purpose : |
104 | //======================================================================= |
105 | |
106 | TDF_Label XCAFDoc_ColorTool::FindColor (const Quantity_Color& col) const |
107 | { |
108 | TDF_Label L; |
109 | FindColor ( col, L ); |
110 | return L; |
111 | } |
112 | |
113 | //======================================================================= |
114 | //function : AddColor |
115 | //purpose : |
116 | //======================================================================= |
117 | |
118 | TDF_Label XCAFDoc_ColorTool::AddColor (const Quantity_Color& col) const |
119 | { |
120 | TDF_Label L; |
121 | if ( FindColor ( col, L ) ) return L; |
122 | |
123 | // create a new color entry |
124 | |
125 | TDF_TagSource aTag; |
126 | L = aTag.NewChild ( Label() ); |
127 | |
128 | XCAFDoc_Color::Set(L, col); |
129 | |
130 | #ifdef AUTONAMING |
131 | // set name according to color value |
132 | TCollection_AsciiString str; |
133 | str += col.StringName ( col.Name() ); |
134 | str += " ("; |
135 | str += TCollection_AsciiString ( col.Red() ); |
136 | str += ","; |
137 | str += TCollection_AsciiString ( col.Green() ); |
138 | str += ","; |
139 | str += TCollection_AsciiString ( col.Blue() ); |
140 | str += ")"; |
141 | TDataStd_Name::Set ( L, str ); |
142 | #endif |
143 | |
144 | return L; |
145 | } |
146 | |
147 | //======================================================================= |
148 | //function : RemoveColor |
149 | //purpose : |
150 | //======================================================================= |
151 | |
152 | void XCAFDoc_ColorTool::RemoveColor (const TDF_Label& lab) const |
153 | { |
154 | lab.ForgetAllAttributes (Standard_True); |
155 | } |
156 | |
157 | //======================================================================= |
158 | //function : GetColors |
159 | //purpose : |
160 | //======================================================================= |
161 | |
162 | void XCAFDoc_ColorTool::GetColors (TDF_LabelSequence& Labels) const |
163 | { |
164 | Labels.Clear(); |
165 | |
166 | TDF_ChildIDIterator ChildIDIterator(Label(),XCAFDoc_Color::GetID()); |
167 | for (; ChildIDIterator.More(); ChildIDIterator.Next()) { |
168 | TDF_Label L = ChildIDIterator.Value()->Label(); |
169 | if ( IsColor ( L ) ) Labels.Append ( L ); |
170 | } |
171 | } |
172 | |
173 | //======================================================================= |
174 | //function : SetColor |
175 | //purpose : |
176 | //======================================================================= |
177 | |
178 | void XCAFDoc_ColorTool::SetColor (const TDF_Label& L, |
179 | const TDF_Label& colorL, |
180 | const XCAFDoc_ColorType type) const |
181 | { |
182 | // set reference |
183 | Handle(TDataStd_TreeNode) refNode, mainNode; |
184 | mainNode = TDataStd_TreeNode::Set ( colorL, XCAFDoc::ColorRefGUID(type) ); |
185 | refNode = TDataStd_TreeNode::Set ( L, XCAFDoc::ColorRefGUID(type) ); |
186 | refNode->Remove(); // abv: fix against bug in TreeNode::Append() |
187 | mainNode->Prepend(refNode); |
188 | } |
189 | |
190 | //======================================================================= |
191 | //function : SetColor |
192 | //purpose : |
193 | //======================================================================= |
194 | |
195 | void XCAFDoc_ColorTool::SetColor (const TDF_Label& L, |
196 | const Quantity_Color& Color, |
197 | const XCAFDoc_ColorType type) const |
198 | { |
199 | TDF_Label colorL = AddColor ( Color ); |
200 | SetColor ( L, colorL, type ); |
201 | } |
202 | |
203 | //======================================================================= |
204 | //function : UnSetColor |
205 | //purpose : |
206 | //======================================================================= |
207 | |
208 | void XCAFDoc_ColorTool::UnSetColor (const TDF_Label& L, const XCAFDoc_ColorType type) const |
209 | { |
210 | L.ForgetAttribute ( XCAFDoc::ColorRefGUID(type) ); |
211 | } |
212 | |
213 | //======================================================================= |
214 | //function : IsSet |
215 | //purpose : |
216 | //======================================================================= |
217 | |
218 | Standard_Boolean XCAFDoc_ColorTool::IsSet (const TDF_Label& L, const XCAFDoc_ColorType type) const |
219 | { |
220 | Handle(TDataStd_TreeNode) Node; |
221 | return L.FindAttribute ( XCAFDoc::ColorRefGUID(type), Node) && Node->HasFather(); |
222 | } |
223 | |
224 | //======================================================================= |
225 | //function : GetColor |
226 | //purpose : |
227 | //======================================================================= |
228 | |
229 | Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& L, |
230 | const XCAFDoc_ColorType type, |
231 | TDF_Label& colorL) |
232 | { |
233 | Handle(TDataStd_TreeNode) Node; |
234 | if ( ! L.FindAttribute ( XCAFDoc::ColorRefGUID(type), Node) || |
235 | ! Node->HasFather() ) return Standard_False; |
236 | colorL = Node->Father()->Label(); |
237 | return Standard_True; |
238 | } |
239 | |
240 | //======================================================================= |
241 | //function : GetColor |
242 | //purpose : |
243 | //======================================================================= |
244 | |
245 | Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& L, |
246 | const XCAFDoc_ColorType type, |
247 | Quantity_Color& color) |
248 | { |
249 | TDF_Label colorL; |
250 | if ( ! GetColor ( L, type, colorL ) ) return Standard_False; |
251 | return GetColor ( colorL, color ); |
252 | } |
253 | |
254 | //======================================================================= |
255 | //function : SetColor |
256 | //purpose : |
257 | //======================================================================= |
258 | |
259 | Standard_Boolean XCAFDoc_ColorTool::SetColor (const TopoDS_Shape& S, |
260 | const TDF_Label& colorL, |
261 | const XCAFDoc_ColorType type) |
262 | { |
263 | TDF_Label L; |
264 | if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False; |
265 | SetColor ( L, colorL, type ); |
266 | return Standard_True; |
267 | } |
268 | |
269 | //======================================================================= |
270 | //function : SetColor |
271 | //purpose : |
272 | //======================================================================= |
273 | |
274 | Standard_Boolean XCAFDoc_ColorTool::SetColor (const TopoDS_Shape& S, |
275 | const Quantity_Color& Color, |
276 | const XCAFDoc_ColorType type) |
277 | { |
278 | TDF_Label colorL = AddColor ( Color ); |
279 | return SetColor ( S, colorL, type ); |
280 | } |
281 | |
282 | //======================================================================= |
283 | //function : UnSetColor |
284 | //purpose : |
285 | //======================================================================= |
286 | |
287 | Standard_Boolean XCAFDoc_ColorTool::UnSetColor (const TopoDS_Shape& S, |
288 | const XCAFDoc_ColorType type) |
289 | { |
290 | TDF_Label L; |
291 | if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False; |
292 | UnSetColor ( L, type ); |
293 | return Standard_True; |
294 | } |
295 | |
296 | //======================================================================= |
297 | //function : IsSet |
298 | //purpose : |
299 | //======================================================================= |
300 | |
301 | Standard_Boolean XCAFDoc_ColorTool::IsSet (const TopoDS_Shape& S, |
302 | const XCAFDoc_ColorType type) |
303 | { |
304 | TDF_Label L; |
305 | if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False; |
306 | return IsSet ( L, type ); |
307 | } |
308 | |
309 | //======================================================================= |
310 | //function : GetColor |
311 | //purpose : |
312 | //======================================================================= |
313 | |
314 | Standard_Boolean XCAFDoc_ColorTool::GetColor (const TopoDS_Shape& S, |
315 | const XCAFDoc_ColorType type, |
316 | TDF_Label& colorL) |
317 | { |
318 | TDF_Label L; |
319 | if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False; |
320 | return GetColor ( L, type, colorL ); |
321 | } |
322 | |
323 | //======================================================================= |
324 | //function : GetColor |
325 | //purpose : |
326 | //======================================================================= |
327 | |
328 | Standard_Boolean XCAFDoc_ColorTool::GetColor (const TopoDS_Shape& S, |
329 | const XCAFDoc_ColorType type, |
330 | Quantity_Color& color) |
331 | { |
332 | TDF_Label colorL; |
333 | if ( ! GetColor ( S, type, colorL ) ) return Standard_False; |
334 | return GetColor ( colorL, color ); |
335 | } |
336 | |
337 | //======================================================================= |
338 | //function : GetID |
339 | //purpose : |
340 | //======================================================================= |
341 | |
342 | const Standard_GUID& XCAFDoc_ColorTool::GetID() |
343 | { |
344 | static Standard_GUID ColorTblID ("efd212ed-6dfd-11d4-b9c8-0060b0ee281b"); |
345 | return ColorTblID; |
346 | } |
347 | |
348 | //======================================================================= |
349 | //function : Set |
350 | //purpose : |
351 | //======================================================================= |
352 | |
353 | Handle(XCAFDoc_ColorTool) XCAFDoc_ColorTool::Set(const TDF_Label& L) |
354 | { |
355 | Handle(XCAFDoc_ColorTool) A; |
356 | if (!L.FindAttribute (XCAFDoc_ColorTool::GetID(), A)) { |
357 | A = new XCAFDoc_ColorTool (); |
358 | L.AddAttribute(A); |
359 | A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L); |
360 | } |
361 | return A; |
362 | } |
363 | |
364 | //======================================================================= |
365 | //function : ID |
366 | //purpose : |
367 | //======================================================================= |
368 | |
369 | const Standard_GUID& XCAFDoc_ColorTool::ID() const |
370 | { |
371 | return GetID(); |
372 | } |
373 | |
374 | //======================================================================= |
375 | //function : Restore |
376 | //purpose : |
377 | //======================================================================= |
378 | |
379 | void XCAFDoc_ColorTool::Restore(const Handle(TDF_Attribute)& /*with*/) |
380 | { |
381 | } |
382 | |
383 | //======================================================================= |
384 | //function : NewEmpty |
385 | //purpose : |
386 | //======================================================================= |
387 | |
388 | Handle(TDF_Attribute) XCAFDoc_ColorTool::NewEmpty() const |
389 | { |
390 | return new XCAFDoc_ColorTool; |
391 | } |
392 | |
393 | //======================================================================= |
394 | //function : Paste |
395 | //purpose : |
396 | //======================================================================= |
397 | |
398 | void XCAFDoc_ColorTool::Paste (const Handle(TDF_Attribute)& /*into*/, |
399 | const Handle(TDF_RelocationTable)& /*RT*/) const |
400 | { |
401 | } |
402 | |
403 | /**/ |
404 | |
405 | //======================================================================= |
406 | //function : XCAFDoc_ColorTool |
407 | //purpose : |
408 | //======================================================================= |
409 | |
410 | XCAFDoc_ColorTool::XCAFDoc_ColorTool() |
411 | { |
412 | } |
413 | |
414 | // PTV 23.01.2003 add visibility flag for objects (CAX-IF TRJ11) |
415 | //======================================================================= |
416 | //function : IsVisible |
417 | //purpose : |
418 | //======================================================================= |
419 | |
420 | Standard_Boolean XCAFDoc_ColorTool::IsVisible (const TDF_Label& L) const |
421 | { |
422 | Handle(TDataStd_UAttribute) aUAttr; |
423 | return (!L.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr)); |
424 | } |
425 | |
426 | //======================================================================= |
427 | //function : SetVisibility |
428 | //purpose : |
429 | //======================================================================= |
430 | |
431 | void XCAFDoc_ColorTool::SetVisibility (const TDF_Label& L, |
432 | const Standard_Boolean isvisible) |
433 | { |
434 | Handle(TDataStd_UAttribute) aUAttr; |
435 | if (! isvisible ) { |
436 | Handle(XCAFDoc_GraphNode) aSHUO; |
437 | if (ShapeTool()->IsShape(L) || ShapeTool()->GetSHUO( L, aSHUO ) ) |
438 | if (!L.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr)) |
439 | aUAttr->Set( L, XCAFDoc::InvisibleGUID() ); |
440 | } |
441 | else L.ForgetAttribute( XCAFDoc::InvisibleGUID() ); |
442 | } |
443 | |
444 | //======================================================================= |
445 | //function : SetInstanceColor |
446 | //purpose : |
447 | //======================================================================= |
448 | |
449 | Standard_Boolean XCAFDoc_ColorTool::SetInstanceColor (const TopoDS_Shape& theShape, |
450 | const XCAFDoc_ColorType type, |
451 | const Quantity_Color& color, |
452 | const Standard_Boolean IsCreateSHUO) |
453 | { |
454 | // find shuo label structure |
455 | TDF_LabelSequence aLabels; |
456 | if ( !ShapeTool()->FindComponent( theShape, aLabels ) ) |
457 | return Standard_False; |
458 | Handle(XCAFDoc_GraphNode) aSHUO; |
459 | // set the SHUO structure for this component if it is not exist |
eafb234b |
460 | if ( !ShapeTool()->FindSHUO( aLabels, aSHUO ) ) { |
7fd59977 |
461 | if (aLabels.Length() == 1) { |
462 | // set color directly for component as NAUO |
463 | SetColor(aLabels.Value(1), color, type); |
464 | return Standard_True; |
465 | } |
eafb234b |
466 | else if ( !IsCreateSHUO || !ShapeTool()->SetSHUO( aLabels, aSHUO ) ) { |
7fd59977 |
467 | return Standard_False; |
eafb234b |
468 | } |
469 | } |
7fd59977 |
470 | TDF_Label aSHUOLabel = aSHUO->Label(); |
471 | SetColor( aSHUOLabel, color, type ); |
472 | return Standard_True; |
473 | } |
474 | |
475 | //======================================================================= |
476 | //function : GetInstanceColor |
477 | //purpose : |
478 | //======================================================================= |
479 | |
480 | Standard_Boolean XCAFDoc_ColorTool::GetInstanceColor (const TopoDS_Shape& theShape, |
481 | const XCAFDoc_ColorType type, |
482 | Quantity_Color& color) |
483 | { |
484 | // find shuo label structure |
485 | TDF_LabelSequence aLabels; |
486 | if ( !ShapeTool()->FindComponent( theShape, aLabels ) ) |
487 | return Standard_False; |
488 | Handle(XCAFDoc_GraphNode) aSHUO; |
489 | // get shuo from document by label structure |
490 | TDF_Label aCompLab = aLabels.Value(aLabels.Length()); |
491 | while (aLabels.Length() > 1) { |
492 | if ( !ShapeTool()->FindSHUO( aLabels, aSHUO ) ) { |
493 | // try to find other shuo |
494 | aLabels.Remove(aLabels.Length()); |
495 | continue; |
496 | } else { |
497 | TDF_Label aSHUOLabel = aSHUO->Label(); |
498 | if (GetColor ( aSHUOLabel, type, color ) ) |
499 | return Standard_True; |
500 | else |
501 | // try to find other shuo |
502 | aLabels.Remove(aLabels.Length()); |
503 | } |
504 | } |
505 | // attempt to get color exactly of component |
506 | if (GetColor( aCompLab, type, color )) |
507 | return Standard_True; |
508 | |
509 | // attempt to get color of solid |
510 | TopLoc_Location aLoc; |
511 | TopoDS_Shape S0 = theShape; |
512 | S0.Location( aLoc ); |
513 | TDF_Label aRefLab = ShapeTool()->FindShape( S0 ); |
514 | if (!aRefLab.IsNull()) |
515 | return GetColor( aRefLab, type, color ); |
516 | // no color assigned |
517 | return Standard_False; |
518 | } |
519 | |
520 | //======================================================================= |
521 | //function : IsInstanceVisible |
522 | //purpose : |
523 | //======================================================================= |
524 | |
525 | Standard_Boolean XCAFDoc_ColorTool::IsInstanceVisible (const TopoDS_Shape& theShape) |
526 | { |
527 | // check visibility status of top-level solid, cause it is have highest priority |
528 | TopLoc_Location NullLoc; |
529 | TopoDS_Shape S0 = theShape; |
530 | S0.Location( NullLoc ); |
531 | TDF_Label aRefL = ShapeTool()->FindShape( S0 ); |
532 | if (!aRefL.IsNull() && !IsVisible(aRefL)) |
533 | return Standard_False; |
534 | // find shuo label structure |
535 | TDF_LabelSequence aLabels; |
536 | if ( !ShapeTool()->FindComponent( theShape, aLabels ) ) |
537 | return Standard_True; |
538 | TDF_Label aCompLab = aLabels.Value(aLabels.Length()); |
539 | // visibility status of component withouts SHUO. |
540 | if (!IsVisible( aCompLab )) |
541 | return Standard_False; |
542 | // check by SHUO structure |
543 | TDF_LabelSequence aCurLabels; |
544 | aCurLabels.Append(aCompLab); |
545 | Standard_Integer i = aLabels.Length() - 1; |
546 | // while (aCurLabels.Length() < aLabels.Length()) { |
547 | while (i >= 1) { |
548 | aCurLabels.Prepend( aLabels.Value(i--) ); |
549 | // get shuo from document by label structure |
550 | Handle(XCAFDoc_GraphNode) aSHUO; |
551 | if ( !ShapeTool()->FindSHUO( aCurLabels, aSHUO ) ) |
552 | continue; |
553 | if ( !IsVisible(aSHUO->Label()) ) |
554 | return Standard_False; |
555 | } |
556 | return Standard_True; //visible, cause cannot find invisibility status |
557 | } |
558 | |
559 | |
560 | //======================================================================= |
561 | //function : ReverseTreeNodes |
562 | //purpose : auxilary |
563 | //======================================================================= |
564 | static void ReverseTreeNodes(Handle(TDataStd_TreeNode)& mainNode) |
565 | { |
566 | if(mainNode->HasFirst()) { |
567 | Handle(TDataStd_TreeNode) tmpNode; |
568 | Handle(TDataStd_TreeNode) pNode = mainNode->First(); |
569 | Handle(TDataStd_TreeNode) nNode = pNode->Next(); |
570 | while(!nNode.IsNull()) { |
571 | tmpNode = pNode->Previous(); |
572 | pNode->SetPrevious(nNode); |
573 | pNode->SetNext(tmpNode); |
574 | pNode = nNode; |
575 | nNode = pNode->Next(); |
576 | } |
577 | tmpNode = pNode->Previous(); |
578 | pNode->SetPrevious(nNode); |
579 | pNode->SetNext(tmpNode); |
580 | mainNode->SetFirst(pNode); |
581 | } |
582 | } |
583 | |
584 | |
585 | //======================================================================= |
586 | //function : ReverseChainsOfTreeNodes |
587 | //purpose : |
588 | //======================================================================= |
589 | |
590 | Standard_Boolean XCAFDoc_ColorTool::ReverseChainsOfTreeNodes() |
591 | { |
592 | TDF_ChildIDIterator it(Label(),XCAFDoc_Color::GetID()); |
593 | for (; it.More(); it.Next()) { |
594 | TDF_Label aLabel = it.Value()->Label(); |
595 | Handle(TDataStd_TreeNode) mainNode; |
596 | if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorSurf),mainNode)) { |
597 | ReverseTreeNodes(mainNode); |
598 | } |
599 | if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorCurv),mainNode)) { |
600 | ReverseTreeNodes(mainNode); |
601 | } |
602 | if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorGen),mainNode)) { |
603 | ReverseTreeNodes(mainNode); |
604 | } |
605 | } |
606 | return Standard_True; |
607 | } |