summaryrefslogtreecommitdiff
path: root/src/DDocStd/DDocStd_ApplicationCommands.cxx
blob: f4f4bdc922d446859884d96539365a7c9c3b6668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// File:	DDocStd_ApplicationCommands.cxx
// Created:	Wed Mar  1 14:06:03 2000
// Author:	Denis PASCAL
//		<dp@dingox.paris1.matra-dtv.fr>


#include <DDocStd.hxx>
#include <Draw.hxx>
#include <Draw_Interpretor.hxx>
#include <Draw_Viewer.hxx>
#include <DDocStd_DrawDocument.hxx>
#include <TDocStd_Application.hxx>
#include <TDocStd_Document.hxx>
#include <TDataStd_Name.hxx>
#include <Draw.hxx>
#include <Standard_GUID.hxx>
#include <Standard_ExtString.hxx>
#include <TCollection_AsciiString.hxx>
#include <TCollection_ExtendedString.hxx>
#include <TDF.hxx>
#include <TDF_Data.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDF_Tool.hxx> 

#include <OSD_Path.hxx>
#include <TDocStd_PathParser.hxx>
//#include <TPrsStd_AISViewer.hxx>
//#include <AIS_InteractiveContext.hxx>

#ifndef WNT
extern Draw_Viewer dout;
#else
Standard_IMPORT Draw_Viewer dout;
#endif

extern int ViewerMainLoop (Standard_Integer, const char**); 
#include <ViewerTest_Tool.hxx>

//=======================================================================
//function : ListDocuments
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_ListDocuments (Draw_Interpretor& di,
					       Standard_Integer nb,
					       const char** /*a*/)
{  
  if (nb == 1) {
    Handle(TDocStd_Application) A;
    if (!DDocStd::Find(A)) return 1;
    Handle(TDocStd_Document) D;
    Standard_Integer nbdoc = A->NbDocuments();
    for (Standard_Integer i = 1; i <= nbdoc; i++) {
      A->GetDocument(i,D);
      di <<"document " << i;
      if (D->IsSaved()) {
	TCollection_AsciiString GetNameAsciiString(D->GetName().ToExtString(),'?');
	TCollection_AsciiString GetPathAsciiString(D->GetPath().ToExtString(),'?');
	//cout << " name : " << D->GetName();
	//cout << " path : " << D->GetPath();
	di << " name : " << GetNameAsciiString.ToCString();
	di << " path : " << GetPathAsciiString.ToCString();
      }
      else di << " not saved";
      di << "\n";
    }
    return 0;
  }
  di << "DDocStd_ListDocuments : Error" << "\n";
  return 1; 
}


//=======================================================================
//function : NewDocument
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_NewDocument (Draw_Interpretor& di,
					     Standard_Integer nb,
					     const char** a)
{    
  Handle(TDocStd_Document) D; 
  Handle(DDocStd_DrawDocument) DD;
  if (nb == 2) {
    if (!DDocStd::GetDocument(a[1],D,Standard_False)) { 
      D = new TDocStd_Document("dummy");  
      DD = new DDocStd_DrawDocument(D);  
      Draw::Set(a[1],DD);       
      di << "document (not handled by application)  " << a[1] << " created" << "\n";  
      DDocStd::ReturnLabel(di,D->Main()); 
    }    
    else di << a[1] << " is already a document" << "\n";
    return 0;
  }
  if (nb == 3) {   
    if (!DDocStd::GetDocument(a[1],D,Standard_False)) {  
      Handle(TDocStd_Application) A;
      if (!DDocStd::Find(A)) return 1;
      A->NewDocument(a[2],D);  
      DD = new DDocStd_DrawDocument(D);  
      TDataStd_Name::Set(D->GetData()->Root(),a[1]);  
      Draw::Set(a[1],DD);    
      di << "document " << a[1] << " created" << "\n";    
      DDocStd::ReturnLabel(di,D->Main()); 
    }
    else di << a[1] << " is already a document" << "\n";
    return 0;
  }   
  di << "DDocStd_NewDocument : Error" << "\n";
  return 1;
}

