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