0026411: Necessary to improve XCAF to store and querying GD&T data.
[occt.git] / src / XCAFDoc / XCAFDoc_DimTolTool.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Precision.hxx>
16 #include <Standard_GUID.hxx>
17 #include <Standard_Type.hxx>
18 #include <TCollection_HAsciiString.hxx>
19 #include <TDataStd_Name.hxx>
20 #include <TDataStd_TreeNode.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TDF_ChildIDIterator.hxx>
23 #include <TDF_Label.hxx>
24 #include <TDF_RelocationTable.hxx>
25 #include <XCAFDoc.hxx>
26 #include <XCAFDoc_Dimension.hxx>
27 #include <XCAFDoc_GeomTolerance.hxx>
28 #include <XCAFDoc_Datum.hxx>
29 #include <XCAFDoc_DimTol.hxx>
30 #include <XCAFDoc_DimTolTool.hxx>
31 #include <XCAFDoc_DocumentTool.hxx>
32 #include <XCAFDoc_GraphNode.hxx>
33 #include <XCAFDoc_ShapeTool.hxx>
34
35 //=======================================================================
36 //function : XCAFDoc_DimTolTool
37 //purpose  : 
38 //=======================================================================
39 XCAFDoc_DimTolTool::XCAFDoc_DimTolTool()
40 {
41 }
42
43
44 //=======================================================================
45 //function : Set
46 //purpose  : 
47 //=======================================================================
48
49 Handle(XCAFDoc_DimTolTool) XCAFDoc_DimTolTool::Set(const TDF_Label& L) 
50 {
51   Handle(XCAFDoc_DimTolTool) A;
52   if (!L.FindAttribute (XCAFDoc_DimTolTool::GetID(), A)) {
53     A = new XCAFDoc_DimTolTool ();
54     L.AddAttribute(A);
55     A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
56   }
57   return A;
58 }
59
60
61 //=======================================================================
62 //function : GetID
63 //purpose  : 
64 //=======================================================================
65
66 const Standard_GUID& XCAFDoc_DimTolTool::GetID() 
67 {
68   static Standard_GUID DGTTblID ("72afb19b-44de-11d8-8776-001083004c77");
69   return DGTTblID; 
70 }
71
72
73 //=======================================================================
74 //function : BaseLabel
75 //purpose  : 
76 //=======================================================================
77
78 TDF_Label XCAFDoc_DimTolTool::BaseLabel() const
79 {
80   return Label();
81 }
82
83
84 //=======================================================================
85 //function : ShapeTool
86 //purpose  : 
87 //=======================================================================
88
89 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_DimTolTool::ShapeTool() 
90 {
91   if(myShapeTool.IsNull())
92     myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
93   return myShapeTool;
94 }
95
96
97 //=======================================================================
98 //function : IsDimTol
99 //purpose  : 
100 //=======================================================================
101
102 Standard_Boolean XCAFDoc_DimTolTool::IsDimTol(const TDF_Label& theDimTolL) const
103 {
104   Handle(XCAFDoc_DimTol) aDimTolAttr;
105   if(theDimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),aDimTolAttr)) {
106     return Standard_True;
107   }
108   return Standard_False;
109 }
110
111 //=======================================================================
112 //function : IsDimension
113 //purpose  : 
114 //=======================================================================
115
116 Standard_Boolean XCAFDoc_DimTolTool::IsDimension(const TDF_Label& theDimTolL) const
117 {
118   Handle(XCAFDoc_Dimension) aDimTolAttr;
119   if(theDimTolL.FindAttribute(XCAFDoc_Dimension::GetID(),aDimTolAttr)) {
120     return Standard_True;
121   }
122   return Standard_False;
123 }
124
125 //=======================================================================
126 //function : IsGeomTolerance
127 //purpose  : 
128 //=======================================================================
129
130 Standard_Boolean XCAFDoc_DimTolTool::IsGeomTolerance(const TDF_Label& theDimTolL) const
131 {
132   Handle(XCAFDoc_GeomTolerance) aDimTolAttr;
133   if(theDimTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(),aDimTolAttr)) {
134     return Standard_True;
135   }
136   return Standard_False;
137 }
138
139 //=======================================================================
140 //function : GetDimTolLabels
141 //purpose  : 
142 //=======================================================================
143
144 void XCAFDoc_DimTolTool::GetDimTolLabels(TDF_LabelSequence& theLabels) const
145 {
146   theLabels.Clear();
147   TDF_ChildIterator aChildIterator( Label() ); 
148   for (; aChildIterator.More(); aChildIterator.Next()) {
149     TDF_Label aL = aChildIterator.Value();
150     if ( IsDimTol(aL)) theLabels.Append(aL);
151   }
152 }
153
154 //=======================================================================
155 //function : GetDimensionLabels
156 //purpose  : 
157 //=======================================================================
158
159 void XCAFDoc_DimTolTool::GetDimensionLabels(TDF_LabelSequence& theLabels) const
160 {
161   theLabels.Clear();
162   TDF_ChildIterator aChildIterator( Label() ); 
163   for (; aChildIterator.More(); aChildIterator.Next()) {
164     TDF_Label aL = aChildIterator.Value();
165     if ( IsDimension(aL)) theLabels.Append(aL);
166   }
167 }
168
169 //=======================================================================
170 //function : GetGeomToleranceLabels
171 //purpose  : 
172 //=======================================================================
173
174 void XCAFDoc_DimTolTool::GetGeomToleranceLabels(TDF_LabelSequence& theLabels) const
175 {
176   theLabels.Clear();
177   TDF_ChildIterator aChildIterator( Label() ); 
178   for (; aChildIterator.More(); aChildIterator.Next()) {
179     TDF_Label aL = aChildIterator.Value();
180     if ( IsGeomTolerance(aL)) theLabels.Append(aL);
181   }
182 }
183
184 //=======================================================================
185 //function : FindDimTol
186 //purpose  : 
187 //=======================================================================
188
189 Standard_Boolean XCAFDoc_DimTolTool::FindDimTol(const Standard_Integer kind,
190                                                 const Handle(TColStd_HArray1OfReal)& aVal,
191                                                 const Handle(TCollection_HAsciiString)& aName,
192                                                 const Handle(TCollection_HAsciiString)& aDescription,
193                                                 TDF_Label& lab) const
194 {
195   TDF_ChildIDIterator it(Label(),XCAFDoc_DimTol::GetID());
196   for(; it.More(); it.Next()) {
197     TDF_Label DimTolL = it.Value()->Label();
198     Handle(XCAFDoc_DimTol) DimTolAttr;
199     if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) continue;
200     Standard_Integer kind1 = DimTolAttr->GetKind();
201     Handle(TColStd_HArray1OfReal) aVal1 = DimTolAttr->GetVal();
202     Handle(TCollection_HAsciiString) aName1 = DimTolAttr->GetName();
203     Handle(TCollection_HAsciiString) aDescription1 = DimTolAttr->GetDescription();
204     Standard_Boolean IsEqual = Standard_True;
205     if(!(kind1==kind)) continue;
206     if(!(aName==aName1)) continue;
207     if(!(aDescription==aDescription1)) continue;
208     if(kind<20) {  //dimension
209       for(Standard_Integer i=1; i<=aVal->Length(); i++) {
210         if(Abs(aVal->Value(i)-aVal1->Value(i))>Precision::Confusion())
211           IsEqual = Standard_False;
212       }
213     }
214     else if(kind<50) { //tolerance
215       if(Abs(aVal->Value(1)-aVal1->Value(1))>Precision::Confusion())
216         IsEqual = Standard_False;
217     }
218     if(IsEqual) {
219       lab = DimTolL;
220       return Standard_True;
221     }
222   }
223   return Standard_False;
224 }
225
226
227 //=======================================================================
228 //function : FindDimTol
229 //purpose  : 
230 //=======================================================================
231
232 TDF_Label XCAFDoc_DimTolTool::FindDimTol(const Standard_Integer kind,
233                                          const Handle(TColStd_HArray1OfReal)& aVal,
234                                          const Handle(TCollection_HAsciiString)& aName,
235                                          const Handle(TCollection_HAsciiString)& aDescription) const
236 {
237   TDF_Label L;
238   FindDimTol(kind,aVal,aName,aDescription,L);
239   return L;
240 }
241
242
243 //=======================================================================
244 //function : AddDimTol
245 //purpose  : 
246 //=======================================================================
247
248 TDF_Label XCAFDoc_DimTolTool::AddDimTol(const Standard_Integer kind,
249                                         const Handle(TColStd_HArray1OfReal)& aVal,
250                                         const Handle(TCollection_HAsciiString)& aName,
251                                         const Handle(TCollection_HAsciiString)& aDescription) const
252 {
253   TDF_Label DimTolL;
254   TDF_TagSource aTag;
255   DimTolL = aTag.NewChild ( Label() );
256   XCAFDoc_DimTol::Set(DimTolL,kind,aVal,aName,aDescription);
257   TCollection_AsciiString str = "DGT:";
258   if(kind<20) str.AssignCat("Dimension");
259   else str.AssignCat("Tolerance");
260   TDataStd_Name::Set(DimTolL,str);
261   return DimTolL;
262 }
263
264 //=======================================================================
265 //function : AddDimension
266 //purpose  : 
267 //=======================================================================
268
269 TDF_Label XCAFDoc_DimTolTool::AddDimension()
270 {
271   TDF_Label aDimTolL;
272   TDF_TagSource aTag;
273   aDimTolL = aTag.NewChild ( Label() );
274   Handle(XCAFDoc_Dimension) aDim = XCAFDoc_Dimension::Set(aDimTolL);
275   TCollection_AsciiString aStr = "DGT:Dimension";
276   TDataStd_Name::Set(aDimTolL,aStr);
277   return aDimTolL;
278 }
279
280 //=======================================================================
281 //function : AddGeomTolerance
282 //purpose  : 
283 //=======================================================================
284
285 TDF_Label XCAFDoc_DimTolTool::AddGeomTolerance()
286 {
287   TDF_Label aDimTolL;
288   TDF_TagSource aTag;
289   aDimTolL = aTag.NewChild ( Label() );
290   Handle(XCAFDoc_GeomTolerance) aTol = XCAFDoc_GeomTolerance::Set(aDimTolL);
291   TCollection_AsciiString aStr = "DGT:Tolerance";
292   TDataStd_Name::Set(aDimTolL,aStr);
293   return aDimTolL;
294 }
295
296 //=======================================================================
297 //function : SetDimension
298 //purpose  : 
299 //=======================================================================
300
301 void XCAFDoc_DimTolTool::SetDimension(const TDF_Label& theL,
302                                    const TDF_Label& theDimTolL) const
303 {
304   TDF_Label nullLab;
305   SetDimension(theL, nullLab, theDimTolL);
306 }
307
308 //=======================================================================
309 //function : SetDimension
310 //purpose  : 
311 //=======================================================================
312
313 void XCAFDoc_DimTolTool::SetDimension(const TDF_Label& theFirstL,
314                                    const TDF_Label& theSecondL,
315                                    const TDF_Label& theDimTolL) const
316 {
317   Handle(XCAFDoc_GraphNode) aChGNode;
318   Handle(XCAFDoc_GraphNode) aFGNode;
319   Handle(XCAFDoc_GraphNode) aSecondFGNode;
320
321   //Handle(XCAFDoc_GraphNode) ChGNode, FGNode;
322   if ( theDimTolL.FindAttribute (XCAFDoc::DimensionRefGUID(), aChGNode) ) {
323     while (aChGNode->NbFathers() > 0) {
324       aFGNode = aChGNode->GetFather(1);
325       aFGNode->UnSetChild(aChGNode);
326     }
327     theDimTolL.ForgetAttribute ( XCAFDoc::DimTolRefGUID() );
328   }
329
330   if(!IsDimension(theDimTolL))
331   {
332     return;
333   }
334
335   if (!theDimTolL.FindAttribute(XCAFDoc::DimensionRefGUID(), aChGNode) ) {
336     aChGNode = new XCAFDoc_GraphNode;
337     aChGNode = XCAFDoc_GraphNode::Set(theDimTolL);
338   }
339   if (!theFirstL.FindAttribute(XCAFDoc::DimensionRefGUID(), aFGNode) ) {
340     aFGNode = new XCAFDoc_GraphNode;
341     aFGNode = XCAFDoc_GraphNode::Set(theFirstL);
342   }
343   aFGNode->SetGraphID(XCAFDoc::DimensionRefGUID());
344   aChGNode->SetGraphID(XCAFDoc::DimensionRefGUID());
345   aFGNode->SetChild(aChGNode);
346   aChGNode->SetFather(aFGNode);
347
348   if (!theSecondL.IsNull()){
349     if(!theSecondL.FindAttribute(XCAFDoc::DimensionRefGUID(), aSecondFGNode) ) {
350       aSecondFGNode = new XCAFDoc_GraphNode;
351       aSecondFGNode = XCAFDoc_GraphNode::Set(theSecondL);
352     }
353     aSecondFGNode->SetGraphID(XCAFDoc::DimensionRefGUID());
354     aSecondFGNode->SetChild(aChGNode);
355     aChGNode->SetFather(aSecondFGNode);
356   }
357 }
358
359 //=======================================================================
360 //function : SetGeomTolerance
361 //purpose  : 
362 //=======================================================================
363
364 void XCAFDoc_DimTolTool::SetGeomTolerance(const TDF_Label& theL,
365                                    const TDF_Label& theDimTolL) const
366 {
367   // set reference
368   Handle(TDataStd_TreeNode) refNode, mainNode;
369   refNode = TDataStd_TreeNode::Set ( theDimTolL, XCAFDoc::GeomToleranceRefGUID() );
370   mainNode  = TDataStd_TreeNode::Set ( theL,       XCAFDoc::GeomToleranceRefGUID() );
371   refNode->Remove(); // abv: fix against bug in TreeNode::Append()
372   mainNode->Append(refNode);
373 }
374
375 //=======================================================================
376 //function : SetDimTol
377 //purpose  : 
378 //=======================================================================
379
380 void XCAFDoc_DimTolTool::SetDimTol(const TDF_Label& theL,
381                                    const TDF_Label& theDimTolL) const
382 {
383   // set reference
384   Handle(TDataStd_TreeNode) refNode, mainNode;
385   refNode = TDataStd_TreeNode::Set ( theDimTolL, XCAFDoc::DimTolRefGUID() );
386   mainNode  = TDataStd_TreeNode::Set (theL,       XCAFDoc::DimTolRefGUID() );
387   refNode->Remove(); // abv: fix against bug in TreeNode::Append()
388   mainNode->Append(refNode);
389 }
390
391
392 //=======================================================================
393 //function : SetDimTol
394 //purpose  : 
395 //=======================================================================
396
397 TDF_Label XCAFDoc_DimTolTool::SetDimTol(const TDF_Label& L,
398                                         const Standard_Integer kind,
399                                         const Handle(TColStd_HArray1OfReal)& aVal,
400                                         const Handle(TCollection_HAsciiString)& aName,
401                                         const Handle(TCollection_HAsciiString)& aDescription) const
402 {
403   TDF_Label DimTolL = AddDimTol(kind,aVal,aName,aDescription);
404   SetDimTol(L,DimTolL);
405   return DimTolL;
406 }
407
408
409 //=======================================================================
410 //function : GetRefShapeLabel
411 //purpose  : 
412 //=======================================================================
413
414 Standard_Boolean XCAFDoc_DimTolTool::GetRefShapeLabel(const TDF_Label& theL,
415                                                       TDF_LabelSequence& theShapeL) const
416 {
417   theShapeL.Clear();
418   Handle(TDataStd_TreeNode) aNode;
419   if( !theL.FindAttribute(XCAFDoc::DimTolRefGUID(),aNode) || !aNode->HasFather() ) {
420     if( !theL.FindAttribute(XCAFDoc::DatumRefGUID(),aNode) || !aNode->HasFather() ) {
421       if( !theL.FindAttribute(XCAFDoc::GeomToleranceRefGUID(),aNode) || !aNode->HasFather() ) {
422         Handle(XCAFDoc_GraphNode) aGNode;
423         if( theL.FindAttribute(XCAFDoc::DimensionRefGUID(),aGNode) && aGNode->NbFathers() > 0 ) {
424           for(Standard_Integer i = 1; i <= aGNode->NbFathers(); i++)
425           {
426             theShapeL.Append(aGNode->GetFather(i)->Label());
427           }
428           return Standard_True;
429         }
430         return Standard_False;
431       }
432     }
433   }
434   theShapeL.Append(aNode->Father()->Label());
435   return Standard_True;
436 }
437
438 //=======================================================================
439 //function : GetRefDimensionLabels
440 //purpose  : 
441 //=======================================================================
442
443 Standard_Boolean XCAFDoc_DimTolTool::GetRefDimensionLabels(const TDF_Label& theShapeL,
444                                                      TDF_LabelSequence& theDimTols) const
445 {
446   Handle(XCAFDoc_GraphNode) aGNode;
447   if( theShapeL.FindAttribute(XCAFDoc::DimensionRefGUID(),aGNode) && aGNode->NbChildren() > 0 ) {
448     for(Standard_Integer i = 1; i <= aGNode->NbChildren(); i++)
449     {
450       theDimTols.Append(aGNode->GetChild(i)->Label());
451     }
452     return Standard_True;
453   }
454   return Standard_False;
455 }
456
457 //=======================================================================
458 //function : GetRefGeomToleranceLabels
459 //purpose  : 
460 //=======================================================================
461
462 Standard_Boolean XCAFDoc_DimTolTool::GetRefGeomToleranceLabels(const TDF_Label& theShapeL,
463                                                      TDF_LabelSequence& theDimTols) const
464 {
465   Handle(TDataStd_TreeNode) aNode;
466   if( !theShapeL.FindAttribute(XCAFDoc::GeomToleranceRefGUID(),aNode) ||
467        !aNode->HasFirst() ) {
468     return Standard_False;
469   }
470   Handle(TDataStd_TreeNode) aLast = aNode->First();
471   theDimTols.Append(aLast->Label());
472   while(aLast->HasNext()) {
473     aLast = aLast->Next();
474     theDimTols.Append(aLast->Label());
475   }
476   return Standard_True;
477 }
478
479 //=======================================================================
480 //function : GetRefDatumLabel
481 //purpose  : 
482 //=======================================================================
483
484 Standard_Boolean XCAFDoc_DimTolTool::GetRefDatumLabel(const TDF_Label& theShapeL,
485                                                      TDF_Label& theDatum) const
486 {
487   Handle(TDataStd_TreeNode) aNode;
488   if( !theShapeL.FindAttribute(XCAFDoc::DatumRefGUID(),aNode) ||
489        !aNode->HasFirst() ) {
490     return Standard_False;
491   }
492   Handle(TDataStd_TreeNode) aLast = aNode->First();
493   theDatum = aLast->Label();
494   return Standard_True;
495 }
496
497 //=======================================================================
498 //function : GetDimTol
499 //purpose  : 
500 //=======================================================================
501
502 Standard_Boolean XCAFDoc_DimTolTool::GetDimTol(const TDF_Label& DimTolL,
503                                                Standard_Integer& kind,
504                                                Handle(TColStd_HArray1OfReal)& aVal,
505                                                Handle(TCollection_HAsciiString)& aName,
506                                                Handle(TCollection_HAsciiString)& aDescription) const
507 {
508   Handle(XCAFDoc_DimTol) DimTolAttr;
509   if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) {
510     return Standard_False;
511   }
512   kind = DimTolAttr->GetKind();
513   aVal = DimTolAttr->GetVal();
514   aName = DimTolAttr->GetName();
515   aDescription = DimTolAttr->GetDescription();
516   
517   return Standard_True;
518 }
519
520
521 //=======================================================================
522 //function : IsDatum
523 //purpose  : 
524 //=======================================================================
525
526 Standard_Boolean XCAFDoc_DimTolTool::IsDatum(const TDF_Label& theDimTolL) const
527 {
528   Handle(XCAFDoc_Datum) aDatumAttr;
529   if(theDimTolL.FindAttribute(XCAFDoc_Datum::GetID(),aDatumAttr)) {
530     return Standard_True;
531   }
532   return Standard_False;
533 }
534
535
536 //=======================================================================
537 //function : GetDatumLabels
538 //purpose  : 
539 //=======================================================================
540
541 void XCAFDoc_DimTolTool::GetDatumLabels(TDF_LabelSequence& theLabels) const
542 {
543   theLabels.Clear();
544   TDF_ChildIterator aChildIterator( Label() ); 
545   for (; aChildIterator.More(); aChildIterator.Next()) {
546     TDF_Label L = aChildIterator.Value();
547     if ( IsDatum(L)) theLabels.Append(L);
548   }
549 }
550
551 //=======================================================================
552 //function : FindDatum
553 //purpose  : 
554 //=======================================================================
555
556 Standard_Boolean XCAFDoc_DimTolTool::FindDatum(const Handle(TCollection_HAsciiString)& aName,
557                                                const Handle(TCollection_HAsciiString)& aDescription,
558                                                const Handle(TCollection_HAsciiString)& anIdentification,
559                                                TDF_Label& lab) const
560 {
561   TDF_ChildIDIterator it(Label(),XCAFDoc_Datum::GetID());
562   for(; it.More(); it.Next()) {
563     Handle(TCollection_HAsciiString) aName1, aDescription1, anIdentification1;
564     TDF_Label aLabel = it.Value()->Label();
565     if ( !GetDatum( aLabel, aName1, aDescription1, anIdentification1 ) )
566       continue;
567     if(!(aName==aName1)) continue;
568     if(!(aDescription==aDescription1)) continue;
569     if(!(anIdentification==anIdentification1)) continue;
570     lab = aLabel;
571     return Standard_True;
572   }
573   return Standard_False;
574 }
575
576
577 //=======================================================================
578 //function : AddDatum
579 //purpose  : 
580 //=======================================================================
581
582 TDF_Label XCAFDoc_DimTolTool::AddDatum(const Handle(TCollection_HAsciiString)& aName,
583                                        const Handle(TCollection_HAsciiString)& aDescription,
584                                        const Handle(TCollection_HAsciiString)& anIdentification) const
585 {
586   TDF_Label DatumL;
587   TDF_TagSource aTag;
588   DatumL = aTag.NewChild ( Label() );
589   XCAFDoc_Datum::Set(DatumL,aName,aDescription,anIdentification);
590   TDataStd_Name::Set(DatumL,"DGT:Datum");
591   return DatumL;
592 }
593
594 //=======================================================================
595 //function : AddDatum
596 //purpose  : 
597 //=======================================================================
598
599 TDF_Label XCAFDoc_DimTolTool::AddDatum()
600 {
601   TDF_Label aDatumL;
602   TDF_TagSource aTag;
603   aDatumL = aTag.NewChild ( Label() );
604   Handle(XCAFDoc_Datum) aDat = XCAFDoc_Datum::Set(aDatumL);
605   TDataStd_Name::Set(aDatumL,"DGT:Datum");
606   return aDatumL;
607 }
608
609 //=======================================================================
610 //function : SetDatum
611 //purpose  : 
612 //=======================================================================
613
614 void XCAFDoc_DimTolTool::SetDatum(const TDF_Label& theL,
615                                   const TDF_Label& theDatumL) const
616 {
617   // set reference
618   Handle(TDataStd_TreeNode) refNode, mainNode;
619   refNode = TDataStd_TreeNode::Set ( theDatumL, XCAFDoc::DatumRefGUID() );
620   mainNode  = TDataStd_TreeNode::Set ( theL, XCAFDoc::DatumRefGUID() );
621   refNode->Remove();
622   mainNode->Append(refNode);
623 }
624
625 //=======================================================================
626 //function : SetDatum
627 //purpose  : 
628 //=======================================================================
629
630 void XCAFDoc_DimTolTool::SetDatum(const TDF_Label& L,
631                                   const TDF_Label& TolerL,
632                                   const Handle(TCollection_HAsciiString)& aName,
633                                   const Handle(TCollection_HAsciiString)& aDescription,
634                                   const Handle(TCollection_HAsciiString)& anIdentification) const
635 {
636   TDF_Label DatumL;
637   if(!FindDatum(aName,aDescription,anIdentification,DatumL))
638     DatumL = AddDatum(aName,aDescription,anIdentification);
639   SetDatum(L,DatumL);
640   // set reference
641   Handle(XCAFDoc_GraphNode) FGNode;
642   Handle(XCAFDoc_GraphNode) ChGNode;
643   if (! TolerL.FindAttribute( XCAFDoc::DatumTolRefGUID(), FGNode) ) {
644     FGNode = new XCAFDoc_GraphNode;
645     FGNode = XCAFDoc_GraphNode::Set(TolerL);
646   }
647   if (! DatumL.FindAttribute( XCAFDoc::DatumTolRefGUID(), ChGNode) ) {
648     ChGNode = new XCAFDoc_GraphNode;
649     ChGNode = XCAFDoc_GraphNode::Set(DatumL);
650   }
651   FGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
652   ChGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
653   FGNode->SetChild(ChGNode);
654   ChGNode->SetFather(FGNode);
655 }
656
657 //=======================================================================
658 //function : SetDatumToGeomTol
659 //purpose  : 
660 //=======================================================================
661
662 void XCAFDoc_DimTolTool::SetDatumToGeomTol(const TDF_Label& theDatumL,
663                                            const TDF_Label& theTolerL) const
664 {
665   // set reference
666   Handle(XCAFDoc_GraphNode) aFGNode;
667   Handle(XCAFDoc_GraphNode) aChGNode;
668   if (! theTolerL.FindAttribute( XCAFDoc::DatumTolRefGUID(), aFGNode) ) {
669     aFGNode = new XCAFDoc_GraphNode;
670     aFGNode = XCAFDoc_GraphNode::Set(theTolerL);
671   }
672   if (! theDatumL.FindAttribute( XCAFDoc::DatumTolRefGUID(), aChGNode) ) {
673     aChGNode = new XCAFDoc_GraphNode;
674     aChGNode = XCAFDoc_GraphNode::Set(theDatumL);
675   }
676   aFGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
677   aChGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
678   aFGNode->SetChild(aChGNode);
679   aChGNode->SetFather(aFGNode);
680 }
681
682 //=======================================================================
683 //function : GetDatum
684 //purpose  : 
685 //=======================================================================
686
687 Standard_Boolean XCAFDoc_DimTolTool::GetDatum(const TDF_Label& theDatumL,
688                                               Handle(TCollection_HAsciiString)& theName,
689                                               Handle(TCollection_HAsciiString)& theDescription,
690                                               Handle(TCollection_HAsciiString)& theIdentification) const
691 {
692   Handle(XCAFDoc_Datum) aDatumAttr;
693   if( theDatumL.IsNull() || 
694       !theDatumL.FindAttribute(XCAFDoc_Datum::GetID(),aDatumAttr) )
695     return Standard_False;
696   
697   theName = aDatumAttr->GetName();
698   theDescription = aDatumAttr->GetDescription();
699   theIdentification = aDatumAttr->GetIdentification();
700   return Standard_True;
701 }
702
703 //=======================================================================
704 //function : GetDatumTolerLabels
705 //purpose  : 
706 //=======================================================================
707
708 Standard_Boolean XCAFDoc_DimTolTool::GetDatumOfTolerLabels(const TDF_Label& theDimTolL,
709                                                          TDF_LabelSequence& theDatums) const
710 {
711   Handle(XCAFDoc_GraphNode) aNode;
712   if( !theDimTolL.FindAttribute(XCAFDoc::DatumTolRefGUID(),aNode) )
713     return Standard_False;
714   for(Standard_Integer i=1; i<=aNode->NbChildren(); i++) {
715     Handle(XCAFDoc_GraphNode) aDatumNode = aNode->GetChild(i);
716     theDatums.Append(aDatumNode->Label());
717   }
718   return Standard_True;
719 }
720
721 //=======================================================================
722 //function : GetTolerDatumLabels
723 //purpose  : 
724 //=======================================================================
725
726 Standard_Boolean XCAFDoc_DimTolTool::GetTolerOfDatumLabels(const TDF_Label& theDatumL,
727                                                          TDF_LabelSequence& theTols) const
728 {
729   Handle(XCAFDoc_GraphNode) aNode;
730   if( !theDatumL.FindAttribute(XCAFDoc::DatumTolRefGUID(),aNode) )
731     return Standard_False;
732   for(Standard_Integer i=1; i<=aNode->NbFathers(); i++) {
733     Handle(XCAFDoc_GraphNode) aDatumNode = aNode->GetFather(i);
734     theTols.Append(aDatumNode->Label());
735   }
736   return Standard_True;
737 }
738
739
740 //=======================================================================
741 //function : ID
742 //purpose  : 
743 //=======================================================================
744
745 const Standard_GUID& XCAFDoc_DimTolTool::ID() const
746 {
747   return GetID();
748 }
749
750
751 //=======================================================================
752 //function : Restore
753 //purpose  : 
754 //=======================================================================
755
756 void XCAFDoc_DimTolTool::Restore(const Handle(TDF_Attribute)& /*with*/) 
757 {
758 }
759
760
761 //=======================================================================
762 //function : NewEmpty
763 //purpose  : 
764 //=======================================================================
765
766 Handle(TDF_Attribute) XCAFDoc_DimTolTool::NewEmpty() const
767 {
768   return new XCAFDoc_DimTolTool;
769 }
770
771
772 //=======================================================================
773 //function : Paste
774 //purpose  : 
775 //=======================================================================
776
777 void XCAFDoc_DimTolTool::Paste(const Handle(TDF_Attribute)& /*into*/,
778                                const Handle(TDF_RelocationTable)& /*RT*/) const
779 {
780 }
781