0028838: Configuration - undefine macros coming from X11 headers in place of collision
[occt.git] / src / DNaming / DNaming_BasicCommands.cxx
CommitLineData
b311480e 1// Created on: 1997-01-13
2// Created by: VAUTHIER Jean-Claude
3// Copyright (c) 1997-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17#include <DNaming.hxx>
18#include <TCollection_AsciiString.hxx>
19#include <TDF_Data.hxx>
20#include <TDF_ListIteratorOfLabelList.hxx>
21#include <TDF_LabelList.hxx>
22#include <TDF_LabelMap.hxx>
23#include <TDF_Tool.hxx>
24#include <DDF.hxx>
25#include <DDF_Data.hxx>
26#include <TNaming.hxx>
27#include <TNaming_NamedShape.hxx>
28#include <TNaming_NewShapeIterator.hxx>
29#include <TNaming_OldShapeIterator.hxx>
30#include <TNaming_Iterator.hxx>
31#include <TNaming_Tool.hxx>
32#include <TNaming_MapOfNamedShape.hxx>
33#include <TNaming_MapIteratorOfMapOfNamedShape.hxx>
34#include <Draw_Drawable3D.hxx>
35#include <Draw.hxx>
36#include <Draw_Interpretor.hxx>
37#include <TopTools_ListOfShape.hxx>
38#include <TopTools_ListIteratorOfListOfShape.hxx>
39#include <TopTools_DataMapOfShapeShape.hxx>
40#include <TopoDS_Shape.hxx>
41#include <DBRep.hxx>
42
43#include <TNaming_Builder.hxx>
44#include <TDataStd_Name.hxx>
45#include <TDF_TagSource.hxx>
46#include <TopExp_Explorer.hxx>
47
48#include <gp.hxx>
49#include <gp_Vec.hxx>
50#include <gp_Ax1.hxx>
51#include <gp_Ax2.hxx>
52#include <gp_Ax3.hxx>
53#include <gp_Trsf.hxx>
54
55#include <TColStd_HArray1OfInteger.hxx>
57c28b61 56//#ifdef _MSC_VER
7fd59977 57#include <stdio.h>
58//#endif
59
60// POP : first Wrong Declaration : now it is correct
61// second not used
62//extern void DNaming_BuildMap(TDF_LabelMap& Updated, const TDF_Label& Lab);
63
64
65//=======================================================================
66//function : Ascendants
67//purpose :
68//=======================================================================
69static Standard_Integer Ascendants (Draw_Interpretor& di, Standard_Integer n, const char** a)
70{
71 if (n < 3) return 1;
72
73 char name[100];
74
75 Handle(TDF_Data) ND;
76// Handle(TNaming_UsedShapes) US;
77
78 if (!DDF::GetDF(a[1],ND)) return 1;
79// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
80
81 TopoDS_Shape S = DBRep::Get (a[2]);
82 if (S.IsNull ()) return 1;
83
84 Standard_Integer T;
85
91322f44 86 if (n > 3) T = Draw::Atoi(a[3]);
7fd59977 87 else T = ND->Transaction ();
88
89 //TNaming_OldShapeIterator it (S, T, US);
90 TNaming_OldShapeIterator it (S, T, ND->Root());
91 Standard_Integer i = 0;
92 TCollection_AsciiString entry;
93 for (;it.More (); it.Next ()) {
94 S = it.Shape ();
91322f44 95 Sprintf (name,"%s_%s_%d",a[2],"old", i++);
7fd59977 96 DBRep::Set (name,it.Shape());
97 TDF_Label Label = it.Label ();
98 TDF_Tool::Entry(Label,entry);
99 di<<entry.ToCString()<<"\n";
100 }
101 return 0;
102}
103
104
105//=======================================================================
106//function : Descendants
107//purpose :
108//=======================================================================
109static Standard_Integer Descendants (Draw_Interpretor& di, Standard_Integer n, const char** a)
110
111{
112 if (n < 3) return 1;
113
114 char name[100];
115 Handle(TDF_Data) ND;
116// Handle(TNaming_UsedShapes) US;
117 if (!DDF::GetDF(a[1],ND)) return 1;
118// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
119
120
121 TopoDS_Shape S = DBRep::Get (a[2]);
122 if (S.IsNull ()) return 1;
123
124 Standard_Integer T;
125
91322f44 126 if (n > 3) T = Draw::Atoi(a[3]);
7fd59977 127 else T = ND->Transaction ();
128
129 TNaming_NewShapeIterator it (S, T, ND->Root());
130 Standard_Integer i = 0;
131 TCollection_AsciiString entry;
132 for (;it.More (); it.Next ()) {
133 S = it.Shape ();
91322f44 134 Sprintf (name,"%s_%s_%d",a[2],"new", i++);
7fd59977 135 DBRep::Set (name,it.Shape ());
136 TDF_Label Label = it.Label ();
137 TDF_Tool::Entry(Label,entry);
138 di<<entry.ToCString()<<"\n";
139 }
140
141 return 0;
142}
143
144//=======================================================================
145//function : GetEntry
146//purpose :
147//=======================================================================
148static Standard_Integer Getentry (Draw_Interpretor& di, Standard_Integer n, const char** a)
149{
150 if (n < 3) return 1;
151 Handle(TDF_Data) ND;
152// Handle(TNaming_UsedShapes) US;
153
154 if (!DDF::GetDF(a[1],ND)) return 1;
155// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
156
157 TopoDS_Shape S = DBRep::Get(a[2]);
158 if (S.IsNull()) {
586db386 159 di <<"Noone shape selected\n";
7fd59977 160 //di << 0;
161 return 0;
162 }
9fd2d2c3 163 Standard_Integer aStatus = 0;
164 TCollection_AsciiString Name = DNaming::GetEntry (S, ND, aStatus);
165 if (aStatus == 0) {
7fd59977 166 di <<"E_NoName";
167 }
168 else {
169 di <<Name.ToCString();
9fd2d2c3 170 if (aStatus == 2) {
586db386 171 di <<"Several shapes have the same name\n";
7fd59977 172 }
173 }
174 return 0;
175}
176
177//=======================================================================
178//function : NamedShape
179//purpose : retrive label of Primitive or a Generated shape
180//=======================================================================
181static Standard_Integer NamedShape(Draw_Interpretor& di, Standard_Integer n, const char** a)
182{
183 if (n < 3) return 1;
184 Handle(TDF_Data) ND;
185// Handle(TNaming_UsedShapes) US;
186
187 if (!DDF::GetDF(a[1],ND)) return 1;
188 TopoDS_Shape SS = DBRep::Get(a[2]);
189 if (SS.IsNull()) {
586db386 190 di <<"Noone shape selected\n";
7fd59977 191 //di << 0;
192 return 0;
193 }
194
195 Handle(TNaming_NamedShape) NS = TNaming_Tool::NamedShape (SS,ND->Root());
196
197 if (NS.IsNull()) {
198 di <<"E_NoName";
199 return 0;
200 }
201 TCollection_AsciiString Name; TDF_Tool::Entry(NS->Label(),Name);
202 di <<Name.ToCString();
203 return 0;
204}
205
206//=======================================================================
207//function : Currentshape
208//purpose :
209//=======================================================================
210static Standard_Integer Currentshape (Draw_Interpretor& , Standard_Integer n, const char** a)
211{
212 if (n < 4) return 1;
213
214 Handle(TDF_Data) ND;
215 if (!DDF::GetDF(a[1],ND)) return 1;
216
217 Standard_CString LabelName = a[2];
218 TopoDS_Shape S = DNaming::CurrentShape(LabelName,ND);
219 if (!S.IsNull()) {
220 if (n == 4) DBRep::Set(a[3],S);
221 else DBRep::Set(a[2],S);
222 return 0;
223 }
224 return 0;
225}
226
227//=======================================================================
228//function : Initialshape
229//purpose :
230//=======================================================================
231static Standard_Integer Initialshape (Draw_Interpretor& di, Standard_Integer n, const char** a)
232{
233 if (n < 4) return 1;
234
235 Handle(TDF_Data) ND;
236
237 if (!DDF::GetDF(a[1],ND)) return 1;
238
239 TopoDS_Shape NS = DBRep::Get(a[2]);
240 if (NS.IsNull()) return 1;
241
242 TDF_LabelList Labels;
243 TopoDS_Shape S = TNaming_Tool::InitialShape (NS,ND->Root(),Labels);
244 if (!S.IsNull()) {
245 DBRep::Set (a[3], S);
246 }
247 TDF_ListIteratorOfLabelList itL(Labels);
248
249 TCollection_AsciiString entry;
250 if (itL.More()) {
251 TDF_Tool::Entry(itL.Value(),entry);
252 di << entry.ToCString();
253 itL.Next();
254 }
255 for (; itL.More(); itL.Next()) {
256 TDF_Tool::Entry(itL.Value(),entry);
257 di <<" , "<< entry.ToCString();
258 }
586db386 259 di <<".\n";
7fd59977 260 return 0;
261}
262
263//=======================================================================
264//function : GetShape
265//purpose :
266//=======================================================================
267static Standard_Integer Exploreshape (Draw_Interpretor& di, Standard_Integer n, const char** a)
268{
269 char name[100];
270
271 if (n < 4) return 1;
272 Handle(TDF_Data) ND;
273// Handle(TNaming_UsedShapes) US;
274
275 if (!DDF::GetDF(a[1],ND)) return 1;
276// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
277
278 Standard_Integer Trans = ND->Transaction();
91322f44 279 if (n == 5) { Trans = (Standard_Integer ) Draw::Atof(a[4]);}
7fd59977 280
281 TDF_Label Lab;
282 DDF::FindLabel(ND,a[2],Lab);
283 Handle(TNaming_NamedShape) NS;
284 if (!Lab.FindAttribute(TNaming_NamedShape::GetID(),NS)) {
586db386 285 di <<"No shape\n";
7fd59977 286 return 0;
287 }
288
289 //TNaming::Print(NS->Evolution(),cout);
290 Standard_SStream aStream;
291 TNaming::Print(NS->Evolution(),aStream);
292 di << aStream << "\n";
293
294 Standard_Integer NbShapes = 1;
295
296 for (TNaming_Iterator itL(Lab,Trans) ; itL.More(); itL.Next()) {
297 if (!itL.OldShape().IsNull()) {
91322f44 298 Sprintf(name,"%s%s_%d","old",a[3],NbShapes);
7fd59977 299 DBRep::Set (name,itL.OldShape());
300 }
301 if (!itL.NewShape().IsNull()) {
91322f44 302 Sprintf(name,"%s_%d",a[3],NbShapes);
7fd59977 303 DBRep::Set (name,itL.NewShape());
304 }
305 NbShapes++;
306 }
307 di <<"\n";
308 if (NbShapes == 0) {
586db386 309 di <<"No shape\n";
7fd59977 310 }
311
312 return 0;
313}
314
315//=======================================================================
316//function : GeneratedShape
317//purpose : Generatedshape df shape Generationentry [drawname]
318//=======================================================================
319
320static Standard_Integer Generatedshape (Draw_Interpretor& di,
321 Standard_Integer nb,
322 const char** arg)
323{
324 TopoDS_Shape S;
325 Handle(TNaming_NamedShape) A;
326 if (nb >= 4) {
327 Handle(TDF_Data) DF;
328 if (!DDF::GetDF(arg[1],DF)) return 1;
329 TopoDS_Shape Gen = DBRep::Get(arg[2]);
330 Handle(TNaming_NamedShape) Generation;
331 if (!DDF::Find(DF,arg[3],TNaming_NamedShape::GetID(),Generation)) return 1;
332 S = TNaming_Tool::GeneratedShape(Gen,Generation);
333 if (!S.IsNull()) {
334 if (nb == 4) DBRep::Set(arg[4],S);
335 else DBRep::Set(arg[3],S);
336 return 0;
337 }
338 }
586db386 339 di << "GetShape : Error\n";
7fd59977 340 return 1;
341}
342
343//=======================================================================
344//function : DDataStd_GetShape
345//purpose :
346//=======================================================================
347
348static Standard_Integer Getshape (Draw_Interpretor& di,
349 Standard_Integer nb,
350 const char** arg)
351{
352 TopoDS_Shape s;
353 Handle(TNaming_NamedShape) A;
354 if (nb >= 3) {
355 Handle(TDF_Data) DF;
356 if (!DDF::GetDF(arg[1],DF)) return 1;
357 if (!DDF::Find(DF,arg[2],TNaming_NamedShape::GetID(),A)) return 1;
358 s = TNaming_Tool::GetShape(A);
359 if (!s.IsNull()) {
360 if (nb == 4) DBRep::Set(arg[3],s);
361 else DBRep::Set(arg[2],s);
362 return 0;
363 }
364 }
586db386 365 di << "DDataStd_GetShape : Error\n";
7fd59977 366 return 1;
367}
368//=======================================================================
369//function : Collect
370//purpose :
371//=======================================================================
372
373static Standard_Integer Collect (Draw_Interpretor& di,
374 Standard_Integer nb,
375 const char** arg)
376{
377 TNaming_MapOfNamedShape MNS;
378 Handle(TNaming_NamedShape) A;
379 Standard_Boolean OnlyModif = 1;
380
381 if (nb >= 3) {
382 Handle(TDF_Data) DF;
383 if (!DDF::GetDF(arg[1],DF)) return 1;
384 if (!DDF::Find(DF,arg[2],TNaming_NamedShape::GetID(),A)) return 1;
385 if (nb >= 4) {
dde68833 386 OnlyModif = (Draw::Atoi(arg[3]) != 0);
7fd59977 387 }
388 TNaming_Tool::Collect(A,MNS,OnlyModif);
389 for (TNaming_MapIteratorOfMapOfNamedShape it(MNS); it.More(); it.Next()) {
390 TCollection_AsciiString Name;
391 TDF_Tool::Entry(it.Key()->Label(),Name);
392 di <<Name.ToCString()<<" ";
393 }
394 }
395 return 1;
396}
397//=======================================================================
398//function : GetCreationEntry
399//purpose : retrive label of Primitive or a Generated shape
400//=======================================================================
401static Standard_Integer Getcreationentry (Draw_Interpretor& di, Standard_Integer n, const char** a)
402{
403 if (n < 3) return 1;
404 Handle(TDF_Data) ND;
405// Handle(TNaming_UsedShapes) US;
406
407 if (!DDF::GetDF(a[1],ND)) return 1;
408// ND->Root().FindAttribute(TNaming_UsedShapes::GetID(),US);
409
410 TopoDS_Shape SS = DBRep::Get(a[2]);
411
412 if (SS.IsNull()) {
586db386 413 di <<"Noone shape selected\n";
7fd59977 414 //di << 0;
415 return 0;
416 }
417
418 TDF_LabelList Labels;
419 TopoDS_Shape S = TNaming_Tool::InitialShape(SS, ND->Root(), Labels);
420
421 if (S.IsNull()) {
422 di <<"E_NoName";
423 return 0;
424 }
9fd2d2c3 425 Standard_Integer aStatus = 0;
426 TCollection_AsciiString Name = DNaming::GetEntry (S, ND, aStatus);
427 if (aStatus == 0) {
7fd59977 428 di <<"E_NoName";
429 }
430 else {
431 di <<Name.ToCString();
9fd2d2c3 432 if (aStatus == 2) {
586db386 433 di <<"Several shapes have the same name\n";
7fd59977 434 }
435 }
436 return 0;
437}
438
439//=======================================================================
440//function : ImportShape
441//purpose : "ImportShape Doc entry Shape Name"
442//=======================================================================
443
444static Standard_Integer DNaming_ImportShape (Draw_Interpretor& di,
445 Standard_Integer nb,
446 const char** a)
447{
448 if (nb >= 4) {
449 Handle(TDF_Data) aDF;
450 if(!DDF::GetDF(a[1], aDF)) return 1;
451 TDF_Label L;
452 DDF::AddLabel(aDF, a[2], L);
453 const TopoDS_Shape& aShape = DBRep::Get(a[3]);
454 if(aShape.IsNull()) return 1;
455 if(nb == 5) {
456 TDataStd_Name::Set(L, a[4]);
457 }
458
459 DNaming::LoadImportedShape(L, aShape);
460
461 DDF::ReturnLabel(di, L);
462 return 0;
463 }
464 cout << "DNaming_NewShape : Error" << endl;
465 return 1;
466}
ddd926fe 467//=======================================================================
468//function : CheckNSIter
469//purpose : "CheckNSIter Doc entry Shape new/old [1|0]"
470//=======================================================================
471
472static Standard_Integer CheckIter (Draw_Interpretor& di,
473 Standard_Integer nb,
474 const char** arg)
475{
476 if (nb > 3) {
477 Handle(TDF_Data) aDF;
478 Standard_Boolean aNew(Standard_True);
479 if (!DDF::GetDF(arg[1],aDF)) return 1;
480 TDF_Label aLabel;
481 DDF::AddLabel(aDF, arg[2], aLabel);
482 TNaming_Builder aNB(aLabel);
483 const TopoDS_Shape& aShape = DBRep::Get(arg[3]);
484 aNB.Generated(aShape);
485 TNaming_Iterator aNameIter(aLabel);
dde68833 486 if(nb == 5) aNew = (Draw::Atoi (arg[4]) != 0);
ddd926fe 487 if(aNew) {
488 TNaming_NewShapeIterator aNewShapeIter(aNameIter);
586db386 489 di << "DNaming_CheckIterator : New It is OK\n";
ddd926fe 490 } else {
491 TNaming_OldShapeIterator oldShapeIter(aNameIter);
586db386 492 di << "DNaming_CheckIterator : Old It is OK\n";
ddd926fe 493 }
494 return 0;
495 }
586db386 496 di << "DNaming_CheckIterator : Error\n";
ddd926fe 497 return 1;
498}
7fd59977 499
ddd926fe 500//
7fd59977 501//=======================================================================
502//function : BasicCommands
503//purpose :
504//=======================================================================
505
506void DNaming::BasicCommands(Draw_Interpretor& theCommands)
507{
508 static Standard_Boolean done = Standard_False;
509 if (done) return;
510 done = Standard_True;
511
512 const char* g = "Naming data commands";
513
514 // Exploration
515 theCommands.Add("Ascendants", "Ascendants df shape [trans]", __FILE__,Ascendants, g);
516 theCommands.Add("Descendants", "Descendants df shape [trans]", __FILE__,Descendants, g);
517 theCommands.Add("ExploreShape","ExploreShape df entry res [trans]",__FILE__,Exploreshape, g);
518 theCommands.Add("GetEntry", "GetEntry df shape", __FILE__,Getentry, g);
519 theCommands.Add("GetCreationEntry", "GetCreationEntry df shape",__FILE__,Getcreationentry,g);
520 theCommands.Add("NamedShape", "NamedShape df shape", __FILE__,NamedShape, g);
521 theCommands.Add("InitialShape","InitialShape df shape res", __FILE__,Initialshape, g);
522 theCommands.Add("CurrentShape","Currentshape df entry [drawname]", __FILE__,Currentshape, g);
523 theCommands.Add("GetShape", "GetShape df entry [drawname]", __FILE__,Getshape , g);
85b70a3b
V
524 theCommands.Add("Collect", "Collect df entry [onlymodif 0/1]",__FILE__,Collect , g);
525 theCommands.Add ("GeneratedShape", "Generatedshape df shape Generationentry [drawname]", __FILE__,Generatedshape,g);
526 theCommands.Add("ImportShape", "ImportShape Doc Entry Shape [Name]",__FILE__,DNaming_ImportShape, g);
ddd926fe 527 //
528 theCommands.Add("CheckNSIter", "CheckNSIter df entry shape new[1|0]", __FILE__, CheckIter , g);
973c2be1 529}