summaryrefslogtreecommitdiff
path: root/src/XCAFDoc/XCAFDoc_ColorTool.cxx
blob: 91f51bfee93abec377d0f8cec99f2491098c2457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include <XCAFDoc_ColorTool.ixx>
#include <XCAFDoc.hxx>
#include <XCAFDoc_Color.hxx>

#include <TDF_ChildIDIterator.hxx>
#include <TDataStd_TreeNode.hxx>
#include <TDataStd_Name.hxx>
#include <TNaming_NamedShape.hxx>
#include <XCAFDoc_DocumentTool.hxx>
#include <XCAFDoc_Color.hxx>
#include <TDataStd_UAttribute.hxx>
#include <XCAFDoc_GraphNode.hxx>

#define AUTONAMING // automatically set names for labels

//=======================================================================
//function : BaseLabel
//purpose  : 
//=======================================================================

TDF_Label XCAFDoc_ColorTool::BaseLabel() const
{
  return Label();
}
//=======================================================================
//function : ShapeTool
//purpose  : 
//=======================================================================

const Handle(XCAFDoc_ShapeTool)& XCAFDoc_ColorTool::ShapeTool() 
{
  if (myShapeTool.IsNull())
    myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
  return myShapeTool;
}


//=======================================================================
//function : IsColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::IsColor (const TDF_Label& lab) const
{
  Quantity_Color C;
  return GetColor ( lab, C );
}

//=======================================================================
//function : GetColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& lab,
					       Quantity_Color& col) const
{
  if ( lab.Father() != Label() ) return Standard_False;
  
  Handle(XCAFDoc_Color) ColorAttribute;
  if ( ! lab.FindAttribute ( XCAFDoc_Color::GetID(), ColorAttribute ))
    return Standard_False;
  
  col = ColorAttribute->GetColor();
  
  return Standard_True;
}

//=======================================================================
//function : FindColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::FindColor (const Quantity_Color& col, TDF_Label& lab) const
{
  TDF_ChildIDIterator it(Label(),XCAFDoc_Color::GetID());
  for (; it.More(); it.Next()) {
    TDF_Label aLabel = it.Value()->Label();
    Quantity_Color C;
    if ( ! GetColor ( aLabel, C ) ) continue;
    if ( C.IsEqual ( col ) ) {
      lab = aLabel;
      return Standard_True;
    }
  }
  return Standard_False;
}

//=======================================================================
//function : FindColor
//purpose  : 
//=======================================================================

TDF_Label XCAFDoc_ColorTool::FindColor (const Quantity_Color& col) const
{
  TDF_Label L;
  FindColor ( col, L );
  return L;
}

//=======================================================================
//function : AddColor
//purpose  : 
//=======================================================================

TDF_Label XCAFDoc_ColorTool::AddColor (const Quantity_Color& col) const
{
  TDF_Label L;
  if ( FindColor ( col, L ) ) return L;

  // create a new color entry
  
  TDF_TagSource aTag;
  L = aTag.NewChild ( Label() );

  XCAFDoc_Color::Set(L, col);
  
#ifdef AUTONAMING
  // set name according to color value
  TCollection_AsciiString str;
  str += col.StringName ( col.Name() );
  str += " (";
  str += TCollection_AsciiString ( col.Red() );
  str += ",";
  str += TCollection_AsciiString ( col.Green() );
  str += ",";
  str += TCollection_AsciiString ( col.Blue() );
  str += ")";
  TDataStd_Name::Set ( L, str );
#endif
  
  return L;
}

//=======================================================================
//function : RemoveColor
//purpose  : 
//=======================================================================

void XCAFDoc_ColorTool::RemoveColor (const TDF_Label& lab) const
{
  lab.ForgetAllAttributes (Standard_True);
}

//=======================================================================
//function : GetColors
//purpose  : 
//=======================================================================

void XCAFDoc_ColorTool::GetColors (TDF_LabelSequence& Labels) const
{
  Labels.Clear();

  TDF_ChildIDIterator ChildIDIterator(Label(),XCAFDoc_Color::GetID()); 
  for (; ChildIDIterator.More(); ChildIDIterator.Next()) {
    TDF_Label L = ChildIDIterator.Value()->Label();
    if ( IsColor ( L ) ) Labels.Append ( L );
  }
}

