0022976: A draw-command CopyDF failes to copy content of a label
[occt.git] / src / DDF / DDF_BasicCommands.cxx
1 // File:        DDF_BasicCommands.cxx
2 //              ---------------------
3 // Author:      DAUTRY Philippe & VAUTHIER Jean-Claude
4 // Copyright:   Matra Datavision 1997
5
6 // Version:     0.0
7 // History:     Version Date            Purpose
8 //              0.0     Feb 10 1997     Creation
9
10
11 #include <DDF.hxx>
12
13 #include <TDF_ComparisonTool.hxx>
14 #include <TDF_CopyTool.hxx>
15 #include <TDF_ClosureMode.hxx>
16 #include <TDF_ClosureTool.hxx>
17
18 #include <DDF_Data.hxx>
19
20 #include <Draw.hxx>
21 #include <Draw_Appli.hxx>
22 #include <Draw_Drawable3D.hxx>
23 #include <Draw_Interpretor.hxx>
24 #include <Standard_GUID.hxx>
25 #include <Standard_NotImplemented.hxx>
26
27 #include <TColStd_HSequenceOfAsciiString.hxx>
28 #include <TColStd_ListOfInteger.hxx>
29 #include <TColStd_SequenceOfAsciiString.hxx>
30
31 #include <TCollection_AsciiString.hxx>
32 #include <TCollection_ExtendedString.hxx>
33
34 #include <TDF_Attribute.hxx>
35 #include <TDF_TagSource.hxx>
36 #include <TDF_AttributeIterator.hxx>
37 #include <TDF_ChildIterator.hxx>
38 #include <TDF_Data.hxx>
39 #include <TDF_DataSet.hxx>
40 #include <TDF_Delta.hxx>
41 #include <TDF_IDFilter.hxx>
42 #include <TDF_Label.hxx>
43 #include <TDF_RelocationTable.hxx>
44 #include <TDF_Tool.hxx>
45
46 #include <DDF_IOStream.hxx>
47
48
49 //=======================================================================
50 //function : Children
51 //purpose  : Returns a list of sub-label entries.
52 //=======================================================================
53
54 static Standard_Integer DDF_Children (Draw_Interpretor& di, 
55                                       Standard_Integer  n, 
56                                       const char**            a)
57 {
58   if (n < 2) return 1;
59   
60   Handle(TDF_Data) DF;
61   TCollection_AsciiString entry;
62
63   if (!DDF::GetDF (a[1], DF)) return 1;
64
65   TDF_Label lab;
66   if (n == 3) TDF_Tool::Label(DF,a[2],lab);
67
68   if (lab.IsNull()) {
69     di<<"0";
70   }
71   else {
72     for (TDF_ChildIterator itr(lab); itr.More(); itr.Next()) {
73       TDF_Tool::Entry(itr.Value(),entry);
74       //TCollection_AsciiString entry(itr.Value().Tag());
75       di<<entry.ToCString()<<" ";
76     }
77   }
78   return 0;
79 }
80
81
82 //=======================================================================
83 //function : Attributes
84 //purpose  : Returns a list of label attributes.
85 //=======================================================================
86
87 static Standard_Integer DDF_Attributes (Draw_Interpretor& di, 
88                                         Standard_Integer  n, 
89                                         const char**            a)
90 {
91   if (n != 3) return 1;
92   
93   Handle(TDF_Data) DF;
94
95   if (!DDF::GetDF (a[1], DF)) return 1;
96
97   TDF_Label lab;
98   TDF_Tool::Label(DF,a[2],lab);
99
100   if (lab.IsNull()) return 1;
101
102   for (TDF_AttributeIterator itr(lab); itr.More(); itr.Next()) {
103     di<<itr.Value()->DynamicType()->Name()<<" ";
104   }
105   return 0;
106 }
107
108
109 //=======================================================================
110 //function : ForgetAll
111 //purpose  : "ForgetAll dfname Label"
112 //=======================================================================
113
114 static Standard_Integer DDF_ForgetAll(Draw_Interpretor& /*di*/, 
115                                       Standard_Integer  n, 
116                                       const char**            a)
117 {
118   if (n != 3) return 1;
119   
120   Handle(TDF_Data) DF;
121
122   if (!DDF::GetDF (a[1], DF)) return 1;
123
124   TDF_Label label;
125   TDF_Tool::Label(DF,a[2],label);
126   if (label.IsNull()) return 1;
127   label.ForgetAllAttributes();
128 //POP pour NT
129   return 0;
130 }
131
132
133
134 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
135 // save/restore & Store/Retrieve commands
136 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
137
138
139
140 //==========================================================
141 // ErrorMessage
142 //==========================================================
143
144 void ErrorMessage (const Storage_Error n) 
145 {
146   cout << "Storage Error: " << flush;
147
148   switch (n) {
149   case Storage_VSOk:
150     cout << "no problem" << endl;
151     break;
152   case Storage_VSOpenError:
153     cout << "while opening the stream" << endl;
154     break;
155   case Storage_VSModeError:
156     cout << "the stream is opened with a wrong mode for operation " << endl;
157     break;
158   case Storage_VSCloseError:
159     cout << "while closing the stream" << endl;
160     break;
161   case Storage_VSAlreadyOpen:
162     cout << "stream is already opened" << endl;
163     break;
164   case Storage_VSNotOpen:
165     cout << "stream not opened" << endl;
166     break;
167   case Storage_VSSectionNotFound:
168     cout << "the section is not found" << endl;
169     break;
170   case Storage_VSWriteError:
171     cout << "error during writing" << endl;
172     break;
173   case Storage_VSFormatError:
174     cout << "wrong format error occured while reading" << endl;
175     break;
176   case Storage_VSUnknownType:
177     cout << "try to read an unknown type" << endl;
178     break;
179   case Storage_VSTypeMismatch:
180     cout << "try to read a wrong primitive type (read a char while expecting a real)" << endl;
181     break;
182   case Storage_VSInternalError:
183     cout << "internal error" << endl;
184     break;
185   case Storage_VSExtCharParityError:      cout << "parity error" << endl;
186     break;
187   default:
188     cout << "unknown error code" << endl;
189     break;
190   }
191 }
192
193
194 //=======================================================================
195 //function : DDF_SetTagger
196 //purpose  : SetTagger (DF, entry)
197 //=======================================================================
198
199 static Standard_Integer DDF_SetTagger (Draw_Interpretor& di,
200                                                Standard_Integer nb, 
201                                                const char** arg) 
202 {   
203   if (nb == 3) {    
204     Handle(TDF_Data) DF;
205     if (!DDF::GetDF(arg[1],DF)) return 1;
206     TDF_Label L;
207     DDF::AddLabel(DF, arg[2], L);
208     TDF_TagSource::Set(L); 
209     return 0;
210   }
211   di << "DDF_SetTagger : Error" << "\n";
212   return 1;
213 }
214
215
216
217 //=======================================================================
218 //function : DDF_NewTag
219 //purpose  : NewTag (DF,[father]
220 //=======================================================================
221
222 static Standard_Integer DDF_NewTag (Draw_Interpretor& di,
223                                          Standard_Integer nb, 
224                                          const char** arg) 
225
226   if (nb == 3) {
227     Handle(TDF_Data) DF;
228     if (!DDF::GetDF(arg[1],DF)) return 1;
229     Handle(TDF_TagSource) A;
230     if (!DDF::Find(DF,arg[2],TDF_TagSource::GetID(),A)) return 1;
231     di << A->NewTag ();
232     return 0;
233   }
234   di << "DDF_NewTag : Error" << "\n";
235   return 1;
236 }
237
238
239 //=======================================================================
240 //function : DDF_NewChild
241 //purpose  : NewChild(DF,[father])
242 //=======================================================================
243
244 static Standard_Integer DDF_NewChild (Draw_Interpretor& di,
245                                            Standard_Integer nb, 
246                                            const char** arg) 
247
248   Handle(TDF_Data) DF;
249   if (nb>=2){
250     if (!DDF::GetDF(arg[1],DF)) return 1;
251     if (nb == 2) {
252       TDF_Label free = TDF_TagSource::NewChild(DF->Root());
253       di << free.Tag();
254       return 0;
255     } 
256     else  if (nb == 3) {
257       TDF_Label fatherlabel;
258       if (!DDF::FindLabel(DF,arg[2],fatherlabel)) return 1;
259       TDF_Label free = TDF_TagSource::NewChild (fatherlabel);
260       di << arg[2] << ":" << free.Tag();
261       return 0;
262     }
263   }
264   di << "DDF_NewChild : Error" << "\n";
265   return 1;
266 }
267
268
269 //=======================================================================
270 //function : Label (DF,freeentry)
271 //=======================================================================
272
273 static Standard_Integer DDF_Label (Draw_Interpretor& di,Standard_Integer n, const char** a)
274 {  
275   if (n == 3) {  
276     Handle(TDF_Data) DF;  
277     if (!DDF::GetDF (a[1],DF)) return 1; 
278     TDF_Label L;
279     if (!DDF::FindLabel(DF,a[2],L,Standard_False)) { 
280       DDF::AddLabel(DF,a[2],L);  
281       //di << "Label : " << a[2] << " created" << "\n";
282     }
283     //else di << "Label : " << a[2] << " retrieved" << "\n";
284     DDF::ReturnLabel(di,L);
285     return 0;
286   }
287   di << "DDF_Label : Error" << "\n";
288   return 1; 
289 }
290
291
292 //=======================================================================
293 //function : BasicCommands
294 //purpose  : 
295 //=======================================================================
296
297 void DDF::BasicCommands (Draw_Interpretor& theCommands) 
298 {
299   static Standard_Boolean done = Standard_False;
300   if (done) return;
301   done = Standard_True;
302
303   const char* g = "DF basic commands";
304
305   // Label :  
306
307   theCommands.Add ("SetTagger", 
308                    "SetTagger (DF, entry)",
309                    __FILE__, DDF_SetTagger, g);  
310
311   theCommands.Add ("NewTag", 
312                    "NewTag (DF, tagger)",
313                    __FILE__, DDF_NewTag, g);
314
315   theCommands.Add ("NewChild", 
316                    "NewChild (DF, [tagger])",
317                    __FILE__, DDF_NewChild, g);
318
319   theCommands.Add ("Children",
320                    " Returns the list of label children: Children DF label",
321                    __FILE__, DDF_Children, g);
322
323   theCommands.Add ("Attributes",
324                    " Returns the list of label attributes: Attributes DF label",
325                    __FILE__, DDF_Attributes, g);
326
327   theCommands.Add ("ForgetAll",
328                    "Forgets all attributes from the label: ForgetAll DF Label",
329                    __FILE__, DDF_ForgetAll, g);
330
331   theCommands.Add ("Label",
332                    "Label DF entry",
333                   __FILE__, DDF_Label, g);  
334
335
336 }