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 <Draw_Interpretor.hxx>
19 #include <Draw_Viewer.hxx>
20 #include <Draw_ProgressIndicator.hxx>
21 #include <DDocStd_DrawDocument.hxx>
22 #include <TDocStd_Application.hxx>
23 #include <TDocStd_Document.hxx>
24 #include <TDataStd_Name.hxx>
26 #include <Standard_GUID.hxx>
27 #include <Standard_ExtString.hxx>
28 #include <TCollection_AsciiString.hxx>
29 #include <TCollection_ExtendedString.hxx>
31 #include <TDF_Data.hxx>
32 #include <TDF_ChildIterator.hxx>
33 #include <TDF_Tool.hxx>
35 #include <OSD_Path.hxx>
36 #include <OSD_OpenFile.hxx>
37 #include <TDocStd_PathParser.hxx>
38 #include <XmlLDrivers.hxx>
40 #include <AIS_InteractiveContext.hxx>
41 #include <TPrsStd_AISViewer.hxx>
42 #include <ViewerTest.hxx>
43 #include <V3d_Viewer.hxx>
46 extern Draw_Viewer dout;
48 Standard_IMPORT Draw_Viewer dout;
51 //=======================================================================
52 //function : ListDocuments
54 //=======================================================================
56 static Standard_Integer DDocStd_ListDocuments (Draw_Interpretor& di,
61 Handle(TDocStd_Application) A = DDocStd::GetApplication();
62 Handle(TDocStd_Document) D;
63 Standard_Integer nbdoc = A->NbDocuments();
64 for (Standard_Integer i = 1; i <= nbdoc; i++) {
66 di <<"document " << i;
68 di << " name : " << D->GetName();
69 di << " path : " << D->GetPath();
71 else di << " not saved";
76 di << "DDocStd_ListDocuments : Error\n";
81 //=======================================================================
82 //function : NewDocument
84 //=======================================================================
86 static Standard_Integer DDocStd_NewDocument (Draw_Interpretor& di,
90 Handle(TDocStd_Document) D;
91 Handle(DDocStd_DrawDocument) DD;
93 if (!DDocStd::GetDocument(a[1],D,Standard_False)) {
94 D = new TDocStd_Document("dummy");
95 DD = new DDocStd_DrawDocument(D);
97 di << "document (not handled by application) " << a[1] << " created\n";
98 DDocStd::ReturnLabel(di,D->Main());
100 else di << a[1] << " is already a document\n";
104 if (!DDocStd::GetDocument(a[1],D,Standard_False)) {
105 Handle(TDocStd_Application) A = DDocStd::GetApplication();
106 A->NewDocument(a[2],D);
107 DD = new DDocStd_DrawDocument(D);
108 TDataStd_Name::Set(D->GetData()->Root(),a[1]);
110 di << "document " << a[1] << " created\n";
111 DDocStd::ReturnLabel(di,D->Main());
113 else di << a[1] << " is already a document\n";
116 di << "DDocStd_NewDocument : Error\n";
120 //=======================================================================
123 //=======================================================================
125 static Standard_Integer DDocStd_Open (Draw_Interpretor& di,
130 TCollection_ExtendedString path (a[1]);
131 Handle(TDocStd_Application) A = DDocStd::GetApplication();
132 Handle(TDocStd_Document) D;
133 Standard_Integer insession = A->IsInSession(path);
135 di <<"document " << insession << " is already in session\n";
138 PCDM_ReaderStatus theStatus;
140 Standard_Boolean anUseStream = Standard_False;
141 for ( Standard_Integer i = 3; i < nb; i++ )
143 if (!strcmp (a[i], "-stream"))
145 di << "standard SEEKABLE stream is used\n";
146 anUseStream = Standard_True;
151 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
154 std::ifstream aFileStream;
155 OSD_OpenStream (aFileStream, path, std::ios::in | std::ios::binary);
157 theStatus = A->Open (aFileStream, D, aProgress->Start());
161 theStatus = A->Open (path, D, aProgress->Start());
163 if (theStatus == PCDM_RS_OK && !D.IsNull())
165 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D);
166 TDataStd_Name::Set(D->GetData()->Root(),a[2]);
172 switch ( theStatus ) {
173 case PCDM_RS_UserBreak: {
174 di << " could not retrieve , user break \n";
177 case PCDM_RS_AlreadyRetrieved:
178 case PCDM_RS_AlreadyRetrievedAndModified: {
179 di << " already retrieved \n" ;
182 case PCDM_RS_NoDriver: {
183 di << " could not retrieve , no Driver to make it \n" ;
186 case PCDM_RS_UnknownDocument:
187 case PCDM_RS_NoModel: {
188 di << " could not retrieve , Unknown Document or No Model \n";
191 case PCDM_RS_TypeNotFoundInSchema:
192 case PCDM_RS_UnrecognizedFileFormat: {
193 di << " could not retrieve , Type not found or Unrecognized File Format\n";
196 case PCDM_RS_PermissionDenied: {
197 di << " could not retrieve , permission denied \n" ;
201 di << " could not retrieve \n" ;
204 di << "DDocStd_Open : Error\n";
210 //=======================================================================
213 //=======================================================================
215 static Standard_Integer DDocStd_Save (Draw_Interpretor& di,
220 Handle(TDocStd_Document) D;
221 if (!DDocStd::GetDocument(a[1],D)) return 1;
222 Handle(TDocStd_Application) A = DDocStd::GetApplication();
224 di << "this document has never been saved\n";
228 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
229 A->Save (D, aProgress->Start());
232 di << "DDocStd_Save : Error\n";
236 //=======================================================================
239 //=======================================================================
241 static Standard_Integer DDocStd_SaveAs (Draw_Interpretor& di,
246 Handle(TDocStd_Document) D;
247 if (!DDocStd::GetDocument(a[1],D)) return 1;
248 TCollection_ExtendedString path (a[2]);
249 Handle(TDocStd_Application) A = DDocStd::GetApplication();
250 PCDM_StoreStatus theStatus;
252 Standard_Boolean anUseStream(Standard_False), isSaveEmptyLabels(Standard_False);
253 for ( Standard_Integer i = 3; i < nb; i++ )
255 if (!strcmp (a[i], "-stream"))
257 di << "standard SEEKABLE stream is used\n";
258 anUseStream = Standard_True;
261 isSaveEmptyLabels = ((atoi (a[3])) != 0);
262 D->SetEmptyLabelsSavingMode(isSaveEmptyLabels);
266 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
269 std::ofstream aFileStream;
270 OSD_OpenStream (aFileStream, path, std::ios::out | std::ios::binary);
271 theStatus = A->SaveAs (D, aFileStream, aProgress->Start());
275 theStatus = A->SaveAs(D,path, aProgress->Start());
278 if (theStatus != PCDM_SS_OK ) {
279 switch ( theStatus ) {
280 case PCDM_SS_DriverFailure: {
281 di << "Error saving document: Could not store , no driver found to make it\n";
284 case PCDM_SS_WriteFailure: {
285 di << "Error saving document: Write access failure\n";
288 case PCDM_SS_Failure: {
289 di << "Error saving document: Write failure\n" ;
292 case PCDM_SS_Doc_IsNull: {
293 di << "Error saving document: No document to save\n";
296 case PCDM_SS_No_Obj: {
297 di << "Error saving document: No objects written\n";
300 case PCDM_SS_Info_Section_Error: {
301 di << "Error saving document: Write info section failure\n" ;
304 case PCDM_SS_UserBreak: {
305 di << "Error saving document: User break \n" ;
316 di << "DDocStd_SaveAs : Error not enough argument\n";
320 //=======================================================================
323 //=======================================================================
325 static Standard_Integer DDocStd_Close (Draw_Interpretor& theDI,
326 Standard_Integer theArgNb,
327 const char** theArgVec)
331 theDI << "DDocStd_Close : Error\n";
335 Handle(TDocStd_Document) aDoc;
336 Standard_CString aDocName = theArgVec[1];
337 if (!DDocStd::GetDocument (aDocName, aDoc))
342 TDF_Label aRoot = aDoc->GetData()->Root();
343 Handle(TPrsStd_AISViewer) aDocViewer;
344 if (TPrsStd_AISViewer::Find (aRoot, aDocViewer)
345 && !aDocViewer->GetInteractiveContext().IsNull())
347 Handle(V3d_Viewer) aViewer = aDocViewer->GetInteractiveContext()->CurrentViewer();
348 V3d_ListOfView aViews;
349 for (V3d_ListOfViewIterator aViewIter (aDocViewer->GetInteractiveContext()->CurrentViewer()->DefinedViewIterator()); aViewIter.More(); aViewIter.Next())
351 aViews.Append (aViewIter.Value());
353 for (V3d_ListOfViewIterator aViewIter (aViews); aViewIter.More(); aViewIter.Next())
355 Handle(V3d_View) aView = aViewIter.Value();
356 ViewerTest::RemoveView (aView);
360 Handle(TDocStd_Application) aDocApp = DDocStd::GetApplication();
362 aDocApp->Close (aDoc);
364 if (Handle(Draw_Drawable3D) aDrawable = Draw::GetExisting (aDocName))
366 dout.RemoveDrawable (aDrawable);
368 Draw::Set (theArgVec[1], Handle(Draw_Drawable3D)());
372 //=======================================================================
373 //function : IsInSession
375 //=======================================================================
377 static Standard_Integer DDocStd_IsInSession (Draw_Interpretor& di,
382 Handle(TDocStd_Application) A = DDocStd::GetApplication();
383 di << A->IsInSession(a[1]);
386 di << "DDocStd_IsInSession : Error\n";
391 //=======================================================================
394 //=======================================================================
396 static Standard_Integer DDocStd_OSDPath (Draw_Interpretor& di,
401 OSD_Path path (a[1]);
402 di << "Node : " << path.Node().ToCString() << "\n";
403 di << "UserName : " << path.UserName().ToCString() << "\n";
404 di << "Password : " << path.Password().ToCString() << "\n";
405 di << "Disk : " << path.Disk().ToCString() << "\n";
406 di << "Trek : " << path.Trek().ToCString() << "\n";
407 di << "Name : " << path.Name().ToCString() << "\n";
408 di << "Extension : " << path.Extension().ToCString() << "\n";
411 di << "DDocStd_OSDPath : Error\n";
416 //=======================================================================
419 //=======================================================================
421 static Standard_Integer DDocStd_Path (Draw_Interpretor& di,
426 TDocStd_PathParser path (a[1]);
427 di << "Trek : " << path.Trek() << "\n";
428 di << "Name : " << path.Name() << "\n";
429 di << "Extension : " << path.Extension() << "\n";
430 di << "Path : " << path.Path() << "\n";
433 di << "DDocStd_Path : Error\n";
437 //=======================================================================
438 //function : AddComment
440 //=======================================================================
441 static Standard_Integer DDocStd_AddComment (Draw_Interpretor& di,
446 Handle(TDocStd_Document) D;
447 if (!DDocStd::GetDocument(a[1],D)) return 1;
448 TCollection_ExtendedString comment (a[2]);
449 // Handle(TDocStd_Application) A = DDocStd::GetApplication();
450 // A->AddComment(D,comment);
451 D->AddComment(comment);
454 di << "DDocStd_AddComment : Wrong arguments number\n";
458 //=======================================================================
459 //function : PrintComments
461 //=======================================================================
462 static Standard_Integer DDocStd_PrintComments (Draw_Interpretor& di,
467 Handle(TDocStd_Document) D;
468 if (!DDocStd::GetDocument(a[1],D)) return 1;
470 TColStd_SequenceOfExtendedString comments;
471 D->Comments(comments);
473 for (int i = 1; i <= comments.Length(); i++)
475 di << comments(i) << "\n";
480 di << "DDocStd_PrintComments : Wrong arguments number\n";
484 //=======================================================================
485 //function : SetStorageVerison
487 //=======================================================================
488 static Standard_Integer DDocStd_SetStorageVersion (Draw_Interpretor& ,
494 Handle(TDocStd_Document) D;
495 if (!DDocStd::GetDocument(a[1], D)) return 1;
496 const int version = atoi(a[2]);
497 D->ChangeStorageFormatVersion(version);
503 //=======================================================================
504 //function : GetStorageVerison
506 //=======================================================================
507 static Standard_Integer DDocStd_GetStorageVersion (Draw_Interpretor& di,
512 Handle(TDocStd_Document) D;
513 if (!DDocStd::GetDocument(a[1], D)) return 1;
514 di << D->StorageFormatVersion() << "\n";
520 //=======================================================================
521 //function : ApplicationCommands
523 //=======================================================================
525 void DDocStd::ApplicationCommands(Draw_Interpretor& theCommands)
528 static Standard_Boolean done = Standard_False;
530 done = Standard_True;
532 const char* g = "DDocStd application commands";
534 // user application commands
535 theCommands.Add("ListDocuments",
537 __FILE__, DDocStd_ListDocuments, g);
539 theCommands.Add("NewDocument",
540 "NewDocument docname format",
541 __FILE__, DDocStd_NewDocument, g);
543 theCommands.Add("Open",
544 "Open path docname [-stream]",
545 __FILE__, DDocStd_Open, g);
547 theCommands.Add("SaveAs",
548 "SaveAs DOC path [saveEmptyLabels: 0|1] [-stream]",
549 __FILE__, DDocStd_SaveAs, g);
551 theCommands.Add("Save",
553 __FILE__, DDocStd_Save, g);
555 theCommands.Add("Close",
557 __FILE__, DDocStd_Close, g);
559 theCommands.Add("IsInSession",
561 __FILE__, DDocStd_IsInSession, g);
563 theCommands.Add("OSDPath",
565 __FILE__, DDocStd_OSDPath, g);
567 theCommands.Add("Path",
569 __FILE__, DDocStd_Path, g);
571 theCommands.Add("AddComment",
572 "AddComment Doc string",
573 __FILE__, DDocStd_AddComment, g);
575 theCommands.Add("PrintComments",
577 __FILE__, DDocStd_PrintComments, g);
579 theCommands.Add("GetStorageVersion",
580 "GetStorageVersion Doc",
581 __FILE__, DDocStd_GetStorageVersion, g);
582 theCommands.Add("SetStorageVersion",
583 "SetStorageVersion Doc Version",
584 __FILE__, DDocStd_SetStorageVersion, g);