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 TCollection_AsciiString GetNameAsciiString(D->GetName().ToExtString(),'?');
69 TCollection_AsciiString GetPathAsciiString(D->GetPath().ToExtString(),'?');
70 //std::cout << " name : " << D->GetName();
71 //std::cout << " path : " << D->GetPath();
72 di << " name : " << GetNameAsciiString.ToCString();
73 di << " path : " << GetPathAsciiString.ToCString();
75 else di << " not saved";
80 di << "DDocStd_ListDocuments : Error\n";
85 //=======================================================================
86 //function : NewDocument
88 //=======================================================================
90 static Standard_Integer DDocStd_NewDocument (Draw_Interpretor& di,
94 Handle(TDocStd_Document) D;
95 Handle(DDocStd_DrawDocument) DD;
97 if (!DDocStd::GetDocument(a[1],D,Standard_False)) {
98 D = new TDocStd_Document("dummy");
99 DD = new DDocStd_DrawDocument(D);
101 di << "document (not handled by application) " << a[1] << " created\n";
102 DDocStd::ReturnLabel(di,D->Main());
104 else di << a[1] << " is already a document\n";
108 if (!DDocStd::GetDocument(a[1],D,Standard_False)) {
109 Handle(TDocStd_Application) A = DDocStd::GetApplication();
110 A->NewDocument(a[2],D);
111 DD = new DDocStd_DrawDocument(D);
112 TDataStd_Name::Set(D->GetData()->Root(),a[1]);
114 di << "document " << a[1] << " created\n";
115 DDocStd::ReturnLabel(di,D->Main());
117 else di << a[1] << " is already a document\n";
120 di << "DDocStd_NewDocument : Error\n";
124 //=======================================================================
127 //=======================================================================
129 static Standard_Integer DDocStd_Open (Draw_Interpretor& di,
134 TCollection_ExtendedString path (a[1]);
135 Handle(TDocStd_Application) A = DDocStd::GetApplication();
136 Handle(TDocStd_Document) D;
137 Standard_Integer insession = A->IsInSession(path);
139 di <<"document " << insession << " is already in session\n";
142 PCDM_ReaderStatus theStatus;
144 Standard_Boolean anUseStream = Standard_False;
145 for ( Standard_Integer i = 3; i < nb; i++ )
147 if (!strcmp (a[i], "-stream"))
149 di << "standard SEEKABLE stream is used\n";
150 anUseStream = Standard_True;
155 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
158 std::ifstream aFileStream;
159 OSD_OpenStream (aFileStream, path, std::ios::in | std::ios::binary);
161 theStatus = A->Open (aFileStream, D, aProgress->Start());
165 theStatus = A->Open (path, D, aProgress->Start());
167 if (theStatus == PCDM_RS_OK && !D.IsNull())
169 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D);
170 TDataStd_Name::Set(D->GetData()->Root(),a[2]);
176 switch ( theStatus ) {
177 case PCDM_RS_UserBreak: {
178 di << " could not retrieve , user break \n";
181 case PCDM_RS_AlreadyRetrieved:
182 case PCDM_RS_AlreadyRetrievedAndModified: {
183 di << " already retrieved \n" ;
186 case PCDM_RS_NoDriver: {
187 di << " could not retrieve , no Driver to make it \n" ;
190 case PCDM_RS_UnknownDocument:
191 case PCDM_RS_NoModel: {
192 di << " could not retrieve , Unknown Document or No Model \n";
195 case PCDM_RS_TypeNotFoundInSchema:
196 case PCDM_RS_UnrecognizedFileFormat: {
197 di << " could not retrieve , Type not found or Unrecognized File Format\n";
200 case PCDM_RS_PermissionDenied: {
201 di << " could not retrieve , permission denied \n" ;
205 di << " could not retrieve \n" ;
208 di << "DDocStd_Open : Error\n";
214 //=======================================================================
217 //=======================================================================
219 static Standard_Integer DDocStd_Save (Draw_Interpretor& di,
224 Handle(TDocStd_Document) D;
225 if (!DDocStd::GetDocument(a[1],D)) return 1;
226 Handle(TDocStd_Application) A = DDocStd::GetApplication();
228 di << "this document has never been saved\n";
232 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
233 A->Save (D, aProgress->Start());
236 di << "DDocStd_Save : Error\n";
240 //=======================================================================
243 //=======================================================================
245 static Standard_Integer DDocStd_SaveAs (Draw_Interpretor& di,
250 Handle(TDocStd_Document) D;
251 if (!DDocStd::GetDocument(a[1],D)) return 1;
252 TCollection_ExtendedString path (a[2]);
253 Handle(TDocStd_Application) A = DDocStd::GetApplication();
254 PCDM_StoreStatus theStatus;
256 Standard_Boolean anUseStream(Standard_False), isSaveEmptyLabels(Standard_False);
257 for ( Standard_Integer i = 3; i < nb; i++ )
259 if (!strcmp (a[i], "-stream"))
261 di << "standard SEEKABLE stream is used\n";
262 anUseStream = Standard_True;
265 isSaveEmptyLabels = ((atoi (a[3])) != 0);
266 D->SetEmptyLabelsSavingMode(isSaveEmptyLabels);
270 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
273 std::ofstream aFileStream;
274 OSD_OpenStream (aFileStream, path, std::ios::out | std::ios::binary);
275 theStatus = A->SaveAs (D, aFileStream, aProgress->Start());
279 theStatus = A->SaveAs(D,path, aProgress->Start());
282 if (theStatus != PCDM_SS_OK ) {
283 switch ( theStatus ) {
284 case PCDM_SS_DriverFailure: {
285 di << "Error saving document: Could not store , no driver found to make it\n";
288 case PCDM_SS_WriteFailure: {
289 di << "Error saving document: Write access failure\n";
292 case PCDM_SS_Failure: {
293 di << "Error saving document: Write failure\n" ;
296 case PCDM_SS_Doc_IsNull: {
297 di << "Error saving document: No document to save\n";
300 case PCDM_SS_No_Obj: {
301 di << "Error saving document: No objects written\n";
304 case PCDM_SS_Info_Section_Error: {
305 di << "Error saving document: Write info section failure\n" ;
308 case PCDM_SS_UserBreak: {
309 di << "Error saving document: User break \n" ;
320 di << "DDocStd_SaveAs : Error not enough argument\n";
324 //=======================================================================
327 //=======================================================================
329 static Standard_Integer DDocStd_Close (Draw_Interpretor& theDI,
330 Standard_Integer theArgNb,
331 const char** theArgVec)
335 theDI << "DDocStd_Close : Error\n";
339 Handle(TDocStd_Document) aDoc;
340 Standard_CString aDocName = theArgVec[1];
341 if (!DDocStd::GetDocument (aDocName, aDoc))
346 TDF_Label aRoot = aDoc->GetData()->Root();
347 Handle(TPrsStd_AISViewer) aDocViewer;
348 if (TPrsStd_AISViewer::Find (aRoot, aDocViewer)
349 && !aDocViewer->GetInteractiveContext().IsNull())
351 Handle(V3d_Viewer) aViewer = aDocViewer->GetInteractiveContext()->CurrentViewer();
352 V3d_ListOfView aViews;
353 for (V3d_ListOfViewIterator aViewIter (aDocViewer->GetInteractiveContext()->CurrentViewer()->DefinedViewIterator()); aViewIter.More(); aViewIter.Next())
355 aViews.Append (aViewIter.Value());
357 for (V3d_ListOfViewIterator aViewIter (aViews); aViewIter.More(); aViewIter.Next())
359 Handle(V3d_View) aView = aViewIter.Value();
360 ViewerTest::RemoveView (aView);
364 Handle(TDocStd_Application) aDocApp = DDocStd::GetApplication();
366 aDocApp->Close (aDoc);
368 if (Handle(Draw_Drawable3D) aDrawable = Draw::GetExisting (aDocName))
370 dout.RemoveDrawable (aDrawable);
372 Draw::Set (theArgVec[1], Handle(Draw_Drawable3D)());
376 //=======================================================================
377 //function : IsInSession
379 //=======================================================================
381 static Standard_Integer DDocStd_IsInSession (Draw_Interpretor& di,
386 Handle(TDocStd_Application) A = DDocStd::GetApplication();
387 di << A->IsInSession(a[1]);
390 di << "DDocStd_IsInSession : Error\n";
395 //=======================================================================
398 //=======================================================================
400 static Standard_Integer DDocStd_OSDPath (Draw_Interpretor& di,
405 OSD_Path path (a[1]);
406 di << "Node : " << path.Node().ToCString() << "\n";
407 di << "UserName : " << path.UserName().ToCString() << "\n";
408 di << "Password : " << path.Password().ToCString() << "\n";
409 di << "Disk : " << path.Disk().ToCString() << "\n";
410 di << "Trek : " << path.Trek().ToCString() << "\n";
411 di << "Name : " << path.Name().ToCString() << "\n";
412 di << "Extension : " << path.Extension().ToCString() << "\n";
415 di << "DDocStd_OSDPath : Error\n";
420 //=======================================================================
423 //=======================================================================
425 static Standard_Integer DDocStd_Path (Draw_Interpretor& di,
430 TDocStd_PathParser path (a[1]);
431 //std::cout << "Trek : " << path.Trek() << std::endl;
432 //std::cout << "Name : " << path.Name() << std::endl;
433 //std::cout << "Extension : " << path.Extension() << std::endl;
434 //std::cout << "Path : " << path.Path() << std::endl;
435 TCollection_AsciiString TrekAsciiString(path.Trek().ToExtString(),'?');
436 TCollection_AsciiString NameAsciiString(path.Name().ToExtString(),'?');
437 TCollection_AsciiString ExtensionAsciiString(path.Extension().ToExtString(),'?');
438 TCollection_AsciiString PathAsciiString(path.Path().ToExtString(),'?');
439 di << "Trek : " << TrekAsciiString.ToCString() << "\n";
440 di << "Name : " << NameAsciiString.ToCString() << "\n";
441 di << "Extension : " << ExtensionAsciiString.ToCString() << "\n";
442 di << "Path : " << PathAsciiString.ToCString() << "\n";
445 di << "DDocStd_Path : Error\n";
449 //=======================================================================
450 //function : AddComment
452 //=======================================================================
453 static Standard_Integer DDocStd_AddComment (Draw_Interpretor& di,
458 Handle(TDocStd_Document) D;
459 if (!DDocStd::GetDocument(a[1],D)) return 1;
460 TCollection_ExtendedString comment (a[2]);
461 // Handle(TDocStd_Application) A = DDocStd::GetApplication();
462 // A->AddComment(D,comment);
463 D->AddComment(comment);
466 di << "DDocStd_AddComment : Wrong arguments number\n";
470 //=======================================================================
471 //function : PrintComments
473 //=======================================================================
474 static Standard_Integer DDocStd_PrintComments (Draw_Interpretor& di,
479 Handle(TDocStd_Document) D;
480 if (!DDocStd::GetDocument(a[1],D)) return 1;
482 TColStd_SequenceOfExtendedString comments;
483 D->Comments(comments);
485 for (int i = 1; i <= comments.Length(); i++)
487 //std::cout << comments(i) << std::endl;
488 TCollection_AsciiString commentAsciiString(comments(i).ToExtString(),'?');
489 di << commentAsciiString.ToCString() << "\n";
494 di << "DDocStd_PrintComments : Wrong arguments number\n";
498 //=======================================================================
499 //function : SetStorageVerison
501 //=======================================================================
502 static Standard_Integer DDocStd_SetStorageVersion (Draw_Interpretor& ,
508 Handle(TDocStd_Document) D;
509 if (!DDocStd::GetDocument(a[1], D)) return 1;
510 const int version = atoi(a[2]);
511 D->ChangeStorageFormatVersion(version);
517 //=======================================================================
518 //function : GetStorageVerison
520 //=======================================================================
521 static Standard_Integer DDocStd_GetStorageVersion (Draw_Interpretor& di,
526 Handle(TDocStd_Document) D;
527 if (!DDocStd::GetDocument(a[1], D)) return 1;
528 di << D->StorageFormatVersion() << "\n";
534 //=======================================================================
535 //function : ApplicationCommands
537 //=======================================================================
539 void DDocStd::ApplicationCommands(Draw_Interpretor& theCommands)
542 static Standard_Boolean done = Standard_False;
544 done = Standard_True;
546 const char* g = "DDocStd application commands";
548 // user application commands
549 theCommands.Add("ListDocuments",
551 __FILE__, DDocStd_ListDocuments, g);
553 theCommands.Add("NewDocument",
554 "NewDocument docname format",
555 __FILE__, DDocStd_NewDocument, g);
557 theCommands.Add("Open",
558 "Open path docname [-stream]",
559 __FILE__, DDocStd_Open, g);
561 theCommands.Add("SaveAs",
562 "SaveAs DOC path [saveEmptyLabels: 0|1] [-stream]",
563 __FILE__, DDocStd_SaveAs, g);
565 theCommands.Add("Save",
567 __FILE__, DDocStd_Save, g);
569 theCommands.Add("Close",
571 __FILE__, DDocStd_Close, g);
573 theCommands.Add("IsInSession",
575 __FILE__, DDocStd_IsInSession, g);
577 theCommands.Add("OSDPath",
579 __FILE__, DDocStd_OSDPath, g);
581 theCommands.Add("Path",
583 __FILE__, DDocStd_Path, g);
585 theCommands.Add("AddComment",
586 "AddComment Doc string",
587 __FILE__, DDocStd_AddComment, g);
589 theCommands.Add("PrintComments",
591 __FILE__, DDocStd_PrintComments, g);
593 theCommands.Add("GetStorageVersion",
594 "GetStorageVersion Doc",
595 __FILE__, DDocStd_GetStorageVersion, g);
596 theCommands.Add("SetStorageVersion",
597 "SetStorageVersion Doc Version",
598 __FILE__, DDocStd_SetStorageVersion, g);