d834692c7d8e3421ae39cf92e5531842034972eb
[occt.git] / src / DNaming / DNaming_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 <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>
56 //#ifdef WNT
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 //=======================================================================
69 static 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
86   if (n > 3) T = Draw::Atoi(a[3]);
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 ();
95     Sprintf (name,"%s_%s_%d",a[2],"old", i++);
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 //=======================================================================
109 static 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
126   if (n > 3) T = Draw::Atoi(a[3]);
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 ();
134     Sprintf (name,"%s_%s_%d",a[2],"new", i++);
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 //=======================================================================
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 Status ;
164   TCollection_AsciiString Name   = DNaming::GetEntry(S,ND,Status);
165   if (Status == 0) {
166     di <<"E_NoName";
167   }
168   else  {
169     di <<Name.ToCString();
170     if (Status == 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 static 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()) {
190     di <<"Noone shape selected"<<"\n";
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 //=======================================================================
210 static 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 //=======================================================================
231 static 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   }
259   di <<"."<<"\n";
260   return 0;
261 }
262
263 //=======================================================================
264 //function : GetShape
265 //purpose  : 
266 //=======================================================================
267 static 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();
279   if (n == 5) { Trans = (Standard_Integer ) Draw::Atof(a[4]);}
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)) { 
285     di <<"No shape"<<"\n";
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()) {
298       Sprintf(name,"%s%s_%d","old",a[3],NbShapes);
299       DBRep::Set (name,itL.OldShape());
300     }
301     if (!itL.NewShape().IsNull()) {    
302       Sprintf(name,"%s_%d",a[3],NbShapes);
303       DBRep::Set (name,itL.NewShape());
304     }
305     NbShapes++;
306   }
307   di <<"\n";
308   if (NbShapes == 0) {
309     di <<"No shape"<<"\n";
310   }
311
312   return 0;
313 }
314
315 //=======================================================================
316 //function : GeneratedShape
317 //purpose  : Generatedshape df shape Generationentry [drawname]
318 //=======================================================================
319
320 static 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   }
339   di << "GetShape : Error" << "\n";
340   return 1;
341 }
342
343 //=======================================================================
344 //function : DDataStd_GetShape
345 //purpose  : 
346 //=======================================================================
347
348 static 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   }
365   di << "DDataStd_GetShape : Error" << "\n";
366   return 1;
367 }
368 //=======================================================================
369 //function : Collect
370 //purpose  : 
371 //=======================================================================
372
373 static 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) {
386       OnlyModif = Draw::Atoi(arg[3]);
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 //=======================================================================
401 static 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()) {
413     di <<"Noone shape selected"<<"\n";
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   }
425   Standard_Integer Status ;
426   
427   TCollection_AsciiString Name   = DNaming::GetEntry(S,ND,Status);
428   if (Status == 0) {
429     di <<"E_NoName";
430   }
431   else  {
432     di <<Name.ToCString();
433     if (Status == 2) {
434       di <<"Several shapes have the same name"<<"\n";
435     }
436   }
437   return 0;
438 }
439
440 //=======================================================================
441 //function : ImportShape
442 //purpose  : "ImportShape Doc  entry Shape Name"
443 //=======================================================================
444
445 static Standard_Integer DNaming_ImportShape (Draw_Interpretor& di,
446                                              Standard_Integer nb, 
447                                              const char** a)
448 {
449   if (nb >= 4) {
450     Handle(TDF_Data) aDF;
451     if(!DDF::GetDF(a[1], aDF)) return 1;
452     TDF_Label L;
453     DDF::AddLabel(aDF, a[2], L);
454     const TopoDS_Shape& aShape = DBRep::Get(a[3]);
455     if(aShape.IsNull()) return 1;
456     if(nb == 5) {
457       TDataStd_Name::Set(L, a[4]);
458     }
459     
460     DNaming::LoadImportedShape(L, aShape);
461     
462     DDF::ReturnLabel(di, L);
463     return 0;
464   }
465   cout << "DNaming_NewShape : Error" << endl;
466   return 1;  
467 }
468
469 //=======================================================================
470 //function : BasicCommands
471 //purpose  : 
472 //=======================================================================
473
474 void  DNaming::BasicCommands(Draw_Interpretor& theCommands)
475 {
476   static Standard_Boolean done = Standard_False;
477   if (done) return;
478   done = Standard_True;
479
480   const char* g = "Naming data commands";
481
482   // Exploration
483   theCommands.Add("Ascendants",  "Ascendants df shape [trans]",      __FILE__,Ascendants,      g);
484   theCommands.Add("Descendants", "Descendants  df shape [trans]",    __FILE__,Descendants,     g);
485   theCommands.Add("ExploreShape","ExploreShape df entry res [trans]",__FILE__,Exploreshape,    g);
486   theCommands.Add("GetEntry",    "GetEntry df shape",                __FILE__,Getentry,        g);  
487   theCommands.Add("GetCreationEntry",    "GetCreationEntry df shape",__FILE__,Getcreationentry,g);
488   theCommands.Add("NamedShape",  "NamedShape df shape",              __FILE__,NamedShape,      g);
489   theCommands.Add("InitialShape","InitialShape df shape res",        __FILE__,Initialshape,    g);
490   theCommands.Add("CurrentShape","Currentshape df entry [drawname]", __FILE__,Currentshape,    g);
491   theCommands.Add("GetShape",    "GetShape df entry [drawname]",     __FILE__,Getshape ,       g);  
492   theCommands.Add("Collect",     "Collect  df entry [onlymodif 0/1]",__FILE__,Collect ,        g);  
493   theCommands.Add ("GeneratedShape", "Generatedshape df shape Generationentry [drawname]", __FILE__,Generatedshape,g);
494   theCommands.Add("ImportShape", "ImportShape Doc Entry Shape [Name]",__FILE__,DNaming_ImportShape, g);  
495 }