0023072: Eliminate compiler warnings (level 3) on Windows / MSVC++
[occt.git] / src / DDF / DDF_DataCommands.cxx
1 // Created by: DAUTRY Philippe
2 // Copyright (c) 1997-1999 Matra Datavision
3 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 //              --------------------
21
22 // Version:     0.0
23 //Version       Date            Purpose
24 //              0.0     Sep 30 1997     Creation
25
26
27
28 #include <TDF_ClosureMode.hxx>
29 #include <TDF_ClosureTool.hxx>
30 #include <TDF_CopyTool.hxx>
31
32 #include <DDF.hxx>
33 #include <DDF_Data.hxx>
34
35 #include <Draw.hxx>
36 #include <Draw_Appli.hxx>
37 #include <Draw_Drawable3D.hxx>
38 #include <Draw_Interpretor.hxx>
39
40 #include <Standard_NotImplemented.hxx>
41
42 #include <TDF_ChildIterator.hxx>
43 #include <TDF_Data.hxx>
44 #include <TDF_DataSet.hxx>
45 #include <TDF_IDFilter.hxx>
46 #include <TDF_Label.hxx>
47 #include <TDF_RelocationTable.hxx>
48 #include <TDF_Tool.hxx>
49 #include <TDF_CopyLabel.hxx>
50 #include <TDF_CopyLabel.hxx>
51 #include <TDF_AttributeIterator.hxx>
52 #include <TDF_AttributeMap.hxx>
53 #include <TDF_MapIteratorOfAttributeMap.hxx>
54
55
56 //=======================================================================
57 //function : MakeDF
58 //purpose  : Creates a new data framework.
59 //=======================================================================
60
61 static Standard_Integer MakeDF (Draw_Interpretor& di, 
62                                 Standard_Integer  n, 
63                                 const char**            a)
64 {
65   if (n < 2) return 1;
66
67   Handle (Draw_Drawable3D) D = Draw::Get(a[1]);
68   Handle(DDF_Data) NewDDF;
69
70   if (!D.IsNull ()) {
71     NewDDF = Handle(DDF_Data)::DownCast (D);
72     if (!NewDDF.IsNull ()) {
73       di<<"DDF_BasicCommands::MakeDF - Sorry, this Data Framework already exists"<<"\n";
74       return 0;
75     }
76   }
77
78   Handle(TDF_Data) NewDF  = new TDF_Data ();
79   NewDDF = new DDF_Data (NewDF);
80   Draw::Set (a[1], NewDDF);
81   //DeltaDS.Nullify();
82   return 0;
83 }
84
85
86
87 //=======================================================================
88 //function : ClearDF
89 //purpose  : Creates a new data framework.
90 //=======================================================================
91
92 static Standard_Integer ClearDF (Draw_Interpretor& di, 
93                                  Standard_Integer  n, 
94                                  const char**            a)
95
96 {
97   if (n < 2) return 1;
98
99   Handle (Draw_Drawable3D) D = Draw::Get(a[1]);
100   Handle(DDF_Data) DDF;
101
102   if (!D.IsNull ()) {
103     DDF = Handle(DDF_Data)::DownCast (D);
104     if (!DDF.IsNull ()) {
105       Handle(TDF_Data) DF  = DDF->DataFramework ();
106       if (!DF.IsNull ()) {
107         Handle(TDF_Data) NewEmpty = new TDF_Data;
108         DDF->DataFramework (NewEmpty);
109         //DeltaDS.Nullify();
110       }
111       return 0;
112     }
113   }
114
115   di<<"DDF_BasicCommands::MakeDF - Sorry, this Data Framework doesn't exist"<<"\n";
116
117   return 0;
118 }
119
120
121 //=======================================================================
122 //function : CopyDF
123 //purpose  : CopyDF.
124 //=======================================================================
125
126 static Standard_Integer CopyDF (Draw_Interpretor& /*di*/, 
127                                 Standard_Integer  n, 
128                                 const char**            a)
129 {
130   if (n < 4 || n > 5) return 1;
131
132
133   Handle(TDF_Data) DF1;
134   Handle(TDF_Data) DF2;
135   Standard_CString  Entry1;
136   Standard_CString  Entry2;
137
138   if (!DDF::GetDF (a[1], DF1)) return 1;
139
140   Entry1 = a[2];
141   Entry2 = a[3];
142
143   if (n == 4) {
144     DF2 = DF1;
145     Entry2 = a[3];
146   }
147   else if (n == 5) {
148     if (!DDF::GetDF (a[3], DF2)) return 1;
149     Entry2 = a[4];
150   }
151
152   TDF_Label Label1;
153   if (!DDF::FindLabel (DF1, Entry1, Label1)) return 1;
154
155   TDF_Label Label2;
156   if (!DDF::FindLabel (DF2, Entry2, Label2, Standard_False)) {
157     DDF::AddLabel(DF2,Entry2,Label2);
158   }
159
160
161   Handle(TDF_DataSet) DataSet = new TDF_DataSet;
162   DataSet->AddLabel(Label1);
163   TDF_ClosureTool::Closure(DataSet);
164   Handle(TDF_RelocationTable) Reloc = new TDF_RelocationTable();
165   Reloc->SetRelocation(Label1,Label2);
166   TDF_CopyTool::Copy (DataSet, Reloc);
167
168   return 0;
169 }
170
171
172 //=======================================================================
173 //function : MiniDumpDF
174 //purpose  : 
175 //=======================================================================
176
177 static Standard_Integer MiniDumpDF (Draw_Interpretor& di, 
178                                     Standard_Integer  n, 
179                                     const char**            a)
180 {
181   if (n < 2) return 1;
182
183   Handle (Draw_Drawable3D) D;
184   Handle(DDF_Data) DDF;
185
186   D = Draw::Get(a[1]);
187
188   if (D.IsNull ()) {
189     di<<"DDF_BasicCommands : Sorry this Data Framework doesn't exist"<<"\n";
190     return Standard_False;
191   }
192
193   DDF = Handle(DDF_Data)::DownCast (D);
194
195   if (DDF.IsNull ()) {
196     di<<"DDF_BasicCommands : Sorry this Data Framework doesn't exist"<<"\n";
197     return Standard_False;
198   }
199
200   di<<"*********** Dump of "<<a[1]<<" ***********"<<"\n";
201
202   //DDF->DataFramework()->Dump(cout);
203   Standard_SStream aSStream;
204   DDF->DataFramework()->Dump(aSStream);
205   aSStream << ends;
206   di << aSStream << "\n";
207   
208   return 0;
209 }
210 //=======================================================================
211 //function : XDumpDF
212 //purpose  : eXtended deep dump of a DataFramework
213 //=======================================================================
214
215 static Standard_Integer XDumpDF (Draw_Interpretor& di, 
216                                 Standard_Integer  n, 
217                                 const char**            a)
218 {
219   if (n < 2) return 1;
220
221   Handle (Draw_Drawable3D) D;
222   Handle(DDF_Data) DDF;
223
224   D = Draw::Get(a[1]);
225
226   if (D.IsNull ()) {
227     di<<"DDF_BasicCommands : Sorry this Data Framework doesn't exist"<<"\n";
228     return Standard_False;
229   }
230
231   DDF = Handle(DDF_Data)::DownCast (D);
232
233   if (DDF.IsNull ()) {
234     di<<"DDF_BasicCommands : Sorry this Data Framework doesn't exist"<<"\n";
235     return Standard_False;
236   }
237
238   di<<"*********** Dump of "<<a[1]<<" ***********"<<"\n";
239
240   TDF_IDFilter filter(Standard_False);
241   //TDF_Tool::ExtendedDeepDump(cout,DDF->DataFramework(),filter);
242   Standard_SStream aSStream;
243   TDF_Tool::ExtendedDeepDump(aSStream,DDF->DataFramework(),filter);
244   aSStream << ends;
245   di << aSStream <<"\n";
246   
247   return 0;
248 }
249
250
251 //=======================================================================
252 //function : CopyLabel_SCopy
253 //purpose  : CopyLabel (DF,fromlabel,tolabel)
254 //=======================================================================
255
256 static Standard_Integer CopyLabel_SCopy (Draw_Interpretor& di,Standard_Integer n, const char** a)
257 {
258   TDF_Label SOURCE,TARGET;
259   if (n == 4) {   
260     Handle(TDF_Data) DF;
261     if(!DDF::GetDF(a[1], DF)) return 1;
262     if (!DDF::FindLabel(DF,a[2],SOURCE)) return 1;   
263     if (DDF::FindLabel(DF,a[3],TARGET)) {
264       di << " target label is already setted "<< "\n";
265       return 1;
266     }
267     DDF::AddLabel(DF,a[3],TARGET);
268     TDF_CopyLabel cop;
269     cop.Load(SOURCE, TARGET);
270     cop.Perform();
271     if (!cop.IsDone()) 
272       di << "copy not done" << "\n";
273     return 0;
274   }
275   di << "DDF_CopyLabel : Error" << "\n";
276   return 1;  
277 }
278
279 //=======================================================================
280 //function : DDF_CheckAttr
281 //purpose  : CheckAttr (DOC,label1,label2)
282 //         : Checks references of attributes of label1 and label2 
283 //         : in order to find shareable attributes
284 //=======================================================================
285
286 static Standard_Integer  DDF_CheckAttrs (Draw_Interpretor& di,Standard_Integer n, const char** a)
287 {
288   TDF_Label SOURCE,TARGET;
289   if (n == 4) {   
290     Handle(TDF_Data) DF;
291     if(!DDF::GetDF(a[1], DF)) return 1;
292     if (!DDF::FindLabel(DF,a[2],SOURCE)) return 1;   
293     if (!DDF::FindLabel(DF,a[3],TARGET)) return 1;
294
295     Handle(TDF_DataSet) ds1 = new TDF_DataSet();
296     Handle(TDF_DataSet) ds2 = new TDF_DataSet();
297     Standard_Boolean Shar = Standard_False;
298     for (TDF_AttributeIterator itr(SOURCE); itr.More(); itr.Next()) {
299       itr.Value()->References(ds1);
300 //      cout<<"\tSource Attribute dynamic type = "<<itr.Value()->DynamicType()<<endl;
301       const TDF_AttributeMap& attMap = ds1->Attributes(); //attMap
302       for (TDF_MapIteratorOfAttributeMap attMItr(attMap);attMItr.More(); attMItr.Next()) {
303         Handle(TDF_Attribute) sAtt = attMItr.Key();
304 //      cout<<"\t\tSource references attribute dynamic type = "<<sAtt->DynamicType()<<endl;
305         for (TDF_AttributeIterator itr2(TARGET); itr2.More(); itr2.Next()) {
306           itr2.Value()->References(ds2);
307 //        cout<<"\t\t\tTARGET attribute dynamic type = "<<itr2.Value()->DynamicType()<<endl;
308           const TDF_AttributeMap& attMap2 = ds2->Attributes(); //attMap
309           for (TDF_MapIteratorOfAttributeMap attMItr2(attMap2);attMItr2.More(); attMItr2.Next()) {
310             Handle(TDF_Attribute) tAtt = attMItr2.Key();
311 //          cout<<"\t\t\t\tTarget reference attribute dynamic type = "<<tAtt->DynamicType()<<endl;
312             if (tAtt->IsInstance(sAtt->DynamicType()))
313               if(tAtt == sAtt) {
314                 TCollection_AsciiString entr1,entr2;
315                 if(!Shar) {
316                   TDF_Tool::Entry(SOURCE, entr1);  
317                   TDF_Tool::Entry(TARGET, entr2);
318                   //cout<<"\tSHAREABLE attribute(s) found between Lab1 = "<<entr1<<" and Lab2 = "<<entr2<<endl;
319                   di<<"\tSHAREABLE attribute(s) found between Lab1 = "<<entr1.ToCString()<<" and Lab2 = "<<entr2.ToCString()<<"\n";
320                   Shar = Standard_True;
321                 }
322                 TDF_Tool::Entry(sAtt->Label(), entr1);
323                 //cout<<"\tAttribute dynamic type = "<<sAtt->DynamicType()<<",\tlocated on Label = "<<entr1<<endl;
324                 di<<"\tAttribute dynamic type = ";
325                 Standard_SStream aSStream;
326                 sAtt->DynamicType()->Print(aSStream);
327                 aSStream << ends;
328                 di << aSStream;
329                 di<<",\tlocated on Label = "<<entr1.ToCString()<<"\n";
330               }
331           }
332           ds2->Clear();
333         }
334       }
335       ds1->Clear();
336     }
337     if(!Shar) 
338       di << "Shareable attributes not found" << "\n";
339     return 0;
340   }
341   di << "DDF_CheckAttrs : Error" << "\n";
342   return 1;    
343 }
344
345 //=======================================================================
346 //function : DDF_Checklabel
347 //purpose  : CheckLabel (DOC,label1,label2)
348 //         : prints all structure of first level attributes with its references
349 //=======================================================================
350 static Standard_Integer  DDF_CheckLabel (Draw_Interpretor& di,Standard_Integer n, const char** a)
351 {
352 //  TDF_Label SOURCE,TARGET;
353   TDF_Label SOURCE;
354   if (n == 3) {   
355     Handle(TDF_Data) DF;
356     if(!DDF::GetDF(a[1], DF)) return 1;
357     if (!DDF::FindLabel(DF,a[2],SOURCE)) return 1;   
358
359     Handle(TDF_DataSet) ds1 = new TDF_DataSet();
360     for (TDF_AttributeIterator itr(SOURCE); itr.More(); itr.Next()) {
361       itr.Value()->References(ds1);
362       //cout<<"\tSource Attribute dynamic type = "<<itr.Value()->DynamicType()<<endl;
363       di<<"\tSource Attribute dynamic type = ";
364       Standard_SStream aSStream1;
365       itr.Value()->DynamicType()->Print(aSStream1);
366       aSStream1 << ends;
367       di << aSStream1 << "\n";
368       const TDF_AttributeMap& attMap = ds1->Attributes(); //attMap
369       for (TDF_MapIteratorOfAttributeMap attMItr(attMap);attMItr.More(); attMItr.Next()) {
370         Handle(TDF_Attribute) sAtt = attMItr.Key();
371         TCollection_AsciiString entry;
372         TDF_Tool::Entry(sAtt->Label(), entry);
373         //cout<<"\t\tReferences attribute dynamic type = "<<sAtt->DynamicType()<<",\tLabel = "<<entry<<endl;
374         di<<"\t\tReferences attribute dynamic type = ";
375         Standard_SStream aSStream2;
376         sAtt->DynamicType()->Print(aSStream2);
377         di << aSStream2;
378         di<<",\tLabel = "<<entry.ToCString()<<"\n";
379       }
380       ds1->Clear();
381     }
382
383     return 0;
384   }
385   di << "DDF_ChecLabel : Error" << "\n";
386   return 1;    
387 }
388
389
390
391 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
392
393
394 //=======================================================================
395 //function : DataCommands
396 //purpose  : 
397 //=======================================================================
398
399 void DDF::DataCommands (Draw_Interpretor& theCommands) 
400 {
401   static Standard_Boolean done = Standard_False;
402   if (done) return;
403   done = Standard_True;
404
405   const char* g = "DF Data Framework commands";
406
407   // Data Framework :
408   // ++++++++++++++++
409   theCommands.Add ("MakeDF",
410                    "Makes a new DF: MakeDF dfname",
411                    __FILE__, MakeDF, g);
412
413   theCommands.Add ("ClearDF",
414                    "Clears a DF: ClearDF dfname",
415                    __FILE__, ClearDF, g);
416
417   theCommands.Add ("CopyDF",
418                    "Copies a label: CopyDF dfname1 entry1 [dfname2] entry2",
419                    __FILE__, CopyDF, g);
420
421   theCommands.Add ("XDumpDF",
422                    "Exented deep dump of a DF (with attributes content): DumpDF dfname",
423                    __FILE__, XDumpDF, g);
424
425   theCommands.Add ("MiniDumpDF",
426                    "Mini dump of a DF (with attributes content): DumpDF dfname",
427                    __FILE__, MiniDumpDF, g);
428
429   theCommands.Add ("CopyLabel", 
430                    "CopyLabel (DOC, from, to)",
431                    __FILE__, CopyLabel_SCopy, g);    
432
433   theCommands.Add("CheckAttrs","CheckAttrs DocName Lab1 Lab2 ",
434                    __FILE__, DDF_CheckAttrs, g);
435
436   theCommands.Add("CheckLabel","CheckLabel DocName Label ",
437                    __FILE__, DDF_CheckLabel, g);
438
439 }