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