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.
14 #include <XCAFDoc_DimTolTool.ixx>
16 #include <XCAFDoc.hxx>
17 #include <XCAFDoc_DimTol.hxx>
18 #include <XCAFDoc_Datum.hxx>
19 #include <XCAFDoc_DocumentTool.hxx>
20 #include <XCAFDoc_GraphNode.hxx>
21 #include <TDataStd_TreeNode.hxx>
22 #include <TDataStd_Name.hxx>
23 #include <TDF_ChildIDIterator.hxx>
24 #include <Precision.hxx>
27 //=======================================================================
28 //function : XCAFDoc_DimTolTool
30 //=======================================================================
32 XCAFDoc_DimTolTool::XCAFDoc_DimTolTool()
37 //=======================================================================
40 //=======================================================================
42 Handle(XCAFDoc_DimTolTool) XCAFDoc_DimTolTool::Set(const TDF_Label& L)
44 Handle(XCAFDoc_DimTolTool) A;
45 if (!L.FindAttribute (XCAFDoc_DimTolTool::GetID(), A)) {
46 A = new XCAFDoc_DimTolTool ();
48 A->myShapeTool = XCAFDoc_DocumentTool::ShapeTool(L);
54 //=======================================================================
57 //=======================================================================
59 const Standard_GUID& XCAFDoc_DimTolTool::GetID()
61 static Standard_GUID DGTTblID ("72afb19b-44de-11d8-8776-001083004c77");
66 //=======================================================================
67 //function : BaseLabel
69 //=======================================================================
71 TDF_Label XCAFDoc_DimTolTool::BaseLabel() const
77 //=======================================================================
78 //function : ShapeTool
80 //=======================================================================
82 const Handle(XCAFDoc_ShapeTool)& XCAFDoc_DimTolTool::ShapeTool()
84 if(myShapeTool.IsNull())
85 myShapeTool = XCAFDoc_DocumentTool::ShapeTool(Label());
90 //=======================================================================
93 //=======================================================================
95 Standard_Boolean XCAFDoc_DimTolTool::IsDimTol(const TDF_Label& DimTolL) const
97 Handle(XCAFDoc_DimTol) DimTolAttr;
98 if(DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) {
101 return Standard_False;
105 //=======================================================================
106 //function : GetDimTolLabels
108 //=======================================================================
110 void XCAFDoc_DimTolTool::GetDimTolLabels(TDF_LabelSequence& Labels) const
113 TDF_ChildIterator ChildIterator( Label() );
114 for (; ChildIterator.More(); ChildIterator.Next()) {
115 TDF_Label L = ChildIterator.Value();
116 if ( IsDimTol(L)) Labels.Append(L);
121 //=======================================================================
122 //function : FindDimTol
124 //=======================================================================
126 Standard_Boolean XCAFDoc_DimTolTool::FindDimTol(const Standard_Integer kind,
127 const Handle(TColStd_HArray1OfReal)& aVal,
128 const Handle(TCollection_HAsciiString)& aName,
129 const Handle(TCollection_HAsciiString)& aDescription,
130 TDF_Label& lab) const
132 TDF_ChildIDIterator it(Label(),XCAFDoc_DimTol::GetID());
133 for(; it.More(); it.Next()) {
134 TDF_Label DimTolL = it.Value()->Label();
135 Handle(XCAFDoc_DimTol) DimTolAttr;
136 if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) continue;
137 Standard_Integer kind1 = DimTolAttr->GetKind();
138 Handle(TColStd_HArray1OfReal) aVal1 = DimTolAttr->GetVal();
139 Handle(TCollection_HAsciiString) aName1 = DimTolAttr->GetName();
140 Handle(TCollection_HAsciiString) aDescription1 = DimTolAttr->GetDescription();
141 Standard_Boolean IsEqual = Standard_True;
142 if(!(kind1==kind)) continue;
143 if(!(aName==aName1)) continue;
144 if(!(aDescription==aDescription1)) continue;
145 if(kind<20) { //dimension
146 for(Standard_Integer i=1; i<=aVal->Length(); i++) {
147 if(Abs(aVal->Value(i)-aVal1->Value(i))>Precision::Confusion())
148 IsEqual = Standard_False;
151 else if(kind<50) { //tolerance
152 if(Abs(aVal->Value(1)-aVal1->Value(1))>Precision::Confusion())
153 IsEqual = Standard_False;
157 return Standard_True;
160 return Standard_False;
164 //=======================================================================
165 //function : FindDimTol
167 //=======================================================================
169 TDF_Label XCAFDoc_DimTolTool::FindDimTol(const Standard_Integer kind,
170 const Handle(TColStd_HArray1OfReal)& aVal,
171 const Handle(TCollection_HAsciiString)& aName,
172 const Handle(TCollection_HAsciiString)& aDescription) const
175 FindDimTol(kind,aVal,aName,aDescription,L);
180 //=======================================================================
181 //function : AddDimTol
183 //=======================================================================
185 TDF_Label XCAFDoc_DimTolTool::AddDimTol(const Standard_Integer kind,
186 const Handle(TColStd_HArray1OfReal)& aVal,
187 const Handle(TCollection_HAsciiString)& aName,
188 const Handle(TCollection_HAsciiString)& aDescription) const
192 DimTolL = aTag.NewChild ( Label() );
193 XCAFDoc_DimTol::Set(DimTolL,kind,aVal,aName,aDescription);
194 TCollection_AsciiString str = "DGT:";
195 if(kind<20) str.AssignCat("Dimension");
196 else str.AssignCat("Tolerance");
197 TDataStd_Name::Set(DimTolL,str);
202 //=======================================================================
203 //function : SetDimTol
205 //=======================================================================
207 void XCAFDoc_DimTolTool::SetDimTol(const TDF_Label& L,
208 const TDF_Label& DimTolL) const
211 Handle(TDataStd_TreeNode) refNode, mainNode;
212 // mainNode = TDataStd_TreeNode::Set ( DimTolL, XCAFDoc::DimTolRefGUID() );
213 // refNode = TDataStd_TreeNode::Set ( L, XCAFDoc::DimTolRefGUID() );
214 refNode = TDataStd_TreeNode::Set ( DimTolL, XCAFDoc::DimTolRefGUID() );
215 mainNode = TDataStd_TreeNode::Set ( L, XCAFDoc::DimTolRefGUID() );
216 refNode->Remove(); // abv: fix against bug in TreeNode::Append()
217 mainNode->Append(refNode);
221 //=======================================================================
222 //function : SetDimTol
224 //=======================================================================
226 TDF_Label XCAFDoc_DimTolTool::SetDimTol(const TDF_Label& L,
227 const Standard_Integer kind,
228 const Handle(TColStd_HArray1OfReal)& aVal,
229 const Handle(TCollection_HAsciiString)& aName,
230 const Handle(TCollection_HAsciiString)& aDescription) const
232 TDF_Label DimTolL = AddDimTol(kind,aVal,aName,aDescription);
233 SetDimTol(L,DimTolL);
238 //=======================================================================
239 //function : GetRefShapeLabel
241 //=======================================================================
243 Standard_Boolean XCAFDoc_DimTolTool::GetRefShapeLabel(const TDF_Label& L,
244 TDF_Label& ShapeL) const
246 Handle(TDataStd_TreeNode) Node;
247 if( !L.FindAttribute(XCAFDoc::DimTolRefGUID(),Node) || !Node->HasFather() ) {
248 if( !L.FindAttribute(XCAFDoc::DatumRefGUID(),Node) || !Node->HasFather() ) {
249 return Standard_False;
252 ShapeL = Node->Father()->Label();
253 return Standard_True;
257 //=======================================================================
258 //function : GetRefDGTLabels
260 //=======================================================================
262 Standard_Boolean XCAFDoc_DimTolTool::GetRefDGTLabels(const TDF_Label& ShapeL,
263 TDF_LabelSequence &DimTols) const
265 Handle(TDataStd_TreeNode) Node;
266 if( !ShapeL.FindAttribute(XCAFDoc::DimTolRefGUID(),Node) ||
267 !Node->HasFirst() ) {
268 return Standard_False;
270 Handle(TDataStd_TreeNode) Last = Node->First();
271 DimTols.Append(Last->Label());
272 while(Last->HasNext()) {
274 DimTols.Append(Last->Label());
276 return Standard_True;
280 //=======================================================================
281 //function : GetDimTol
283 //=======================================================================
285 Standard_Boolean XCAFDoc_DimTolTool::GetDimTol(const TDF_Label& DimTolL,
286 Standard_Integer& kind,
287 Handle(TColStd_HArray1OfReal)& aVal,
288 Handle(TCollection_HAsciiString)& aName,
289 Handle(TCollection_HAsciiString)& aDescription) const
291 Handle(XCAFDoc_DimTol) DimTolAttr;
292 if(!DimTolL.FindAttribute(XCAFDoc_DimTol::GetID(),DimTolAttr)) {
293 return Standard_False;
295 kind = DimTolAttr->GetKind();
296 aVal = DimTolAttr->GetVal();
297 aName = DimTolAttr->GetName();
298 aDescription = DimTolAttr->GetDescription();
300 return Standard_True;
304 //=======================================================================
307 //=======================================================================
309 Standard_Boolean XCAFDoc_DimTolTool::IsDatum(const TDF_Label& DimTolL) const
311 Handle(XCAFDoc_Datum) DatumAttr;
312 if(DimTolL.FindAttribute(XCAFDoc_Datum::GetID(),DatumAttr)) {
313 return Standard_True;
315 return Standard_False;
319 //=======================================================================
320 //function : GetDatumLabels
322 //=======================================================================
324 void XCAFDoc_DimTolTool::GetDatumLabels(TDF_LabelSequence& Labels) const
327 TDF_ChildIterator ChildIterator( Label() );
328 for (; ChildIterator.More(); ChildIterator.Next()) {
329 TDF_Label L = ChildIterator.Value();
330 if ( IsDatum(L)) Labels.Append(L);
335 //=======================================================================
336 //function : FindDatum
338 //=======================================================================
340 Standard_Boolean XCAFDoc_DimTolTool::FindDatum(const Handle(TCollection_HAsciiString)& aName,
341 const Handle(TCollection_HAsciiString)& aDescription,
342 const Handle(TCollection_HAsciiString)& anIdentification,
343 TDF_Label& lab) const
345 TDF_ChildIDIterator it(Label(),XCAFDoc_Datum::GetID());
346 for(; it.More(); it.Next()) {
347 Handle(TCollection_HAsciiString) aName1, aDescription1, anIdentification1;
348 TDF_Label aLabel = it.Value()->Label();
349 if ( !GetDatum( aLabel, aName1, aDescription1, anIdentification1 ) )
351 if(!(aName==aName1)) continue;
352 if(!(aDescription==aDescription1)) continue;
353 if(!(anIdentification==anIdentification1)) continue;
355 return Standard_True;
357 return Standard_False;
361 //=======================================================================
362 //function : AddDatum
364 //=======================================================================
366 TDF_Label XCAFDoc_DimTolTool::AddDatum(const Handle(TCollection_HAsciiString)& aName,
367 const Handle(TCollection_HAsciiString)& aDescription,
368 const Handle(TCollection_HAsciiString)& anIdentification) const
372 DatumL = aTag.NewChild ( Label() );
373 XCAFDoc_Datum::Set(DatumL,aName,aDescription,anIdentification);
374 TDataStd_Name::Set(DatumL,"DGT:Datum");
379 //=======================================================================
380 //function : SetDatum
382 //=======================================================================
384 void XCAFDoc_DimTolTool::SetDatum(const TDF_Label& L,
385 const TDF_Label& DatumL) const
388 Handle(TDataStd_TreeNode) refNode, mainNode;
389 refNode = TDataStd_TreeNode::Set ( DatumL, XCAFDoc::DatumRefGUID() );
390 mainNode = TDataStd_TreeNode::Set ( L, XCAFDoc::DatumRefGUID() );
392 mainNode->Append(refNode);
396 //=======================================================================
397 //function : SetDatum
399 //=======================================================================
401 void XCAFDoc_DimTolTool::SetDatum(const TDF_Label& L,
402 const TDF_Label& TolerL,
403 const Handle(TCollection_HAsciiString)& aName,
404 const Handle(TCollection_HAsciiString)& aDescription,
405 const Handle(TCollection_HAsciiString)& anIdentification) const
408 if(!FindDatum(aName,aDescription,anIdentification,DatumL))
409 DatumL = AddDatum(aName,aDescription,anIdentification);
412 Handle(XCAFDoc_GraphNode) FGNode;
413 Handle(XCAFDoc_GraphNode) ChGNode;
414 if (! TolerL.FindAttribute( XCAFDoc::DatumTolRefGUID(), FGNode) ) {
415 FGNode = new XCAFDoc_GraphNode;
416 FGNode = XCAFDoc_GraphNode::Set(TolerL);
418 if (! DatumL.FindAttribute( XCAFDoc::DatumTolRefGUID(), ChGNode) ) {
419 ChGNode = new XCAFDoc_GraphNode;
420 ChGNode = XCAFDoc_GraphNode::Set(DatumL);
422 FGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
423 ChGNode->SetGraphID( XCAFDoc::DatumTolRefGUID() );
424 FGNode->SetChild(ChGNode);
425 ChGNode->SetFather(FGNode);
428 //=======================================================================
429 //function : GetDatum
431 //=======================================================================
433 Standard_Boolean XCAFDoc_DimTolTool::GetDatum(const TDF_Label& theDatumL,
434 Handle(TCollection_HAsciiString)& theName,
435 Handle(TCollection_HAsciiString)& theDescription,
436 Handle(TCollection_HAsciiString)& theIdentification) const
438 Handle(XCAFDoc_Datum) aDatumAttr;
439 if( theDatumL.IsNull() ||
440 !theDatumL.FindAttribute(XCAFDoc_Datum::GetID(),aDatumAttr) )
441 return Standard_False;
443 theName = aDatumAttr->GetName();
444 theDescription = aDatumAttr->GetDescription();
445 theIdentification = aDatumAttr->GetIdentification();
446 return Standard_True;
449 //=======================================================================
450 //function : GetDatumTolerLabels
452 //=======================================================================
454 Standard_Boolean XCAFDoc_DimTolTool::GetDatumTolerLabels(const TDF_Label& DimTolL,
455 TDF_LabelSequence &Datums) const
457 Handle(XCAFDoc_GraphNode) Node;
458 if( !DimTolL.FindAttribute(XCAFDoc::DatumTolRefGUID(),Node) )
459 return Standard_False;
460 for(Standard_Integer i=1; i<=Node->NbChildren(); i++) {
461 Handle(XCAFDoc_GraphNode) DatumNode = Node->GetChild(i);
462 Datums.Append(DatumNode->Label());
464 return Standard_True;
468 //=======================================================================
471 //=======================================================================
473 const Standard_GUID& XCAFDoc_DimTolTool::ID() const
479 //=======================================================================
482 //=======================================================================
484 void XCAFDoc_DimTolTool::Restore(const Handle(TDF_Attribute)& /*with*/)
489 //=======================================================================
490 //function : NewEmpty
492 //=======================================================================
494 Handle(TDF_Attribute) XCAFDoc_DimTolTool::NewEmpty() const
496 return new XCAFDoc_DimTolTool;
500 //=======================================================================
503 //=======================================================================
505 void XCAFDoc_DimTolTool::Paste(const Handle(TDF_Attribute)& /*into*/,
506 const Handle(TDF_RelocationTable)& /*RT*/) const