//=======================================================================
//function : SetColor
//purpose  : 
//=======================================================================

void XCAFDoc_ColorTool::SetColor (const TDF_Label& L,
			       const TDF_Label& colorL,
			       const XCAFDoc_ColorType type) const
{
  // set reference
  Handle(TDataStd_TreeNode) refNode, mainNode;
  mainNode = TDataStd_TreeNode::Set ( colorL, XCAFDoc::ColorRefGUID(type) );
  refNode  = TDataStd_TreeNode::Set ( L,      XCAFDoc::ColorRefGUID(type) );
  refNode->Remove(); // abv: fix against bug in TreeNode::Append()
  mainNode->Prepend(refNode);
}

//=======================================================================
//function : SetColor
//purpose  : 
//=======================================================================

void XCAFDoc_ColorTool::SetColor (const TDF_Label& L,
			       const Quantity_Color& Color,
			       const XCAFDoc_ColorType type) const
{
  TDF_Label colorL = AddColor ( Color );
  SetColor ( L, colorL, type );
}

//=======================================================================
//function : UnSetColor
//purpose  : 
//=======================================================================

void XCAFDoc_ColorTool::UnSetColor (const TDF_Label& L, const XCAFDoc_ColorType type) const
{
  L.ForgetAttribute ( XCAFDoc::ColorRefGUID(type) );
}

//=======================================================================
//function : IsSet
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::IsSet (const TDF_Label& L, const XCAFDoc_ColorType type) const
{
  Handle(TDataStd_TreeNode) Node;
  return L.FindAttribute ( XCAFDoc::ColorRefGUID(type), Node) && Node->HasFather();
}

//=======================================================================
//function : GetColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& L,
					   const XCAFDoc_ColorType type,
					   TDF_Label& colorL) 
{
  Handle(TDataStd_TreeNode) Node;
  if ( ! L.FindAttribute ( XCAFDoc::ColorRefGUID(type), Node) ||
       ! Node->HasFather() ) return Standard_False;
  colorL = Node->Father()->Label();
  return Standard_True;
}

//=======================================================================
//function : GetColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::GetColor (const TDF_Label& L,
					   const XCAFDoc_ColorType type,
					   Quantity_Color& color) 
{
  TDF_Label colorL;
  if ( ! GetColor ( L, type, colorL ) ) return Standard_False;
  return GetColor ( colorL, color );
}

//=======================================================================
//function : SetColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::SetColor (const TopoDS_Shape& S,
					   const TDF_Label& colorL,
					   const XCAFDoc_ColorType type) 
{
  TDF_Label L;
  if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
  SetColor ( L, colorL, type );
  return Standard_True;
}

//=======================================================================
//function : SetColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::SetColor (const TopoDS_Shape& S,
					   const Quantity_Color& Color,
					   const XCAFDoc_ColorType type) 
{
  TDF_Label colorL = AddColor ( Color );
  return SetColor ( S, colorL, type );
}

//=======================================================================
//function : UnSetColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::UnSetColor (const TopoDS_Shape& S,
					     const XCAFDoc_ColorType type) 
{
  TDF_Label L;
  if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
  UnSetColor ( L, type );
  return Standard_True;
}

//=======================================================================
//function : IsSet
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::IsSet (const TopoDS_Shape& S,
					const XCAFDoc_ColorType type) 
{
  TDF_Label L;
  if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
  return IsSet ( L, type );
}

//=======================================================================
//function : GetColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::GetColor (const TopoDS_Shape& S, 
					   const XCAFDoc_ColorType type,
					   TDF_Label& colorL) 
{
  TDF_Label L;
  if ( ! ShapeTool()->Search ( S, L ) ) return Standard_False;
  return GetColor ( L, type, colorL );
}

//=======================================================================
//function : GetColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::GetColor (const TopoDS_Shape& S,
					   const XCAFDoc_ColorType type,
					   Quantity_Color& color) 
{
  TDF_Label colorL;
  if ( ! GetColor ( S, type, colorL ) ) return Standard_False;
  return GetColor ( colorL, color );
}

//=======================================================================
//function : GetID
//purpose  : 
//=======================================================================