//=======================================================================
//function : InitViewer
//purpose  : 
//=======================================================================

// static Standard_Integer DDocStd_InitViewer (Draw_Interpretor& di,
// 					    Standard_Integer nb,
// 					    const char** arg)
// {
//   if (nb == 2) {    
//     Handle(TDocStd_Application) A;
//     if (!DDocStd::Find(A)) return 1;       
//     Handle(TDocStd_Document) D;
//     if (!DDocStd::GetDocument(arg[1],D)) return 1;     
//     Handle(TPrsStd_AISViewer) viewer;
//     if (!TPrsStd_AISViewer::Find (D->Main(),viewer)) {
//       TCollection_AsciiString title;  
//       title.Prepend(arg[1]);   
//       title.Prepend("_"); 
//       title.Prepend("Document");  
//       Handle(V3d_Viewer) vw = ViewerTest_Tool::MakeViewer (title.ToCString()); 
//       Handle(AIS_InteractiveContext) IC = new AIS_InteractiveContext(vw);
//       A->SetViewer (D,IC);
//     } 
//     ViewerTest_Tool::InitViewerTest (viewer->GetInteractiveContext());
//     return 0;
//   }
//   cout << "DDocStd_InitViewer : Error" << endl;
//   return 1;
// }


//=======================================================================
//function : Open
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_Open (Draw_Interpretor& di,
				      Standard_Integer nb,
				      const char** a)
{   
  if (nb == 3) {
    TCollection_ExtendedString path (a[1]); 
    Handle(TDocStd_Application) A;
    if (!DDocStd::Find(A)) return 1;
    Handle(TDocStd_Document) D;
    Standard_Integer insession = A->IsInSession(path);
    if (insession > 0) {  
      di <<"document " << insession << "  is already in session" << "\n";
      return 0;
    }
    PCDM_ReaderStatus theStatus = A->Open(path,D);
    if (theStatus == PCDM_RS_OK && !D.IsNull()) {
      Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D);
      TDataStd_Name::Set(D->GetData()->Root(),a[2]);
      Draw::Set(a[2],DD);
      return 0; 
    } else {
      switch ( theStatus ) {
      case PCDM_RS_AlreadyRetrieved: 
      case PCDM_RS_AlreadyRetrievedAndModified: {
	di << " already retrieved " << "\n" ;  
	break;
      }
      case PCDM_RS_NoDriver: {
	di << " could not retrieve , no Driver to make it " <<"\n" ;
	break ;
      }
      case PCDM_RS_UnknownDocument:
      case PCDM_RS_NoModel: {
	di << " could not retrieve , Unknown Document or No Model " <<"\n";
	break ; 
      }
      case PCDM_RS_TypeNotFoundInSchema:
      case PCDM_RS_UnrecognizedFileFormat: {
	di << " could not retrieve , Type not found or Unrecognized File Format" <<"\n";
	break ;
      }
      case PCDM_RS_PermissionDenied: {
	di << " could not retrieve , permission denied " << "\n" ;  
	break;
      }
      default:
	di << " could not retrieve " << "\n" ;  
	break;
      }
      di << "DDocStd_Open : Error" << "\n";
    }	
  }
  return 1;
}

//=======================================================================
//function : Save
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_Save (Draw_Interpretor& di,
				      Standard_Integer nb,
				      const char** a)
{  
  if (nb == 2) {
    Handle(TDocStd_Document) D;    
    if (!DDocStd::GetDocument(a[1],D)) return 1;
    Handle(TDocStd_Application) A;
    if (!DDocStd::Find(A)) return 1;  
    if (!D->IsSaved()) {
      di << "this document has never been saved" << "\n";
      return 0;
    }
    A->Save(D);
    return 0; 
  }
  di << "DDocStd_Save : Error" << "\n";
  return 1;
}

