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