0027925: Visualization - implement order-independent transparency algorithm within...
[occt.git] / src / DDocStd / DDocStd_ApplicationCommands.cxx
CommitLineData
b311480e 1// Created on: 2000-03-01
2// Created by: Denis PASCAL
973c2be1 3// Copyright (c) 2000-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
16#include <DDocStd.hxx>
17#include <Draw.hxx>
18#include <Draw_Interpretor.hxx>
19#include <Draw_Viewer.hxx>
20#include <DDocStd_DrawDocument.hxx>
21#include <TDocStd_Application.hxx>
22#include <TDocStd_Document.hxx>
23#include <TDataStd_Name.hxx>
24#include <Draw.hxx>
25#include <Standard_GUID.hxx>
26#include <Standard_ExtString.hxx>
27#include <TCollection_AsciiString.hxx>
28#include <TCollection_ExtendedString.hxx>
29#include <TDF.hxx>
30#include <TDF_Data.hxx>
31#include <TDF_ChildIterator.hxx>
32#include <TDF_Tool.hxx>
33
34#include <OSD_Path.hxx>
4ff92abe 35#include <OSD_OpenFile.hxx>
7fd59977 36#include <TDocStd_PathParser.hxx>
7fd59977 37
0e93d9e5 38#include <AIS_InteractiveContext.hxx>
39#include <TPrsStd_AISViewer.hxx>
40#include <ViewerTest.hxx>
41#include <V3d_Viewer.hxx>
42
43#ifndef _WIN32
7fd59977 44extern Draw_Viewer dout;
45#else
46Standard_IMPORT Draw_Viewer dout;
47#endif
48
7fd59977 49//=======================================================================
50//function : ListDocuments
51//purpose :
52//=======================================================================
53
54static Standard_Integer DDocStd_ListDocuments (Draw_Interpretor& di,
55 Standard_Integer nb,
56 const char** /*a*/)
57{
58 if (nb == 1) {
6fe96f84 59 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 60 Handle(TDocStd_Document) D;
61 Standard_Integer nbdoc = A->NbDocuments();
62 for (Standard_Integer i = 1; i <= nbdoc; i++) {
63 A->GetDocument(i,D);
64 di <<"document " << i;
65 if (D->IsSaved()) {
66 TCollection_AsciiString GetNameAsciiString(D->GetName().ToExtString(),'?');
67 TCollection_AsciiString GetPathAsciiString(D->GetPath().ToExtString(),'?');
68 //cout << " name : " << D->GetName();
69 //cout << " path : " << D->GetPath();
70 di << " name : " << GetNameAsciiString.ToCString();
71 di << " path : " << GetPathAsciiString.ToCString();
72 }
73 else di << " not saved";
74 di << "\n";
75 }
76 return 0;
77 }
586db386 78 di << "DDocStd_ListDocuments : Error\n";
7fd59977 79 return 1;
80}
81
82
83//=======================================================================
84//function : NewDocument
85//purpose :
86//=======================================================================
87
88static Standard_Integer DDocStd_NewDocument (Draw_Interpretor& di,
89 Standard_Integer nb,
90 const char** a)
91{
92 Handle(TDocStd_Document) D;
93 Handle(DDocStd_DrawDocument) DD;
94 if (nb == 2) {
95 if (!DDocStd::GetDocument(a[1],D,Standard_False)) {
96 D = new TDocStd_Document("dummy");
97 DD = new DDocStd_DrawDocument(D);
98 Draw::Set(a[1],DD);
586db386 99 di << "document (not handled by application) " << a[1] << " created\n";
7fd59977 100 DDocStd::ReturnLabel(di,D->Main());
101 }
586db386 102 else di << a[1] << " is already a document\n";
7fd59977 103 return 0;
104 }
105 if (nb == 3) {
106 if (!DDocStd::GetDocument(a[1],D,Standard_False)) {
6fe96f84 107 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 108 A->NewDocument(a[2],D);
109 DD = new DDocStd_DrawDocument(D);
110 TDataStd_Name::Set(D->GetData()->Root(),a[1]);
111 Draw::Set(a[1],DD);
586db386 112 di << "document " << a[1] << " created\n";
7fd59977 113 DDocStd::ReturnLabel(di,D->Main());
114 }
586db386 115 else di << a[1] << " is already a document\n";
7fd59977 116 return 0;
117 }
586db386 118 di << "DDocStd_NewDocument : Error\n";
7fd59977 119 return 1;
120}
121
7fd59977 122//=======================================================================
123//function : Open
124//purpose :
125//=======================================================================
126
127static Standard_Integer DDocStd_Open (Draw_Interpretor& di,
128 Standard_Integer nb,
129 const char** a)
130{
4ff92abe 131 if (nb >= 3) {
7fd59977 132 TCollection_ExtendedString path (a[1]);
6fe96f84 133 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 134 Handle(TDocStd_Document) D;
135 Standard_Integer insession = A->IsInSession(path);
136 if (insession > 0) {
586db386 137 di <<"document " << insession << " is already in session\n";
7fd59977 138 return 0;
139 }
4ff92abe 140 PCDM_ReaderStatus theStatus;
141
142 Standard_Boolean anUseStream = Standard_False;
143 for ( Standard_Integer i = 3; i < nb; i++ )
144 {
145 if (!strcmp (a[i], "-stream"))
146 {
147 di << "standard SEEKABLE stream is used\n";
148 anUseStream = Standard_True;
149 break;
150 }
151 }
152
153 if (anUseStream)
154 {
155 std::ifstream aFileStream;
156 OSD_OpenStream (aFileStream, path, std::ios::in | std::ios::binary);
157
158 theStatus = A->Open (aFileStream, D);
159 }
160 else
161 {
162 theStatus = A->Open(path,D);
163 }
15e8b082 164 if (theStatus == PCDM_RS_OK && !D.IsNull()) {
7fd59977 165 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D);
166 TDataStd_Name::Set(D->GetData()->Root(),a[2]);
167 Draw::Set(a[2],DD);
168 return 0;
169 } else {
170 switch ( theStatus ) {
15e8b082
M
171 case PCDM_RS_AlreadyRetrieved:
172 case PCDM_RS_AlreadyRetrievedAndModified: {
586db386 173 di << " already retrieved \n" ;
7fd59977 174 break;
175 }
15e8b082 176 case PCDM_RS_NoDriver: {
586db386 177 di << " could not retrieve , no Driver to make it \n" ;
7fd59977 178 break ;
179 }
15e8b082
M
180 case PCDM_RS_UnknownDocument:
181 case PCDM_RS_NoModel: {
586db386 182 di << " could not retrieve , Unknown Document or No Model \n";
7fd59977 183 break ;
184 }
15e8b082
M
185 case PCDM_RS_TypeNotFoundInSchema:
186 case PCDM_RS_UnrecognizedFileFormat: {
586db386 187 di << " could not retrieve , Type not found or Unrecognized File Format\n";
7fd59977 188 break ;
189 }
15e8b082 190 case PCDM_RS_PermissionDenied: {
586db386 191 di << " could not retrieve , permission denied \n" ;
7fd59977 192 break;
193 }
15e8b082 194 default:
586db386 195 di << " could not retrieve \n" ;
15e8b082 196 break;
7fd59977 197 }
586db386 198 di << "DDocStd_Open : Error\n";
7fd59977 199 }
200 }
201 return 1;
202}
203
204//=======================================================================
205//function : Save
206//purpose :
207//=======================================================================
208
209static Standard_Integer DDocStd_Save (Draw_Interpretor& di,
210 Standard_Integer nb,
211 const char** a)
212{
213 if (nb == 2) {
214 Handle(TDocStd_Document) D;
215 if (!DDocStd::GetDocument(a[1],D)) return 1;
6fe96f84 216 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 217 if (!D->IsSaved()) {
586db386 218 di << "this document has never been saved\n";
7fd59977 219 return 0;
220 }
221 A->Save(D);
222 return 0;
223 }
586db386 224 di << "DDocStd_Save : Error\n";
7fd59977 225 return 1;
226}
227
228//=======================================================================
229//function : SaveAs
230//purpose :
231//=======================================================================
232
233static Standard_Integer DDocStd_SaveAs (Draw_Interpretor& di,
234 Standard_Integer nb,
235 const char** a)
236{
4ff92abe 237 if (nb >= 3) {
7fd59977 238 Handle(TDocStd_Document) D;
239 if (!DDocStd::GetDocument(a[1],D)) return 1;
240 TCollection_ExtendedString path (a[2]);
6fe96f84 241 Handle(TDocStd_Application) A = DDocStd::GetApplication();
4ff92abe 242 PCDM_StoreStatus theStatus;
243
244 Standard_Boolean anUseStream = Standard_False;
245 for ( Standard_Integer i = 3; i < nb; i++ )
246 {
247 if (!strcmp (a[i], "-stream"))
248 {
249 di << "standard SEEKABLE stream is used\n";
250 anUseStream = Standard_True;
251 break;
252 }
253 }
254
255 if (anUseStream)
256 {
257 std::ofstream aFileStream;
258 OSD_OpenStream (aFileStream, path, std::ios::out | std::ios::binary);
259 theStatus = A->SaveAs (D, aFileStream);
260 }
261 else
262 {
263 theStatus = A->SaveAs(D,path);
264 }
265
15e8b082 266 if (theStatus != PCDM_SS_OK ) {
7fd59977 267 switch ( theStatus ) {
566f8441 268 case PCDM_SS_DriverFailure: {
586db386 269 di << "Error saving document: Could not store , no driver found to make it\n";
566f8441 270 break ;
271 }
272 case PCDM_SS_WriteFailure: {
586db386 273 di << "Error saving document: Write access failure\n";
566f8441 274 break;
275 }
276 case PCDM_SS_Failure: {
586db386 277 di << "Error saving document: Write failure\n" ;
566f8441 278 break;
279 }
280 case PCDM_SS_Doc_IsNull: {
586db386 281 di << "Error saving document: No document to save\n";
566f8441 282 break ;
283 }
284 case PCDM_SS_No_Obj: {
586db386 285 di << "Error saving document: No objects written\n";
566f8441 286 break;
287 }
288 case PCDM_SS_Info_Section_Error: {
586db386 289 di << "Error saving document: Write info section failure\n" ;
566f8441 290 break;
291 }
292 default:
293 break;
7fd59977 294 }
295 return 1;
296 } else {
297 return 0;
298 }
299 }
586db386 300 di << "DDocStd_SaveAs : Error not enough argument\n";
7fd59977 301 return 1;
302}
303
304//=======================================================================
305//function : Close
306//purpose :
307//=======================================================================
308
0e93d9e5 309static Standard_Integer DDocStd_Close (Draw_Interpretor& /*theDI*/,
310 Standard_Integer theArgNb,
311 const char** theArgVec)
7fd59977 312{
0e93d9e5 313 if (theArgNb != 2)
314 {
315 std::cout << "DDocStd_Close : Error\n";
316 return 1;
317 }
24de79c3 318
0e93d9e5 319 Handle(TDocStd_Document) aDoc;
320 Standard_CString aDocName = theArgVec[1];
321 if (!DDocStd::GetDocument (aDocName, aDoc))
322 {
323 return 1;
7fd59977 324 }
0e93d9e5 325
326 TDF_Label aRoot = aDoc->GetData()->Root();
327 Handle(TPrsStd_AISViewer) aDocViewer;
328 if (TPrsStd_AISViewer::Find (aRoot, aDocViewer)
329 && !aDocViewer->GetInteractiveContext().IsNull())
330 {
331 Handle(V3d_Viewer) aViewer = aDocViewer->GetInteractiveContext()->CurrentViewer();
6a24c6de 332 V3d_ListOfView aViews;
333 for (V3d_ListOfViewIterator aViewIter (aDocViewer->GetInteractiveContext()->CurrentViewer()->DefinedViewIterator()); aViewIter.More(); aViewIter.Next())
0e93d9e5 334 {
6a24c6de 335 aViews.Append (aViewIter.Value());
336 }
337 for (V3d_ListOfViewIterator aViewIter (aViews); aViewIter.More(); aViewIter.Next())
338 {
339 Handle(V3d_View) aView = aViewIter.Value();
0e93d9e5 340 ViewerTest::RemoveView (aView);
341 }
342 }
343
6fe96f84 344 Handle(TDocStd_Application) aDocApp = DDocStd::GetApplication();
0e93d9e5 345
346 aDocApp->Close (aDoc);
347
348 Handle(Draw_Drawable3D) aDrawable = Draw::Get (aDocName, Standard_False);
349 dout.RemoveDrawable (aDrawable);
350 Draw::Set (theArgVec[1], Handle(Draw_Drawable3D)());
351 return 0;
7fd59977 352}
353
354//=======================================================================
355//function : IsInSession
356//purpose :
357//=======================================================================
358
359static Standard_Integer DDocStd_IsInSession (Draw_Interpretor& di,
360 Standard_Integer nb,
361 const char** a)
362{
363 if (nb == 2) {
6fe96f84 364 Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 365 di << A->IsInSession(a[1]);
366 return 0;
367 }
586db386 368 di << "DDocStd_IsInSession : Error\n";
7fd59977 369 return 1;
370}
371
372
373//=======================================================================
374//function : OSDPath
375//purpose :
376//=======================================================================
377
378static Standard_Integer DDocStd_OSDPath (Draw_Interpretor& di,
379 Standard_Integer nb,
380 const char** a)
381{
382 if (nb == 2) {
383 OSD_Path path (a[1]);
384 di << "Node : " << path.Node().ToCString() << "\n";
385 di << "UserName : " << path.UserName().ToCString() << "\n";
386 di << "Password : " << path.Password().ToCString() << "\n";
387 di << "Disk : " << path.Disk().ToCString() << "\n";
388 di << "Trek : " << path.Trek().ToCString() << "\n";
389 di << "Name : " << path.Name().ToCString() << "\n";
390 di << "Extension : " << path.Extension().ToCString() << "\n";
391 return 0;
392 }
586db386 393 di << "DDocStd_OSDPath : Error\n";
7fd59977 394 return 1;
395}
396
397
398//=======================================================================
399//function : Path
400//purpose :
401//=======================================================================
402
403static Standard_Integer DDocStd_Path (Draw_Interpretor& di,
404 Standard_Integer nb,
405 const char** a)
406{
407 if (nb == 2) {
408 TDocStd_PathParser path (a[1]);
409 //cout << "Trek : " << path.Trek() << endl;
410 //cout << "Name : " << path.Name() << endl;
411 //cout << "Extension : " << path.Extension() << endl;
412 //cout << "Path : " << path.Path() << endl;
413 TCollection_AsciiString TrekAsciiString(path.Trek().ToExtString(),'?');
414 TCollection_AsciiString NameAsciiString(path.Name().ToExtString(),'?');
415 TCollection_AsciiString ExtensionAsciiString(path.Extension().ToExtString(),'?');
416 TCollection_AsciiString PathAsciiString(path.Path().ToExtString(),'?');
417 di << "Trek : " << TrekAsciiString.ToCString() << "\n";
418 di << "Name : " << NameAsciiString.ToCString() << "\n";
419 di << "Extension : " << ExtensionAsciiString.ToCString() << "\n";
420 di << "Path : " << PathAsciiString.ToCString() << "\n";
421 return 0;
422 }
586db386 423 di << "DDocStd_Path : Error\n";
7fd59977 424 return 1;
425}
426
427//=======================================================================
428//function : AddComment
429//purpose :
430//=======================================================================
431static Standard_Integer DDocStd_AddComment (Draw_Interpretor& di,
432 Standard_Integer nb,
433 const char** a)
434{
435 if (nb == 3) {
436 Handle(TDocStd_Document) D;
437 if (!DDocStd::GetDocument(a[1],D)) return 1;
438 TCollection_ExtendedString comment (a[2]);
6fe96f84 439// Handle(TDocStd_Application) A = DDocStd::GetApplication();
7fd59977 440// A->AddComment(D,comment);
441 D->AddComment(comment);
442 return 0;
443 }
586db386 444 di << "DDocStd_AddComment : Wrong arguments number\n";
7fd59977 445 return 1;
446}
447
448//=======================================================================
449//function : PrintComments
450//purpose :
451//=======================================================================
452static Standard_Integer DDocStd_PrintComments (Draw_Interpretor& di,
453 Standard_Integer nb,
454 const char** a)
455{
456 if (nb == 2) {
457 Handle(TDocStd_Document) D;
458 if (!DDocStd::GetDocument(a[1],D)) return 1;
459
460 TColStd_SequenceOfExtendedString comments;
461 D->Comments(comments);
462
463 for (int i = 1; i <= comments.Length(); i++)
464 {
465 //cout << comments(i) << endl;
466 TCollection_AsciiString commentAsciiString(comments(i).ToExtString(),'?');
467 di << commentAsciiString.ToCString() << "\n";
468 }
469
470 return 0;
471 }
586db386 472 di << "DDocStd_PrintComments : Wrong arguments number\n";
7fd59977 473 return 1;
474}
475
476//=======================================================================
477//function : ApplicationCommands
478//purpose :
479//=======================================================================
480
481void DDocStd::ApplicationCommands(Draw_Interpretor& theCommands)
482{
483
484 static Standard_Boolean done = Standard_False;
485 if (done) return;
486 done = Standard_True;
487
488 const char* g = "DDocStd application commands";
489
490 // user application commands
7fd59977 491 theCommands.Add("ListDocuments",
492 "ListDocuments",
493 __FILE__, DDocStd_ListDocuments, g);
494
495 theCommands.Add("NewDocument",
496 "NewDocument docname format",
497 __FILE__, DDocStd_NewDocument, g);
498
7fd59977 499 theCommands.Add("Open",
4ff92abe 500 "Open path docname [-stream]",
7fd59977 501 __FILE__, DDocStd_Open, g);
502
503 theCommands.Add("SaveAs",
4ff92abe 504 "SaveAs DOC path [-stream]",
7fd59977 505 __FILE__, DDocStd_SaveAs, g);
506
507 theCommands.Add("Save",
508 "Save",
509 __FILE__, DDocStd_Save, g);
510
511 theCommands.Add("Close",
512 "Close DOC",
513 __FILE__, DDocStd_Close, g);
514
515 theCommands.Add("IsInSession",
516 "IsInSession path",
517 __FILE__, DDocStd_IsInSession, g);
518
519 theCommands.Add("OSDPath",
520 "OSDPath string",
521 __FILE__, DDocStd_OSDPath, g);
522
523 theCommands.Add("Path",
524 "Path string",
525 __FILE__, DDocStd_Path, g);
526
527 theCommands.Add("AddComment",
528 "AddComment Doc string",
529 __FILE__, DDocStd_AddComment, g);
530
531 theCommands.Add("PrintComments",
532 "PrintComments Doc",
533 __FILE__, DDocStd_PrintComments, g);
7fd59977 534}