//=======================================================================
//function : SaveAs
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_SaveAs (Draw_Interpretor& di,
					Standard_Integer nb,
					const char** a)
{  
  if (nb == 3) {
    Handle(TDocStd_Document) D;    
    if (!DDocStd::GetDocument(a[1],D)) return 1;  
    TCollection_ExtendedString path (a[2]); 
    Handle(TDocStd_Application) A;
    if (!DDocStd::Find(A)) return 1;
    PCDM_StoreStatus theStatus = A->SaveAs(D,path);
    if (theStatus != PCDM_SS_OK ) {
      switch ( theStatus ) {
      case PCDM_SS_DriverFailure: {
	di << " could not store , no driver found to make it " <<"\n" ;
	break ;
      }
      case PCDM_SS_WriteFailure: {
	di << " Write access failure " << "\n" ;  
	break;
      }
      case PCDM_SS_Failure: {
	di << " Write failure " << "\n" ;  
      }
      default: /* Do nothing */ break;
      }
      return 1;
    } else {
      return 0; 
    }
  }
  di << "DDocStd_SaveAs : Error not enough argument" << "\n";
  return 1;
}

//=======================================================================
//function : Close
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_Close (Draw_Interpretor& di,
				       Standard_Integer nb,
				       const char** a)
{   
  if (nb == 2) {   
    Handle(TDocStd_Document) D;   
    if (!DDocStd::GetDocument(a[1],D)) return 1;
    Handle(TDocStd_Application) A;
    if (!DDocStd::Find(A)) return 1;
    A->Close(D); 
    Handle(Draw_Drawable3D) DD = Draw::Get(a[1],Standard_False);
    dout.RemoveDrawable (DD);
    return 0;
  }
  di << "DDocStd_Close : Error" << "\n";
  return 1;
}

//=======================================================================
//function : IsInSession
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_IsInSession (Draw_Interpretor& di,
					     Standard_Integer nb,
					     const char** a)
{   
  if (nb == 2) {   
    Handle(TDocStd_Application) A;
    if (!DDocStd::Find(A)) return 1;
    di << A->IsInSession(a[1]);
    return 0;
  }  
  di << "DDocStd_IsInSession : Error" << "\n";
  return 1;
}

 
//=======================================================================
//function : OSDPath
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_OSDPath (Draw_Interpretor& di,
                                         Standard_Integer nb,
                                         const char** a)
{   
  if (nb == 2) { 
    OSD_Path path (a[1]);
    di << "Node      : " << path.Node().ToCString() << "\n";  
    di << "UserName  : " << path.UserName().ToCString() << "\n"; 
    di << "Password  : " << path.Password().ToCString() << "\n";  
    di << "Disk      : " << path.Disk().ToCString() << "\n";
    di << "Trek      : " << path.Trek().ToCString() << "\n"; 
    di << "Name      : " << path.Name().ToCString() << "\n";  
    di << "Extension : " << path.Extension().ToCString() << "\n";
    return 0;
  }
  di << "DDocStd_OSDPath : Error" << "\n";
  return 1;
}


//=======================================================================
//function : Path
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_Path (Draw_Interpretor& di,
				       Standard_Integer nb,
				       const char** a)
{   
  if (nb == 2) { 
    TDocStd_PathParser path (a[1]);
    //cout << "Trek      : " << path.Trek() << endl;  
    //cout << "Name      : " << path.Name() << endl; 
    //cout << "Extension : " << path.Extension() << endl;
    //cout << "Path      : " << path.Path() << endl;
    TCollection_AsciiString TrekAsciiString(path.Trek().ToExtString(),'?');
    TCollection_AsciiString NameAsciiString(path.Name().ToExtString(),'?');
    TCollection_AsciiString ExtensionAsciiString(path.Extension().ToExtString(),'?');
    TCollection_AsciiString PathAsciiString(path.Path().ToExtString(),'?');
    di << "Trek      : " << TrekAsciiString.ToCString() << "\n";  
    di << "Name      : " << NameAsciiString.ToCString() << "\n"; 
    di << "Extension : " << ExtensionAsciiString.ToCString() << "\n";
    di << "Path      : " << PathAsciiString.ToCString() << "\n";
    return 0;
  }
  di << "DDocStd_Path : Error" << "\n";
  return 1;
}

