0027619: Improvement on manuipulation of data by TPrsStd_AISPresentation attribute.
[occt.git] / src / DDocStd / DDocStd_DocumentCommands.cxx
1 // Created on: 2000-03-01
2 // Created by: Denis PASCAL
3 // Copyright (c) 2000-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 <DDocStd.hxx>
17
18 #include <BinDrivers_DocumentStorageDriver.hxx>
19 #include <DDF.hxx>
20 #include <Draw.hxx>
21 #include <Draw_Interpretor.hxx>
22 #include <TDocStd_XLinkTool.hxx>
23 #include <TDF_Reference.hxx>
24 #include <TDocStd_Document.hxx>
25 #include <CDM_Document.hxx>
26 #include <TDocStd_Modified.hxx>
27 #include <TDF_Label.hxx>
28 #include <DDocStd_DrawDocument.hxx>
29 #include <TDocStd_Document.hxx>
30 #include <TDocStd_XLink.hxx>
31 #include <TDocStd_XLinkRoot.hxx>
32 #include <TDocStd_XLinkIterator.hxx>
33 #include <TDocStd_Application.hxx>
34 #include <Draw.hxx>
35 #include <Draw_Interpretor.hxx>
36 #include <TDF_MapIteratorOfLabelMap.hxx>
37 #include <Plugin.hxx>
38 #include <TCollection_ExtendedString.hxx>
39 #include <TDF_Tool.hxx>
40 #include <TDF_ChildIterator.hxx>
41 #include <TDF_Tool.hxx>
42 #include <TPrsStd_AISViewer.hxx>
43 #include <AIS_InteractiveContext.hxx>
44 // pour propagate
45 #include <TDocStd_XLinkTool.hxx>
46
47 #include <OSD_Function.hxx>
48 #include <OSD_SharedLibrary.hxx>
49 #include <OSD_LoadMode.hxx>
50
51 //typedef Standard_Integer (* DFBROWSER_CALL)(const Handle(TDocStd_Document)&);
52 //static DFBROWSER_CALL gDFunc = 0;
53
54 //=======================================================================
55 //function : Main
56 //purpose  : Main (DOC)
57 //=======================================================================
58
59 static Standard_Integer DDocStd_Main (Draw_Interpretor& di,Standard_Integer nb, const char** a)
60 {
61   if (nb == 2) {
62     Handle(TDocStd_Document) DOC;   
63     if (!DDocStd::GetDocument(a[1],DOC)) return 1;
64     DDocStd::ReturnLabel(di,DOC->Main());
65     return 0;
66   }   
67   di << "DDocStd_Main : Error\n";
68   return 1;  
69 }
70
71
72 //=======================================================================
73 //function : Format
74 //purpose  : 
75 //=======================================================================
76
77 static Standard_Integer DDocStd_Format (Draw_Interpretor& di,Standard_Integer n, const char** a)
78
79   Handle(TDocStd_Document) D;
80   if (n == 2) {
81     if (!DDocStd::GetDocument(a[1],D)) return 1;    
82     //cout << "FORMAT : " << D->StorageFormat() << endl;
83     di << "FORMAT : ";
84     Standard_SStream aStream;
85     D->StorageFormat().Print(aStream);
86     di << aStream;
87     di << "\n";
88     return 0;
89   }  
90   if (n == 3) {
91     if (!DDocStd::GetDocument(a[1],D)) return 1;    
92     D->ChangeStorageFormat(a[2]);  
93     return 0;
94   }  
95   di << "DDocStd_Format : Error\n";
96   return 1;
97 }
98
99 //=======================================================================
100 //function : Copy "Copy DOC entry XDOC xentry",
101 //=======================================================================
102
103 static Standard_Integer DDocStd_Copy (Draw_Interpretor& di,Standard_Integer n, const char** a)
104 {
105   if (n == 5) {  
106     Handle(TDocStd_Document) DOC, XDOC;  
107     if (!DDocStd::GetDocument(a[1],DOC)) return 1; 
108     if (!DDocStd::GetDocument(a[3],XDOC)) return 1;
109     TDF_Label L,XL;
110     if (!DDocStd::Find(DOC,a[2],L)) return 1; 
111     if (!DDocStd::Find(XDOC,a[4],XL)) return 1;
112     TDocStd_XLinkTool XLinkTool;
113     XLinkTool.Copy(L,XL);
114     if (!XLinkTool.IsDone()) {
115       di << "DDocStd_XLinkTool : not done\n";
116     }
117     return 0;
118   }  
119   di << "DDocStd_XLinkTool : Error\n";
120   return 1; 
121 }
122
123
124 //=======================================================================
125 //function : CopyWithLink "Copy DOC entry XDOC xentry",
126 //=======================================================================
127
128 static Standard_Integer DDocStd_CopyWithLink (Draw_Interpretor& di,Standard_Integer n, const char** a)
129 {  
130   if (n == 5) {  
131     Handle(TDocStd_Document) DOC, XDOC;  
132     if (!DDocStd::GetDocument(a[1],DOC)) return 1; 
133     if (!DDocStd::GetDocument(a[3],XDOC)) return 1;   
134     TDF_Label L,XL;
135     if (!DDocStd::Find(DOC,a[2],L)) return 1; 
136     if (!DDocStd::Find(XDOC,a[4],XL)) return 1;
137     TDocStd_XLinkTool XLinkTool;
138     XLinkTool.CopyWithLink(L,XL);
139     if (!XLinkTool.IsDone()) {
140       di << "DDocStd_CopyWithLink : not done\n";
141     }
142     return 0;
143   }  
144   di << "DDocStd_CopyWithLink : Error\n";
145   return 1; 
146 }
147
148 //=======================================================================
149 //function : UpdateLink (D,[xrefentry])
150 //=======================================================================
151
152 static Standard_Integer DDocStd_UpdateLink (Draw_Interpretor& di,Standard_Integer nb, const char** a)
153 {  
154   if (nb == 2 || nb == 3) {  
155     Handle(TDocStd_Document) DOC;  
156     if (!DDocStd::GetDocument(a[1],DOC)) return 1; 
157     Handle(TDF_Reference) REF;
158     TDocStd_XLinkTool XLinkTool;  
159     if (nb == 3) {
160       if (!DDocStd::Find(DOC,a[2],TDF_Reference::GetID(),REF)) return 1;
161       XLinkTool.UpdateLink(REF->Label());
162       if (!XLinkTool.IsDone()) {
163         di << "DDocStd_UpdateXLink : not done\n";
164       }
165     }
166     else {
167       for (TDocStd_XLinkIterator xit (DOC); xit.More(); xit.Next()) {
168         XLinkTool.UpdateLink(xit.Value()->Label());
169         if (!XLinkTool.IsDone()) {
170           di << "DDocStd_UpdateXLink : not done\n";
171         }
172       }
173     }
174     return 0;
175   }
176   di << "DDocStd_UpdateXLink : Error\n";
177   return 1; 
178 }
179
180 //=======================================================================
181 //function : UndoLimit
182 //purpose  : 
183 //=======================================================================
184
185 static Standard_Integer DDocStd_UndoLimit (Draw_Interpretor& di,Standard_Integer n, const char** a)
186 {
187   if (n < 2) return 1;
188   
189   Handle(TDocStd_Document) D;
190   if (!DDocStd::GetDocument(a[1],D)) return 1;
191   
192   if (n > 2) {
193     Standard_Integer lim = Draw::Atoi(a[2]);
194     D->SetUndoLimit(lim);
195   }
196   
197   // display current values
198   di << D->GetUndoLimit() << " ";
199   di << D->GetAvailableUndos() << " ";
200   di << D->GetAvailableRedos();
201   return 0;
202 }
203
204 //=======================================================================
205 //function : Undo, Redo
206 //purpose  : Undo (DOC)
207 //=======================================================================
208
209 static Standard_Integer DDocStd_Undo (Draw_Interpretor& di,Standard_Integer n, const char** a)
210 {
211   if (n < 2) return 1;
212   
213   Handle(TDocStd_Document) D;
214   if (!DDocStd::GetDocument(a[1],D)) return 1;
215   
216   Standard_Integer i,step = 1;
217   if (n > 2) {
218     step = Draw::Atoi(a[2]);
219   }
220
221   // test if the command was undo or redo
222   Standard_Boolean undo = a[0][0] == 'U';
223
224   for (i = 1; i <= step; i++) {
225     if (undo) {
226       if (!D->Undo()) di << "Undo not done\n";
227     }
228     else {
229       if (!D->Redo()) di << "Redo not done\n";
230     }
231   }
232
233   // Redraw the viewer.
234   Handle(AIS_InteractiveContext) IC;
235   if (TPrsStd_AISViewer::Find(D->Main(), IC))
236       IC->UpdateCurrentViewer();
237   
238   return 0;
239 }
240
241 //=======================================================================
242 //function : NewCommand
243 //purpose  : 
244 //=======================================================================
245
246 static Standard_Integer DDocStd_NewCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
247 {
248   if (n < 2) return 1;
249   Handle(TDocStd_Document) D;
250   if (!DDocStd::GetDocument(a[1],D)) return 1;
251   D->NewCommand();
252   return 0;
253 }
254
255 //=======================================================================
256 //function : OpenCommand
257 //purpose  : 
258 //=======================================================================
259
260 static Standard_Integer DDocStd_OpenCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
261 {
262   if (n < 2) return 1;
263
264   Handle(TDocStd_Document) D;
265   if (!DDocStd::GetDocument(a[1],D)) return 1;
266   D->OpenCommand();
267   return 0;
268 }
269
270 //=======================================================================
271 //function : AbortCommand
272 //purpose  : 
273 //=======================================================================
274
275 static Standard_Integer DDocStd_AbortCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
276 {
277   if (n < 2) return 1;
278   Handle(TDocStd_Document) D;
279   if (!DDocStd::GetDocument(a[1],D)) return 1;
280   D->AbortCommand();
281   return 0;
282 }
283
284 //=======================================================================
285 //function : CommitCommand
286 //purpose  : 
287 //=======================================================================
288
289 static Standard_Integer DDocStd_CommitCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
290 {
291   if (n < 2) return 1;
292   Handle(TDocStd_Document) D;
293   if (!DDocStd::GetDocument(a[1],D)) return 1;
294   D->CommitCommand();
295   return 0;
296 }
297
298
299 //=======================================================================
300 //function : DDocStd_DumpDocument
301 //purpose  : DumpDocument (DOC)
302 //=======================================================================
303
304 static Standard_Integer DDocStd_DumpDocument (Draw_Interpretor& di,
305                                               Standard_Integer nb, 
306                                               const char** arg) 
307 {   
308   if (nb == 2) {   
309     Handle(TDocStd_Document) D;       
310     if (!DDocStd::GetDocument(arg[1],D)) return 1;     
311     di << "\n"; 
312     // document name
313     if (D->IsSaved()) 
314       di << "DOCUMENT      : " << TCollection_AsciiString(D->GetName(),'?').ToCString();
315     else 
316       di << "DOCUMENT      : not saved";
317     di << "\n";
318     // format
319     //cout << "FORMAT        : " << D->StorageFormat();
320     di << "FORMAT        : ";
321     Standard_SStream aStream;
322     D->StorageFormat().Print(aStream);
323     di << aStream;
324     di << "\n";
325     // command
326     di << "COMMAND       : ";
327     if (D->HasOpenCommand()) di << " Is Open";
328     else di << " Is Not Open";
329     // undo
330     di << "UNDO          :"; 
331     di << " limit :" << D->GetUndoLimit();
332     di << " undos :" << D->GetAvailableUndos() << " ";  
333     di << " redos :" << D->GetAvailableRedos(); 
334     di << "\n";
335     // cout << "CURRENT :";   
336     //     TCollection_AsciiString string;  
337     //     TDF_Tool::Entry(D->CurrentLabel(),string); 
338     //     cout << string;
339     //     cout << endl;
340     // modified  
341     di << "MODIFIED      : ";
342     if (D->IsModified()) di << "true";
343     else di << "false";  
344     di << "\n"; 
345     if (!TDocStd_Modified::IsEmpty(D->Main())) {
346       di << "MODIFICATIONS : ";     
347       TDF_MapIteratorOfLabelMap it (D->GetModified()); 
348       if (!it.More()) di << "VALID\n"; 
349       else {
350         TCollection_AsciiString string;  
351         for (;it.More();it.Next()) { 
352           TDF_Tool::Entry(it.Key(),string);
353           di << string.ToCString() << " "; 
354         }
355         di << "\n";
356       } 
357     }
358     return 0;
359   } 
360   di << "DDocStd_DumpDocument : Error\n";
361   return 1;
362 }
363
364 //=======================================================================
365 //function : SetModified
366 //purpose  : Set modifications in a document
367 //=======================================================================
368
369 static Standard_Integer DDocStd_SetModified (Draw_Interpretor& di,Standard_Integer n, const char** a)
370 {
371   if (n > 2) {
372     Handle(TDocStd_Document) D;
373     if (!DDocStd::GetDocument(a[1],D)) return 1;
374     TDF_Label L;
375     for (Standard_Integer i = 2; i < n; i++) {
376       if (DDocStd::Find(D,a[i],L)) D->SetModified(L);
377     }
378     return 0; 
379   }
380   di << "DDocStd_SetModified : Error\n";
381   return 1;
382 }
383
384 //=======================================================================
385 //function : Propagate
386 //purpose  : 
387 //=======================================================================
388
389 static Standard_Integer DDocStd_Propagate (Draw_Interpretor& di,Standard_Integer /*n*/, const char** /*a*/)
390 {
391 //   if (n == 2) {
392 //     Handle(TDocStd_Document) D;
393 //     if (!DDocStd::GetDocument(a[1],D)) return 1;
394 //     if (D->IsValid()) {
395 //       cout << "the document is valid" << endl;
396 //       return 0;
397 //     }
398 //     Handle(TDesign_Function) F; 
399 //     if (!D->Main().FindAttribute(TDesign_Function::GetID(),F)) {
400 //       cout << "no function found at main" << endl;
401 //       return 0;
402 //     }
403 //     TFunction_Logbook mdf (Standard_True);   
404 //     for (TDF_MapIteratorOfLabelMap it (D->GetModified());it.More();it.Next()) {
405 //       mdf.SetTouched(it.Key());
406 //     }  
407 //     F->Execute(mdf);
408 //     D->PurgeModified();
409 //     return 0;
410 //   }
411   di << "DDocStd_Propagate : not implemented\n";
412   return 1;
413 }
414
415 //=======================================================================
416 //function : DDocStd_StoreTriangulation
417 //purpose  :
418 //=======================================================================
419
420 static Standard_Integer DDocStd_StoreTriangulation (Draw_Interpretor& theDi,
421                                                     Standard_Integer theNbArgs,
422                                                     const char** theArgVec)
423 {
424   const Handle(TDocStd_Application)& anApp = DDocStd::GetApplication();
425   Handle(BinDrivers_DocumentStorageDriver) aDriverXCaf = Handle(BinDrivers_DocumentStorageDriver)::DownCast(anApp->WriterFromFormat ("BinXCAF"));
426   Handle(BinDrivers_DocumentStorageDriver) aDriverOcaf = Handle(BinDrivers_DocumentStorageDriver)::DownCast(anApp->WriterFromFormat ("BinOcaf"));
427   if (aDriverXCaf.IsNull()
428    || aDriverOcaf.IsNull())
429   {
430     std::cout << "Error: BinXCAF or BinOcaf storage formats are not registered\n";
431     return 1;
432   }
433
434   if (theNbArgs == 1)
435   {
436     theDi << (aDriverXCaf->IsWithTriangles() ? "1" : "0");
437     return 0;
438   }
439   else if (theNbArgs != 2)
440   {
441     std::cout << "Syntax error: wrong number of arguments\n";
442     return 1;
443   }
444
445   const Standard_Boolean toEnable = (Draw::Atoi (theArgVec[1]) != 0);
446   aDriverXCaf->SetWithTriangles (anApp->MessageDriver(), toEnable);
447   aDriverOcaf->SetWithTriangles (anApp->MessageDriver(), toEnable);
448   return 0;
449 }
450
451 //=======================================================================
452 //function : DocumentCommands
453 //purpose  : 
454 //=======================================================================
455
456 void DDocStd::DocumentCommands(Draw_Interpretor& theCommands) 
457 {
458   
459   static Standard_Boolean done = Standard_False;
460   if (done) return;
461   done = Standard_True;
462   
463   
464   const char* g = "DDocStd commands";
465   
466   // Data Framework Access
467   
468   theCommands.Add("Main","Main (DOC)",
469                   __FILE__, DDocStd_Main, g);  
470
471   
472   // DUMP
473   
474   theCommands.Add ("Format", 
475                    "Format (DOC, [format])",
476                    __FILE__, DDocStd_Format, g); 
477
478   theCommands.Add ("DumpDocument", 
479                    "DumpDocument (DOC)",
480                    __FILE__, DDocStd_DumpDocument, g);   
481
482   theCommands.Add ("StoreTriangulation",
483                    "StoreTriangulation [toStore={0|1}]"
484                    "\nSetup BinXCAF/BinOcaf storage drivers to write triangulation",
485                    __FILE__, DDocStd_StoreTriangulation, g);
486
487   // XREF
488
489   theCommands.Add("Copy","Copy DOC entry XDOC xentry",
490                   __FILE__, DDocStd_Copy, g);  
491
492   theCommands.Add("CopyWithLink","CopyWithLink DOC entry XDOC xentry",
493                   __FILE__, DDocStd_CopyWithLink, g);  
494
495   theCommands.Add("UpdateLink","UpdateLink DOC [entry]",
496                   __FILE__, DDocStd_UpdateLink, g);  
497
498
499   // UNDO/REDO
500
501   theCommands.Add("UndoLimit","UndoLimit DOC (Value), return UndoLimit Undos Redos",
502                   __FILE__, DDocStd_UndoLimit, g);
503   
504   theCommands.Add("Undo","Undo DOC (steps = 1)",
505                   __FILE__, DDocStd_Undo, g);
506   
507   theCommands.Add("Redo","Redo DOC (steps = 1)",
508                   __FILE__, DDocStd_Undo, g);
509   
510   theCommands.Add("NewCommand","NewCommand DOC",
511                   __FILE__, DDocStd_NewCommand, g);  
512
513   theCommands.Add("OpenCommand","OpenCommand DOC",
514                   __FILE__, DDocStd_OpenCommand, g);  
515
516   theCommands.Add("AbortCommand","AbortCommand DOC",
517                   __FILE__, DDocStd_AbortCommand, g);    
518
519   theCommands.Add("CommitCommand","CommitCommand DOC",
520                   __FILE__, DDocStd_CommitCommand, g);    
521
522
523   // Modif and Propagation
524
525   theCommands.Add("SetModified","SetModified DOC Label1 Label2 ....",
526                   __FILE__, DDocStd_SetModified, g);
527
528   theCommands.Add("Propagate","Propagate DOC",
529                   __FILE__, DDocStd_Propagate, g);
530
531 }
532