const Standard_GUID& XCAFDoc_ColorTool::GetID() 
{
  static Standard_GUID ColorTblID ("efd212ed-6dfd-11d4-b9c8-0060b0ee281b");
  return ColorTblID; 
}

//=======================================================================
//function : Set
//purpose  : 
//=======================================================================

Handle(XCAFDoc_ColorTool) XCAFDoc_ColorTool::Set(const TDF_Label& L) 
{
  Handle(XCAFDoc_ColorTool) A;
  if (!L.FindAttribute (XCAFDoc_ColorTool::GetID(), A)) {
    A = new XCAFDoc_ColorTool ();
    L.AddAttribute(A);
    A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
  }
  return A;
}

//=======================================================================
//function : ID
//purpose  : 
//=======================================================================

const Standard_GUID& XCAFDoc_ColorTool::ID() const
{
  return GetID();
}

//=======================================================================
//function : Restore
//purpose  : 
//=======================================================================

void XCAFDoc_ColorTool::Restore(const Handle(TDF_Attribute)& /*with*/) 
{
}

//=======================================================================
//function : NewEmpty
//purpose  : 
//=======================================================================

Handle(TDF_Attribute) XCAFDoc_ColorTool::NewEmpty() const
{
  return new XCAFDoc_ColorTool;
}

//=======================================================================
//function : Paste
//purpose  : 
//=======================================================================

void XCAFDoc_ColorTool::Paste (const Handle(TDF_Attribute)& /*into*/,
			       const Handle(TDF_RelocationTable)& /*RT*/) const
{
}

/**/

//=======================================================================
//function : XCAFDoc_ColorTool
//purpose  : 
//=======================================================================

XCAFDoc_ColorTool::XCAFDoc_ColorTool()
{
}

// PTV 23.01.2003 add visibility flag for objects (CAX-IF TRJ11)
//=======================================================================
//function : IsVisible
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::IsVisible (const TDF_Label& L) const
{
  Handle(TDataStd_UAttribute) aUAttr;
  return (!L.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr));
}

//=======================================================================
//function : SetVisibility
//purpose  : 
//=======================================================================

void XCAFDoc_ColorTool::SetVisibility (const TDF_Label& L,
				       const Standard_Boolean isvisible)
{
  Handle(TDataStd_UAttribute) aUAttr;
  if (! isvisible ) {
    Handle(XCAFDoc_GraphNode) aSHUO;
    if (ShapeTool()->IsShape(L) || ShapeTool()->GetSHUO( L, aSHUO ) )
      if (!L.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr))
        aUAttr->Set( L, XCAFDoc::InvisibleGUID() );
  }
  else L.ForgetAttribute( XCAFDoc::InvisibleGUID() );
}

//=======================================================================
//function : SetInstanceColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::SetInstanceColor (const TopoDS_Shape& theShape,
                                                      const XCAFDoc_ColorType type,
                                                      const Quantity_Color& color,
                                                      const Standard_Boolean IsCreateSHUO)
{
  // find shuo label structure 
  TDF_LabelSequence aLabels;
  if ( !ShapeTool()->FindComponent( theShape, aLabels ) )
    return Standard_False;
  Handle(XCAFDoc_GraphNode) aSHUO;
  // set the SHUO structure for this component if it is not exist
  if ( !ShapeTool()->FindSHUO( aLabels, aSHUO ) ) {
    if (aLabels.Length() == 1) {
      // set color directly for component as NAUO
      SetColor(aLabels.Value(1), color, type);
      return Standard_True;
    }
    else if ( !IsCreateSHUO ||  !ShapeTool()->SetSHUO( aLabels, aSHUO ) )
      return Standard_False;
  }
  TDF_Label aSHUOLabel = aSHUO->Label();
  SetColor( aSHUOLabel, color, type );
  return Standard_True;
}

