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