1 // Created on: 2000-03-01
2 // Created by: Denis PASCAL
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <DDocStd.hxx>
18 #include <BinDrivers_DocumentStorageDriver.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>
35 #include <Draw_Interpretor.hxx>
36 #include <TDF_MapIteratorOfLabelMap.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>
45 #include <TDocStd_XLinkTool.hxx>
47 #include <OSD_Function.hxx>
48 #include <OSD_SharedLibrary.hxx>
49 #include <OSD_LoadMode.hxx>
51 //typedef Standard_Integer (* DFBROWSER_CALL)(const Handle(TDocStd_Document)&);
52 //static DFBROWSER_CALL gDFunc = 0;
54 //=======================================================================
56 //purpose : Main (DOC)
57 //=======================================================================
59 static Standard_Integer DDocStd_Main (Draw_Interpretor& di,Standard_Integer nb, const char** a)
62 Handle(TDocStd_Document) DOC;
63 if (!DDocStd::GetDocument(a[1],DOC)) return 1;
64 DDocStd::ReturnLabel(di,DOC->Main());
67 di << "DDocStd_Main : Error\n";
72 //=======================================================================
75 //=======================================================================
77 static Standard_Integer DDocStd_Format (Draw_Interpretor& di,Standard_Integer n, const char** a)
79 Handle(TDocStd_Document) D;
81 if (!DDocStd::GetDocument(a[1],D)) return 1;
82 //std::cout << "FORMAT : " << D->StorageFormat() << std::endl;
84 Standard_SStream aStream;
85 D->StorageFormat().Print(aStream);
91 if (!DDocStd::GetDocument(a[1],D)) return 1;
92 D->ChangeStorageFormat(a[2]);
95 di << "DDocStd_Format : Error\n";
99 //=======================================================================
100 //function : Copy "Copy DOC entry XDOC xentry",
101 //=======================================================================
103 static Standard_Integer DDocStd_Copy (Draw_Interpretor& di,Standard_Integer n, const char** a)
106 Handle(TDocStd_Document) DOC, XDOC;
107 if (!DDocStd::GetDocument(a[1],DOC)) return 1;
108 if (!DDocStd::GetDocument(a[3],XDOC)) return 1;
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";
119 di << "DDocStd_XLinkTool : Error\n";
124 //=======================================================================
125 //function : CopyWithLink "Copy DOC entry XDOC xentry",
126 //=======================================================================
128 static Standard_Integer DDocStd_CopyWithLink (Draw_Interpretor& di,Standard_Integer n, const char** a)
131 Handle(TDocStd_Document) DOC, XDOC;
132 if (!DDocStd::GetDocument(a[1],DOC)) return 1;
133 if (!DDocStd::GetDocument(a[3],XDOC)) return 1;
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";
144 di << "DDocStd_CopyWithLink : Error\n";
148 //=======================================================================
149 //function : UpdateLink (D,[xrefentry])
150 //=======================================================================
152 static Standard_Integer DDocStd_UpdateLink (Draw_Interpretor& di,Standard_Integer nb, const char** a)
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;
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";
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";
176 di << "DDocStd_UpdateXLink : Error\n";
180 //=======================================================================
181 //function : UndoLimit
183 //=======================================================================
185 static Standard_Integer DDocStd_UndoLimit (Draw_Interpretor& di,Standard_Integer n, const char** a)
189 Handle(TDocStd_Document) D;
190 if (!DDocStd::GetDocument(a[1],D)) return 1;
193 Standard_Integer lim = Draw::Atoi(a[2]);
194 D->SetUndoLimit(lim);
197 // display current values
198 di << D->GetUndoLimit() << " ";
199 di << D->GetAvailableUndos() << " ";
200 di << D->GetAvailableRedos();
204 //=======================================================================
205 //function : Undo, Redo
206 //purpose : Undo (DOC)
207 //=======================================================================
209 static Standard_Integer DDocStd_Undo (Draw_Interpretor& di,Standard_Integer n, const char** a)
213 Handle(TDocStd_Document) D;
214 if (!DDocStd::GetDocument(a[1],D)) return 1;
216 Standard_Integer i,step = 1;
218 step = Draw::Atoi(a[2]);
221 // test if the command was undo or redo
222 Standard_Boolean undo = a[0][0] == 'U';
224 for (i = 1; i <= step; i++) {
226 if (!D->Undo()) di << "Undo not done\n";
229 if (!D->Redo()) di << "Redo not done\n";
233 // Redraw the viewer.
234 Handle(AIS_InteractiveContext) IC;
235 if (TPrsStd_AISViewer::Find(D->Main(), IC))
236 IC->UpdateCurrentViewer();
241 //=======================================================================
242 //function : NewCommand
244 //=======================================================================
246 static Standard_Integer DDocStd_NewCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
249 Handle(TDocStd_Document) D;
250 if (!DDocStd::GetDocument(a[1],D)) return 1;
255 //=======================================================================
256 //function : OpenCommand
258 //=======================================================================
260 static Standard_Integer DDocStd_OpenCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
264 Handle(TDocStd_Document) D;
265 if (!DDocStd::GetDocument(a[1],D)) return 1;
270 //=======================================================================
271 //function : AbortCommand
273 //=======================================================================
275 static Standard_Integer DDocStd_AbortCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
278 Handle(TDocStd_Document) D;
279 if (!DDocStd::GetDocument(a[1],D)) return 1;
284 //=======================================================================
285 //function : CommitCommand
287 //=======================================================================
289 static Standard_Integer DDocStd_CommitCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
292 Handle(TDocStd_Document) D;
293 if (!DDocStd::GetDocument(a[1],D)) return 1;
299 //=======================================================================
300 //function : DDocStd_DumpDocument
301 //purpose : DumpDocument (DOC)
302 //=======================================================================
304 static Standard_Integer DDocStd_DumpDocument (Draw_Interpretor& di,
309 Handle(TDocStd_Document) D;
310 if (!DDocStd::GetDocument(arg[1],D)) return 1;
314 di << "DOCUMENT : " << TCollection_AsciiString(D->GetName(),'?').ToCString();
316 di << "DOCUMENT : not saved";
319 //std::cout << "FORMAT : " << D->StorageFormat();
321 Standard_SStream aStream;
322 D->StorageFormat().Print(aStream);
327 if (D->HasOpenCommand()) di << " Is Open";
328 else di << " Is Not Open";
331 di << " limit :" << D->GetUndoLimit();
332 di << " undos :" << D->GetAvailableUndos() << " ";
333 di << " redos :" << D->GetAvailableRedos();
335 // std::cout << "CURRENT :";
336 // TCollection_AsciiString string;
337 // TDF_Tool::Entry(D->CurrentLabel(),string);
338 // std::cout << string;
339 // std::cout << std::endl;
342 if (D->IsModified()) di << "true";
345 if (!TDocStd_Modified::IsEmpty(D->Main())) {
346 di << "MODIFICATIONS : ";
347 TDF_MapIteratorOfLabelMap it (D->GetModified());
348 if (!it.More()) di << "VALID\n";
350 TCollection_AsciiString string;
351 for (;it.More();it.Next()) {
352 TDF_Tool::Entry(it.Key(),string);
353 di << string.ToCString() << " ";
360 di << "DDocStd_DumpDocument : Error\n";
364 //=======================================================================
365 //function : SetModified
366 //purpose : Set modifications in a document
367 //=======================================================================
369 static Standard_Integer DDocStd_SetModified (Draw_Interpretor& di,Standard_Integer n, const char** a)
372 Handle(TDocStd_Document) D;
373 if (!DDocStd::GetDocument(a[1],D)) return 1;
375 for (Standard_Integer i = 2; i < n; i++) {
376 if (DDocStd::Find(D,a[i],L)) D->SetModified(L);
380 di << "DDocStd_SetModified : Error\n";
384 //=======================================================================
385 //function : Propagate
387 //=======================================================================
389 static Standard_Integer DDocStd_Propagate (Draw_Interpretor& di,Standard_Integer /*n*/, const char** /*a*/)
392 // Handle(TDocStd_Document) D;
393 // if (!DDocStd::GetDocument(a[1],D)) return 1;
394 // if (D->IsValid()) {
395 // std::cout << "the document is valid" << std::endl;
398 // Handle(TDesign_Function) F;
399 // if (!D->Main().FindAttribute(TDesign_Function::GetID(),F)) {
400 // std::cout << "no function found at main" << std::endl;
403 // TFunction_Logbook mdf (Standard_True);
404 // for (TDF_MapIteratorOfLabelMap it (D->GetModified());it.More();it.Next()) {
405 // mdf.SetTouched(it.Key());
408 // D->PurgeModified();
411 di << "DDocStd_Propagate : not implemented\n";
415 //=======================================================================
416 //function : DDocStd_StoreTriangulation
418 //=======================================================================
420 static Standard_Integer DDocStd_StoreTriangulation (Draw_Interpretor& theDi,
421 Standard_Integer theNbArgs,
422 const char** theArgVec)
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())
430 std::cout << "Error: BinXCAF or BinOcaf storage formats are not registered\n";
436 theDi << (aDriverXCaf->IsWithTriangles() ? "1" : "0");
439 else if (theNbArgs != 2)
441 std::cout << "Syntax error: wrong number of arguments\n";
445 const Standard_Boolean toEnable = (Draw::Atoi (theArgVec[1]) != 0);
446 aDriverXCaf->SetWithTriangles (anApp->MessageDriver(), toEnable);
447 aDriverOcaf->SetWithTriangles (anApp->MessageDriver(), toEnable);
451 //=======================================================================
452 //function : DocumentCommands
454 //=======================================================================
456 void DDocStd::DocumentCommands(Draw_Interpretor& theCommands)
459 static Standard_Boolean done = Standard_False;
461 done = Standard_True;
464 const char* g = "DDocStd commands";
466 // Data Framework Access
468 theCommands.Add("Main","Main (DOC)",
469 __FILE__, DDocStd_Main, g);
474 theCommands.Add ("Format",
475 "Format (DOC, [format])",
476 __FILE__, DDocStd_Format, g);
478 theCommands.Add ("DumpDocument",
479 "DumpDocument (DOC)",
480 __FILE__, DDocStd_DumpDocument, g);
482 theCommands.Add ("StoreTriangulation",
483 "StoreTriangulation [toStore={0|1}]"
484 "\nSetup BinXCAF/BinOcaf storage drivers to write triangulation",
485 __FILE__, DDocStd_StoreTriangulation, g);
489 theCommands.Add("Copy","Copy DOC entry XDOC xentry",
490 __FILE__, DDocStd_Copy, g);
492 theCommands.Add("CopyWithLink","CopyWithLink DOC entry XDOC xentry",
493 __FILE__, DDocStd_CopyWithLink, g);
495 theCommands.Add("UpdateLink","UpdateLink DOC [entry]",
496 __FILE__, DDocStd_UpdateLink, g);
501 theCommands.Add("UndoLimit","UndoLimit DOC (Value), return UndoLimit Undos Redos",
502 __FILE__, DDocStd_UndoLimit, g);
504 theCommands.Add("Undo","Undo DOC (steps = 1)",
505 __FILE__, DDocStd_Undo, g);
507 theCommands.Add("Redo","Redo DOC (steps = 1)",
508 __FILE__, DDocStd_Undo, g);
510 theCommands.Add("NewCommand","NewCommand DOC",
511 __FILE__, DDocStd_NewCommand, g);
513 theCommands.Add("OpenCommand","OpenCommand DOC",
514 __FILE__, DDocStd_OpenCommand, g);
516 theCommands.Add("AbortCommand","AbortCommand DOC",
517 __FILE__, DDocStd_AbortCommand, g);
519 theCommands.Add("CommitCommand","CommitCommand DOC",
520 __FILE__, DDocStd_CommitCommand, g);
523 // Modif and Propagation
525 theCommands.Add("SetModified","SetModified DOC Label1 Label2 ....",
526 __FILE__, DDocStd_SetModified, g);
528 theCommands.Add("Propagate","Propagate DOC",
529 __FILE__, DDocStd_Propagate, g);