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