0027772: Foundation Classes - define Standard_Boolean using C++ type "bool" instead...
[occt.git] / src / DDataStd / DDataStd_TreeCommands.cxx
CommitLineData
b311480e 1// Created on: 1999-11-05
2// Created by: Denis PASCAL
3// Copyright (c) 1999-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
7fd59977 17
42cf5bc1 18#include <DDataStd.hxx>
19#include <DDataStd_TreeBrowser.hxx>
20#include <DDF.hxx>
7fd59977 21#include <Draw.hxx>
22#include <Draw_Appli.hxx>
23#include <Draw_Drawable3D.hxx>
24#include <Draw_Interpretor.hxx>
7fd59977 25#include <Standard_GUID.hxx>
26#include <TCollection_AsciiString.hxx>
27#include <TCollection_ExtendedString.hxx>
42cf5bc1 28#include <TDataStd_ChildNodeIterator.hxx>
29#include <TDataStd_TreeNode.hxx>
30#include <TDataXtd_Constraint.hxx>
31#include <TDF.hxx>
32#include <TDF_Data.hxx>
33#include <TDF_Label.hxx>
34#include <TDF_Tool.hxx>
7fd59977 35
36#include <stdio.h>
7fd59977 37//=======================================================================
38//TreeNode : SetNode
39//purpose : SetNode (DOC Entry [ID])
40//=======================================================================
7fd59977 41static Standard_Integer DDataStd_SetNode (Draw_Interpretor& di,
42 Standard_Integer n,
43 const char** a)
44{
45 Handle(TDF_Data) DF;
46 if (!DDF::GetDF(a[1],DF)) return 1;
47 TDF_Label L;
48 Handle(TDataStd_TreeNode) TN;
49 DDF::AddLabel(DF,a[2],L);
50 if (n == 3) {
51 TN = TDataStd_TreeNode::Set(L);
52 DDF::ReturnLabel(di,TN->Label());
53 return 0;
54 } else if (n == 4) {
55 if (!Standard_GUID::CheckGUIDFormat(a[3])) {
586db386 56 di<<"DDataStd_SetNode: The format of GUID is invalid\n";
7fd59977 57 return 1;
58 }
59 const Standard_GUID ID(a[3]);
60 TN = TDataStd_TreeNode::Set(L, ID);
61 DDF::ReturnLabel(di,TN->Label());
62 return 0;
63 }
586db386 64 di << "DDataStd_SetNode : Error\n";
7fd59977 65 return 1;
66}
67
68//=======================================================================
69//TreeNode : AppendNode
70//purpose : AppendNode (DOC FatherEntry childEntry [fatherID])
71//=======================================================================
72
73static Standard_Integer DDataStd_AppendNode (Draw_Interpretor& di,
74 Standard_Integer n,
75 const char** a)
76{
77 if (n >= 4) {
78 Handle(TDF_Data) DF;
79 if (!DDF::GetDF(a[1],DF))return 1;
80 Handle(TDataStd_TreeNode) father, child;
81
82 Standard_GUID ID;
83 if (n==4)
84 ID=Standard_GUID(TDataStd_TreeNode::GetDefaultTreeID());
85 else{
86 if (!Standard_GUID::CheckGUIDFormat(a[4])) {
586db386 87 di<<"DDataStd_AppendNode: The format of GUID is invalid\n";
7fd59977 88 return 1;
89 }
90 const Standard_GUID varID(a[4]);
91 ID=varID;
92 }
93
94 if (!DDF::Find(DF,a[2],ID,father)) return 1;
95
96 TDF_Label L;
97 DDF::AddLabel(DF,a[3],L);
98 if ( (L.FindAttribute(ID,child))
99 &&(!child->IsRoot()) ){
586db386 100 di << "DDataStd_AppendNode : Error : childTreeNode don't detach\n";
7fd59977 101 return 1;
102 }
103 child = TDataStd_TreeNode::Set(L,ID);
104 if (!father->Append(child)) return 1;
0797d9d3 105#ifdef OCCT_DEBUG
586db386 106 di<<"AppendNode: OK\n";
7fd59977 107#endif
108 return 0;
109 }
586db386 110 di << "DDataStd_AppendNode : Error\n";
7fd59977 111 return 1;
112}
113
114//=======================================================================
115//TreeNode : PrependNode
116//purpose : PrependNode (DOC FatherEntry childEntry [fatherID])
117//=======================================================================
118
119static Standard_Integer DDataStd_PrependNode (Draw_Interpretor& di,
120 Standard_Integer n,
121 const char** a)
122{
123 if (n >= 4) {
124 Handle(TDF_Data) DF;
125 if (!DDF::GetDF(a[1],DF))return 1;
126 Handle(TDataStd_TreeNode) father, child;
127
128 Standard_GUID ID;
129 if (n==4)
130 ID=Standard_GUID(TDataStd_TreeNode::GetDefaultTreeID());
131 else{
132 if (!Standard_GUID::CheckGUIDFormat(a[4])) {
586db386 133 di<<"DDataStd_PrependNode: The format of GUID is invalid\n";
7fd59977 134 return 1;
135 }
136 const Standard_GUID varID(a[4]);
137 ID=varID;
138 }
139
140 if (!DDF::Find(DF,a[2],ID,father)) return 1;
141
142 TDF_Label L;
143 DDF::AddLabel(DF,a[3],L);
144 if ( (L.FindAttribute(ID,child))
145 &&(!child->IsRoot()) ){
586db386 146 di << "DDataStd_PrependNode : Error : childTreeNode don't detach\n";
7fd59977 147 return 1;
148 }
149 child = TDataStd_TreeNode::Set(L,ID);
150 if (!father->Prepend(child)) return 1;
0797d9d3 151#ifdef OCCT_DEBUG
586db386 152 di<<"PrependNode: OK\n";
7fd59977 153#endif
154 return 0;
155 }
586db386 156 di << "DDataStd_PrependNode : Error\n";
7fd59977 157 return 1;
158}
159
160//=======================================================================
161//TreeNode : RootNode
162//purpose : RootNode (DOC TreeNodeEntry [ID])
163//=======================================================================
164
165static Standard_Integer DDataStd_RootNode (Draw_Interpretor& di,
166 Standard_Integer n,
167 const char** a)
168{
169 if (n >= 3) {
170 Handle(TDF_Data) DF;
171 if (!DDF::GetDF(a[1],DF)) return 1;
172 Handle(TDataStd_TreeNode) TN;
173
174 Standard_GUID ID;
175 if (n==3)
176 ID=Standard_GUID(TDataStd_TreeNode::GetDefaultTreeID());
177 else{
178 if (!Standard_GUID::CheckGUIDFormat(a[3])) {
586db386 179 di<<"DDataStd_RootNode: The format of GUID is invalid\n";
7fd59977 180 return 1;
181 }
182 const Standard_GUID varID(a[3]);
183 ID=varID;
184 }
185
186 if (!DDF::Find(DF,a[2],ID,TN)) return 1;
187 DDF::ReturnLabel(di,TN->Root()->Label());
188 return 0;
189 }
586db386 190 di << "DDataStd_RootNode : Error\n";
7fd59977 191 return 1;
192}
193
194//=======================================================================
195//TreeNode : InsertNodeBefore
196//purpose : InsertNodeBefore (DOC TreeNodeEntry TreeNodeWhichHasToBeBefore [ID])
197//=======================================================================
198
199static Standard_Integer DDataStd_InsertNodeBefore(Draw_Interpretor& di,
200 Standard_Integer n,
201 const char** a)
202{
203 if (n >= 4) {
204 Handle(TDF_Data) DF;
205 if (!DDF::GetDF(a[1],DF)) return 1;
206 Handle(TDataStd_TreeNode) current, TN;
207
208 Standard_GUID ID;
209 if (n==4)
210 ID=Standard_GUID(TDataStd_TreeNode::GetDefaultTreeID());
211 else{
212 if (!Standard_GUID::CheckGUIDFormat(a[4])) {
586db386 213 di<<"DDataStd_InsertNodeBefore: The format of GUID is invalid\n";
7fd59977 214 return 1;
215 }
216 const Standard_GUID varID(a[4]);
217 ID=varID;
218 }
219
220 if (!DDF::Find(DF,a[2],ID,current)) return 1;
221// if (!DDF::Find(DF,a[3],ID,TN)) return 1;
222
223 TDF_Label L;
224 if (!DDF::FindLabel(DF,a[3],L)) return 1;
225 TN = TDataStd_TreeNode::Set(L,ID);
226 if (!current->InsertBefore(TN)) return 1;
227 return 0;
228 }
586db386 229 di << "DDataStd_InsertBefore : Error\n";
7fd59977 230 return 1;
231}
232
233//=======================================================================
234//TreeNode : InsertNodeAfter
235//purpose : InsertNodeAfter (DOC TreeNodeEntry TreeNodeWhichHasToBeAfter [ID])
236//=======================================================================
237
238static Standard_Integer DDataStd_InsertNodeAfter(Draw_Interpretor& di,
239 Standard_Integer n,
240 const char** a)
241{
242 if (n >= 4) {
243 Handle(TDF_Data) DF;
244 if (!DDF::GetDF(a[1],DF)) return 1;
245 Handle(TDataStd_TreeNode) current, TN;
246
247 Standard_GUID ID;
248 if (n==4)
249 ID=Standard_GUID(TDataStd_TreeNode::GetDefaultTreeID());
250 else{
251 if (!Standard_GUID::CheckGUIDFormat(a[4])) {
586db386 252 di<<"DDataStd_InsertNodeAfter: The format of GUID is invalid\n";
7fd59977 253 return 1;
254 }
255 const Standard_GUID varID(a[4]);
256 ID=varID;
257 }
258
259 if (!DDF::Find(DF,a[2],ID,current)) return 1;
260 //if (!DDF::Find(DF,a[3],TDataStd_TreeNode::GetDefaultTreeID(),TN)) return 1;
261 TDF_Label L;
262 if (!DDF::FindLabel(DF,a[3],L)) return 1;
263 TN = TDataStd_TreeNode::Set(L);
264 if (!current->InsertAfter(TN)) return 1;
265 return 0;
266 }
586db386 267 di << "DDataStd_InsertNodeAfter : Error\n";
7fd59977 268 return 1;
269}
270
271//=======================================================================
272//TreeNode : DetachNode
273//purpose : DetachNode (DOC TreeNodeEntry [ID])
274//=======================================================================
275
276static Standard_Integer DDataStd_DetachNode (Draw_Interpretor& di,Standard_Integer n, const char** a)
277{
278 if (n >= 3) {
279 Handle(TDF_Data) DF;
280 if (!DDF::GetDF(a[1],DF)) return 1;
281 Handle(TDataStd_TreeNode) TN;
282
283 Standard_GUID ID;
284 if (n==3)
285 ID=Standard_GUID(TDataStd_TreeNode::GetDefaultTreeID());
286 else{
287 if (!Standard_GUID::CheckGUIDFormat(a[3])) {
586db386 288 di<<"DDataStd_DetachNode: The format of GUID is invalid\n";
7fd59977 289 return 1;
290 }
291 const Standard_GUID varID(a[3]);
292 ID=varID;
293 }
294
295 if (!DDF::Find(DF,a[2],ID,TN)) return 1;
296 if (!TN->Remove())
586db386 297 di << "Can't Detach the TreeNode\n";
7fd59977 298 return 0;
299 }
586db386 300 di << "DDataStd_DetachNode : Error\n";
7fd59977 301 return 1;
302}
303
304//=======================================================================
305//function : TreeBrowse
306//purpose :
307// arg 1 : DF name
308// arg 2 : Entry
309// [arg 3] : Browser name
310//=======================================================================
311
312static Standard_Integer DDataStd_TreeBrowse (Draw_Interpretor& di,
313 Standard_Integer n,
314 const char** a)
315{
316 if (n<3) return 1;
317
318 Handle(TDF_Data) DF;
319 if (!DDF::GetDF (a[1], DF)) return 1;
320
321 TDF_Label lab;
322 TDF_Tool::Label(DF,a[2],lab);
323
324 Handle(DDataStd_TreeBrowser) NewTreeNode = new DDataStd_TreeBrowser (lab);
325 char *name = new char[50];
91322f44 326 if (n == 4) Sprintf(name,"treebrowser_%s",a[3]);
327 else Sprintf(name,"treebrowser_%s",a[1]);
7fd59977 328
329 Draw::Set(name, NewTreeNode);
330 TCollection_AsciiString inst1("treebrowser ");
331 inst1.AssignCat(name);
332 di.Eval(inst1.ToCString());
333 return 0;
334}
335
336
337//=======================================================================
338//function : OpenNode
339//purpose :
340// arg 1 : Browser name
341// [arg 2] : Label entry
342//=======================================================================
343
344static Standard_Integer DDataStd_OpenNode (Draw_Interpretor& di,
345 Standard_Integer n,
346 const char** a)
347{
348 if (n < 2) return 1;
349
350 Handle(DDataStd_TreeBrowser) browser =
351 Handle(DDataStd_TreeBrowser)::DownCast (Draw::Get(a[1], Standard_True));
352
353 TDF_Label lab;
354 if (n == 3) TDF_Tool::Label(browser->Label().Data(),a[2],lab);
355
356 if (n == 2 || lab.IsNull()) {
357 TCollection_AsciiString list = browser->OpenRoot();
358 di<<list.ToCString();
359 }
360 else {
361 TCollection_AsciiString list = browser->OpenNode(lab);
362 di<<list.ToCString();
363 }
364 return 0;
365}
366
367//=======================================================================
368//TreeNode : ChildNodeIterate
369//purpose : ChildNodeIterate Doc TreeNode AllLevels [ID]
370//=======================================================================
371
372static Standard_Integer DDataStd_ChildNodeIterate (Draw_Interpretor& di,
373 Standard_Integer n,
374 const char** a)
375{
376 if (n >= 4) {
377 Handle(TDF_Data) DF;
378 if (!DDF::GetDF(a[1],DF)) return 1;
dde68833 379 const Standard_Boolean AllLevels = (Draw::Atoi(a[3]) != 0);
7fd59977 380 Handle(TDataStd_TreeNode) TN, Value;
381
382 Standard_GUID ID;
383 if (n==4)
384 ID=Standard_GUID(TDataStd_TreeNode::GetDefaultTreeID());
385 else{
386 if (!Standard_GUID::CheckGUIDFormat(a[4])) {
586db386 387 di<<"DDataStd_ChildNodeIterate: The format of GUID is invalid\n";
7fd59977 388 return 1;
389 }
390 const Standard_GUID varID(a[4]);
391 ID=varID;
392 }
393
394 if (!DDF::Find(DF, a[2], ID, TN)) return 1;
586db386 395 //di<<"TDataStd_ChildNodeIterator: \n";
7fd59977 396 TDataStd_ChildNodeIterator itr(TN, AllLevels);
397 for (; itr.More(); itr.Next()) {
398 Value = itr.Value();
399 TCollection_AsciiString entry;
400 TDF_Tool::Entry(Value->Label(), entry);
401 di<<entry<<"\n";
402 //di<<entry;
403 //if(itr.More()==Standard_True)
404 //di<<" ";
405 }
406 di<<"\n";
407 return 0;
408 }
586db386 409 di << "DDataStd_ChildNodeIterate : Error\n";
7fd59977 410 return 1;
411}
412
413static TDataStd_ChildNodeIterator cni;
414
415//=======================================================================
416//TreeNode : InitChildNodeIterator
417//purpose : InitChildNodeIterator Doc TreeNode AllLevels [ID]
418//=======================================================================
419
420static Standard_Integer DDataStd_InitChildNodeIterator (Draw_Interpretor& di,
421 Standard_Integer n,
422 const char** a)
423{
424 if (n >= 4) {
425 Handle(TDF_Data) DF;
426 if (!DDF::GetDF(a[1],DF)) return 1;
427 Handle(TDataStd_TreeNode) TN, Value;
428
429 Standard_GUID ID;
430 if (n==4)
431 ID=Standard_GUID(TDataStd_TreeNode::GetDefaultTreeID());
432 else{
433 if (!Standard_GUID::CheckGUIDFormat(a[4])) {
586db386 434 di<<"DDataStd_ChildNodeIterator: The format of GUID is invalid\n";
7fd59977 435 return 1;
436 }
437 const Standard_GUID varID(a[4]);
438 ID=varID;
439 }
440
441 if (!DDF::Find(DF, a[2], ID, TN)) return 1;
dde68833 442 const Standard_Boolean AllLevels = (Draw::Atoi(a[3]) != 0);
7fd59977 443 cni.Initialize(TN, AllLevels);
444 return 0;
445 }
586db386 446 di << "DDataStd_InitChildNodeIterator : Error\n";
7fd59977 447 return 1;
448}
449
450//=======================================================================
451//TreeNode : ChildNodeMore
452//purpose : ChildNodeMore
453//=======================================================================
454
455static Standard_Integer DDataStd_ChildNodeMore (Draw_Interpretor& di,
456 Standard_Integer /*n*/,
457 const char** /*a*/)
458{
459 if (cni.More()) {
460 //cout<<"TRUE"<<endl;
586db386 461 di<<"TRUE\n";
7fd59977 462 }
463 else {
464 //cout<<"FALSE"<<endl;
586db386 465 di<<"FALSE\n";
7fd59977 466 }
467 return 0;
468}
469
470//=======================================================================
471//TreeNode : ChildNodeNext
472//purpose : ChildNodeNext
473//=======================================================================
474
475static Standard_Integer DDataStd_ChildNodeNext (Draw_Interpretor& /*di*/,
476 Standard_Integer /*n*/,
477 const char** /*a*/)
478{
479 cni.Next();
480 return 0;
481}
482
483//=======================================================================
484//TreeNode : ChildNodeNextBrother
485//purpose : ChildNodeNext Brother
486//=======================================================================
487
488static Standard_Integer DDataStd_ChildNodeNextBrother (Draw_Interpretor& /*di*/,
489 Standard_Integer /*n*/,
490 const char** /*a*/)
491{
492 cni.NextBrother();
493 return 0;
494}
495
496//=======================================================================
497//TreeNode : ChildNodeValue
498//purpose : ChildNodeValue
499//=======================================================================
500
501static Standard_Integer DDataStd_ChildNodeValue (Draw_Interpretor& di,
502 Standard_Integer /*n*/,
503 const char** /*a*/)
504{
505 TCollection_AsciiString entry;
506 TDF_Tool::Entry(cni.Value()->Label(), entry);
507 //cout<<entry<<endl;
508 di <<entry<<"\n";
509 return 0;
510}
511
512//=======================================================================
513//TreeNode : TreeCommands
514//purpose :
515//=======================================================================
516
517void DDataStd::TreeCommands (Draw_Interpretor& theCommands)
518{
519 static Standard_Boolean done = Standard_False;
520 if (done) return;
521 done = Standard_True;
522
523 const char* g = "DData : Standard Attribute Commands";
524
525 // SET
526
527 theCommands.Add ("SetNode",
528 "SetNode (DOC Entry [GUID])",
529 __FILE__, DDataStd_SetNode, g);
530
531 // EDIT
532
533 theCommands.Add ("AppendNode",
534 "AppendNode (DOC FatherEntry childEntry [fatherGUID])",
535 __FILE__, DDataStd_AppendNode, g);
536
537 theCommands.Add ("PrependNode",
538 "PrependNode (DOC FatherEntry childEntry [fatherGUID])",
539 __FILE__, DDataStd_PrependNode, g);
540
541 theCommands.Add ("InsertNodeBefore",
542 "InsertNodeBefore (DOC TreeNodeEntry TreeNodeWhichHasToBeBefore [GUID])",
543 __FILE__, DDataStd_InsertNodeBefore, g);
544
545 theCommands.Add ("InsertNodeAfter",
546 "InsertNodeAfter (DOC TreeNodeEntry TreeNodeWhichHasToBeAfter [GUID])",
547 __FILE__, DDataStd_InsertNodeAfter, g);
548
549 theCommands.Add ("DetachNode",
550 "DetachNode (DOC TreeNodeEntry [GUID])",
551 __FILE__, DDataStd_DetachNode, g);
552
553 theCommands.Add ("RootNode",
554 "RootNode (DOC TreeNodeEntry [GUID])",
555 __FILE__, DDataStd_RootNode, g);
556
557
558 // TREE BROWSER
559
560 theCommands.Add ("TreeBrowse",
561 "TreeBrowse dfname entry [browsername]",
562 __FILE__, DDataStd_TreeBrowse, g);
563
564 theCommands.Add ("OpenNode",
565 "PRIVATE COMMAND FOR TREE BROWSER!\nReturns the list of sub-TreeNodes : OpenTreeNode browsername [entry]",
566 __FILE__, DDataStd_OpenNode, g);
567
568
569
570 // TREE ITERATOR
571
572 theCommands.Add ("ChildNodeIterate",
573 "ChildNodeIterate Doc TreeNode AllLevels [GUID]",
574 __FILE__, DDataStd_ChildNodeIterate, g);
575
576 theCommands.Add ("InitChildNodeIterator",
577 "InitChildNodeIterator Doc TreeNode AllLevels [GUID]",
578 __FILE__, DDataStd_InitChildNodeIterator, g);
579
580 theCommands.Add ("ChildNodeMore",
581 "ChildNodeMore",
582 __FILE__, DDataStd_ChildNodeMore, g);
583
584 theCommands.Add ("ChildNodeNext",
585 "ChildNodeNext",
586 __FILE__, DDataStd_ChildNodeNext, g);
587
588 theCommands.Add ("ChildNodeNextBrother",
589 "ChildNodeNextBrother",
590 __FILE__, DDataStd_ChildNodeNextBrother, g);
591
592 theCommands.Add ("ChildNodeValue",
593 "ChildNodeValue",
594 __FILE__, DDataStd_ChildNodeValue, g);
595
596}