//=======================================================================
//function : GetInstanceColor
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::GetInstanceColor (const TopoDS_Shape& theShape,
                                                      const XCAFDoc_ColorType type,
                                                      Quantity_Color& color)
{
  // find shuo label structure 
  TDF_LabelSequence aLabels;
  if ( !ShapeTool()->FindComponent( theShape, aLabels ) )
    return Standard_False;
  Handle(XCAFDoc_GraphNode) aSHUO;
  // get shuo from document by label structure
  TDF_Label aCompLab = aLabels.Value(aLabels.Length());
  while (aLabels.Length() > 1) {
    if ( !ShapeTool()->FindSHUO( aLabels, aSHUO ) ) {
      // try to find other shuo 
      aLabels.Remove(aLabels.Length());
      continue;
    } else {
      TDF_Label aSHUOLabel = aSHUO->Label();
      if (GetColor ( aSHUOLabel, type, color ) )
        return Standard_True;
      else 
        // try to find other shuo 
        aLabels.Remove(aLabels.Length());
    }
  }
  // attempt to get color exactly of component
  if (GetColor( aCompLab, type, color ))
    return Standard_True;
  
  // attempt to get color of solid
  TopLoc_Location aLoc;
  TopoDS_Shape S0 = theShape;
  S0.Location( aLoc );
  TDF_Label aRefLab = ShapeTool()->FindShape( S0 );
  if (!aRefLab.IsNull())
    return GetColor( aRefLab, type, color );
  // no color assigned
  return Standard_False;
}

//=======================================================================
//function : IsInstanceVisible
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::IsInstanceVisible (const TopoDS_Shape& theShape)
{
  // check visibility status of top-level solid, cause it is have highest priority
  TopLoc_Location NullLoc;
  TopoDS_Shape S0 = theShape;
  S0.Location( NullLoc );
  TDF_Label aRefL = ShapeTool()->FindShape( S0 );
  if (!aRefL.IsNull() && !IsVisible(aRefL))
    return Standard_False;
  // find shuo label structure 
  TDF_LabelSequence aLabels;
  if ( !ShapeTool()->FindComponent( theShape, aLabels ) )
    return Standard_True;
  TDF_Label aCompLab = aLabels.Value(aLabels.Length());
  // visibility status of component withouts SHUO.
  if (!IsVisible( aCompLab ))
    return Standard_False;
  // check by SHUO structure
  TDF_LabelSequence aCurLabels;
  aCurLabels.Append(aCompLab);
  Standard_Integer i = aLabels.Length() - 1;
  //   while (aCurLabels.Length() < aLabels.Length()) {
  while (i >= 1) {
    aCurLabels.Prepend( aLabels.Value(i--) );
    // get shuo from document by label structure
    Handle(XCAFDoc_GraphNode) aSHUO;
    if ( !ShapeTool()->FindSHUO( aCurLabels, aSHUO ) )
      continue;
    if ( !IsVisible(aSHUO->Label()) )
      return Standard_False;
  }
  return Standard_True; //visible, cause cannot find invisibility status
}


//=======================================================================
//function : ReverseTreeNodes
//purpose  : auxilary
//=======================================================================
static void ReverseTreeNodes(Handle(TDataStd_TreeNode)& mainNode)
{
  if(mainNode->HasFirst()) {
    Handle(TDataStd_TreeNode) tmpNode;
    Handle(TDataStd_TreeNode) pNode = mainNode->First();
    Handle(TDataStd_TreeNode) nNode = pNode->Next();
    while(!nNode.IsNull()) {
      tmpNode = pNode->Previous();
      pNode->SetPrevious(nNode);
      pNode->SetNext(tmpNode);
      pNode = nNode;
      nNode = pNode->Next();
    }
    tmpNode = pNode->Previous();
    pNode->SetPrevious(nNode);
    pNode->SetNext(tmpNode);
    mainNode->SetFirst(pNode);
  }
}


//=======================================================================
//function : ReverseChainsOfTreeNodes
//purpose  : 
//=======================================================================

Standard_Boolean XCAFDoc_ColorTool::ReverseChainsOfTreeNodes()
{
  TDF_ChildIDIterator it(Label(),XCAFDoc_Color::GetID());
  for (; it.More(); it.Next()) {
    TDF_Label aLabel = it.Value()->Label();
    Handle(TDataStd_TreeNode) mainNode;
    if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorSurf),mainNode)) {
      ReverseTreeNodes(mainNode);
    }
    if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorCurv),mainNode)) {
      ReverseTreeNodes(mainNode);
    }
    if(aLabel.FindAttribute(XCAFDoc::ColorRefGUID(XCAFDoc_ColorGen),mainNode)) {
      ReverseTreeNodes(mainNode);
    }
  }
  return Standard_True;
}