0023921: IGES reader cannot map subshapes colors inside nested assemblies
[occt.git] / src / XCAFDoc / XCAFDoc_LayerTool.cxx
1 // Created on: 2000-10-02
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21
22 #include <XCAFDoc_LayerTool.ixx>
23 #include <XCAFDoc_DocumentTool.hxx>
24 #include <XCAFDoc_GraphNode.hxx>
25 #include <TDataStd_Name.hxx>
26 #include <TDF_ChildIDIterator.hxx>
27 #include <TDF_ChildIterator.hxx>
28 #include <XCAFDoc.hxx>
29 #include <TDataStd_UAttribute.hxx>
30
31 //=======================================================================
32 //function : Constructor
33 //purpose  : 
34 //=======================================================================
35
36 XCAFDoc_LayerTool::XCAFDoc_LayerTool()
37 {
38 }
39
40
41 //=======================================================================
42 //function : Set
43 //purpose  : 
44 //=======================================================================
45
46 Handle(XCAFDoc_LayerTool) XCAFDoc_LayerTool::Set(const TDF_Label& L) 
47 {
48   Handle(XCAFDoc_LayerTool) A;
49   if (!L.FindAttribute (XCAFDoc_LayerTool::GetID(), A)) {
50     A = new XCAFDoc_LayerTool ();
51     L.AddAttribute(A);
52     A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
53   }
54   return A;
55 }
56
57
58 //=======================================================================
59 //function : GetID
60 //purpose  : 
61 //=======================================================================
62
63 const Standard_GUID& XCAFDoc_LayerTool::GetID() 
64 {
65   static Standard_GUID LayerTblID ("efd212f4-6dfd-11d4-b9c8-0060b0ee281b");
66   return LayerTblID; 
67 }
68
69
70 //=======================================================================
71 //function : BaseLabel
72 //purpose  : 
73 //=======================================================================
74
75 TDF_Label XCAFDoc_LayerTool::BaseLabel() const
76 {
77   return Label();
78 }
79
80
81 //=======================================================================
82 //function : ShapeTool
83 //purpose  : 
84 //=======================================================================
85
86 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_LayerTool::ShapeTool() 
87 {
88   if (myShapeTool.IsNull())
89     myShapeTool = XCAFDoc_DocumentTool::ShapeTool( Label() );
90   return myShapeTool;
91 }
92
93
94 //=======================================================================
95 //function : IsLayer
96 //purpose  : 
97 //=======================================================================
98
99 Standard_Boolean XCAFDoc_LayerTool::IsLayer(const TDF_Label& lab) const
100 {
101   TCollection_ExtendedString aLayer;
102   return GetLayer ( lab, aLayer);
103 }
104
105
106 //=======================================================================
107 //function : GetLayer
108 //purpose  : 
109 //=======================================================================
110
111 Standard_Boolean XCAFDoc_LayerTool::GetLayer(const TDF_Label& lab,
112                                              TCollection_ExtendedString& aLayer) const
113 {
114   if ( lab.Father() != Label() ) return Standard_False;
115 //   Handle(XCAFDoc_GraphNode) aGN;
116 //   if (! lab.FindAttribute (XCAFDoc::LayerRefGUID(), aGN))
117 //     return Standard_False;
118   Handle(TDataStd_Name) aName;
119   Standard_Boolean status = Standard_False;
120   if ( lab.FindAttribute (TDataStd_Name::GetID(), aName) ) {
121     aLayer = aName->Get();
122     status = Standard_True;
123   }
124   return status;
125 }
126
127 //=======================================================================
128 //function : FindLayer
129 //purpose  : 
130 //=======================================================================
131
132 Standard_Boolean XCAFDoc_LayerTool::FindLayer(const TCollection_ExtendedString& aLayer,
133                                               TDF_Label& lab) const
134 {
135   lab = FindLayer(aLayer);
136   return ( !lab.IsNull() );
137 }
138
139
140 //=======================================================================
141 //function : FindLayer
142 //purpose  : 
143 //=======================================================================
144
145 TDF_Label XCAFDoc_LayerTool::FindLayer(const TCollection_ExtendedString& aLayer) const
146 {
147   TDF_ChildIterator it( Label() );
148   TDF_Label lab;
149   for (; it.More(); it.Next()) {
150     TDF_Label aLabel = it.Value();
151     Handle(TDataStd_Name) aName;
152     if ( aLabel.FindAttribute (TDataStd_Name::GetID(), aName) && (aName->Get().IsEqual(aLayer)) ) {
153       lab =  aLabel;
154       break;
155     }
156   }
157   return lab;
158 }
159
160
161 //=======================================================================
162 //function : AddLayer
163 //purpose  : 
164 //=======================================================================
165
166 TDF_Label XCAFDoc_LayerTool::AddLayer(const TCollection_ExtendedString& aLayer) const
167 {
168   TDF_Label lab;
169   if ( FindLayer(aLayer, lab) )
170     return lab;
171   TDF_TagSource aTag;
172   TDF_Label aLabel = aTag.NewChild( Label() );
173   Handle(TDataStd_Name) aName = new TDataStd_Name;
174   aName->Set(aLabel, aLayer);
175   return aLabel;
176 }
177
178
179 //=======================================================================
180 //function : RemoveLayer
181 //purpose  : 
182 //=======================================================================
183
184 void XCAFDoc_LayerTool::RemoveLayer(const TDF_Label& lab) const
185 {
186   lab.ForgetAllAttributes (Standard_True);
187 }
188
189
190 //=======================================================================
191 //function : GetLayerLabels
192 //purpose  : 
193 //=======================================================================
194
195 void XCAFDoc_LayerTool::GetLayerLabels(TDF_LabelSequence& Labels) const
196 {
197   Labels.Clear();
198   TDF_ChildIterator ChildIterator( Label() ); 
199   for (; ChildIterator.More(); ChildIterator.Next()) {
200     TDF_Label L = ChildIterator.Value();
201     if ( IsLayer(L)) Labels.Append(L);
202   }
203 }
204
205
206 //=======================================================================
207 //function : SetLayer
208 //purpose  : 
209 //=======================================================================
210
211 void XCAFDoc_LayerTool::SetLayer(const TDF_Label& L,
212                                  const TDF_Label& LayerL,
213                                  const Standard_Boolean shapeInOneLayer) const
214 {
215   if (shapeInOneLayer) UnSetLayers( L );
216   Handle(XCAFDoc_GraphNode) FGNode;
217   Handle(XCAFDoc_GraphNode) ChGNode;
218   if (! LayerL.FindAttribute( XCAFDoc::LayerRefGUID(), FGNode) ) {
219     FGNode = new XCAFDoc_GraphNode;
220     FGNode = XCAFDoc_GraphNode::Set(LayerL);
221   }
222   if (! L.FindAttribute( XCAFDoc::LayerRefGUID(), ChGNode) ) {
223     ChGNode = new XCAFDoc_GraphNode;
224     ChGNode = XCAFDoc_GraphNode::Set(L);
225   }
226   FGNode->SetGraphID( XCAFDoc::LayerRefGUID() );
227   ChGNode->SetGraphID( XCAFDoc::LayerRefGUID() );
228   FGNode->SetChild(ChGNode);
229   ChGNode->SetFather(FGNode);
230 }
231
232
233 //=======================================================================
234 //function : SetLayer
235 //purpose  : 
236 //=======================================================================
237
238 void XCAFDoc_LayerTool::SetLayer(const TDF_Label& L,
239                                  const TCollection_ExtendedString& aLayer,
240                                  const Standard_Boolean shapeInOneLayer) const
241 {
242   TDF_Label aLayerL = AddLayer(aLayer);
243   SetLayer(L, aLayerL, shapeInOneLayer);
244 }
245
246
247 //=======================================================================
248 //function : UnSetLayers
249 //purpose  : 
250 //=======================================================================
251
252 void XCAFDoc_LayerTool::UnSetLayers(const TDF_Label& L) const
253 {
254   Handle(XCAFDoc_GraphNode) ChGNode, FGNode;
255   if ( L.FindAttribute (XCAFDoc::LayerRefGUID(), ChGNode) ) {
256     while (ChGNode->NbFathers()!= 0) {
257       FGNode = ChGNode->GetFather(1);
258       FGNode-> UnSetChild(ChGNode);
259 //       ChGNode->GetFather(1)->UnSetChild(ChGNode);
260     }
261     L.ForgetAttribute ( XCAFDoc::LayerRefGUID() );
262   }
263 }
264
265
266 //=======================================================================
267 //function : UnSetOneLayer
268 //purpose  : 
269 //=======================================================================
270
271 Standard_Boolean XCAFDoc_LayerTool::UnSetOneLayer(const TDF_Label& L,
272                                                   const TCollection_ExtendedString& aLayer) const
273 {
274   TDF_Label alab;
275   if ( !FindLayer(aLayer, alab) ) return Standard_False;
276   return UnSetOneLayer (L, alab);
277 }
278
279 //=======================================================================
280 //function : UnSetOneLayer
281 //purpose  : 
282 //=======================================================================
283
284 Standard_Boolean XCAFDoc_LayerTool::UnSetOneLayer(const TDF_Label& L,
285                                                   const TDF_Label& aLayerL) const
286 {
287   Handle(XCAFDoc_GraphNode) FGNode, ChGNode;
288   if ( !L.FindAttribute (XCAFDoc::LayerRefGUID(), ChGNode) ) return Standard_False;
289   if ( !aLayerL.FindAttribute (XCAFDoc::LayerRefGUID(), FGNode) ) return Standard_False;
290   ChGNode->UnSetFather(FGNode);
291   return Standard_True;
292 }
293
294 //=======================================================================
295 //function : IsSet
296 //purpose  : 
297 //=======================================================================
298
299 Standard_Boolean XCAFDoc_LayerTool::IsSet(const TDF_Label& L,
300                                           const TCollection_ExtendedString& aLayer) const
301 {
302   Handle(XCAFDoc_GraphNode) Node;
303   Handle(TDataStd_Name) aName;
304   TDF_Label lab;
305   if (L.FindAttribute(XCAFDoc::LayerRefGUID(), Node) && (Node->NbFathers() != 0 ) ) {
306     Standard_Integer i = 1;
307     for (; i <= Node->NbFathers(); i++) {
308       lab = Node->GetFather(i)->Label();
309       if (lab.FindAttribute(TDataStd_Name::GetID(), aName) && ( aName->Get().IsEqual(aLayer) ) )
310         return Standard_True;
311     }
312   }
313   return Standard_False;
314 }
315
316
317 //=======================================================================
318 //function : IsSet
319 //purpose  : 
320 //=======================================================================
321
322 Standard_Boolean XCAFDoc_LayerTool::IsSet(const TDF_Label& L,
323                                           const TDF_Label& aLayerL) const
324 {
325   Handle(XCAFDoc_GraphNode) Node;
326   Handle(TDataStd_Name) aName;
327   TDF_Label lab;
328   if (L.FindAttribute(XCAFDoc::LayerRefGUID(), Node) && (Node->NbFathers() != 0 ) ) {
329     Standard_Integer i = 1;
330     for (; i <= Node->NbFathers(); i++) {
331       lab = Node->GetFather(i)->Label();
332       if (lab == aLayerL)
333         return Standard_True;
334     }
335   }
336   return Standard_False;
337 }
338
339 //=======================================================================
340 //function : GetLayers
341 //purpose  : 
342 //=======================================================================
343
344 Standard_Boolean XCAFDoc_LayerTool::GetLayers(const TDF_Label& L,
345                                               Handle(TColStd_HSequenceOfExtendedString)& aLayerS) 
346 {
347   aLayerS = GetLayers(L);
348   return (aLayerS->Length() != 0);
349 }
350
351 //=======================================================================
352 //function : GetLayers
353 //purpose  : 
354 //=======================================================================
355
356 Standard_Boolean XCAFDoc_LayerTool::GetLayers(const TDF_Label& L,
357                                               TDF_LabelSequence& aLayerLS) 
358 {
359   aLayerLS.Clear();
360   Handle(XCAFDoc_GraphNode) aGNode;
361   if ( L.FindAttribute( XCAFDoc::LayerRefGUID(), aGNode) ) {
362     for (Standard_Integer i = 1; i <= aGNode->NbFathers(); i++) {
363       aLayerLS.Append (aGNode->GetFather(i)->Label());
364     }
365   }
366   return aLayerLS.Length() > 0;
367 }
368
369 //=======================================================================
370 //function : GetLayers
371 //purpose  : 
372 //=======================================================================
373
374 Handle(TColStd_HSequenceOfExtendedString) XCAFDoc_LayerTool::GetLayers(const TDF_Label& L) 
375 {
376   Handle(TColStd_HSequenceOfExtendedString) aLayerS = new TColStd_HSequenceOfExtendedString;
377   TDF_LabelSequence aLayerLS;
378   if ( GetLayers( L, aLayerLS ) ) {
379     for (Standard_Integer i = 1; i <= aLayerLS.Length(); ++i) {
380       const TDF_Label& aLab = aLayerLS(i);
381       Handle(TDataStd_Name) aName;
382       if ( aLab.FindAttribute( TDataStd_Name::GetID(), aName ) ) {
383         aLayerS->Append( aName->Get() );
384       }
385     }
386   }
387   return aLayerS;
388 }
389
390 //=======================================================================
391 //function : GetShapesOfLayer
392 //purpose  : 
393 //=======================================================================
394
395 void XCAFDoc_LayerTool::GetShapesOfLayer(const TDF_Label& layerL,
396                                          TDF_LabelSequence& ShLabels) const
397 {
398   ShLabels.Clear();
399   Handle(XCAFDoc_GraphNode) aGNode;
400   if ( layerL.FindAttribute( XCAFDoc::LayerRefGUID(), aGNode) ) {
401     for (Standard_Integer i = 1; i <= aGNode->NbChildren(); i++) {
402       ShLabels.Append( aGNode->GetChild(i)->Label() );
403     }
404   }
405 }
406
407
408 //=======================================================================
409 //function : IsVisible
410 //purpose  : 
411 //=======================================================================
412
413 Standard_Boolean XCAFDoc_LayerTool::IsVisible (const TDF_Label& layerL) const
414 {
415   Handle(TDataStd_UAttribute) aUAttr;
416   return (!layerL.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr));
417 }
418
419
420 //=======================================================================
421 //function : SetVisibility
422 //purpose  : 
423 //=======================================================================
424
425 void XCAFDoc_LayerTool::SetVisibility (const TDF_Label& layerL,
426                                        const Standard_Boolean isvisible) const
427 {
428   Handle(TDataStd_UAttribute) aUAttr;
429   if (! isvisible ) {
430     if (!layerL.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr)) {
431       aUAttr->Set( layerL, XCAFDoc::InvisibleGUID() );
432     }
433   }
434   else layerL.ForgetAttribute( XCAFDoc::InvisibleGUID() );
435 }
436        
437
438 //=======================================================================
439 //function : SetLayer
440 //purpose  : 
441 //=======================================================================
442
443 Standard_Boolean XCAFDoc_LayerTool::SetLayer(const TopoDS_Shape& Sh,
444                                              const TDF_Label& LayerL, 
445                                              const Standard_Boolean shapeInOneLayer)
446 {
447   TDF_Label aLab;
448 //   if (! myShapeTool->FindShape(Sh, aLab) ) return Standard_False;
449   // PTV 22.01.2003 set layer for shape with location if it is necessary
450   if (! myShapeTool->Search( Sh, aLab ) ) return Standard_False;
451   SetLayer(aLab, LayerL, shapeInOneLayer);
452   return Standard_True;
453 }
454
455
456 //=======================================================================
457 //function : SetLayer
458 //purpose  : 
459 //=======================================================================
460
461 Standard_Boolean XCAFDoc_LayerTool::SetLayer(const TopoDS_Shape& Sh,
462                                              const TCollection_ExtendedString& aLayer,
463                                              const Standard_Boolean shapeInOneLayer)
464 {
465   TDF_Label aLayerL = AddLayer(aLayer);
466   return SetLayer(Sh, aLayerL, shapeInOneLayer);
467 }
468
469
470 //=======================================================================
471 //function : UnSetLayers
472 //purpose  : 
473 //=======================================================================
474
475 Standard_Boolean XCAFDoc_LayerTool::UnSetLayers(const TopoDS_Shape& Sh) 
476 {
477   TDF_Label aLab;
478   if (! myShapeTool->FindShape(Sh, aLab) ) return Standard_False;
479   UnSetLayers(aLab);
480   return Standard_True;
481 }
482
483
484 //=======================================================================
485 //function : UnSetOneLayer
486 //purpose  : 
487 //=======================================================================
488
489 Standard_Boolean XCAFDoc_LayerTool::UnSetOneLayer(const TopoDS_Shape& Sh,
490                                                   const TCollection_ExtendedString& aLayer)
491 {
492   TDF_Label aLab;
493   if (! myShapeTool->FindShape(Sh, aLab) ) return Standard_False;
494   return UnSetOneLayer(aLab, aLayer);
495 }
496
497 //=======================================================================
498 //function : UnSetOneLayer
499 //purpose  : 
500 //=======================================================================
501
502 Standard_Boolean XCAFDoc_LayerTool::UnSetOneLayer(const TopoDS_Shape& Sh,
503                                                   const TDF_Label& aLayerL)
504 {
505   TDF_Label aLab;
506   if (! myShapeTool->FindShape(Sh, aLab) ) return Standard_False;
507   return UnSetOneLayer(aLab, aLayerL);
508 }
509
510 //=======================================================================
511 //function : IsSet
512 //purpose  : 
513 //=======================================================================
514
515 Standard_Boolean XCAFDoc_LayerTool::IsSet(const TopoDS_Shape& Sh,
516                                           const TCollection_ExtendedString& aLayer) 
517 {
518   TDF_Label aLab;
519   if (! myShapeTool->FindShape(Sh, aLab) ) return Standard_False;
520   return IsSet(aLab, aLayer);
521 }
522
523
524 //=======================================================================
525 //function : IsSet
526 //purpose  : 
527 //=======================================================================
528
529 Standard_Boolean XCAFDoc_LayerTool::IsSet(const TopoDS_Shape& Sh,
530                                           const TDF_Label& aLayerL) 
531 {
532   TDF_Label aLab;
533   if (! myShapeTool->FindShape(Sh, aLab) ) return Standard_False;
534   return IsSet(aLab, aLayerL);
535 }
536
537 //=======================================================================
538 //function : GetLayers
539 //purpose  : 
540 //=======================================================================
541
542 Standard_Boolean XCAFDoc_LayerTool::GetLayers(const TopoDS_Shape& Sh,
543                                               Handle(TColStd_HSequenceOfExtendedString)& aLayerS) 
544 {
545   TDF_Label aLab;
546   if (! myShapeTool->FindShape(Sh, aLab) ) return Standard_False;
547   return GetLayers(aLab, aLayerS);
548 }
549
550 //=======================================================================
551 //function : GetLayers
552 //purpose  : 
553 //=======================================================================
554
555 Standard_Boolean XCAFDoc_LayerTool::GetLayers(const TopoDS_Shape& Sh,
556                                               TDF_LabelSequence& aLayerLS) 
557 {
558   TDF_Label aLab;
559   if (! myShapeTool->FindShape(Sh, aLab) ) return Standard_False;
560   return GetLayers(aLab, aLayerLS);
561 }
562
563
564 //=======================================================================
565 //function : GetLayers
566 //purpose  : 
567 //=======================================================================
568
569 Handle(TColStd_HSequenceOfExtendedString) XCAFDoc_LayerTool::GetLayers(const TopoDS_Shape& Sh) 
570 {
571   Handle(TColStd_HSequenceOfExtendedString) aLayerS = new TColStd_HSequenceOfExtendedString;
572   TDF_Label aLab;
573   if ( myShapeTool->FindShape(Sh, aLab) )
574     aLayerS = GetLayers(aLab);
575   return aLayerS;
576 }
577
578
579 //=======================================================================
580 //function : ID
581 //purpose  : 
582 //=======================================================================
583
584 const Standard_GUID& XCAFDoc_LayerTool::ID() const
585 {
586   return GetID();
587 }
588
589
590 //=======================================================================
591 //function : Restore
592 //purpose  : 
593 //=======================================================================
594
595 void XCAFDoc_LayerTool::Restore(const Handle(TDF_Attribute)& /* with */) 
596 {
597 }
598
599
600 //=======================================================================
601 //function : NewEmpty
602 //purpose  : 
603 //=======================================================================
604
605 Handle(TDF_Attribute) XCAFDoc_LayerTool::NewEmpty() const
606 {
607   return new XCAFDoc_LayerTool;
608 }
609
610
611 //=======================================================================
612 //function : Paste
613 //purpose  : 
614 //=======================================================================
615
616 void XCAFDoc_LayerTool::Paste(const Handle(TDF_Attribute)& /* into */,
617                               const Handle(TDF_RelocationTable)& /* RT */) const
618 {
619 }
620