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