0027961: Visualization - remove unused and no more working OpenGl_AVIWriter
[occt.git] / src / QADNaming / QADNaming_IteratorsCommands.cxx
1 // Created on: 2002-04-25
2 // Created by: Michael PONIKAROV
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <stdio.h>
17
18 #include <QADNaming.hxx>
19 #include <TDF_Label.hxx>
20 #include <Draw_Interpretor.hxx>
21 #include <TNaming_Iterator.hxx>
22 #include <TNaming_NewShapeIterator.hxx>
23 #include <TNaming_OldShapeIterator.hxx>
24 #include <TNaming_SameShapeIterator.hxx>
25 #include <TNaming_Tool.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <DBRep.hxx>
28 #include <TNaming.hxx>
29 #include <TNaming_NamedShape.hxx>
30
31 #include <DDF.hxx>
32
33 #include <TDF_Data.hxx>
34 #include <TDF_Tool.hxx>
35
36 static const char* EvolutionString(TNaming_Evolution theEvolution) {
37   switch(theEvolution){
38   case TNaming_PRIMITIVE :
39     return "PRIMITIVE";
40   case TNaming_GENERATED :
41     return "GENERATED";
42   case TNaming_MODIFY :
43     return "MODIFY";
44   case TNaming_DELETE :
45     return "DELETE";
46   case TNaming_SELECTED :
47     return "SELECTED";
48   case TNaming_REPLACE :
49     return "MODIFY";
50   }
51   return "UNKNOWN_Evolution";
52 }
53
54
55 static Standard_Integer GetNewShapes (Draw_Interpretor& di,
56                                       Standard_Integer nb, 
57                                       const char** arg) {
58   if (nb==3 || nb==4) {
59     TDF_Label aLabel;
60     if (!QADNaming::Entry(arg, aLabel)) return 1;
61     Handle(TNaming_NamedShape) aNS;
62     if (!aLabel.FindAttribute(TNaming_NamedShape::GetID(),aNS)) {
63       di<<"Label has no NamedShape\n";
64       return 1;
65     }
66     di<<EvolutionString(aNS->Evolution());
67     TNaming_Iterator anIter(aNS);
68     Standard_Integer a;
69     char aName[200];
70     for(a=1;anIter.More();anIter.Next(),a++) {
71       if (anIter.NewShape().IsNull()) a--;
72       else if (nb==4) {
73         Sprintf(aName,"%s_%d",arg[3],a);
74         DBRep::Set (aName,anIter.NewShape());
75       }
76     }
77     di<<" "<<a-1;
78   } else {
79     di<<"Usage: GetNewShapes df entry [res]\n";
80     return 1;
81   }
82   return 0;
83 }
84
85 static Standard_Integer GetOldShapes (Draw_Interpretor& di,
86                                       Standard_Integer nb, 
87                                       const char** arg) {
88   if (nb==3 || nb==4) {
89     TDF_Label aLabel;
90     if (!QADNaming::Entry(arg, aLabel)) return 1;
91     Handle(TNaming_NamedShape) aNS;
92     if (!aLabel.FindAttribute(TNaming_NamedShape::GetID(),aNS)) {
93       di<<"Label has no NamedShape\n";
94       return 1;
95     }
96     di<<EvolutionString(aNS->Evolution());
97     TNaming_Iterator anIter(aNS);
98     Standard_Integer a;
99     char aName[200];
100     for(a=1;anIter.More();anIter.Next(),a++) {
101       if (anIter.OldShape().IsNull()) a--;
102       else if (nb==4) {
103         Sprintf(aName,"%s_%d",arg[3],a);
104         DBRep::Set (aName,anIter.OldShape());
105       }
106     }
107     di<<" "<<a-1;
108   } else {
109     di<<"Usage: GetOldShapes df entry [res]\n";
110     return 1;
111   }
112   return 0;
113 }
114
115 static int GetAllNew(const TopoDS_Shape& theShape, const TDF_Label& theAccess, 
116                      const TCollection_AsciiString& theName, Standard_Integer theIndex)
117 {
118   TNaming_NewShapeIterator anIter(theShape,theAccess);
119   TCollection_AsciiString aName;
120   for(;anIter.More();anIter.Next())
121   {
122     if (!anIter.Shape().IsNull())
123     {
124       theIndex++;
125       if (!theName.IsEmpty())
126       {
127         aName = theName + "_" + theIndex;
128         DBRep::Set(aName.ToCString(),anIter.Shape());
129       }
130       theIndex = GetAllNew(anIter.Shape(),theAccess,theName,theIndex);
131     }
132   }
133   return theIndex;
134 }
135
136 static Standard_Integer GetAllNewShapes (Draw_Interpretor& di,
137                                          Standard_Integer nb, 
138                                          const char** arg) {
139   Standard_Integer aResult = 0;
140   if (nb==3 || nb==4) {
141     TCollection_AsciiString aName ((nb==4) ? arg[3] : "");
142
143     if (arg[2][0]=='0') { // label
144       TDF_Label aLabel;
145       if (!QADNaming::Entry(arg, aLabel)) return 1;
146       Handle(TNaming_NamedShape) aNS;
147       if (!aLabel.FindAttribute(TNaming_NamedShape::GetID(),aNS)) {
148         di<<"Label has no NamedShape\n";
149         return 1;
150       }
151       Standard_Integer a;
152       TNaming_Iterator anIter(aNS);
153       for(a=1;anIter.More();anIter.Next(),a++) {
154         if (!anIter.NewShape().IsNull()) {
155       TCollection_AsciiString aSubName;
156       if (!aName.IsEmpty())
157       {
158         aSubName += aName + "_";
159         aSubName += a;
160       }
161       aResult+=GetAllNew(anIter.NewShape(),aLabel,aSubName,0);
162         }
163       }
164     } else { // shape
165       Handle(TDF_Data) DF;
166       if (!DDF::GetDF(arg[1],DF)) {
167         di<<"Wrong df\n";
168         return 1;
169       }
170       TopoDS_Shape aShape = DBRep::Get(arg[2]);
171       aResult=GetAllNew(aShape,DF->Root(),aName,0);
172     }
173   } else {
174     di<<"Usage: GetAllNewShapes df entry/shape [res]\n";
175     return 1;
176   }
177   di<<aResult;
178   return 0;
179 }
180
181 static int GetAllOld(const TopoDS_Shape& theShape, const TDF_Label& theAccess, 
182                      const TCollection_AsciiString& theName, Standard_Integer theIndex)
183 {
184   TCollection_AsciiString aName;
185   Handle(TNaming_NamedShape) aNS = TNaming_Tool::NamedShape(theShape,theAccess);
186   if (aNS.IsNull()) return theIndex;
187   TNaming_Iterator anIter(aNS);
188   for(;anIter.More();anIter.Next())
189   {
190     if (!anIter.OldShape().IsNull() && !anIter.NewShape().IsNull()) if (anIter.NewShape().IsSame(theShape))
191     {
192       theIndex++;
193       if (!theName.IsEmpty())
194       {
195         aName = theName + "_" + theIndex;
196         DBRep::Set(aName.ToCString(),anIter.OldShape());
197       }
198       theIndex = GetAllOld(anIter.OldShape(),theAccess,theName,theIndex);
199     }
200   }
201   return theIndex;
202 }
203
204 static Standard_Integer GetAllOldShapes (Draw_Interpretor& di,
205                                          Standard_Integer nb, 
206                                          const char** arg) {
207   Standard_Integer aResult = 0;
208   if (nb==3 || nb==4) {
209     TCollection_AsciiString aName((nb==4) ? arg[3] : "");
210
211     if (arg[2][0]=='0') { // label
212       TDF_Label aLabel;
213       if (!QADNaming::Entry(arg, aLabel)) return 1;
214       Handle(TNaming_NamedShape) aNS;
215       if (!aLabel.FindAttribute(TNaming_NamedShape::GetID(),aNS)) {
216         di<<"Label has no NamedShape\n";
217         return 1;
218       }
219       Standard_Integer a;
220       TNaming_Iterator anIter(aNS);
221       for(a=1;anIter.More();anIter.Next(),a++) {
222         if (!anIter.NewShape().IsNull()) {
223       TCollection_AsciiString aSubName;
224       if (!aName.IsEmpty())
225       {
226         aSubName += aName + "_";
227         aSubName += a;
228           }
229       aResult+=GetAllOld(anIter.NewShape(),aLabel,aSubName,0);
230         }
231       }
232     } else { // shape
233       Handle(TDF_Data) DF;
234       if (!DDF::GetDF(arg[1],DF)) {
235         di<<"Wrong df\n";
236         return 1;
237       }
238       TopoDS_Shape aShape = DBRep::Get(arg[2]);
239       aResult=GetAllOld(aShape,DF->Root(),aName,0);
240     }
241   } else {
242     di<<"Usage: GetAllNewShapes df entry/shape [res]\n";
243     return 1;
244   }
245   di<<aResult;
246   return 0;
247 }
248
249 static Standard_Integer GetSameShapes (Draw_Interpretor& di,
250                                        Standard_Integer nb, 
251                                        const char** arg) {
252   TCollection_AsciiString aRes;
253   if (nb == 3) {
254     Standard_Integer aResult = 0;
255     Handle(TDF_Data) DF;
256     if (!DDF::GetDF(arg[1],DF)) {
257       di<<"Wrong df\n";
258       return 1;
259     }
260     TopoDS_Shape aShape = DBRep::Get(arg[2]);
261     TNaming_SameShapeIterator anIter(aShape,DF->Root());
262     for(;anIter.More();anIter.Next()) {
263       if (!anIter.Label().IsNull()) {
264         TCollection_AsciiString Name;
265         TDF_Tool::Entry(anIter.Label(),Name);
266         if (aResult != 0) aRes=aRes+Name+" "; else aRes=Name;
267         aResult++;
268       }
269     }
270   } else {
271     di<<"Usage: GetSameShapes df shape\n";
272     return 1;
273   }
274   di<<aRes.ToCString();
275   return 0;
276 }
277
278 void QADNaming::IteratorsCommands(Draw_Interpretor& theCommands)
279 {
280   static Standard_Boolean done = Standard_False;
281   if (done) return;
282   done = Standard_True;
283
284   const char* g = "Naming builder commands";
285
286   theCommands.Add("GetNewShapes","GetNewShapes df entry [res]",__FILE__,GetNewShapes,g);
287
288   theCommands.Add("GetOldShapes","GetOldShapes df entry [res]",__FILE__,GetOldShapes,g);
289
290   theCommands.Add("GetAllNewShapes","GetAllNewShapes df entry/shape [res]",__FILE__,GetAllNewShapes,g);
291
292   theCommands.Add("GetAllOldShapes","GetAllOldShapes df entry/shape [res]",__FILE__,GetAllOldShapes,g);
293
294   theCommands.Add("GetSameShapes","GetSameShapes df shape",__FILE__,GetSameShapes,g);
295
296 }