948582f329147240b8d673d50a795ce06313a66a
[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_Datum.hxx>
27 #include <XCAFDoc_DimTol.hxx>
28 #include <XCAFDoc_DimTolTool.hxx>
29 #include <XCAFDoc_DocumentTool.hxx>
30 #include <XCAFDoc_GraphNode.hxx>
31 #include <XCAFDoc_ShapeTool.hxx>
32
33 //=======================================================================
34 //function : XCAFDoc_DimTolTool
35 //purpose  : 
36 //=======================================================================
37 XCAFDoc_DimTolTool::XCAFDoc_DimTolTool()
38 {
39 }
40
41
42 //=======================================================================
43 //function : Set
44 //purpose  : 
45 //=======================================================================
46
47 Handle(XCAFDoc_DimTolTool) XCAFDoc_DimTolTool::Set(const TDF_Label& L) 
48 {
49   Handle(XCAFDoc_DimTolTool) A;
50   if (!L.FindAttribute (XCAFDoc_DimTolTool::GetID(), A)) {
51     A = new XCAFDoc_DimTolTool ();
52     L.AddAttribute(A);
53     A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
54   }
55   return A;
56 }
57
58
59 //=======================================================================
60 //function : GetID
61 //purpose  : 
62 //=======================================================================
63
64 const Standard_GUID& XCAFDoc_DimTolTool::GetID() 
65 {
66   static Standard_GUID DGTTblID ("72afb19b-44de-11d8-8776-001083004c77");
67   return DGTTblID; 
68 }
69
70
71 //=======================================================================
72 //function : BaseLabel
73 //purpose  : 
74 //=======================================================================
75
76 TDF_Label XCAFDoc_DimTolTool::BaseLabel() const
77 {
78   return Label();
79 }
80
81
82 //=======================================================================
83 //function : ShapeTool
84 //purpose  : 
85 //=======================================================================
86
87 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_DimTolTool::ShapeTool() 
88 {
89   if(myShapeTool.IsNull())
90     myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
91   return myShapeTool;
92 }
93
94
95 //=======================================================================
96 //function : IsDimTol
97 //purpose  : 
98 //=======================================================================
99
100 Standard_Boolean XCAFDoc_DimTolTool::IsDimTol(const TDF_Label& DimTolL) const
101 {
102   Handle(XCAFDoc_DimTol) DimTolAttr;
103   if(DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) {
104     return Standard_True;
105   }
106   return Standard_False;
107 }
108
109
110 //=======================================================================
111 //function : GetDimTolLabels
112 //purpose  : 
113 //=======================================================================
114
115 void XCAFDoc_DimTolTool::GetDimTolLabels(TDF_LabelSequence& Labels) const
116 {
117   Labels.Clear();
118   TDF_ChildIterator ChildIterator( Label() ); 
119   for (; ChildIterator.More(); ChildIterator.Next()) {
120     TDF_Label L = ChildIterator.Value();
121     if ( IsDimTol(L)) Labels.Append(L);
122   }
123 }
124
125
126 //=======================================================================
127 //function : FindDimTol
128 //purpose  : 
129 //=======================================================================
130
131 Standard_Boolean XCAFDoc_DimTolTool::FindDimTol(const Standard_Integer kind,
132                                                 const Handle(TColStd_HArray1OfReal)& aVal,
133                                                 const Handle(TCollection_HAsciiString)& aName,
134                                                 const Handle(TCollection_HAsciiString)& aDescription,
135                                                 TDF_Label& lab) const
136 {
137   TDF_ChildIDIterator it(Label(),XCAFDoc_DimTol::GetID());
138   for(; it.More(); it.Next()) {
139     TDF_Label DimTolL = it.Value()->Label();
140     Handle(XCAFDoc_DimTol) DimTolAttr;
141     if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) continue;
142     Standard_Integer kind1 = DimTolAttr->GetKind();
143     Handle(TColStd_HArray1OfReal) aVal1 = DimTolAttr->GetVal();
144     Handle(TCollection_HAsciiString) aName1 = DimTolAttr->GetName();
145     Handle(TCollection_HAsciiString) aDescription1 = DimTolAttr->GetDescription();
146     Standard_Boolean IsEqual = Standard_True;
147     if(!(kind1==kind)) continue;
148     if(!(aName==aName1)) continue;
149     if(!(aDescription==aDescription1)) continue;
150     if(kind<20) {  //dimension
151       for(Standard_Integer i=1; i<=aVal->Length(); i++) {
152         if(Abs(aVal->Value(i)-aVal1->Value(i))>Precision::Confusion())
153           IsEqual = Standard_False;
154       }
155     }
156     else if(kind<50) { //tolerance
157       if(Abs(aVal->Value(1)-aVal1->Value(1))>Precision::Confusion())
158         IsEqual = Standard_False;
159     }
160     if(IsEqual) {
161       lab = DimTolL;
162       return Standard_True;
163     }
164   }
165   return Standard_False;
166 }
167
168
169 //=======================================================================
170 //function : FindDimTol
171 //purpose  : 
172 //=======================================================================
173
174 TDF_Label XCAFDoc_DimTolTool::FindDimTol(const Standard_Integer kind,
175                                          const Handle(TColStd_HArray1OfReal)& aVal,
176                                          const Handle(TCollection_HAsciiString)& aName,
177                                          const Handle(TCollection_HAsciiString)& aDescription) const
178 {
179   TDF_Label L;
180   FindDimTol(kind,aVal,aName,aDescription,L);
181   return L;
182 }
183
184
185 //=======================================================================
186 //function : AddDimTol
187 //purpose  : 
188 //=======================================================================
189
190 TDF_Label XCAFDoc_DimTolTool::AddDimTol(const Standard_Integer kind,
191                                         const Handle(TColStd_HArray1OfReal)& aVal,
192                                         const Handle(TCollection_HAsciiString)& aName,
193                                         const Handle(TCollection_HAsciiString)& aDescription) const
194 {
195   TDF_Label DimTolL;
196   TDF_TagSource aTag;
197   DimTolL = aTag.NewChild ( Label() );
198   XCAFDoc_DimTol::Set(DimTolL,kind,aVal,aName,aDescription);
199   TCollection_AsciiString str = "DGT:";
200   if(kind<20) str.AssignCat("Dimension");
201   else str.AssignCat("Tolerance");
202   TDataStd_Name::Set(DimTolL,str);
203   return DimTolL;
204 }
205
206
207 //=======================================================================
208 //function : SetDimTol
209 //purpose  : 
210 //=======================================================================
211
212 void XCAFDoc_DimTolTool::SetDimTol(const TDF_Label& L,
213                                    const TDF_Label& DimTolL) const
214 {
215   // set reference
216   Handle(TDataStd_TreeNode) refNode, mainNode;
217 //  mainNode = TDataStd_TreeNode::Set ( DimTolL, XCAFDoc::DimTolRefGUID() );
218 //  refNode  = TDataStd_TreeNode::Set ( L,       XCAFDoc::DimTolRefGUID() );
219   refNode = TDataStd_TreeNode::Set ( DimTolL, XCAFDoc::DimTolRefGUID() );
220   mainNode  = TDataStd_TreeNode::Set ( L,       XCAFDoc::DimTolRefGUID() );
221   refNode->Remove(); // abv: fix against bug in TreeNode::Append()
222   mainNode->Append(refNode);
223 }
224
225
226 //=======================================================================
227 //function : SetDimTol
228 //purpose  : 
229 //=======================================================================
230
231 TDF_Label XCAFDoc_DimTolTool::SetDimTol(const TDF_Label& L,
232                                         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 DimTolL = AddDimTol(kind,aVal,aName,aDescription);
238   SetDimTol(L,DimTolL);
239   return DimTolL;
240 }
241
242
243 //=======================================================================
244 //function : GetRefShapeLabel
245 //purpose  : 
246 //=======================================================================
247
248 Standard_Boolean XCAFDoc_DimTolTool::GetRefShapeLabel(const TDF_Label& L,
249                                                       TDF_Label& ShapeL) const
250 {
251   Handle(TDataStd_TreeNode) Node;
252   if( !L.FindAttribute(XCAFDoc::DimTolRefGUID(),Node) || !Node->HasFather() ) {
253     if( !L.FindAttribute(XCAFDoc::DatumRefGUID(),Node) || !Node->HasFather() ) {
254       return Standard_False;
255     }
256   }
257   ShapeL = Node->Father()->Label();
258   return Standard_True;
259 }
260
261
262 //=======================================================================
263 //function : GetRefDGTLabels
264 //purpose  : 
265 //=======================================================================
266
267 Standard_Boolean XCAFDoc_DimTolTool::GetRefDGTLabels(const TDF_Label& ShapeL,
268                                                      TDF_LabelSequence &DimTols) const
269 {
270   Handle(TDataStd_TreeNode) Node;
271   if( !ShapeL.FindAttribute(XCAFDoc::DimTolRefGUID(),Node) ||
272        !Node->HasFirst() ) {
273     return Standard_False;
274   }
275   Handle(TDataStd_TreeNode) Last = Node->First();
276   DimTols.Append(Last->Label());
277   while(Last->HasNext()) {
278     Last = Last->Next();
279     DimTols.Append(Last->Label());
280   }
281   return Standard_True;
282 }
283
284
285 //=======================================================================
286 //function : GetDimTol
287 //purpose  : 
288 //=======================================================================
289
290 Standard_Boolean XCAFDoc_DimTolTool::GetDimTol(const TDF_Label& DimTolL,
291                                                Standard_Integer& kind,
292                                                Handle(TColStd_HArray1OfReal)& aVal,
293                                                Handle(TCollection_HAsciiString)& aName,
294                                                Handle(TCollection_HAsciiString)& aDescription) const
295 {
296   Handle(XCAFDoc_DimTol) DimTolAttr;
297   if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) {
298     return Standard_False;
299   }
300   kind = DimTolAttr->GetKind();
301   aVal = DimTolAttr->GetVal();
302   aName = DimTolAttr->GetName();
303   aDescription = DimTolAttr->GetDescription();
304   
305   return Standard_True;
306 }
307
308
309 //=======================================================================
310 //function : IsDatum
311 //purpose  : 
312 //=======================================================================
313
314 Standard_Boolean XCAFDoc_DimTolTool::IsDatum(const TDF_Label& DimTolL) const
315 {
316   Handle(XCAFDoc_Datum) DatumAttr;
317   if(DimTolL.FindAttribute(XCAFDoc_Datum::GetID(),DatumAttr)) {
318     return Standard_True;
319   }
320   return Standard_False;
321 }
322
323
324 //=======================================================================
325 //function : GetDatumLabels
326 //purpose  : 
327 //=======================================================================
328
329 void XCAFDoc_DimTolTool::GetDatumLabels(TDF_LabelSequence& Labels) const
330 {
331   Labels.Clear();
332   TDF_ChildIterator ChildIterator( Label() ); 
333   for (; ChildIterator.More(); ChildIterator.Next()) {
334     TDF_Label L = ChildIterator.Value();
335     if ( IsDatum(L)) Labels.Append(L);
336   }
337 }
338
339
340 //=======================================================================
341 //function : FindDatum
342 //purpose  : 
343 //=======================================================================
344
345 Standard_Boolean XCAFDoc_DimTolTool::FindDatum(const Handle(TCollection_HAsciiString)& aName,
346                                                const Handle(TCollection_HAsciiString)& aDescription,
347                                                const Handle(TCollection_HAsciiString)& anIdentification,
348                                                TDF_Label& lab) const
349 {
350   TDF_ChildIDIterator it(Label(),XCAFDoc_Datum::GetID());
351   for(; it.More(); it.Next()) {
352     Handle(TCollection_HAsciiString) aName1, aDescription1, anIdentification1;
353     TDF_Label aLabel = it.Value()->Label();
354     if ( !GetDatum( aLabel, aName1, aDescription1, anIdentification1 ) )
355       continue;
356     if(!(aName==aName1)) continue;
357     if(!(aDescription==aDescription1)) continue;
358     if(!(anIdentification==anIdentification1)) continue;
359     lab = aLabel;
360     return Standard_True;
361   }
362   return Standard_False;
363 }
364
365
366 //=======================================================================
367 //function : AddDatum
368 //purpose  : 
369 //=======================================================================
370
371 TDF_Label XCAFDoc_DimTolTool::AddDatum(const Handle(TCollection_HAsciiString)& aName,
372                                        const Handle(TCollection_HAsciiString)& aDescription,
373                                        const Handle(TCollection_HAsciiString)& anIdentification) const
374 {
375   TDF_Label DatumL;
376   TDF_TagSource aTag;
377   DatumL = aTag.NewChild ( Label() );
378   XCAFDoc_Datum::Set(DatumL,aName,aDescription,anIdentification);
379   TDataStd_Name::Set(DatumL,"DGT:Datum");
380   return DatumL;
381 }
382
383
384 //=======================================================================
385 //function : SetDatum
386 //purpose  : 
387 //=======================================================================
388
389 void XCAFDoc_DimTolTool::SetDatum(const TDF_Label& L,
390                                   const TDF_Label& DatumL) const
391 {
392   // set reference
393   Handle(TDataStd_TreeNode) refNode, mainNode;
394   refNode = TDataStd_TreeNode::Set ( DatumL, XCAFDoc::DatumRefGUID() );
395   mainNode  = TDataStd_TreeNode::Set ( L, XCAFDoc::DatumRefGUID() );
396   refNode->Remove();
397   mainNode->Append(refNode);
398 }
399
400
401 //=======================================================================
402 //function : SetDatum
403 //purpose  : 
404 //=======================================================================
405
406 void XCAFDoc_DimTolTool::SetDatum(const TDF_Label& L,
407                                   const TDF_Label& TolerL,
408                                   const Handle(TCollection_HAsciiString)& aName,
409                                   const Handle(TCollection_HAsciiString)& aDescription,
410                                   const Handle(TCollection_HAsciiString)& anIdentification) const
411 {
412   TDF_Label DatumL;
413   if(!FindDatum(aName,aDescription,anIdentification,DatumL))
414     DatumL = AddDatum(aName,aDescription,anIdentification);
415   SetDatum(L,DatumL);
416   // set reference
417   Handle(XCAFDoc_GraphNode) FGNode;
418   Handle(XCAFDoc_GraphNode) ChGNode;
419   if (! TolerL.FindAttribute( XCAFDoc::DatumTolRefGUID(), FGNode) ) {
420     FGNode = new XCAFDoc_GraphNode;
421     FGNode = XCAFDoc_GraphNode::Set(TolerL);
422   }
423   if (! DatumL.FindAttribute( XCAFDoc::DatumTolRefGUID(), ChGNode) ) {
424     ChGNode = new XCAFDoc_GraphNode;
425     ChGNode = XCAFDoc_GraphNode::Set(DatumL);
426   }
427   FGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
428   ChGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
429   FGNode->SetChild(ChGNode);
430   ChGNode->SetFather(FGNode);
431 }
432
433 //=======================================================================
434 //function : GetDatum
435 //purpose  : 
436 //=======================================================================
437
438 Standard_Boolean XCAFDoc_DimTolTool::GetDatum(const TDF_Label& theDatumL,
439                                               Handle(TCollection_HAsciiString)& theName,
440                                               Handle(TCollection_HAsciiString)& theDescription,
441                                               Handle(TCollection_HAsciiString)& theIdentification) const
442 {
443   Handle(XCAFDoc_Datum) aDatumAttr;
444   if( theDatumL.IsNull() || 
445       !theDatumL.FindAttribute(XCAFDoc_Datum::GetID(),aDatumAttr) )
446     return Standard_False;
447   
448   theName = aDatumAttr->GetName();
449   theDescription = aDatumAttr->GetDescription();
450   theIdentification = aDatumAttr->GetIdentification();
451   return Standard_True;
452 }
453
454 //=======================================================================
455 //function : GetDatumTolerLabels
456 //purpose  : 
457 //=======================================================================
458
459 Standard_Boolean XCAFDoc_DimTolTool::GetDatumTolerLabels(const TDF_Label& DimTolL,
460                                                          TDF_LabelSequence &Datums) const
461 {
462   Handle(XCAFDoc_GraphNode) Node;
463   if( !DimTolL.FindAttribute(XCAFDoc::DatumTolRefGUID(),Node) )
464     return Standard_False;
465   for(Standard_Integer i=1; i<=Node->NbChildren(); i++) {
466     Handle(XCAFDoc_GraphNode) DatumNode = Node->GetChild(i);
467     Datums.Append(DatumNode->Label());
468   }
469   return Standard_True;
470 }
471
472
473 //=======================================================================
474 //function : ID
475 //purpose  : 
476 //=======================================================================
477
478 const Standard_GUID& XCAFDoc_DimTolTool::ID() const
479 {
480   return GetID();
481 }
482
483
484 //=======================================================================
485 //function : Restore
486 //purpose  : 
487 //=======================================================================
488
489 void XCAFDoc_DimTolTool::Restore(const Handle(TDF_Attribute)& /*with*/) 
490 {
491 }
492
493
494 //=======================================================================
495 //function : NewEmpty
496 //purpose  : 
497 //=======================================================================
498
499 Handle(TDF_Attribute) XCAFDoc_DimTolTool::NewEmpty() const
500 {
501   return new XCAFDoc_DimTolTool;
502 }
503
504
505 //=======================================================================
506 //function : Paste
507 //purpose  : 
508 //=======================================================================
509
510 void XCAFDoc_DimTolTool::Paste(const Handle(TDF_Attribute)& /*into*/,
511                                const Handle(TDF_RelocationTable)& /*RT*/) const
512 {
513 }
514