//=======================================================================
//function : AddComment
//purpose  : 
//=======================================================================
static Standard_Integer DDocStd_AddComment (Draw_Interpretor& di,
                                            Standard_Integer nb,
                                            const char** a)
{  
  if (nb == 3) {
    Handle(TDocStd_Document) D;    
    if (!DDocStd::GetDocument(a[1],D)) return 1;  
    TCollection_ExtendedString comment (a[2]); 
//    Handle(TDocStd_Application) A;
//    if (!DDocStd::Find(A)) return 1;
//    A->AddComment(D,comment);
    D->AddComment(comment);
    return 0; 
  }
  di << "DDocStd_AddComment : Wrong arguments number" << "\n";
  return 1;
}

//=======================================================================
//function : PrintComments
//purpose  : 
//=======================================================================
static Standard_Integer DDocStd_PrintComments (Draw_Interpretor& di,
                                               Standard_Integer nb,
                                               const char** a)
{  
  if (nb == 2) {
    Handle(TDocStd_Document) D;    
    if (!DDocStd::GetDocument(a[1],D)) return 1;  

    TColStd_SequenceOfExtendedString comments;
    D->Comments(comments);

    for (int i = 1; i <= comments.Length(); i++)
    {
      //cout << comments(i) << endl;
      TCollection_AsciiString commentAsciiString(comments(i).ToExtString(),'?');
      di << commentAsciiString.ToCString() << "\n";
    }

    return 0; 
  }
  di << "DDocStd_PrintComments : Wrong arguments number" << "\n";
  return 1;
}

//=======================================================================
//function : ApplicationCommands
//purpose  : 
//=======================================================================

void DDocStd::ApplicationCommands(Draw_Interpretor& theCommands) 
{
  
  static Standard_Boolean done = Standard_False;
  if (done) return;
  done = Standard_True;

  const char* g = "DDocStd application commands";

  // user application commands

  
  theCommands.Add("ListDocuments",
		  "ListDocuments",
		  __FILE__, DDocStd_ListDocuments, g);  

  theCommands.Add("NewDocument",
		  "NewDocument docname format",
		  __FILE__, DDocStd_NewDocument, g);  

  //theCommands.Add("InitViewer",
	//	  "InitViewer DOC",
	//	  __FILE__, DDocStd_InitViewer, g);

  theCommands.Add("Open",
		  "Open path docname",
		  __FILE__, DDocStd_Open, g);   

  theCommands.Add("SaveAs",
		  "SaveAs DOC path",
		  __FILE__, DDocStd_SaveAs, g);  

  theCommands.Add("Save",
		  "Save",
		  __FILE__, DDocStd_Save, g);  

  theCommands.Add("Close",
		  "Close DOC",
		  __FILE__, DDocStd_Close, g);   

  theCommands.Add("IsInSession",
		  "IsInSession path",
		  __FILE__, DDocStd_IsInSession, g);  

  theCommands.Add("OSDPath",
		  "OSDPath string",
		  __FILE__, DDocStd_OSDPath, g);  

  theCommands.Add("Path",
		  "Path string",
		  __FILE__, DDocStd_Path, g);

  theCommands.Add("AddComment",
		  "AddComment Doc string",
		  __FILE__, DDocStd_AddComment, g);

  theCommands.Add("PrintComments",
		  "PrintComments Doc",
		  __FILE__, DDocStd_PrintComments, g);

 // active document

  //theCommands.Add("Active",
	//	  "Active [D]",
	//	  __FILE__, DDocStd_Active, g);  

}