de9dbb86070a73a57dfa2799cb4ef3c6a5ed400f
[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 //=======================================================================
59 //function : Children
60 //purpose  : Returns a list of sub-label entries.
61 //=======================================================================
62
63 static Standard_Integer DDF_Children (Draw_Interpretor& di, 
64                                       Standard_Integer  n, 
65                                       const char**            a)
66 {
67   if (n < 2) return 1;
68
69   Handle(TDF_Data) DF;
70   TCollection_AsciiString entry;
71
72   if (!DDF::GetDF (a[1], DF)) return 1;
73
74   TDF_Label lab;
75   if (n == 3) TDF_Tool::Label(DF,a[2],lab);
76
77   if (lab.IsNull()) {
78     di<<"0";
79   }
80   else {
81     for (TDF_ChildIterator itr(lab); itr.More(); itr.Next()) {
82       TDF_Tool::Entry(itr.Value(),entry);
83       //TCollection_AsciiString entry(itr.Value().Tag());
84       di<<entry.ToCString()<<" ";
85     }
86   }
87   return 0;
88 }
89
90
91 //=======================================================================
92 //function : Attributes
93 //purpose  : Returns a list of label attributes.
94 //=======================================================================
95
96 static Standard_Integer DDF_Attributes (Draw_Interpretor& di, 
97                                         Standard_Integer  n, 
98                                         const char**            a)
99 {
100   if (n != 3) return 1;
101
102   Handle(TDF_Data) DF;
103
104   if (!DDF::GetDF (a[1], DF)) return 1;
105
106   TDF_Label lab;
107   TDF_Tool::Label(DF,a[2],lab);
108
109   if (lab.IsNull()) return 1;
110
111   for (TDF_AttributeIterator itr(lab); itr.More(); itr.Next()) {
112     di<<itr.Value()->DynamicType()->Name()<<" ";
113   }
114   return 0;
115 }
116
117
118 //=======================================================================
119 //function : ForgetAll
120 //purpose  : "ForgetAll dfname Label"
121 //=======================================================================
122
123 static Standard_Integer DDF_ForgetAll(Draw_Interpretor& /*di*/, 
124                                       Standard_Integer  n, 
125                                       const char**            a)
126 {
127   if (n != 3) return 1;
128
129   Handle(TDF_Data) DF;
130
131   if (!DDF::GetDF (a[1], DF)) return 1;
132
133   TDF_Label label;
134   TDF_Tool::Label(DF,a[2],label);
135   if (label.IsNull()) return 1;
136   label.ForgetAllAttributes();
137   //POP pour NT
138   return 0;
139 }
140
141 //=======================================================================
142 //function : ForgetAttribute
143 //purpose  : "ForgetAtt dfname Label guid"
144 //=======================================================================
145
146 static Standard_Integer DDF_ForgetAttribute(Draw_Interpretor& di,
147                                             Standard_Integer  n,
148                                             const char**      a)
149 {
150   if (n != 4) return 1;
151   Handle(TDF_Data) DF;
152   if (!DDF::GetDF (a[1], DF)) return 1;
153
154   TDF_Label aLabel;
155   TDF_Tool::Label(DF,a[2],aLabel);
156   if (aLabel.IsNull()) return 1;
157   if (!Standard_GUID::CheckGUIDFormat(a[3]))
158   {
159     di<<"DDF: The format of GUID is invalid\n";
160     return 1;
161   }
162   Standard_GUID guid(a[3]);
163   aLabel.ForgetAttribute(guid);
164   return 0;
165 }
166
167 //=======================================================================
168 //function : DDF_SetTagger
169 //purpose  : SetTagger (DF, entry)
170 //=======================================================================
171
172 static Standard_Integer DDF_SetTagger (Draw_Interpretor& di,
173                                        Standard_Integer nb, 
174                                        const char** arg) 
175 {   
176   if (nb == 3) {    
177     Handle(TDF_Data) DF;
178     if (!DDF::GetDF(arg[1],DF)) return 1;
179     TDF_Label L;
180     DDF::AddLabel(DF, arg[2], L);
181     TDF_TagSource::Set(L); 
182     return 0;
183   }
184   di << "DDF_SetTagger : Error\n";
185   return 1;
186 }
187
188
189
190 //=======================================================================
191 //function : DDF_NewTag
192 //purpose  : NewTag (DF,[father]
193 //=======================================================================
194
195 static Standard_Integer DDF_NewTag (Draw_Interpretor& di,
196                                     Standard_Integer nb, 
197                                     const char** arg) 
198
199   if (nb == 3) {
200     Handle(TDF_Data) DF;
201     if (!DDF::GetDF(arg[1],DF)) return 1;
202     Handle(TDF_TagSource) A;
203     if (!DDF::Find(DF,arg[2],TDF_TagSource::GetID(),A)) return 1;
204     di << A->NewTag ();
205     return 0;
206   }
207   di << "DDF_NewTag : Error\n";
208   return 1;
209 }
210
211
212 //=======================================================================
213 //function : DDF_NewChild
214 //purpose  : NewChild(DF,[father])
215 //=======================================================================
216
217 static Standard_Integer DDF_NewChild (Draw_Interpretor& di,
218                                       Standard_Integer nb, 
219                                       const char** arg) 
220
221   Handle(TDF_Data) DF;
222   if (nb>=2){
223     if (!DDF::GetDF(arg[1],DF)) return 1;
224     if (nb == 2) {
225       TDF_Label free = TDF_TagSource::NewChild(DF->Root());
226       di << free.Tag();
227       return 0;
228     } 
229     else  if (nb == 3) {
230       TDF_Label fatherlabel;
231       if (!DDF::FindLabel(DF,arg[2],fatherlabel)) return 1;
232       TDF_Label free = TDF_TagSource::NewChild (fatherlabel);
233       di << arg[2] << ":" << free.Tag();
234       return 0;
235     }
236   }
237   di << "DDF_NewChild : Error\n";
238   return 1;
239 }
240
241
242 //=======================================================================
243 //function : Label (DF,freeentry)
244 //=======================================================================
245
246 static Standard_Integer DDF_Label (Draw_Interpretor& di,Standard_Integer n, const char** a)
247 {  
248   if (n == 3) {  
249     Handle(TDF_Data) DF;  
250     if (!DDF::GetDF (a[1],DF)) return 1; 
251     TDF_Label L;
252     if (!DDF::FindLabel(DF,a[2],L,Standard_False)) { 
253       DDF::AddLabel(DF,a[2],L);  
254       //di << "Label : " << a[2] << " created\n";
255     }
256     //else di << "Label : " << a[2] << " retrieved\n";
257     DDF::ReturnLabel(di,L);
258     return 0;
259   }
260   di << "DDF_Label : Error\n";
261   return 1; 
262 }
263
264
265 //=======================================================================
266 //function : BasicCommands
267 //purpose  : 
268 //=======================================================================
269
270 void DDF::BasicCommands (Draw_Interpretor& theCommands) 
271 {
272   static Standard_Boolean done = Standard_False;
273   if (done) return;
274   done = Standard_True;
275
276   const char* g = "DF basic commands";
277
278   // Label :
279
280   theCommands.Add ("SetTagger", 
281                    "SetTagger (DF, entry)",
282                    __FILE__, DDF_SetTagger, g);  
283
284   theCommands.Add ("NewTag", 
285                    "NewTag (DF, tagger)",
286                    __FILE__, DDF_NewTag, g);
287
288   theCommands.Add ("NewChild", 
289                    "NewChild (DF, [tagger])",
290                    __FILE__, DDF_NewChild, g);
291
292   theCommands.Add ("Children",
293                    " Returns the list of label children: Children DF label",
294                    __FILE__, DDF_Children, g);
295
296   theCommands.Add ("Attributes",
297                    " Returns the list of label attributes: Attributes DF label",
298                    __FILE__, DDF_Attributes, g);
299
300   theCommands.Add ("ForgetAll",
301                    "Forgets all attributes from the label: ForgetAll DF Label",
302                    __FILE__, DDF_ForgetAll, g);
303
304   theCommands.Add ("ForgetAtt",
305                    "Forgets the specified by guid attribute from the label: ForgetAtt DF Label guid",
306                    __FILE__, DDF_ForgetAttribute, g);
307
308   theCommands.Add ("Label",
309                    "Label DF entry",
310                    __FILE__, DDF_Label, g);  
311
312 }