0033041: Coding - get rid of unused headers [TopTools to Xw]
[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 #include <XCAFDoc_DimTolTool.hxx>
15
16 #include <Standard_Type.hxx>
17 #include <TColStd_MapOfAsciiString.hxx>
18 #include <TDataStd_Name.hxx>
19 #include <TDataStd_TreeNode.hxx>
20 #include <TDataStd_UAttribute.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TDF_ChildIDIterator.hxx>
23 #include <TDF_Label.hxx>
24 #include <TDF_RelocationTable.hxx>
25 #include <TDF_Tool.hxx>
26 #include <XCAFDimTolObjects_DatumObject.hxx>
27 #include <XCAFDimTolObjects_DimensionObject.hxx>
28 #include <XCAFDimTolObjects_GeomToleranceObject.hxx>
29 #include <XCAFDoc.hxx>
30 #include <XCAFDoc_Dimension.hxx>
31 #include <XCAFDoc_GeomTolerance.hxx>
32 #include <XCAFDoc_Datum.hxx>
33 #include <XCAFDoc_DimTol.hxx>
34 #include <XCAFDoc_DimTolTool.hxx>
35 #include <XCAFDoc_DocumentTool.hxx>
36 #include <XCAFDoc_GraphNode.hxx>
37 #include <XCAFDoc_ShapeTool.hxx>
38
39
40 IMPLEMENT_DERIVED_ATTRIBUTE_WITH_TYPE(XCAFDoc_DimTolTool,TDataStd_GenericEmpty,"xcaf","DimTolTool")
41
42 //=======================================================================
43 //function : XCAFDoc_DimTolTool
44 //purpose  : 
45 //=======================================================================
46 XCAFDoc_DimTolTool::XCAFDoc_DimTolTool()
47 {
48 }
49
50
51 //=======================================================================
52 //function : Set
53 //purpose  : 
54 //=======================================================================
55
56 Handle(XCAFDoc_DimTolTool) XCAFDoc_DimTolTool::Set(const TDF_Label& L) 
57 {
58   Handle(XCAFDoc_DimTolTool) A;
59   if (!L.FindAttribute (XCAFDoc_DimTolTool::GetID(), A)) {
60     A = new XCAFDoc_DimTolTool ();
61     L.AddAttribute(A);
62     A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
63   }
64   return A;
65 }
66
67
68 //=======================================================================
69 //function : GetID
70 //purpose  : 
71 //=======================================================================
72
73 const Standard_GUID& XCAFDoc_DimTolTool::GetID() 
74 {
75   static Standard_GUID DGTTblID ("72afb19b-44de-11d8-8776-001083004c77");
76   return DGTTblID; 
77 }
78
79
80 //=======================================================================
81 //function : BaseLabel
82 //purpose  : 
83 //=======================================================================
84
85 TDF_Label XCAFDoc_DimTolTool::BaseLabel() const
86 {
87   return Label();
88 }
89
90
91 //=======================================================================
92 //function : ShapeTool
93 //purpose  : 
94 //=======================================================================
95
96 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_DimTolTool::ShapeTool() 
97 {
98   if(myShapeTool.IsNull())
99     myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
100   return myShapeTool;
101 }
102
103
104 //=======================================================================
105 //function : IsDimTol
106 //purpose  : 
107 //=======================================================================
108
109 Standard_Boolean XCAFDoc_DimTolTool::IsDimTol(const TDF_Label& theDimTolL) const
110 {
111   Handle(XCAFDoc_DimTol) aDimTolAttr;
112   if(theDimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),aDimTolAttr)) {
113     return Standard_True;
114   }
115   return Standard_False;
116 }
117
118 //=======================================================================
119 //function : IsDimension
120 //purpose  : 
121 //=======================================================================
122
123 Standard_Boolean XCAFDoc_DimTolTool::IsDimension(const TDF_Label& theDimTolL) const
124 {
125   Handle(XCAFDoc_Dimension) aDimTolAttr;
126   if(theDimTolL.FindAttribute(XCAFDoc_Dimension::GetID(),aDimTolAttr)) {
127     return Standard_True;
128   }
129   return Standard_False;
130 }
131
132 //=======================================================================
133 //function : IsGeomTolerance
134 //purpose  : 
135 //=======================================================================
136
137 Standard_Boolean XCAFDoc_DimTolTool::IsGeomTolerance(const TDF_Label& theDimTolL) const
138 {
139   Handle(XCAFDoc_GeomTolerance) aDimTolAttr;
140   if(theDimTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(),aDimTolAttr)) {
141     return Standard_True;
142   }
143   return Standard_False;
144 }
145
146 //=======================================================================
147 //function : GetDimTolLabels
148 //purpose  : 
149 //=======================================================================
150
151 void XCAFDoc_DimTolTool::GetDimTolLabels(TDF_LabelSequence& theLabels) const
152 {
153   theLabels.Clear();
154   TDF_ChildIterator aChildIterator( Label() ); 
155   for (; aChildIterator.More(); aChildIterator.Next()) {
156     TDF_Label aL = aChildIterator.Value();
157     if ( IsDimTol(aL)) theLabels.Append(aL);
158   }
159 }
160
161 //=======================================================================
162 //function : GetDimensionLabels
163 //purpose  : 
164 //=======================================================================
165
166 void XCAFDoc_DimTolTool::GetDimensionLabels(TDF_LabelSequence& theLabels) const
167 {
168   theLabels.Clear();
169   TDF_ChildIterator aChildIterator( Label() ); 
170   for (; aChildIterator.More(); aChildIterator.Next()) {
171     TDF_Label aL = aChildIterator.Value();
172     if ( IsDimension(aL)) theLabels.Append(aL);
173   }
174 }
175
176 //=======================================================================
177 //function : GetGeomToleranceLabels
178 //purpose  : 
179 //=======================================================================
180
181 void XCAFDoc_DimTolTool::GetGeomToleranceLabels(TDF_LabelSequence& theLabels) const
182 {
183   theLabels.Clear();
184   TDF_ChildIterator aChildIterator( Label() ); 
185   for (; aChildIterator.More(); aChildIterator.Next()) {
186     TDF_Label aL = aChildIterator.Value();
187     if ( IsGeomTolerance(aL)) theLabels.Append(aL);
188   }
189 }
190
191 //=======================================================================
192 //function : FindDimTol
193 //purpose  : 
194 //=======================================================================
195
196 Standard_Boolean XCAFDoc_DimTolTool::FindDimTol(const Standard_Integer kind,
197                                                 const Handle(TColStd_HArray1OfReal)& aVal,
198                                                 const Handle(TCollection_HAsciiString)& aName,
199                                                 const Handle(TCollection_HAsciiString)& aDescription,
200                                                 TDF_Label& lab) const
201 {
202   TDF_ChildIDIterator it(Label(),XCAFDoc_DimTol::GetID());
203   for(; it.More(); it.Next()) {
204     TDF_Label DimTolL = it.Value()->Label();
205     Handle(XCAFDoc_DimTol) DimTolAttr;
206     if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) continue;
207     Standard_Integer kind1 = DimTolAttr->GetKind();
208     Handle(TColStd_HArray1OfReal) aVal1 = DimTolAttr->GetVal();
209     Handle(TCollection_HAsciiString) aName1 = DimTolAttr->GetName();
210     Handle(TCollection_HAsciiString) aDescription1 = DimTolAttr->GetDescription();
211     Standard_Boolean IsEqual = Standard_True;
212     if(!(kind1==kind)) continue;
213     if(!(aName==aName1)) continue;
214     if(!(aDescription==aDescription1)) continue;
215     if(kind<20) {  //dimension
216       for(Standard_Integer i=1; i<=aVal->Length(); i++) {
217         if(Abs(aVal->Value(i)-aVal1->Value(i))>Precision::Confusion())
218           IsEqual = Standard_False;
219       }
220     }
221     else if(kind<50) { //tolerance
222       if(Abs(aVal->Value(1)-aVal1->Value(1))>Precision::Confusion())
223         IsEqual = Standard_False;
224     }
225     if(IsEqual) {
226       lab = DimTolL;
227       return Standard_True;
228     }
229   }
230   return Standard_False;
231 }
232
233
234 //=======================================================================
235 //function : FindDimTol
236 //purpose  : 
237 //=======================================================================
238
239 TDF_Label XCAFDoc_DimTolTool::FindDimTol(const Standard_Integer kind,
240                                          const Handle(TColStd_HArray1OfReal)& aVal,
241                                          const Handle(TCollection_HAsciiString)& aName,
242                                          const Handle(TCollection_HAsciiString)& aDescription) const
243 {
244   TDF_Label L;
245   FindDimTol(kind,aVal,aName,aDescription,L);
246   return L;
247 }
248
249
250 //=======================================================================
251 //function : AddDimTol
252 //purpose  : 
253 //=======================================================================
254
255 TDF_Label XCAFDoc_DimTolTool::AddDimTol(const Standard_Integer kind,
256                                         const Handle(TColStd_HArray1OfReal)& aVal,
257                                         const Handle(TCollection_HAsciiString)& aName,
258                                         const Handle(TCollection_HAsciiString)& aDescription) const
259 {
260   TDF_Label DimTolL;
261   TDF_TagSource aTag;
262   DimTolL = aTag.NewChild ( Label() );
263   XCAFDoc_DimTol::Set(DimTolL,kind,aVal,aName,aDescription);
264   TCollection_AsciiString str = "DGT:";
265   if(kind<20) str.AssignCat("Dimension");
266   else str.AssignCat("Tolerance");
267   TDataStd_Name::Set(DimTolL,str);
268   return DimTolL;
269 }
270
271 //=======================================================================
272 //function : AddDimension
273 //purpose  : 
274 //=======================================================================
275
276 TDF_Label XCAFDoc_DimTolTool::AddDimension()
277 {
278   TDF_Label aDimTolL;
279   TDF_TagSource aTag;
280   aDimTolL = aTag.NewChild ( Label() );
281   Handle(XCAFDoc_Dimension) aDim = XCAFDoc_Dimension::Set(aDimTolL);
282   TCollection_AsciiString aStr = "DGT:Dimension";
283   TDataStd_Name::Set(aDimTolL,aStr);
284   return aDimTolL;
285 }
286
287 //=======================================================================
288 //function : AddGeomTolerance
289 //purpose  : 
290 //=======================================================================
291
292 TDF_Label XCAFDoc_DimTolTool::AddGeomTolerance()
293 {
294   TDF_Label aDimTolL;
295   TDF_TagSource aTag;
296   aDimTolL = aTag.NewChild ( Label() );
297   Handle(XCAFDoc_GeomTolerance) aTol = XCAFDoc_GeomTolerance::Set(aDimTolL);
298   TCollection_AsciiString aStr = "DGT:Tolerance";
299   TDataStd_Name::Set(aDimTolL,aStr);
300   return aDimTolL;
301 }
302
303 //=======================================================================
304 //function : SetDimension
305 //purpose  : 
306 //=======================================================================
307
308 void XCAFDoc_DimTolTool::SetDimension(const TDF_Label& theL,
309                                    const TDF_Label& theDimTolL) const
310 {
311   TDF_Label nullLab;
312   SetDimension(theL, nullLab, theDimTolL);
313 }
314
315 //=======================================================================
316 //function : SetDimension
317 //purpose  : 
318 //=======================================================================
319
320 void XCAFDoc_DimTolTool::SetDimension(const TDF_Label& theFirstL,
321                                    const TDF_Label& theSecondL,
322                                    const TDF_Label& theDimTolL) const
323 {
324   TDF_LabelSequence aFirstLS, aSecondLS;
325   if(!theFirstL.IsNull())
326     aFirstLS.Append(theFirstL);
327   if(!theSecondL.IsNull())
328     aSecondLS.Append(theSecondL);
329   SetDimension(aFirstLS, aSecondLS, theDimTolL);
330 }
331
332 //=======================================================================
333 //function : SetDimension
334 //purpose  : 
335 //=======================================================================
336
337 void XCAFDoc_DimTolTool::SetDimension(const TDF_LabelSequence& theFirstL,
338                                    const TDF_LabelSequence& theSecondL,
339                                    const TDF_Label& theDimTolL) const
340 {
341   if(!IsDimension(theDimTolL) || theFirstL.Length() == 0)
342   {
343     return;
344   }
345
346   Handle(XCAFDoc_GraphNode) aChGNode;
347   Handle(XCAFDoc_GraphNode) aFGNode;
348   Handle(XCAFDoc_GraphNode) aSecondFGNode;
349
350   if ( theDimTolL.FindAttribute (XCAFDoc::DimensionRefFirstGUID(), aChGNode) ) {
351     while (aChGNode->NbFathers() > 0) {
352       aFGNode = aChGNode->GetFather(1);
353       aFGNode->UnSetChild(aChGNode);
354       if(aFGNode->NbChildren() == 0)
355         aFGNode->ForgetAttribute( XCAFDoc::DimensionRefFirstGUID() );
356     }
357     theDimTolL.ForgetAttribute ( XCAFDoc::DimensionRefFirstGUID() );
358   }
359   if ( theDimTolL.FindAttribute (XCAFDoc::DimensionRefSecondGUID(), aChGNode) ) {
360     while (aChGNode->NbFathers() > 0) {
361       aFGNode = aChGNode->GetFather(1);
362       aFGNode->UnSetChild(aChGNode);
363       if(aFGNode->NbChildren() == 0)
364         aFGNode->ForgetAttribute( XCAFDoc::DimensionRefSecondGUID() );
365     }
366     theDimTolL.ForgetAttribute ( XCAFDoc::DimensionRefSecondGUID() );
367   }
368
369   if (!theDimTolL.FindAttribute(XCAFDoc::DimensionRefFirstGUID(), aChGNode)) {
370     aChGNode = new XCAFDoc_GraphNode;
371     aChGNode = XCAFDoc_GraphNode::Set(theDimTolL);
372     aChGNode->SetGraphID(XCAFDoc::DimensionRefFirstGUID());
373   }
374   for(Standard_Integer i = theFirstL.Lower(); i <= theFirstL.Upper(); i++)
375   {
376     if (!theFirstL.Value(i).FindAttribute(XCAFDoc::DimensionRefFirstGUID(), aFGNode) ) {
377       aFGNode = new XCAFDoc_GraphNode;
378       aFGNode = XCAFDoc_GraphNode::Set(theFirstL.Value(i));
379     }
380     aFGNode->SetGraphID(XCAFDoc::DimensionRefFirstGUID());
381     aFGNode->SetChild(aChGNode);
382     aChGNode->SetFather(aFGNode);
383   }
384
385   if (!theDimTolL.FindAttribute(XCAFDoc::DimensionRefSecondGUID(), aChGNode) && theSecondL.Length() > 0) {
386     aChGNode = new XCAFDoc_GraphNode;
387     aChGNode = XCAFDoc_GraphNode::Set(theDimTolL);
388     aChGNode->SetGraphID(XCAFDoc::DimensionRefSecondGUID());
389   }
390   for(Standard_Integer i = theSecondL.Lower(); i <= theSecondL.Upper(); i++)
391   {
392     if(!theSecondL.Value(i).FindAttribute(XCAFDoc::DimensionRefSecondGUID(), aSecondFGNode) ) {
393       aSecondFGNode = new XCAFDoc_GraphNode;
394       aSecondFGNode = XCAFDoc_GraphNode::Set(theSecondL.Value(i));
395     }
396     aSecondFGNode->SetGraphID(XCAFDoc::DimensionRefSecondGUID());
397     aSecondFGNode->SetChild(aChGNode);
398     aChGNode->SetFather(aSecondFGNode);
399   }
400 }
401
402 //=======================================================================
403 //function : SetGeomTolerance
404 //purpose  : 
405 //=======================================================================
406
407 void XCAFDoc_DimTolTool::SetGeomTolerance(const TDF_Label& theL,
408                                           const TDF_Label& theGeomTolL) const
409 {
410   TDF_LabelSequence aSeq;
411   aSeq.Append(theL);
412   SetGeomTolerance(aSeq, theGeomTolL);
413 }
414
415 //=======================================================================
416 //function : SetGeomTolerance
417 //purpose  : 
418 //=======================================================================
419
420 void XCAFDoc_DimTolTool::SetGeomTolerance(const TDF_LabelSequence& theL,
421                                           const TDF_Label& theGeomTolL) const
422 {
423   //  // set reference
424   //  Handle(TDataStd_TreeNode) refNode, mainNode;
425   //  refNode = TDataStd_TreeNode::Set ( theDimTolL, XCAFDoc::GeomToleranceRefGUID() );
426   //  mainNode  = TDataStd_TreeNode::Set ( theL,       XCAFDoc::GeomToleranceRefGUID() );
427   //  refNode->Remove(); // abv: fix against bug in TreeNode::Append()
428   //  mainNode->Append(refNode);
429   
430   if (!IsGeomTolerance(theGeomTolL) || theL.Length() == 0)
431   {
432     return;
433   }
434
435   Handle(XCAFDoc_GraphNode) aChGNode;
436   Handle(XCAFDoc_GraphNode) aFGNode;
437
438   if (theGeomTolL.FindAttribute(XCAFDoc::GeomToleranceRefGUID(), aChGNode)) {
439     while (aChGNode->NbFathers() > 0) {
440       aFGNode = aChGNode->GetFather(1);
441       aFGNode->UnSetChild(aChGNode);
442       if(aFGNode->NbChildren() == 0)
443         aFGNode->ForgetAttribute( XCAFDoc::GeomToleranceRefGUID() );
444     }
445     theGeomTolL.ForgetAttribute(XCAFDoc::GeomToleranceRefGUID());
446   }
447
448   if (!theGeomTolL.FindAttribute(XCAFDoc::GeomToleranceRefGUID(), aChGNode)) {
449     aChGNode = new XCAFDoc_GraphNode;
450     aChGNode = XCAFDoc_GraphNode::Set(theGeomTolL);
451     aChGNode->SetGraphID(XCAFDoc::GeomToleranceRefGUID());
452   }
453   for(Standard_Integer i = theL.Lower(); i <= theL.Upper(); i++)
454   {
455     if (!theL.Value(i).FindAttribute(XCAFDoc::GeomToleranceRefGUID(), aFGNode) ) {
456       aFGNode = new XCAFDoc_GraphNode;
457       aFGNode = XCAFDoc_GraphNode::Set(theL.Value(i));
458     }
459     aFGNode->SetGraphID(XCAFDoc::GeomToleranceRefGUID());
460     aFGNode->SetChild(aChGNode);
461     aChGNode->SetFather(aFGNode);
462   }
463 }
464
465 //=======================================================================
466 //function : SetDimTol
467 //purpose  : 
468 //=======================================================================
469
470 void XCAFDoc_DimTolTool::SetDimTol(const TDF_Label& theL,
471                                    const TDF_Label& theDimTolL) const
472 {
473   // set reference
474   Handle(TDataStd_TreeNode) refNode, mainNode;
475   refNode = TDataStd_TreeNode::Set ( theDimTolL, XCAFDoc::DimTolRefGUID() );
476   mainNode  = TDataStd_TreeNode::Set (theL,       XCAFDoc::DimTolRefGUID() );
477   refNode->Remove(); // abv: fix against bug in TreeNode::Append()
478   mainNode->Append(refNode);
479 }
480
481
482 //=======================================================================
483 //function : SetDimTol
484 //purpose  : 
485 //=======================================================================
486
487 TDF_Label XCAFDoc_DimTolTool::SetDimTol(const TDF_Label& L,
488                                         const Standard_Integer kind,
489                                         const Handle(TColStd_HArray1OfReal)& aVal,
490                                         const Handle(TCollection_HAsciiString)& aName,
491                                         const Handle(TCollection_HAsciiString)& aDescription) const
492 {
493   TDF_Label DimTolL = AddDimTol(kind,aVal,aName,aDescription);
494   SetDimTol(L,DimTolL);
495   return DimTolL;
496 }
497
498
499 //=======================================================================
500 //function : GetRefShapeLabel
501 //purpose  : 
502 //=======================================================================
503
504 Standard_Boolean XCAFDoc_DimTolTool::GetRefShapeLabel(const TDF_Label& theL,
505                                                       TDF_LabelSequence& theShapeLFirst,
506                                                       TDF_LabelSequence& theShapeLSecond) const
507 {
508   theShapeLFirst.Clear();
509   theShapeLSecond.Clear();
510   Handle(TDataStd_TreeNode) aNode;
511   if( !theL.FindAttribute(XCAFDoc::DimTolRefGUID(),aNode) || !aNode->HasFather() ) {
512     if( !theL.FindAttribute(XCAFDoc::DatumRefGUID(),aNode) || !aNode->HasFather() ) {
513       Handle(XCAFDoc_GraphNode) aGNode;
514       if( theL.FindAttribute(XCAFDoc::GeomToleranceRefGUID(),aGNode) && aGNode->NbFathers() > 0 ) {
515         for(Standard_Integer i = 1; i <= aGNode->NbFathers(); i++)
516         {
517           theShapeLFirst.Append(aGNode->GetFather(i)->Label());
518         }
519         return Standard_True;
520       }
521       else if (theL.FindAttribute(XCAFDoc::DatumRefGUID(), aGNode) && aGNode->NbFathers() > 0) {
522         for (Standard_Integer i = 1; i <= aGNode->NbFathers(); i++)
523         {
524           theShapeLFirst.Append(aGNode->GetFather(i)->Label());
525         }
526         return Standard_True;
527       }
528       else if( theL.FindAttribute(XCAFDoc::DimensionRefFirstGUID(),aGNode) && aGNode->NbFathers() > 0 ) {
529         for(Standard_Integer i = 1; i <= aGNode->NbFathers(); i++)
530         {
531           theShapeLFirst.Append(aGNode->GetFather(i)->Label());
532         }
533         if( theL.FindAttribute(XCAFDoc::DimensionRefSecondGUID(),aGNode) && aGNode->NbFathers() > 0 ) {
534           for(Standard_Integer i = 1; i <= aGNode->NbFathers(); i++)
535           {
536             theShapeLSecond.Append(aGNode->GetFather(i)->Label());
537           }
538         }
539         return Standard_True;
540       }
541       else
542       {
543         return Standard_False;
544       }
545     }
546   }
547
548   theShapeLFirst.Append(aNode->Father()->Label());
549   return Standard_True;
550 }
551
552 //=======================================================================
553 //function : GetRefDimensionLabels
554 //purpose  : 
555 //=======================================================================
556
557 Standard_Boolean XCAFDoc_DimTolTool::GetRefDimensionLabels(const TDF_Label& theShapeL,
558                                                      TDF_LabelSequence& theDimTols) const
559 {
560   Handle(XCAFDoc_GraphNode) aGNode;
561   Standard_Boolean aResult = Standard_False;
562   if( theShapeL.FindAttribute(XCAFDoc::DimensionRefFirstGUID(),aGNode) && aGNode->NbChildren() > 0 ) {
563     for(Standard_Integer i = 1; i <= aGNode->NbChildren(); i++)
564     {
565       theDimTols.Append(aGNode->GetChild(i)->Label());
566     }
567     aResult = Standard_True;
568   }
569   if( theShapeL.FindAttribute(XCAFDoc::DimensionRefSecondGUID(),aGNode) && aGNode->NbChildren() > 0 ) {
570     for(Standard_Integer i = 1; i <= aGNode->NbChildren(); i++)
571     {
572       theDimTols.Append(aGNode->GetChild(i)->Label());
573     }
574     aResult = Standard_True;
575   }
576   return aResult;
577 }
578
579 //=======================================================================
580 //function : GetRefGeomToleranceLabels
581 //purpose  : 
582 //=======================================================================
583
584 Standard_Boolean XCAFDoc_DimTolTool::GetRefGeomToleranceLabels(const TDF_Label& theShapeL,
585                                                      TDF_LabelSequence& theDimTols) const
586 {
587   Handle(XCAFDoc_GraphNode) aGNode;
588   if( !theShapeL.FindAttribute(XCAFDoc::GeomToleranceRefGUID(),aGNode) ||
589     aGNode->NbChildren() == 0 ) {
590     return Standard_False;
591   }
592   for(Standard_Integer i = 1; i <= aGNode->NbChildren(); i++)
593   {
594     theDimTols.Append(aGNode->GetChild(i)->Label());
595   }
596   return Standard_True;
597 }
598
599 //=======================================================================
600 //function : GetRefDatumLabel
601 //purpose  : 
602 //=======================================================================
603
604 Standard_Boolean XCAFDoc_DimTolTool::GetRefDatumLabel(const TDF_Label& theShapeL,
605                                                      TDF_LabelSequence& theDatum) const
606 {
607   Handle(XCAFDoc_GraphNode) aGNode;
608   if (!theShapeL.FindAttribute(XCAFDoc::DatumRefGUID(), aGNode)) {
609     return Standard_False;
610   }
611   for (Standard_Integer i = 1; i <= aGNode->NbChildren(); i++) {
612     theDatum.Append(aGNode->GetChild(i)->Label());
613   }
614   return Standard_True;
615 }
616
617 //=======================================================================
618 //function : GetDimTol
619 //purpose  : 
620 //=======================================================================
621
622 Standard_Boolean XCAFDoc_DimTolTool::GetDimTol(const TDF_Label& DimTolL,
623                                                Standard_Integer& kind,
624                                                Handle(TColStd_HArray1OfReal)& aVal,
625                                                Handle(TCollection_HAsciiString)& aName,
626                                                Handle(TCollection_HAsciiString)& aDescription) const
627 {
628   Handle(XCAFDoc_DimTol) DimTolAttr;
629   if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) {
630     return Standard_False;
631   }
632   kind = DimTolAttr->GetKind();
633   aVal = DimTolAttr->GetVal();
634   aName = DimTolAttr->GetName();
635   aDescription = DimTolAttr->GetDescription();
636   
637   return Standard_True;
638 }
639
640
641 //=======================================================================
642 //function : IsDatum
643 //purpose  : 
644 //=======================================================================
645
646 Standard_Boolean XCAFDoc_DimTolTool::IsDatum(const TDF_Label& theDimTolL) const
647 {
648   Handle(XCAFDoc_Datum) aDatumAttr;
649   if(theDimTolL.FindAttribute(XCAFDoc_Datum::GetID(),aDatumAttr)) {
650     return Standard_True;
651   }
652   return Standard_False;
653 }
654
655
656 //=======================================================================
657 //function : GetDatumLabels
658 //purpose  : 
659 //=======================================================================
660
661 void XCAFDoc_DimTolTool::GetDatumLabels(TDF_LabelSequence& theLabels) const
662 {
663   theLabels.Clear();
664   TDF_ChildIterator aChildIterator( Label() ); 
665   for (; aChildIterator.More(); aChildIterator.Next()) {
666     TDF_Label L = aChildIterator.Value();
667     if ( IsDatum(L)) theLabels.Append(L);
668   }
669 }
670
671 //=======================================================================
672 //function : FindDatum
673 //purpose  : 
674 //=======================================================================
675
676 Standard_Boolean XCAFDoc_DimTolTool::FindDatum(const Handle(TCollection_HAsciiString)& aName,
677                                                const Handle(TCollection_HAsciiString)& aDescription,
678                                                const Handle(TCollection_HAsciiString)& anIdentification,
679                                                TDF_Label& lab) const
680 {
681   TDF_ChildIDIterator it(Label(),XCAFDoc_Datum::GetID());
682   for(; it.More(); it.Next()) {
683     Handle(TCollection_HAsciiString) aName1, aDescription1, anIdentification1;
684     TDF_Label aLabel = it.Value()->Label();
685     if ( !GetDatum( aLabel, aName1, aDescription1, anIdentification1 ) )
686       continue;
687     if(!(aName==aName1)) continue;
688     if(!(aDescription==aDescription1)) continue;
689     if(!(anIdentification==anIdentification1)) continue;
690     lab = aLabel;
691     return Standard_True;
692   }
693   return Standard_False;
694 }
695
696
697 //=======================================================================
698 //function : AddDatum
699 //purpose  : 
700 //=======================================================================
701
702 TDF_Label XCAFDoc_DimTolTool::AddDatum(const Handle(TCollection_HAsciiString)& aName,
703                                        const Handle(TCollection_HAsciiString)& aDescription,
704                                        const Handle(TCollection_HAsciiString)& anIdentification) const
705 {
706   TDF_Label DatumL;
707   TDF_TagSource aTag;
708   DatumL = aTag.NewChild ( Label() );
709   XCAFDoc_Datum::Set(DatumL,aName,aDescription,anIdentification);
710   TDataStd_Name::Set(DatumL,"DGT:Datum");
711   return DatumL;
712 }
713
714 //=======================================================================
715 //function : AddDatum
716 //purpose  : 
717 //=======================================================================
718
719 TDF_Label XCAFDoc_DimTolTool::AddDatum()
720 {
721   TDF_Label aDatumL;
722   TDF_TagSource aTag;
723   aDatumL = aTag.NewChild ( Label() );
724   Handle(XCAFDoc_Datum) aDat = XCAFDoc_Datum::Set(aDatumL);
725   TDataStd_Name::Set(aDatumL,"DGT:Datum");
726   return aDatumL;
727 }
728
729 //=======================================================================
730 //function : SetDatum
731 //purpose  : 
732 //=======================================================================
733
734 void XCAFDoc_DimTolTool::SetDatum(const TDF_LabelSequence& theL,
735                                   const TDF_Label& theDatumL) const
736 {
737   if (!IsDatum(theDatumL))
738   {
739     return;
740   }
741
742   Handle(XCAFDoc_GraphNode) aChGNode;
743   Handle(XCAFDoc_GraphNode) aFGNode;
744
745   if (theDatumL.FindAttribute(XCAFDoc::DatumRefGUID(), aChGNode)) {
746     while (aChGNode->NbFathers() > 0) {
747       aFGNode = aChGNode->GetFather(1);
748       aFGNode->UnSetChild(aChGNode);
749       if (aFGNode->NbChildren() == 0)
750         aFGNode->ForgetAttribute(XCAFDoc::DatumRefGUID());
751     }
752     theDatumL.ForgetAttribute(XCAFDoc::DatumRefGUID());
753   }
754
755   if (!theDatumL.FindAttribute(XCAFDoc::DatumRefGUID(), aChGNode)) {
756     aChGNode = new XCAFDoc_GraphNode;
757     aChGNode = XCAFDoc_GraphNode::Set(theDatumL);
758     aChGNode->SetGraphID(XCAFDoc::DatumRefGUID());
759   }
760   for (Standard_Integer i = theL.Lower(); i <= theL.Upper(); i++)
761   {
762     if (!theL.Value(i).FindAttribute(XCAFDoc::DatumRefGUID(), aFGNode)) {
763       aFGNode = new XCAFDoc_GraphNode;
764       aFGNode = XCAFDoc_GraphNode::Set(theL.Value(i));
765     }
766     aFGNode->SetGraphID(XCAFDoc::DatumRefGUID());
767     aFGNode->SetChild(aChGNode);
768     aChGNode->SetFather(aFGNode);
769   }
770 }
771
772 //=======================================================================
773 //function : SetDatum
774 //purpose  : 
775 //=======================================================================
776
777 void XCAFDoc_DimTolTool::SetDatum(const TDF_Label& L,
778                                   const TDF_Label& TolerL,
779                                   const Handle(TCollection_HAsciiString)& aName,
780                                   const Handle(TCollection_HAsciiString)& aDescription,
781                                   const Handle(TCollection_HAsciiString)& anIdentification) const
782 {
783   TDF_Label DatumL;
784   if(!FindDatum(aName,aDescription,anIdentification,DatumL))
785     DatumL = AddDatum(aName,aDescription,anIdentification);
786   TDF_LabelSequence aLabels;
787   aLabels.Append(L);
788   SetDatum(aLabels,DatumL);
789   // set reference
790   Handle(XCAFDoc_GraphNode) FGNode;
791   Handle(XCAFDoc_GraphNode) ChGNode;
792   if (! TolerL.FindAttribute( XCAFDoc::DatumTolRefGUID(), FGNode) ) {
793     FGNode = new XCAFDoc_GraphNode;
794     FGNode = XCAFDoc_GraphNode::Set(TolerL);
795   }
796   if (! DatumL.FindAttribute( XCAFDoc::DatumTolRefGUID(), ChGNode) ) {
797     ChGNode = new XCAFDoc_GraphNode;
798     ChGNode = XCAFDoc_GraphNode::Set(DatumL);
799   }
800   FGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
801   ChGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
802   FGNode->SetChild(ChGNode);
803   ChGNode->SetFather(FGNode);
804 }
805
806 //=======================================================================
807 //function : SetDatumToGeomTol
808 //purpose  : 
809 //=======================================================================
810
811 void XCAFDoc_DimTolTool::SetDatumToGeomTol(const TDF_Label& theDatumL,
812                                            const TDF_Label& theTolerL) const
813 {
814   // set reference
815   Handle(XCAFDoc_GraphNode) aFGNode;
816   Handle(XCAFDoc_GraphNode) aChGNode;
817   if (! theTolerL.FindAttribute( XCAFDoc::DatumTolRefGUID(), aFGNode) ) {
818     aFGNode = new XCAFDoc_GraphNode;
819     aFGNode = XCAFDoc_GraphNode::Set(theTolerL);
820   }
821   if (! theDatumL.FindAttribute( XCAFDoc::DatumTolRefGUID(), aChGNode) ) {
822     aChGNode = new XCAFDoc_GraphNode;
823     aChGNode = XCAFDoc_GraphNode::Set(theDatumL);
824   }
825   aFGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
826   aChGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
827   aFGNode->SetChild(aChGNode);
828   aChGNode->SetFather(aFGNode);
829 }
830
831 //=======================================================================
832 //function : GetDatum
833 //purpose  : 
834 //=======================================================================
835
836 Standard_Boolean XCAFDoc_DimTolTool::GetDatum(const TDF_Label& theDatumL,
837                                               Handle(TCollection_HAsciiString)& theName,
838                                               Handle(TCollection_HAsciiString)& theDescription,
839                                               Handle(TCollection_HAsciiString)& theIdentification) const
840 {
841   Handle(XCAFDoc_Datum) aDatumAttr;
842   if( theDatumL.IsNull() || 
843       !theDatumL.FindAttribute(XCAFDoc_Datum::GetID(),aDatumAttr) )
844     return Standard_False;
845   
846   theName = aDatumAttr->GetName();
847   theDescription = aDatumAttr->GetDescription();
848   theIdentification = aDatumAttr->GetIdentification();
849   return Standard_True;
850 }
851
852 //=======================================================================
853 //function : GetDatumTolerLabels
854 //purpose  : 
855 //=======================================================================
856
857 Standard_Boolean XCAFDoc_DimTolTool::GetDatumOfTolerLabels(const TDF_Label& theDimTolL,
858                                                          TDF_LabelSequence& theDatums) const
859 {
860   Handle(XCAFDoc_GraphNode) aNode;
861   if( !theDimTolL.FindAttribute(XCAFDoc::DatumTolRefGUID(),aNode) )
862     return Standard_False;
863
864   for(Standard_Integer i=1; i<=aNode->NbChildren(); i++) {
865     Handle(XCAFDoc_GraphNode) aDatumNode = aNode->GetChild(i);
866     theDatums.Append(aDatumNode->Label());
867   }
868   return Standard_True;
869 }
870
871 //=======================================================================
872 //function : GetDatumWthObjectsTolerLabels
873 //purpose  : 
874 //=======================================================================
875
876 Standard_Boolean XCAFDoc_DimTolTool::GetDatumWithObjectOfTolerLabels(const TDF_Label& theDimTolL,
877                                                                      TDF_LabelSequence& theDatums) const
878 {
879   Handle(XCAFDoc_GraphNode) aNode;
880   if( !theDimTolL.FindAttribute(XCAFDoc::DatumTolRefGUID(),aNode) )
881     return Standard_False;
882
883   TColStd_MapOfAsciiString aDatumNameMap;
884   for(Standard_Integer i=1; i<=aNode->NbChildren(); i++) {
885     Handle(XCAFDoc_GraphNode) aDatumNode = aNode->GetChild(i);
886     TDF_Label aDatumL = aDatumNode->Label();
887     Handle(XCAFDoc_Datum) aDatumAttr;
888     if (!aDatumL.FindAttribute(XCAFDoc_Datum::GetID(), aDatumAttr)) 
889       continue;
890     Handle(XCAFDimTolObjects_DatumObject) aDatumObj = aDatumAttr->GetObject();
891     if (aDatumObj.IsNull())
892       continue;
893     Handle(TCollection_HAsciiString) aName = aDatumObj->GetName();
894     if (!aDatumNameMap.Add(aName->String())) {
895       // the datum has already been appended to sequence, due to one of its datum targets
896       continue;
897     }
898     theDatums.Append(aDatumNode->Label());
899   }
900   return Standard_True;
901 }
902
903 //=======================================================================
904 //function : GetTolerDatumLabels
905 //purpose  : 
906 //=======================================================================
907
908 Standard_Boolean XCAFDoc_DimTolTool::GetTolerOfDatumLabels(const TDF_Label& theDatumL,
909                                                          TDF_LabelSequence& theTols) const
910 {
911   Handle(XCAFDoc_GraphNode) aNode;
912   if( !theDatumL.FindAttribute(XCAFDoc::DatumTolRefGUID(),aNode) )
913     return Standard_False;
914   for(Standard_Integer i=1; i<=aNode->NbFathers(); i++) {
915     Handle(XCAFDoc_GraphNode) aDatumNode = aNode->GetFather(i);
916     theTols.Append(aDatumNode->Label());
917   }
918   return Standard_True;
919 }
920
921 //=======================================================================
922 //function : IsLocked
923 //purpose  : 
924 //=======================================================================
925
926 Standard_Boolean XCAFDoc_DimTolTool::IsLocked(const TDF_Label& theViewL) const
927 {
928   Handle(TDataStd_UAttribute) anAttr;
929   return theViewL.FindAttribute(XCAFDoc::LockGUID(), anAttr);
930 }
931
932 //=======================================================================
933 //function : Lock
934 //purpose  : 
935 //=======================================================================
936
937 void XCAFDoc_DimTolTool::Lock(const TDF_Label& theViewL) const
938 {
939   TDataStd_UAttribute::Set(theViewL, XCAFDoc::LockGUID());
940 }
941
942 //=======================================================================
943 //function : Unlock
944 //purpose  : 
945 //=======================================================================
946
947 void XCAFDoc_DimTolTool::Unlock(const TDF_Label& theViewL) const
948 {
949   theViewL.ForgetAttribute(XCAFDoc::LockGUID());
950 }
951
952 //=======================================================================
953 //function : ID
954 //purpose  : 
955 //=======================================================================
956
957 const Standard_GUID& XCAFDoc_DimTolTool::ID() const
958 {
959   return GetID();
960 }
961
962
963 //=======================================================================
964 //function : GetGDTPresentations
965 //purpose  : 
966 //=======================================================================
967 void XCAFDoc_DimTolTool::GetGDTPresentations(NCollection_IndexedDataMap<TDF_Label, 
968   TopoDS_Shape, TDF_LabelMapHasher>& theGDTLabelToShape) const
969 {
970   TDF_LabelSequence aGDTs;
971   GetDimensionLabels(aGDTs);
972   for (Standard_Integer i = 1; i <= aGDTs.Length(); i++) {
973     Handle(XCAFDoc_Dimension) aDimAttr;
974     const TDF_Label& aCL = aGDTs.Value(i);
975     if (!aCL.FindAttribute(XCAFDoc_Dimension::GetID(),aDimAttr)) 
976       continue;
977     Handle(XCAFDimTolObjects_DimensionObject) anObject = aDimAttr->GetObject();
978     if (anObject.IsNull())
979       continue;
980     TopoDS_Shape aShape = anObject->GetPresentation();
981     if (!aShape.IsNull())
982       theGDTLabelToShape.Add(aCL, aShape);
983   }
984
985   aGDTs.Clear();
986   GetGeomToleranceLabels(aGDTs);
987   for (Standard_Integer i = 1; i <= aGDTs.Length(); i++) {
988     Handle(XCAFDoc_GeomTolerance) aGTAttr;
989     const TDF_Label& aCL = aGDTs.Value(i);
990     if (!aCL.FindAttribute(XCAFDoc_GeomTolerance::GetID(),aGTAttr)) 
991       continue;
992     Handle(XCAFDimTolObjects_GeomToleranceObject) anObject = aGTAttr->GetObject();
993     if (anObject.IsNull())
994       continue;
995     TopoDS_Shape aShape = anObject->GetPresentation();
996     if (!aShape.IsNull())
997       theGDTLabelToShape.Add(aCL, aShape);
998   }
999
1000   aGDTs.Clear();
1001   GetDatumLabels(aGDTs);
1002   for (Standard_Integer i = 1; i <= aGDTs.Length(); i++) {
1003     Handle(XCAFDoc_Datum) aGTAttr;
1004     const TDF_Label& aCL = aGDTs.Value(i);
1005     if (!aCL.FindAttribute(XCAFDoc_Datum::GetID(),aGTAttr)) 
1006       continue;
1007     Handle(XCAFDimTolObjects_DatumObject) anObject = aGTAttr->GetObject();
1008     if (anObject.IsNull())
1009       continue;
1010     TopoDS_Shape aShape = anObject->GetPresentation();
1011     if (!aShape.IsNull())
1012       theGDTLabelToShape.Add(aCL, aShape);
1013   }
1014 }
1015
1016 //=======================================================================
1017 //function : SetGDTPresentations
1018 //purpose  : 
1019 //=======================================================================
1020 void XCAFDoc_DimTolTool::SetGDTPresentations(NCollection_IndexedDataMap<TDF_Label, TopoDS_Shape, TDF_LabelMapHasher>& theGDTLabelToPrs)
1021
1022   for (Standard_Integer i = 1; i <= theGDTLabelToPrs.Extent(); i++)
1023   {    
1024     const TDF_Label& aCL = theGDTLabelToPrs.FindKey(i);
1025     Handle(XCAFDoc_Dimension) aDimAttrDim;
1026     if (aCL.FindAttribute(XCAFDoc_Dimension::GetID(),aDimAttrDim)) 
1027     {
1028       Handle(XCAFDimTolObjects_DimensionObject) anObject = aDimAttrDim->GetObject();
1029       if (anObject.IsNull())
1030         continue;
1031       const TopoDS_Shape& aPrs = theGDTLabelToPrs.FindFromIndex(i);
1032       anObject->SetPresentation(aPrs, anObject->GetPresentationName());
1033       aDimAttrDim->SetObject(anObject);
1034       continue;
1035     }
1036     Handle(XCAFDoc_GeomTolerance) aDimAttrG;
1037     if (aCL.FindAttribute(XCAFDoc_GeomTolerance::GetID(),aDimAttrG)) 
1038     {
1039       Handle(XCAFDimTolObjects_GeomToleranceObject) anObject = aDimAttrG->GetObject();
1040       if (anObject.IsNull())
1041         continue;
1042       const TopoDS_Shape& aPrs = theGDTLabelToPrs.FindFromIndex(i);
1043       anObject->SetPresentation(aPrs, anObject->GetPresentationName());
1044       aDimAttrG->SetObject(anObject);
1045       continue;
1046     }
1047     Handle(XCAFDoc_Datum) aDimAttrD;
1048     if (aCL.FindAttribute(XCAFDoc_Datum::GetID(),aDimAttrD)) 
1049     {
1050       Handle(XCAFDimTolObjects_DatumObject) anObject = aDimAttrD->GetObject();
1051       if (anObject.IsNull())
1052         continue;
1053       const TopoDS_Shape& aPrs = theGDTLabelToPrs.FindFromIndex(i);
1054       anObject->SetPresentation(aPrs, anObject->GetPresentationName());
1055       aDimAttrD->SetObject(anObject);
1056       continue;
1057     }
1058   }
1059 }
1060
1061 //=======================================================================
1062 //function : DumpJson
1063 //purpose  : 
1064 //=======================================================================
1065 void XCAFDoc_DimTolTool::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
1066 {
1067   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
1068
1069   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
1070
1071   TDF_LabelSequence aLabels;
1072   GetDimensionLabels (aLabels);
1073   for (TDF_LabelSequence::Iterator aDimLabelIt (aLabels); aDimLabelIt.More(); aDimLabelIt.Next())
1074   {
1075     TCollection_AsciiString aDimensionLabel;
1076     TDF_Tool::Entry (aDimLabelIt.Value(), aDimensionLabel);
1077     OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aDimensionLabel)
1078   }
1079
1080   aLabels.Clear();
1081   GetGeomToleranceLabels (aLabels);
1082   for (TDF_LabelSequence::Iterator aGeomToleranceLabelIt (aLabels); aGeomToleranceLabelIt.More(); aGeomToleranceLabelIt.Next())
1083   {
1084     TCollection_AsciiString aGeomToleranceLabel;
1085     TDF_Tool::Entry (aGeomToleranceLabelIt.Value(), aGeomToleranceLabel);
1086     OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aGeomToleranceLabel)
1087   }
1088
1089   aLabels.Clear();
1090   GetDimTolLabels (aLabels);
1091   for (TDF_LabelSequence::Iterator aDimTolLabelIt (aLabels); aDimTolLabelIt.More(); aDimTolLabelIt.Next())
1092   {
1093     TCollection_AsciiString aDimTolLabelLabel;
1094     TDF_Tool::Entry (aDimTolLabelIt.Value(), aDimTolLabelLabel);
1095     OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aDimTolLabelLabel)
1096   }
1097
1098   aLabels.Clear();
1099   GetDatumLabels (aLabels);
1100   for (TDF_LabelSequence::Iterator aDatumLabelIt (aLabels); aDatumLabelIt.More(); aDatumLabelIt.Next())
1101   {
1102     TCollection_AsciiString aDatumLabel;
1103     TDF_Tool::Entry (aDatumLabelIt.Value(), aDatumLabel);
1104     OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aDatumLabel)
1105   }
1106 }