0031313: Foundation Classes - Dump improvement for classes
[occt.git] / src / XDEDRAW / XDEDRAW_Layers.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
7fd59977 16
17#include <DBRep.hxx>
18#include <DDocStd.hxx>
42cf5bc1 19#include <Draw.hxx>
7fd59977 20#include <TCollection_AsciiString.hxx>
21#include <TColStd_HSequenceOfExtendedString.hxx>
7fd59977 22#include <TDF_Label.hxx>
23#include <TDF_LabelSequence.hxx>
42cf5bc1 24#include <TDF_Tool.hxx>
7fd59977 25#include <TDocStd_Document.hxx>
42cf5bc1 26#include <TopoDS_Shape.hxx>
9e6cdbca 27#include <XCAFDoc.hxx>
7fd59977 28#include <XCAFDoc_DocumentTool.hxx>
9e6cdbca 29#include <XCAFDoc_GraphNode.hxx>
7fd59977 30#include <XCAFDoc_LayerTool.hxx>
42cf5bc1 31#include <XCAFDoc_ShapeTool.hxx>
32#include <XDEDRAW_Layers.hxx>
7fd59977 33
34//=======================================================================
35// Section: Work with layers
36//=======================================================================
7fd59977 37static Standard_Integer addLayer (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
38{
39 if (argc!=3) {
586db386 40 di<<"Use: "<<argv[0]<<" DocName StringLayer \n";
7fd59977 41 return 1;
42 }
43 Handle(TDocStd_Document) Doc;
44 DDocStd::GetDocument(argv[1], Doc);
586db386 45 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 46 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
47
48 TCollection_ExtendedString aLayer = argv[2];
49 TDF_Label aLabel = localLayerTool->AddLayer(aLayer);
50 TCollection_AsciiString Entry;
51 TDF_Tool::Entry(aLabel, Entry);
52 di << Entry.ToCString();
53 return 0;
54}
55
56static Standard_Integer findLayer (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
57{
58 if (argc!=3) {
586db386 59 di<<"Use: "<<argv[0]<<" DocName StringLayer \n";
7fd59977 60 return 1;
61 }
62 Handle(TDocStd_Document) Doc;
63 DDocStd::GetDocument(argv[1], Doc);
586db386 64 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 65 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
66
67 TCollection_ExtendedString aLayer = argv[2];
68 TDF_Label aLabel = localLayerTool->FindLayer(aLayer);
69 TCollection_AsciiString Entry;
70 TDF_Tool::Entry(aLabel, Entry);
71 di << Entry.ToCString();
72 return 0;
73}
74
75static Standard_Integer removeLayer (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
76{
77 if (argc!=3) {
586db386 78 di<<"Use: "<<argv[0]<<" DocName {Label|string}\n";
7fd59977 79 return 1;
80 }
81 Handle(TDocStd_Document) Doc;
82 DDocStd::GetDocument(argv[1], Doc);
586db386 83 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 84 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
85
86 TDF_Label aLabel;
87 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
88 if ( aLabel.IsNull() ) {
89 TCollection_ExtendedString aLayer = argv[2];
90 aLabel = localLayerTool->FindLayer(aLayer);
91 }
92 if ( aLabel.IsNull() ) return 1;
93 localLayerTool->RemoveLayer( aLabel);
94 return 0;
95}
96
97
98static Standard_Integer setLayer (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
99{
100 if (argc<4) {
586db386 101 di<<"Use: "<<argv[0]<<" DocName {Shape|Label} StringLayer [shapeInOneLayer(0/1)]\n";
7fd59977 102 return 1;
103 }
104 Standard_Boolean shapeInOneLayer = Standard_False;
91322f44 105 if ( (argc==5) && (Draw::Atoi(argv[4])==1) ) shapeInOneLayer = Standard_True;
7fd59977 106 Handle(TDocStd_Document) Doc;
107 DDocStd::GetDocument(argv[1], Doc);
586db386 108 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 109 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
110 TDF_Label aLabel;
111 TCollection_ExtendedString aLayer = argv[3];
112
113 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
114 if ( !aLabel.IsNull() ) {
115 localLayerTool->SetLayer(aLabel, aLayer, shapeInOneLayer);
116 }
117 else {
118 TopoDS_Shape aShape= DBRep::Get(argv[2]);
119 if ( !aShape.IsNull() ) {
120 localLayerTool->SetLayer(aShape, aLayer, shapeInOneLayer);
121 }
122 }
123 return 0;
124}
125
126
127static Standard_Integer getLayers (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
128{
129 if (argc!=3) {
586db386 130 di<<"Use: "<<argv[0]<<" DocName {Shape|Label} \n";
7fd59977 131 return 1;
132 }
133 //
134 Handle(TDocStd_Document) Doc;
135 DDocStd::GetDocument(argv[1], Doc);
586db386 136 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 137 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
138 TDF_Label aLabel;
139 Handle(TColStd_HSequenceOfExtendedString) aLayerS;
140 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
141 if ( !aLabel.IsNull() ) {
142 localLayerTool->GetLayers(aLabel, aLayerS);
143 }
144 else {
145 TopoDS_Shape aShape= DBRep::Get(argv[2]);
146 if ( !aShape.IsNull() ) {
147 localLayerTool->GetLayers(aShape, aLayerS);
148 }
149 }
150 Standard_Integer i = 1;
151 if (!aLayerS.IsNull() && aLayerS->Length()!=0)
152 for (; i <= aLayerS->Length(); i++) {
153 TCollection_AsciiString Entry(aLayerS->Value(i),'?');
586db386 154 di << "\"" << Entry.ToCString() << "\" ";
7fd59977 155 }
156 return 0;
157}
158
159
160static Standard_Integer getLayerLabels (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
161{
162 if (argc!=2) {
586db386 163 di<<"Use: "<<argv[0]<<" DocName\n";
7fd59977 164 return 1;
165 }
166 Handle(TDocStd_Document) Doc;
167 DDocStd::GetDocument(argv[1], Doc);
586db386 168 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 169 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
170 TDF_LabelSequence aLabs;
171 localLayerTool->GetLayerLabels(aLabs);
172 if (aLabs.Length() ==0 ) {
173 return 0;
174 }
175 Standard_Integer i = 1;
176 for (; i <= aLabs.Length(); i++) {
177 TDF_Label L = aLabs.Value(i);
178 if ( !L.IsNull() ) {
179 TCollection_AsciiString Entry;
180 TDF_Tool::Entry(L, Entry);
181 di << Entry.ToCString() << " ";
182 }
183 }
184 return 0;
185}
186
187
188static Standard_Integer getOneLayer (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
189{
190 if (argc!=3) {
586db386 191 di<<"Use: "<<argv[0]<<" DocName LayerLabel\n";
7fd59977 192 return 1;
193 }
194 Handle(TDocStd_Document) Doc;
195 DDocStd::GetDocument(argv[1], Doc);
586db386 196 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 197 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
198 TDF_Label aLabel;
199 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
200 if ( !aLabel.IsNull() ) {
201 TCollection_ExtendedString layerName;
202 localLayerTool->GetLayer(aLabel, layerName);
203 TCollection_AsciiString Entry(layerName,'?');
204 di << "\"" << Entry.ToCString() <<"\"";
205 }
206 return 0;
207}
208
209
210static Standard_Integer setLinkLayer (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
211{
212 if (argc<4) {
586db386 213 di<<"Use: "<<argv[0]<<" DocName {Shape|Label} LayerL [shapeInOneLayer(0/1)]\n";
7fd59977 214 return 1;
215 }
216 Standard_Boolean shapeInOneLayer = Standard_False;
91322f44 217 if ( (argc==5) && (Draw::Atoi(argv[4])==1) ) shapeInOneLayer = Standard_True;
7fd59977 218 Handle(TDocStd_Document) Doc;
219 DDocStd::GetDocument(argv[1], Doc);
586db386 220 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 221 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
222 TDF_Label aLabel, layerLabel;
223 TDF_Tool::Label(Doc->GetData(), argv[3], layerLabel);
224
225 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
226 if ( !layerLabel.IsNull() ) {
227 if ( !aLabel.IsNull() ) {
228 localLayerTool->SetLayer(aLabel, layerLabel, shapeInOneLayer);
229 }
230 else {
231 TopoDS_Shape aShape= DBRep::Get(argv[2]);
232 if ( !aShape.IsNull() ) {
233 localLayerTool->SetLayer(aShape, layerLabel, shapeInOneLayer);
234 }
235 }
236 }
237 return 0;
238}
239
240
241static Standard_Integer getAllLayers (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
242{
243 if (argc!=2) {
586db386 244 di<<"Use: "<<argv[0]<<" DocName\n";
7fd59977 245 return 1;
246 }
247 Handle(TDocStd_Document) Doc;
248 DDocStd::GetDocument(argv[1], Doc);
586db386 249 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 250 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
251 TDF_LabelSequence aLabs;
252 localLayerTool->GetLayerLabels(aLabs);
253 if (aLabs.Length() ==0 ) {
254 return 0;
255 }
256 Standard_Integer i = 1;
257 TCollection_ExtendedString layerName;
258
259 for (; i <= aLabs.Length(); i++) {
260 TDF_Label L = aLabs.Value(i);
261 if ( !L.IsNull() ) {
262 localLayerTool->GetLayer(L, layerName);
263 TCollection_AsciiString Entry(layerName,'?');
264 di << "\"" << Entry.ToCString() <<"\"";
265 di << " ";
266 }
267 }
268 return 0;
269}
270
271
272static Standard_Integer unSetLayer (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
273{
274 if (argc!=4) {
586db386 275 di<<"Use: "<<argv[0]<<" DocName {Shape|Label} stringL\n";
7fd59977 276 return 1;
277 }
278 Handle(TDocStd_Document) Doc;
279 DDocStd::GetDocument(argv[1], Doc);
586db386 280 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 281 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
282 TDF_Label aLabel;
283 TCollection_ExtendedString aLayer = argv[3];
284
285 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
286 if ( !aLabel.IsNull() ) {
287 localLayerTool->UnSetOneLayer(aLabel, aLayer);
288 }
289 else {
290 TopoDS_Shape aShape= DBRep::Get(argv[2]);
291 if ( !aShape.IsNull() ) {
292 localLayerTool->UnSetOneLayer(aShape, aLayer);
293 }
294 }
295 return 0;
296}
297
298
299static Standard_Integer unSetAllLayers (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
300{
301 if (argc!=3) {
586db386 302 di<<"Use: "<<argv[0]<<" DocName {Shape|Label}\n";
7fd59977 303 return 1;
304 }
305 Handle(TDocStd_Document) Doc;
306 DDocStd::GetDocument(argv[1], Doc);
586db386 307 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 308 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
309 TDF_Label aLabel;
310
311 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
312 if ( !aLabel.IsNull() ) {
313 localLayerTool->UnSetLayers(aLabel);
314 }
315 else {
316 TopoDS_Shape aShape= DBRep::Get(argv[2]);
317 if ( !aShape.IsNull() ) {
318 localLayerTool->UnSetLayers(aShape);
319 }
320 }
321 return 0;
322}
323
324
325static Standard_Integer removeAllLayers (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
326{
327 if (argc!=2) {
586db386 328 di<<"Use: "<<argv[0]<<" DocName\n";
7fd59977 329 return 1;
330 }
331 Handle(TDocStd_Document) Doc;
332 DDocStd::GetDocument(argv[1], Doc);
586db386 333 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 334 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
335
336 TDF_LabelSequence aLabs;
337 localLayerTool->GetLayerLabels(aLabs);
338 if (aLabs.Length() ==0 ) {
339 return 0;
340 }
341 Standard_Integer i = 1;
342 for (; i <= aLabs.Length(); i++) {
343 TDF_Label L = aLabs.Value(i);
344 if ( !L.IsNull() ) {
345 localLayerTool->RemoveLayer(L);
346 }
347 }
348 return 0;
349}
350
351static Standard_Integer setVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
352{
353 if (argc<3) {
586db386 354 di<<"Use: "<<argv[0]<<"DocName {layerLable|StringLayer} [isvisible(1/0)]\n";
7fd59977 355 return 1;
356 }
357 Handle(TDocStd_Document) Doc;
358 DDocStd::GetDocument(argv[1], Doc);
586db386 359 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 360 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
361 Standard_Boolean isvisible = Standard_False;
91322f44 362 if ( (argc==4) && (Draw::Atoi(argv[3])==1) ) isvisible = Standard_True;
7fd59977 363
364 TDF_Label aLabel;
365 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
366 if ( aLabel.IsNull() ) {
367 TCollection_ExtendedString aLayer = argv[2];
368 aLabel = localLayerTool->FindLayer(aLayer);
369 }
370 if ( aLabel.IsNull() ) return 1;
371 localLayerTool->SetVisibility(aLabel, isvisible);
372 return 0;
373}
374
375
376static Standard_Integer isVisible (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
377{
378 if (argc!=3) {
586db386 379 di<<"Use: "<<argv[0]<<" DocName {layerLable|StringLayer}\n";
7fd59977 380 return 1;
381 }
382 Handle(TDocStd_Document) Doc;
383 DDocStd::GetDocument(argv[1], Doc);
586db386 384 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 385 Handle(XCAFDoc_LayerTool) localLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc->Main());
386
387 TDF_Label aLabel;
388 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
389 if ( aLabel.IsNull() ) {
390 TCollection_ExtendedString aLayer = argv[2];
391 aLabel = localLayerTool->FindLayer(aLayer);
392 }
393 if ( aLabel.IsNull() ) return 1;
394 if (localLayerTool->IsVisible( aLabel) ) di << 1;
395 else di << 0;
396 return 0;
397}
398
9e6cdbca 399static Standard_Integer getLayerRefs(Draw_Interpretor& theDI,
400 Standard_Integer theArgc,
401 const char** theArgv)
402{
403 if (theArgc != 3)
404 {
405 theDI << "Use: " << theArgv[0] << "DocName Label\n";
406 return 1;
407 }
408
409 Handle(TDocStd_Document) aDoc;
410 DDocStd::GetDocument(theArgv[1], aDoc);
411 if (aDoc.IsNull())
412 {
413 theDI << "Error: \"" << theArgv[1] << "\" is not a document.\n";
414 return 1;
415 }
416
417 TDF_Label aLabel;
418 TDF_Tool::Label(aDoc->GetData(), theArgv[2], aLabel);
419 if (aLabel.IsNull())
420 {
421 theDI << "Error: Document \"" << theArgv[1] << "\" does not have a label \"" << theArgv[2] << "\".\n";
422 return 1;
423 }
424
425 Handle(XCAFDoc_GraphNode) aGraphNode;
426 aLabel.FindAttribute(XCAFDoc::LayerRefGUID(), aGraphNode);
427 if (aGraphNode.IsNull())
428 {
429 theDI << "Error: Label \"" << theArgv[2] << "\" does not have a layer ref.\n";
430 return 1;
431 }
432
433 if (aGraphNode->NbChildren() > 0)
434 {
435 theDI << "Label \"" << theArgv[2] << "\" childs:\n";
436 for (int anIndex = 1; anIndex <= aGraphNode->NbChildren(); ++anIndex)
437 {
438 Handle(XCAFDoc_GraphNode) aChild = aGraphNode->GetChild(anIndex);
439 TCollection_AsciiString anEntry;
440 TDF_Tool::Entry(aChild->Label(), anEntry);
441 theDI << anEntry << "\n";
442 }
443 }
444
445 if (aGraphNode->NbFathers() > 0)
446 {
447 theDI << "Label \"" << theArgv[2] << "\" fathers:\n";
448 for (int anIndex = 1; anIndex <= aGraphNode->NbFathers(); ++anIndex)
449 {
450 Handle(XCAFDoc_GraphNode) aFather = aGraphNode->GetFather(anIndex);
451 TCollection_AsciiString anEntry;
452 TDF_Tool::Entry(aFather->Label(), anEntry);
453 theDI << anEntry << "\n";
454 }
455 }
456
457 return 0;
458}
459
7fd59977 460//=======================================================================
461//function : InitCommands
462//purpose :
463//=======================================================================
464
465void XDEDRAW_Layers::InitCommands(Draw_Interpretor& di)
466{
7fd59977 467 static Standard_Boolean initactor = Standard_False;
c48e2889 468 if (initactor)
469 {
470 return;
471 }
472 initactor = Standard_True;
7fd59977 473
474 //=====================================
475 // Work with layers
476 //=====================================
477
478 Standard_CString g = "XDE layer's commands";
479
480 di.Add ("XSetLayer","DocName {Shape|Label} StringLayer [shapeInOneLayer(0/1)] \t: Set reference between Shape and Layer (add layer if nesessary). shapeInOneLayer 0 is default",
481 __FILE__, setLayer, g);
482
483 di.Add ("XGetLayers","DocName {Shape|Label} \t: Get layers of indicated shape",
484 __FILE__, getLayers, g);
485
486 di.Add ("XGetOneLayer","DocName LayerLabel \t: Print name of layer.",
487 __FILE__, getOneLayer, g);
488
489 di.Add ("XAddLayer","DocName StringLayer \t: Adding layer in XCAFDocument.",
490 __FILE__, addLayer, g);
491
492 di.Add ("XSetLinkLayer","DocName {Shape|Label} LayerL [shapeInOneLayer(0/1)] \t: Set reference between shape and existing layer. shapeInOneLayer 0 is default",
493 __FILE__, setLinkLayer, g);
494
495 di.Add ("XGetAllLayers","DocName \t: Get all layers in XCAFDocument.",
496 __FILE__, getAllLayers, g);
497
498 di.Add ("XUnSetLayer","DocName {Shape|Label} stringL \t: unset shape from indicated layer.",
499 __FILE__, unSetLayer, g);
500
501 di.Add ("XUnSetAllLayers","DocName {Shape|Label} \t: unset shape from all layers.",
502 __FILE__, unSetAllLayers, g);
503
504 di.Add ("XRemoveLayer","DocName {Label|string} \t:remove layer from XCAFDocument.",
505 __FILE__, removeLayer, g);
506
507 di.Add ("XRemoveAllLayers","DocName \t: remove all layers from XCAFDocument.",
508 __FILE__, removeAllLayers, g);
509
510 di.Add ("XFindLayer","DocName string \t: Print label where are layer is situated.",
511 __FILE__, findLayer, g);
512
513 di.Add ("XGetLayerLabels","DocName \t: Print labels from layertable.",
514 __FILE__, getLayerLabels, g);
515
516 di.Add ("XSetVisibility","DocName {layerLable|StringLayer} [isvisible(1/0)] \t: Set visibility of layer",
517 __FILE__, setVisibility, g);
518
519 di.Add ("XIsVisible","DocName {layerLable|StringLayer} \t: Return 1 if layer is visible, 0 if not",
520 __FILE__, isVisible, g);
9e6cdbca 521
522 di.Add("XGetLayerRefs", "DocName Label \t: Prints layers labels which are referenced in passed label or prints labels which reference passed layer label.",
523 __FILE__, getLayerRefs, g);
7fd59977 524}