1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
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>
33 //=======================================================================
34 //function : XCAFDoc_DimTolTool
36 //=======================================================================
37 XCAFDoc_DimTolTool::XCAFDoc_DimTolTool()
42 //=======================================================================
45 //=======================================================================
47 Handle(XCAFDoc_DimTolTool) XCAFDoc_DimTolTool::Set(const TDF_Label& L)
49 Handle(XCAFDoc_DimTolTool) A;
50 if (!L.FindAttribute (XCAFDoc_DimTolTool::GetID(), A)) {
51 A = new XCAFDoc_DimTolTool ();
53 A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
59 //=======================================================================
62 //=======================================================================
64 const Standard_GUID& XCAFDoc_DimTolTool::GetID()
66 static Standard_GUID DGTTblID ("72afb19b-44de-11d8-8776-001083004c77");
71 //=======================================================================
72 //function : BaseLabel
74 //=======================================================================
76 TDF_Label XCAFDoc_DimTolTool::BaseLabel() const
82 //=======================================================================
83 //function : ShapeTool
85 //=======================================================================
87 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_DimTolTool::ShapeTool()
89 if(myShapeTool.IsNull())
90 myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
95 //=======================================================================
98 //=======================================================================
100 Standard_Boolean XCAFDoc_DimTolTool::IsDimTol(const TDF_Label& DimTolL) const
102 Handle(XCAFDoc_DimTol) DimTolAttr;
103 if(DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) {
104 return Standard_True;
106 return Standard_False;
110 //=======================================================================
111 //function : GetDimTolLabels
113 //=======================================================================
115 void XCAFDoc_DimTolTool::GetDimTolLabels(TDF_LabelSequence& Labels) const
118 TDF_ChildIterator ChildIterator( Label() );
119 for (; ChildIterator.More(); ChildIterator.Next()) {
120 TDF_Label L = ChildIterator.Value();
121 if ( IsDimTol(L)) Labels.Append(L);
126 //=======================================================================
127 //function : FindDimTol
129 //=======================================================================
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
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;
156 else if(kind<50) { //tolerance
157 if(Abs(aVal->Value(1)-aVal1->Value(1))>Precision::Confusion())
158 IsEqual = Standard_False;
162 return Standard_True;
165 return Standard_False;
169 //=======================================================================
170 //function : FindDimTol
172 //=======================================================================
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
180 FindDimTol(kind,aVal,aName,aDescription,L);
185 //=======================================================================
186 //function : AddDimTol
188 //=======================================================================
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
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);
207 //=======================================================================
208 //function : SetDimTol
210 //=======================================================================
212 void XCAFDoc_DimTolTool::SetDimTol(const TDF_Label& L,
213 const TDF_Label& DimTolL) const
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);
226 //=======================================================================
227 //function : SetDimTol
229 //=======================================================================
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
237 TDF_Label DimTolL = AddDimTol(kind,aVal,aName,aDescription);
238 SetDimTol(L,DimTolL);
243 //=======================================================================
244 //function : GetRefShapeLabel
246 //=======================================================================
248 Standard_Boolean XCAFDoc_DimTolTool::GetRefShapeLabel(const TDF_Label& L,
249 TDF_Label& ShapeL) const
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;
257 ShapeL = Node->Father()->Label();
258 return Standard_True;
262 //=======================================================================
263 //function : GetRefDGTLabels
265 //=======================================================================
267 Standard_Boolean XCAFDoc_DimTolTool::GetRefDGTLabels(const TDF_Label& ShapeL,
268 TDF_LabelSequence &DimTols) const
270 Handle(TDataStd_TreeNode) Node;
271 if( !ShapeL.FindAttribute(XCAFDoc::DimTolRefGUID(),Node) ||
272 !Node->HasFirst() ) {
273 return Standard_False;
275 Handle(TDataStd_TreeNode) Last = Node->First();
276 DimTols.Append(Last->Label());
277 while(Last->HasNext()) {
279 DimTols.Append(Last->Label());
281 return Standard_True;
285 //=======================================================================
286 //function : GetDimTol
288 //=======================================================================
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
296 Handle(XCAFDoc_DimTol) DimTolAttr;
297 if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) {
298 return Standard_False;
300 kind = DimTolAttr->GetKind();
301 aVal = DimTolAttr->GetVal();
302 aName = DimTolAttr->GetName();
303 aDescription = DimTolAttr->GetDescription();
305 return Standard_True;
309 //=======================================================================
312 //=======================================================================
314 Standard_Boolean XCAFDoc_DimTolTool::IsDatum(const TDF_Label& DimTolL) const
316 Handle(XCAFDoc_Datum) DatumAttr;
317 if(DimTolL.FindAttribute(XCAFDoc_Datum::GetID(),DatumAttr)) {
318 return Standard_True;
320 return Standard_False;
324 //=======================================================================
325 //function : GetDatumLabels
327 //=======================================================================
329 void XCAFDoc_DimTolTool::GetDatumLabels(TDF_LabelSequence& Labels) const
332 TDF_ChildIterator ChildIterator( Label() );
333 for (; ChildIterator.More(); ChildIterator.Next()) {
334 TDF_Label L = ChildIterator.Value();
335 if ( IsDatum(L)) Labels.Append(L);
340 //=======================================================================
341 //function : FindDatum
343 //=======================================================================
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
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 ) )
356 if(!(aName==aName1)) continue;
357 if(!(aDescription==aDescription1)) continue;
358 if(!(anIdentification==anIdentification1)) continue;
360 return Standard_True;
362 return Standard_False;
366 //=======================================================================
367 //function : AddDatum
369 //=======================================================================
371 TDF_Label XCAFDoc_DimTolTool::AddDatum(const Handle(TCollection_HAsciiString)& aName,
372 const Handle(TCollection_HAsciiString)& aDescription,
373 const Handle(TCollection_HAsciiString)& anIdentification) const
377 DatumL = aTag.NewChild ( Label() );
378 XCAFDoc_Datum::Set(DatumL,aName,aDescription,anIdentification);
379 TDataStd_Name::Set(DatumL,"DGT:Datum");
384 //=======================================================================
385 //function : SetDatum
387 //=======================================================================
389 void XCAFDoc_DimTolTool::SetDatum(const TDF_Label& L,
390 const TDF_Label& DatumL) const
393 Handle(TDataStd_TreeNode) refNode, mainNode;
394 refNode = TDataStd_TreeNode::Set ( DatumL, XCAFDoc::DatumRefGUID() );
395 mainNode = TDataStd_TreeNode::Set ( L, XCAFDoc::DatumRefGUID() );
397 mainNode->Append(refNode);
401 //=======================================================================
402 //function : SetDatum
404 //=======================================================================
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
413 if(!FindDatum(aName,aDescription,anIdentification,DatumL))
414 DatumL = AddDatum(aName,aDescription,anIdentification);
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);
423 if (! DatumL.FindAttribute( XCAFDoc::DatumTolRefGUID(), ChGNode) ) {
424 ChGNode = new XCAFDoc_GraphNode;
425 ChGNode = XCAFDoc_GraphNode::Set(DatumL);
427 FGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
428 ChGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
429 FGNode->SetChild(ChGNode);
430 ChGNode->SetFather(FGNode);
433 //=======================================================================
434 //function : GetDatum
436 //=======================================================================
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
443 Handle(XCAFDoc_Datum) aDatumAttr;
444 if( theDatumL.IsNull() ||
445 !theDatumL.FindAttribute(XCAFDoc_Datum::GetID(),aDatumAttr) )
446 return Standard_False;
448 theName = aDatumAttr->GetName();
449 theDescription = aDatumAttr->GetDescription();
450 theIdentification = aDatumAttr->GetIdentification();
451 return Standard_True;
454 //=======================================================================
455 //function : GetDatumTolerLabels
457 //=======================================================================
459 Standard_Boolean XCAFDoc_DimTolTool::GetDatumTolerLabels(const TDF_Label& DimTolL,
460 TDF_LabelSequence &Datums) const
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());
469 return Standard_True;
473 //=======================================================================
476 //=======================================================================
478 const Standard_GUID& XCAFDoc_DimTolTool::ID() const
484 //=======================================================================
487 //=======================================================================
489 void XCAFDoc_DimTolTool::Restore(const Handle(TDF_Attribute)& /*with*/)
494 //=======================================================================
495 //function : NewEmpty
497 //=======================================================================
499 Handle(TDF_Attribute) XCAFDoc_DimTolTool::NewEmpty() const
501 return new XCAFDoc_DimTolTool;
505 //=======================================================================
508 //=======================================================================
510 void XCAFDoc_DimTolTool::Paste(const Handle(TDF_Attribute)& /*into*/,
511 const Handle(TDF_RelocationTable)& /*RT*/) const