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