0023403: Crash when parsing an expression with lexical error
[occt.git] / src / XDEDRAW / XDEDRAW_Shapes.cxx
1 // Created on: 2000-08-04
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #include <XDEDRAW_Shapes.ixx>
22
23 #include <DBRep.hxx>
24 #include <DDocStd.hxx>
25
26 #include <TopoDS_Shape.hxx>
27 #include <TopoDS_Compound.hxx>
28 #include <BRep_Builder.hxx>
29
30 #include <gp_Trsf.hxx>
31
32 #include <TDF_Tool.hxx>
33 #include <TDF_Label.hxx>
34 #include <TDF_LabelSequence.hxx>
35 #include <TDocStd_Document.hxx>
36
37 #include <XCAFDoc_DocumentTool.hxx>
38 #include <XCAFDoc_ShapeTool.hxx>
39 #include <XCAFDoc_Location.hxx>
40
41 #include <TCollection_AsciiString.hxx>
42
43 #include <stdio.h>
44 #include <XCAFDoc_GraphNode.hxx>
45 #include <TDF_AttributeSequence.hxx>
46 #include <TopTools_SequenceOfShape.hxx>
47
48 //=======================================================================
49 // Section: Work with shapes
50 //=======================================================================
51
52 static Standard_Integer addShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
53 {
54   if (argc<3) {
55     di<<"Use: "<<argv[0]<<" DocName Shape [int makeAssembly (1/0)]"<<"\n";
56     return 1;
57   }
58   Handle(TDocStd_Document) Doc;   
59   DDocStd::GetDocument(argv[1], Doc);
60   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
61
62   TopoDS_Shape aShape;
63   aShape = DBRep::Get(argv[2]);
64   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
65   Standard_Boolean makeAssembly = Standard_True;
66   if ( argc==4 && atoi(argv[3]) == 0 ) makeAssembly = Standard_False;
67   TDF_Label aLabel;
68   aLabel = myAssembly->AddShape(aShape, makeAssembly);
69   if (aLabel.IsNull()) di<<"Null Label"<<"\n";
70   TCollection_AsciiString Entry;
71   TDF_Tool::Entry(aLabel, Entry);
72   di << Entry.ToCString();
73   return 0;
74 }
75
76 static Standard_Integer newShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
77 {
78   if (argc!=2) {
79     di<<"Use: "<<argv[0]<<" DocName "<<"\n";
80     return 1;
81   }
82   Handle(TDocStd_Document) Doc;
83   TDF_Label aLabel;
84   DDocStd::GetDocument(argv[1], Doc);
85   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
86
87   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
88 //XCAFDoc_ShapeTool myAssembly;
89 //  myAssembly.Init(Doc);
90   aLabel=myAssembly->NewShape();
91   //  di<<"New Shape at ChildTag"<<aLabel.Tag()<<"\n";
92   TCollection_AsciiString Entry;
93   TDF_Tool::Entry(aLabel, Entry);
94   di << Entry.ToCString();
95   return 0;
96 }
97
98 static Standard_Integer setShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
99 {
100   if (argc!=4) {
101     di<<"Use: "<<argv[0]<<" DocName Label Shape "<<"\n";
102     return 1;
103   }
104   Handle(TDocStd_Document) Doc;   
105   DDocStd::GetDocument(argv[1], Doc);
106   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
107
108   TDF_Label aLabel;
109   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
110   TopoDS_Shape aShape;
111   //   if (aLabel.IsNull()) di<<"Null Label"<<"\n";
112   aShape = DBRep::Get(argv[3]);
113 //  XCAFDoc_ShapeTool myAssembly;
114 //  myAssembly.Init(Doc);
115   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
116   myAssembly->SetShape(aLabel, aShape);
117   return 0;
118 }
119
120 static Standard_Integer getShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
121 {
122   if (argc!=4) {
123     di<<"Use: "<<argv[0]<<" Result DocName Label"<<"\n";
124     return 1;
125   }
126   Handle(TDocStd_Document) Doc;   
127   DDocStd::GetDocument(argv[2], Doc);
128   if ( Doc.IsNull() ) { di << argv[2] << " is not a document" << "\n"; return 1; }
129
130   TDF_Label aLabel;
131   TDF_Tool::Label(Doc->GetData(), argv[3], aLabel);
132   if (aLabel.IsNull()) {di<<"No such Label"<<"\n"; return 1;}
133   TopoDS_Shape aShape;
134 //  XCAFDoc_ShapeTool myAssembly;
135 //  myAssembly.Init(Doc);
136   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
137   aShape = myAssembly->GetShape(aLabel);
138   Standard_CString name1 = argv[1];
139   DBRep::Set(name1, aShape);
140   
141   return 0;
142 }
143
144 static Standard_Integer removeShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
145 {
146   if (argc != 3 && argc != 4)
147   {
148     di<<"Use: "<<argv[0]<<" DocName Label [int removeCompletely (1/0)]"<<"\n";
149     return 1;
150   }
151   Handle(TDocStd_Document) Doc;   
152   DDocStd::GetDocument(argv[1], Doc);
153   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
154
155   TDF_Label aLabel;
156   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
157   if (aLabel.IsNull()) {di<<"No such Label"<<"\n"; return 1;}
158   TopoDS_Shape aShape;
159 //  XCAFDoc_ShapeTool myAssembly;
160 //  myAssembly.Init(Doc);
161   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
162   Standard_Boolean removeCompletely = Standard_True;
163   if ( argc == 4 && atoi(argv[3]) == 0 ) 
164     removeCompletely = Standard_False;
165   myAssembly->RemoveShape(aLabel, removeCompletely);
166   
167   return 0;
168 }
169
170 static Standard_Integer findShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
171 {
172   if (argc!=3) {
173     di<<"Use: "<<argv[0]<<" DocName Shape"<<"\n";
174     return 1;
175   }
176   Handle(TDocStd_Document) Doc;   
177   DDocStd::GetDocument(argv[1], Doc);
178   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
179
180   TDF_Label aLabel;
181   TopoDS_Shape aShape;
182   aShape = DBRep::Get(argv[2]);
183 //  XCAFDoc_ShapeTool myAssembly;
184 //  myAssembly.Init(Doc);
185   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
186   aLabel = myAssembly->FindShape(aShape);
187   TCollection_AsciiString Entry;
188   TDF_Tool::Entry(aLabel, Entry);
189   di << Entry.ToCString();
190   //di<<"Label with Shape is "<<Entry<<"\n";
191   return 0;
192 }
193
194 static Standard_Integer labelInfo (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
195 {
196   if (argc!=3) {
197     di<<"Use: "<<argv[0]<<" DocName Label "<<"\n";
198     return 1;
199   }
200   Handle(TDocStd_Document) Doc;   
201   DDocStd::GetDocument(argv[1], Doc);
202   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
203
204   TDF_Label aLabel;
205   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
206 //  XCAFDoc_ShapeTool myAssembly;
207 //  myAssembly.Init(Doc);
208   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
209   TCollection_AsciiString Entry;
210
211   if ( myAssembly->IsShape(aLabel) ) {
212     //di<<"There are a TopLevelShape"<<"\n";
213     Entry="There are a TopLevelShape";
214     di << Entry.ToCString();
215   }
216   if ( myAssembly->IsComponent(aLabel) ) {
217     //di<<"There are a Component"<<"\n";
218     Entry="There are a Component";
219     di << Entry.ToCString();
220   }
221   if ( myAssembly->IsAssembly(aLabel) ) {
222     //di<<"There are an Assembly"<<"\n";
223     Entry="There are an Assembly";
224     di << Entry.ToCString();
225   }
226   if ( myAssembly->IsFree(aLabel) ) {
227     //di<<"This Shape don't used"<<"\n";
228     Entry="This Shape don't used";
229     di << Entry.ToCString();
230   }
231   return 0;
232 }
233
234 static Standard_Integer getUsers (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
235 {
236   if (argc<3) {
237     di<<"Use: "<<argv[0]<<" Doc Label [withSubChilds(int)]"<<"\n";
238     return 1;
239   }
240   Standard_Boolean getsubchilds = Standard_False;
241   if ( (argc==4) && ( atoi(argv[3])==1 ) ) getsubchilds = Standard_True;
242   
243   Handle(TDocStd_Document) Doc;   
244   DDocStd::GetDocument(argv[1], Doc);
245   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
246
247   TDF_Label aLabel;
248   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
249   TDF_LabelSequence labseq;
250   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
251   TCollection_AsciiString Entry;
252   Entry=myAssembly->GetUsers(aLabel, labseq, getsubchilds);
253   di << Entry.ToCString();
254   //di<<myAssembly->GetUsers(aLabel, labseq, getsubchilds)<<" assemblies use this component"<<"\n";
255   return 0;
256 }
257
258 static Standard_Integer nbComponents (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
259 {
260   if (argc<3) {
261     di<<"Use: "<<argv[0]<<" Doc Label [withSubChilds(int)]"<<"\n";
262     return 1;
263   }
264   Standard_Boolean getsubchilds = Standard_False;
265   if ( (argc==4) && ( atoi(argv[3])==1 ) ) getsubchilds = Standard_True;
266   Handle(TDocStd_Document) Doc;   
267   DDocStd::GetDocument(argv[1], Doc);
268   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
269
270   TDF_Label aLabel;
271   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
272   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
273 //  XCAFDoc_ShapeTool myAssembly->
274 //  myAssembly->Init(Doc);
275   //di<<"This assembly has ";
276   TCollection_AsciiString Entry;
277   Entry=myAssembly->NbComponents( aLabel, getsubchilds);
278   di << Entry.ToCString();
279   //di<<" components"<<"\n";
280   //di<<"This assembly has "<<myAssembly->NbComponents( aLabel, getsubchilds )<<" components"<<"\n";
281
282   return 0;
283 }
284
285 static Standard_Integer addComponent (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
286 {
287   if (argc!=4) {
288     di<<"Use: "<<argv[0]<<" DocName Label Shape "<<"\n";
289     return 1;
290   }
291   Handle(TDocStd_Document) Doc;   
292   DDocStd::GetDocument(argv[1], Doc);
293   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
294
295   TDF_Label aLabel;
296   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
297   TopoDS_Shape aShape;
298   aShape = DBRep::Get(argv[3]);
299   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
300 //  XCAFDoc_ShapeTool myAssembly->
301 //  myAssembly->Init(Doc);
302   myAssembly->AddComponent(aLabel, aShape);
303   TCollection_AsciiString Entry;
304   TDF_Tool::Entry(aLabel, Entry);
305   di << Entry.ToCString();
306   
307   return 0;
308 }
309
310 static Standard_Integer removeComponent (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
311 {
312   if (argc!=3) {
313     di<<"Use: "<<argv[0]<<" DocName Label "<<"\n";
314     return 1;
315   }
316   Handle(TDocStd_Document) Doc;   
317   DDocStd::GetDocument(argv[1], Doc);
318   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
319
320   TDF_Label aLabel;
321   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
322   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
323 //  XCAFDoc_ShapeTool myAssembly->
324 //  myAssembly->Init(Doc);
325   myAssembly->RemoveComponent(aLabel);
326   return 0;
327 }
328
329 static Standard_Integer getReferredShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
330 {
331   if (argc!=3) {
332     di<<"Use: "<<argv[0]<<" DocName Label "<<"\n";
333     return 1;
334   }
335   Handle(TDocStd_Document) Doc;   
336   DDocStd::GetDocument(argv[1], Doc);
337   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
338
339   TDF_Label aLabel, RootLabel;
340   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
341   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
342 //  XCAFDoc_ShapeTool myAssembly->
343 //  myAssembly->Init(Doc);
344   myAssembly->GetReferredShape(aLabel, RootLabel);
345   
346   TCollection_AsciiString Entry;
347   TDF_Tool::Entry(RootLabel, Entry);
348   //di<<"Label with Shape is ";
349   di << Entry.ToCString();
350   return 0;
351 }
352
353 static Standard_Integer getTopLevelShapes (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
354 {
355   if (argc!=2) {
356     di<<"Use: "<<argv[0]<<" DocName "<<"\n";
357     return 1;
358   }
359   Handle(TDocStd_Document) Doc;   
360   DDocStd::GetDocument(argv[1], Doc);
361   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
362
363   TDF_Label aLabel;
364   TDF_LabelSequence Labels;
365   
366   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
367 //  XCAFDoc_ShapeTool myAssembly->
368 //  myAssembly->Init(Doc);
369   myAssembly->GetShapes(Labels);
370   TCollection_AsciiString Entry;
371   if (Labels.Length() >= 1) {
372     for ( Standard_Integer i = 1; i<= Labels.Length(); i++) {
373       aLabel = Labels.Value(i);
374       TDF_Tool::Entry( aLabel, Entry);
375       di << Entry.ToCString() << " ";
376     }
377   }
378   return 0;
379 }
380
381 static Standard_Integer getFreeShapes (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
382 {
383   if (argc <2) {
384     di<<"Use: "<<argv[0]<<" DocName [shape_prefix]"<<"\n";
385     return 1;
386   }
387   
388   Handle(TDocStd_Document) Doc;   
389   DDocStd::GetDocument(argv[1], Doc);
390   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
391
392   TDF_LabelSequence Labels;
393   Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
394   STool->GetFreeShapes(Labels);
395   if ( Labels.Length() <=0 ) {
396     di << "Document " << argv[1] << " contain no shapes" << "\n";
397     return 0;
398   }
399   
400   if ( argc ==2 ) {
401     for ( Standard_Integer i = 1; i<= Labels.Length(); i++) {
402       TCollection_AsciiString Entry;
403       TDF_Tool::Entry( Labels.Value(i), Entry);
404       di << Entry.ToCString() << " ";
405     }
406   }
407   else if ( Labels.Length() ==1 ) {
408     TopoDS_Shape S = STool->GetShape ( Labels.Value(1) );
409     DBRep::Set ( argv[2], S );
410     di << argv[2];
411   }
412   else {
413     for ( Standard_Integer i = 1; i<= Labels.Length(); i++) {
414       TopoDS_Shape S = STool->GetShape ( Labels.Value(i) );
415       char string[260];
416       sprintf ( string, "%s_%d", argv[2], i );
417       DBRep::Set ( string, S );
418       di << string << " ";
419     }
420   }
421   return 0;
422 }
423
424 static Standard_Integer getOneShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
425 {
426   if (argc!=3) {
427     di<<"Use: "<<argv[0]<<" shape DocName "<<"\n";
428     return 1;
429   }
430   
431   Handle(TDocStd_Document) Doc;   
432   DDocStd::GetDocument(argv[2], Doc);
433   if ( Doc.IsNull() ) { di << argv[2] << " is not a document" << "\n"; return 1; }
434
435   TDF_LabelSequence Labels;
436   Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
437   STool->GetFreeShapes(Labels);
438   if ( Labels.Length() <=0 ) {
439     di << "Document " << argv[2] << " contain no shapes" << "\n";
440     return 0;
441   }
442   
443   if ( Labels.Length() ==1 ) {
444     TopoDS_Shape S = STool->GetShape ( Labels.Value(1) );
445     DBRep::Set ( argv[1], S );
446   }
447   else {
448     TopoDS_Compound C;
449     BRep_Builder B;
450     B.MakeCompound ( C );
451     for ( Standard_Integer i = 1; i<= Labels.Length(); i++) {
452       TopoDS_Shape S = STool->GetShape ( Labels.Value(i) );
453       B.Add ( C, S );
454     }
455     DBRep::Set ( argv[1], C );
456   }
457   di << argv[1];
458   return 0;
459 }
460
461 //=======================================================================
462 //function : XDumpLocation
463 //purpose  : Dump Transformation() of XCAFDoc_Location attribute
464 //=======================================================================
465 static Standard_Integer XDumpLocation (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
466 {
467   if (argc != 3)
468   {
469     di << "Use: " << argv[0] << " Doc Label " << "\n";
470     return 1;
471   }
472   Handle(TDocStd_Document) Doc;   
473   DDocStd::GetDocument(argv[1], Doc);
474   if (Doc.IsNull())
475   {
476     di << argv[1] << " is not a document" << "\n";
477     return 1;
478   }
479
480   TDF_Label aLabel;
481   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
482
483   Handle(XCAFDoc_Location) aLoc;
484   if (!aLabel.FindAttribute(XCAFDoc_Location::GetID(), aLoc))
485   {
486     di << "Label " << argv[2] << " doesn't contain XCAFDoc_Location attribute" << "\n";
487     return 1;
488   }
489   
490   TopLoc_Location aTopLoc = aLoc->Get();
491   gp_Trsf aTrsf = aTopLoc.Transformation();
492
493   di << "Transformation (3 rows * 4 columns matrix):";
494   for (int i = 1; i <= 3; i++) // row number
495   {
496     di << " (";
497     for (int j = 1; j <= 4; j++) // column number
498     {
499       if (j > 1) di << ",";
500       di << TCollection_AsciiString(aTrsf.Value(i,j)).ToCString();
501     }
502     di << ")";
503   }
504
505   return 0;
506 }
507
508 static Standard_Integer setSHUO (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
509 {
510   if (argc < 4)
511   {
512     di << "Use: " << argv[0] << " Doc UU_Label NU_Label " << "\n";
513     return 1;
514   }
515   Handle(TDocStd_Document) Doc;   
516   DDocStd::GetDocument(argv[1], Doc);
517   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
518   
519   TDF_LabelSequence aLabSeq;
520   for (Standard_Integer i = 3; i <= argc; i++) {
521     TDF_Label L;
522     TDF_Tool::Label(Doc->GetData(), argv[i - 1], L);
523     if (!L.IsNull())
524       aLabSeq.Append( L );
525     else
526       di << argv[i - 1] << " is null label"  << "\n";
527   }
528   if (aLabSeq.Length() < 2) {
529     di << "Error: couldnot set SHUO between on less then 2 labels" << "\n";
530   }
531   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
532   Handle(XCAFDoc_GraphNode) aMainSHUO;
533   myAssembly->SetSHUO( aLabSeq, aMainSHUO );
534   if (aMainSHUO.IsNull()) {
535     di << "Error: cannot set the SHUO" << "\n";
536     return 1;
537   }
538     
539   return 0;
540 }
541
542 static Standard_Integer getSHUOUpperUsage (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
543 {
544   if (argc < 3)
545   {
546     di << "Use: " << argv[0] << " Doc NU_Label " << "\n";
547     return 1;
548   }
549   Handle(TDocStd_Document) Doc;   
550   DDocStd::GetDocument(argv[1], Doc);
551   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
552   TDF_Label NL;
553   TDF_Tool::Label(Doc->GetData(), argv[2], NL);
554   if (NL.IsNull()) {
555     di << argv[2] << " is null label"  << "\n";
556     return 1;
557   }
558   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
559   TDF_LabelSequence labseq;
560   myAssembly->GetSHUOUpperUsage( NL, labseq );
561   TCollection_AsciiString Entry;
562   if (labseq.Length() >= 1) {
563     for ( Standard_Integer i = 1; i<= labseq.Length(); i++) {
564       TDF_Label aLabel = labseq.Value(i);
565       TDF_Tool::Entry( aLabel, Entry);
566       di << Entry.ToCString() << " ";
567     }
568   }
569   return 0;
570 }
571
572 static Standard_Integer getSHUONextUsage (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
573 {
574   if (argc < 3)
575   {
576     di << "Use: " << argv[0] << " Doc UU_Label " << "\n";
577     return 1;
578   }
579   Handle(TDocStd_Document) Doc;   
580   DDocStd::GetDocument(argv[1], Doc);
581   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
582   TDF_Label UL;
583   TDF_Tool::Label(Doc->GetData(), argv[2], UL);
584   if (UL.IsNull()) {
585     di << argv[2] << " is null label"  << "\n";
586     return 1;
587   }
588   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
589   TDF_LabelSequence labseq;
590   myAssembly->GetSHUONextUsage( UL, labseq );
591   TCollection_AsciiString Entry;
592   if (labseq.Length() >= 1) {
593     for ( Standard_Integer i = 1; i<= labseq.Length(); i++) {
594       TDF_Label aLabel = labseq.Value(i);
595       TDF_Tool::Entry( aLabel, Entry);
596       di << Entry.ToCString() << " ";
597     }
598   }
599   return 0;
600 }
601
602 static Standard_Integer removeSHUO (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
603 {
604   if (argc < 3)
605   {
606     di << "Use: " << argv[0] << " Doc SHUOComponent_Label " << "\n";
607     return 1;
608   }
609   Handle(TDocStd_Document) Doc;   
610   DDocStd::GetDocument(argv[1], Doc);
611   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
612   TDF_Label UL;
613   TDF_Tool::Label(Doc->GetData(), argv[2], UL);
614   if (UL.IsNull()) {
615     di << argv[2] << " is null label"  << "\n";
616     return 1;
617   }
618   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
619   myAssembly->RemoveSHUO( UL );
620     
621   return 0;
622 }
623
624 static Standard_Integer hasSHUO (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
625 {
626   if (argc < 3)
627   {
628     di << "Use: " << argv[0] << " Doc SHUO_Label " << "\n";
629     return 1;
630   }
631   Handle(TDocStd_Document) Doc;   
632   DDocStd::GetDocument(argv[1], Doc);
633   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
634   TDF_Label UL;
635   TDF_Tool::Label(Doc->GetData(), argv[2], UL);
636   if (UL.IsNull()) {
637     di << argv[2] << " is null label"  << "\n";
638     return 1;
639   }
640   Handle(XCAFDoc_GraphNode) anAttrSHUO;
641   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
642   if (myAssembly->GetSHUO( UL, anAttrSHUO ))
643     di << 1; 
644   else
645     di << 0;
646   
647   return 0;
648 }
649
650 static Standard_Integer getAllSHUO (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
651 {
652   if (argc < 3)
653   {
654     di << "Use: " << argv[0] << " Doc SHUO_Label " << "\n";
655     return 1;
656   }
657   Handle(TDocStd_Document) Doc;   
658   DDocStd::GetDocument(argv[1], Doc);
659   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
660   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
661   TDF_Label UL;
662   TDF_Tool::Label(Doc->GetData(), argv[2], UL);
663   if (UL.IsNull()) {
664     di << argv[2] << " is null label"  << "\n";
665     return 1;
666   }
667   TDF_AttributeSequence SHUOAttrs;
668   myAssembly->GetAllComponentSHUO( UL, SHUOAttrs );
669   TCollection_AsciiString Entry;
670   if (SHUOAttrs.Length() >= 1) {
671     for ( Standard_Integer i = 1; i<= SHUOAttrs.Length(); i++) {
672       TDF_Label aLabel = SHUOAttrs.Value(i)->Label();
673       TDF_Tool::Entry( aLabel, Entry);
674       di << Entry.ToCString() << " ";
675     }
676   }
677   return 0;
678 }
679
680 static Standard_Integer findComponent (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
681 {
682   if (argc < 3)
683   {
684     di << "Use: " << argv[0] << " Doc shape " << "\n";
685     return 1;
686   }
687   Handle(TDocStd_Document) Doc;   
688   DDocStd::GetDocument(argv[1], Doc);
689   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
690   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
691   TopoDS_Shape aShape;
692   aShape = DBRep::Get(argv[2]);
693   TDF_LabelSequence labseq;
694   myAssembly->FindComponent( aShape, labseq );
695   TCollection_AsciiString Entry;
696   if (labseq.Length() >= 1) {
697     for ( Standard_Integer i = 1; i<= labseq.Length(); i++) {
698       TDF_Label aLabel = labseq.Value(i);
699       TDF_Tool::Entry( aLabel, Entry);
700       di << Entry.ToCString() << " ";
701     }
702   }
703   return 0;
704 }
705
706 static Standard_Integer getStyledComponent (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
707 {
708   if (argc < 4)
709   {
710     di << "Use: " << argv[0] << " Doc res SHUO_label " << "\n";
711     return 1;
712   }
713   Handle(TDocStd_Document) Doc;   
714   DDocStd::GetDocument(argv[1], Doc);
715   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
716   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
717   TopoDS_Shape aShape;
718   TDF_Label aLabel;
719   TDF_Tool::Label(Doc->GetData(), argv[3], aLabel);
720   Handle(XCAFDoc_GraphNode) SHUO;
721   if (myAssembly->GetSHUO( aLabel, SHUO ))
722     aShape = myAssembly->GetSHUOInstance( SHUO );
723   
724   if (aShape.IsNull()) {
725     di << "cannot get component" << "\n";
726     return 1;
727   }
728   DBRep::Set ( argv[2], aShape );
729   di << argv[2];
730   return 0;
731 }
732
733 static Standard_Integer getAllStyledComponents (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
734 {
735   if (argc < 4)
736   {
737     di << "Use: " << argv[0] << " Doc res SHUO_label " << "\n";
738     return 1;
739   }
740   Handle(TDocStd_Document) Doc;   
741   DDocStd::GetDocument(argv[1], Doc);
742   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
743   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
744   TopTools_SequenceOfShape aShapes;
745   TDF_Label aLabel;
746   TDF_Tool::Label(Doc->GetData(), argv[3], aLabel);
747   Handle(XCAFDoc_GraphNode) SHUO;
748   if (myAssembly->GetSHUO( aLabel, SHUO ))
749     if (myAssembly->GetAllSHUOInstances(SHUO, aShapes)) {
750       TopoDS_Compound aShape;
751       BRep_Builder B;
752       B.MakeCompound(aShape);
753       for (Standard_Integer jj = 1; jj <= aShapes.Length(); jj++) {
754         TopoDS_Shape aCurShape = aShapes.Value(jj);
755         B.Add( aShape, aCurShape );
756       }
757       DBRep::Set ( argv[2], aShape );
758       di << argv[2];
759     }
760       
761   return 0;
762 }
763
764 static Standard_Integer findSHUO (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
765 {
766   if (argc < 4)
767   {
768     di << "Use: " << argv[0] << " Doc labels " << "\n";
769     return 1;
770   }
771   Handle(TDocStd_Document) Doc;   
772   DDocStd::GetDocument(argv[1], Doc);
773   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
774   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
775   TDF_LabelSequence aLabSeq;
776   for (Standard_Integer i = 3; i <= argc; i++) {
777     TDF_Label L;
778     TDF_Tool::Label(Doc->GetData(), argv[i - 1], L);
779     if (!L.IsNull())
780       aLabSeq.Append( L );
781     else
782       di << argv[i - 1] << " is null label"  << "\n";
783   }
784   if (aLabSeq.Length() < 2) {
785     di << "Error: couldnot find SHUO between on less then 2 labels" << "\n";
786   }
787   Handle(XCAFDoc_GraphNode) SHUO;
788   myAssembly->FindSHUO( aLabSeq, SHUO );
789   if (SHUO.IsNull()) {
790     di << "cannot find SHUO" << "\n";
791     return 1;
792   }
793   TCollection_AsciiString Entry;
794   TDF_Tool::Entry( SHUO->Label(), Entry);
795   di << Entry.ToCString() << " ";
796   
797   return 0;
798 }
799
800 static Standard_Integer setStyledComponent (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
801 {
802   if (argc < 3)
803   {
804     di << "Use: " << argv[0] << " Doc shape " << "\n";
805     return 1;
806   }
807   Handle(TDocStd_Document) Doc;   
808   DDocStd::GetDocument(argv[1], Doc);
809   if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
810   Handle(XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
811   TopoDS_Shape aShape;
812   aShape = DBRep::Get(argv[2]);
813   if (aShape.IsNull()) {
814     di << "Shape " << argv[2] << " is null" << "\n";
815     return 1;
816   }
817   Handle(XCAFDoc_GraphNode) aSHUOAttr;
818   aSHUOAttr = myAssembly->SetInstanceSHUO( aShape );
819   if (aSHUOAttr.IsNull()) {
820     di << "Error: cannot set a SHUO structure for indicated component"  << "\n";
821     return 1;
822   }
823   TCollection_AsciiString Entry;
824   TDF_Tool::Entry( aSHUOAttr->Label(), Entry);
825   di << Entry.ToCString() << " ";
826   
827   return 0;
828 }
829 //=======================================================================
830 //function : InitCommands
831 //purpose  : 
832 //=======================================================================
833
834 void XDEDRAW_Shapes::InitCommands(Draw_Interpretor& di) 
835 {
836
837   static Standard_Boolean initactor = Standard_False;
838   if (initactor) return;  initactor = Standard_True;
839
840   //=====================================
841   // Work with shapes
842   //=====================================  
843   
844   Standard_CString g = "XDE shape's commands";
845
846   di.Add ("XAddShape","Doc Shape [makeAssembly = 1]\t: Add shape (or assembly) to Document",
847                    __FILE__, addShape, g);
848
849   di.Add ("XNewShape","Doc \t: Create new empty top-level shape",
850                    __FILE__, newShape, g);
851
852   di.Add ("XSetShape","Doc Label Shape \t: Set shape at indicated label",
853                    __FILE__, setShape, g);
854
855   di.Add ("XGetShape","Result Doc Label \t: Put shape from tree to Result",
856                    __FILE__, getShape, g);
857
858   di.Add ("XRemoveShape","Doc Label \t: Remove shape from document",
859                    __FILE__, removeShape, g);
860
861   di.Add ("XFindShape","Doc Shape \t: Find and print label with indicated top-level shape",
862                    __FILE__, findShape, g);
863
864   di.Add ("XLabelInfo","Doc Label \t: Print information about object at following label",
865                    __FILE__, labelInfo, g);
866
867   di.Add ("XGetUsers","Doc Label [withSubChilds(int)] \t: Print number of assemblies that use shape at following label",
868                    __FILE__, getUsers, g);
869
870   di.Add ("XNbComponents","Doc Label [withSubChilds(int)] \t: Print number of component of assembly ",
871                    __FILE__, nbComponents, g);
872
873   di.Add ("XAddComponent","Doc Label Shape \t: Add component shape to assembly",
874                    __FILE__, addComponent, g);
875
876   di.Add ("XRemoveComponent","Doc Label \t: Remove component from components label",
877                    __FILE__, removeComponent, g);
878
879   di.Add ("XGetReferredShape","Doc Label \t: Print label, that contain a top-level shape, that corresponds shape at following label",
880                    __FILE__, getReferredShape, g);
881
882   di.Add ("XGetTopLevelShapes","Doc \t: Print labels, that contain a top-level shapes",
883                    __FILE__, getTopLevelShapes, g);
884
885   di.Add ("XGetFreeShapes","Doc [shape_prefix]\t: Print labels or create DRAW shapes for all free shapes in the Doc",
886                    __FILE__, getFreeShapes, g);
887
888   di.Add ("XGetOneShape","shape Doc \t: Put all free shapes of the Doc into signle DRAW shape",
889                    __FILE__, getOneShape, g);
890
891   di.Add ("XDumpLocation","Doc Label \t: Dump Transformation() of XCAFDoc_Location attribute",
892                    __FILE__, XDumpLocation, g);
893
894   di.Add ("XSetSHUO","Doc UU_Label [ multi-level labels ] NU_Label \t: sets the SHUO structure between UpperUsage and NextUsage",
895                    __FILE__, setSHUO, g);
896
897   di.Add ("XGetUU_SHUO","Doc NU_Label \t: prints the UpperUsages of indicated NextUsage",
898                    __FILE__, getSHUOUpperUsage, g);
899
900   di.Add ("XGetNU_SHUO","Doc UU_Label \t: prints the NextUsages of indicated UpperUsage",
901                    __FILE__, getSHUONextUsage, g);
902
903   di.Add ("XRemoveSHUO","Doc SHUO_Label \t: remove SHUO of indicated component",
904                    __FILE__, removeSHUO, g);
905
906   di.Add ("XIsHasSHUO","Doc SHUO_Label \t: remove SHUO of indicated component",
907                    __FILE__, hasSHUO, g);
908
909   di.Add ("XGetAllSHUO","Doc Comp_Label \t: remove SHUO of indicated component",
910                    __FILE__, getAllSHUO, g);
911
912   di.Add ("XFindComponent","Doc Shape \t: prints sequence of labels of assembly path",
913                    __FILE__, findComponent, g);
914   
915   di.Add ("XGetSHUOInstance","Doc res SHUO_Label \t: returns SHUO_styled shape",
916                    __FILE__, getStyledComponent, g);
917   
918   di.Add ("XGetAllSHUOInstances","Doc res SHUO_Label \t: returns SHUO_styled shapes as compound",
919                    __FILE__, getAllStyledComponents, g);
920   
921   di.Add ("XFindSHUO","Doc labels of SHUO structure \t: prints label of SHUO that found by labels structure",
922                    __FILE__, findSHUO, g);
923
924   di.Add ("XSetInstanceSHUO","Doc shape \t: sets the SHUO structure for indicated component",
925                    __FILE__, setStyledComponent, g);
926   
927 }