593fde7e1179477e09dcf5eb9676f9fe4944be54
[occt.git] / src / DDocStd / DDocStd_ApplicationCommands.cxx
1 // Created on: 2000-03-01
2 // Created by: Denis PASCAL
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21
22 #include <DDocStd.hxx>
23 #include <Draw.hxx>
24 #include <Draw_Interpretor.hxx>
25 #include <Draw_Viewer.hxx>
26 #include <DDocStd_DrawDocument.hxx>
27 #include <TDocStd_Application.hxx>
28 #include <TDocStd_Document.hxx>
29 #include <TDataStd_Name.hxx>
30 #include <Draw.hxx>
31 #include <Standard_GUID.hxx>
32 #include <Standard_ExtString.hxx>
33 #include <TCollection_AsciiString.hxx>
34 #include <TCollection_ExtendedString.hxx>
35 #include <TDF.hxx>
36 #include <TDF_Data.hxx>
37 #include <TDF_ChildIterator.hxx>
38 #include <TDF_Tool.hxx> 
39
40 #include <OSD_Path.hxx>
41 #include <TDocStd_PathParser.hxx>
42 //#include <TPrsStd_AISViewer.hxx>
43 //#include <AIS_InteractiveContext.hxx>
44
45 #ifndef WNT
46 extern Draw_Viewer dout;
47 #else
48 Standard_IMPORT Draw_Viewer dout;
49 #endif
50
51 extern int ViewerMainLoop (Standard_Integer, const char**); 
52 #include <ViewerTest_Tool.hxx>
53
54 //=======================================================================
55 //function : ListDocuments
56 //purpose  : 
57 //=======================================================================
58
59 static Standard_Integer DDocStd_ListDocuments (Draw_Interpretor& di,
60                                                Standard_Integer nb,
61                                                const char** /*a*/)
62 {  
63   if (nb == 1) {
64     Handle(TDocStd_Application) A;
65     if (!DDocStd::Find(A)) return 1;
66     Handle(TDocStd_Document) D;
67     Standard_Integer nbdoc = A->NbDocuments();
68     for (Standard_Integer i = 1; i <= nbdoc; i++) {
69       A->GetDocument(i,D);
70       di <<"document " << i;
71       if (D->IsSaved()) {
72         TCollection_AsciiString GetNameAsciiString(D->GetName().ToExtString(),'?');
73         TCollection_AsciiString GetPathAsciiString(D->GetPath().ToExtString(),'?');
74         //cout << " name : " << D->GetName();
75         //cout << " path : " << D->GetPath();
76         di << " name : " << GetNameAsciiString.ToCString();
77         di << " path : " << GetPathAsciiString.ToCString();
78       }
79       else di << " not saved";
80       di << "\n";
81     }
82     return 0;
83   }
84   di << "DDocStd_ListDocuments : Error" << "\n";
85   return 1; 
86 }
87
88
89 //=======================================================================
90 //function : NewDocument
91 //purpose  : 
92 //=======================================================================
93
94 static Standard_Integer DDocStd_NewDocument (Draw_Interpretor& di,
95                                              Standard_Integer nb,
96                                              const char** a)
97 {    
98   Handle(TDocStd_Document) D; 
99   Handle(DDocStd_DrawDocument) DD;
100   if (nb == 2) {
101     if (!DDocStd::GetDocument(a[1],D,Standard_False)) { 
102       D = new TDocStd_Document("dummy");  
103       DD = new DDocStd_DrawDocument(D);  
104       Draw::Set(a[1],DD);       
105       di << "document (not handled by application)  " << a[1] << " created" << "\n";  
106       DDocStd::ReturnLabel(di,D->Main()); 
107     }    
108     else di << a[1] << " is already a document" << "\n";
109     return 0;
110   }
111   if (nb == 3) {   
112     if (!DDocStd::GetDocument(a[1],D,Standard_False)) {  
113       Handle(TDocStd_Application) A;
114       if (!DDocStd::Find(A)) return 1;
115       A->NewDocument(a[2],D);  
116       DD = new DDocStd_DrawDocument(D);  
117       TDataStd_Name::Set(D->GetData()->Root(),a[1]);  
118       Draw::Set(a[1],DD);    
119       di << "document " << a[1] << " created" << "\n";    
120       DDocStd::ReturnLabel(di,D->Main()); 
121     }
122     else di << a[1] << " is already a document" << "\n";
123     return 0;
124   }   
125   di << "DDocStd_NewDocument : Error" << "\n";
126   return 1;
127 }
128
129 //=======================================================================
130 //function : InitViewer
131 //purpose  : 
132 //=======================================================================
133
134 // static Standard_Integer DDocStd_InitViewer (Draw_Interpretor& di,
135 //                                          Standard_Integer nb,
136 //                                          const char** arg)
137 // {
138 //   if (nb == 2) {    
139 //     Handle(TDocStd_Application) A;
140 //     if (!DDocStd::Find(A)) return 1;       
141 //     Handle(TDocStd_Document) D;
142 //     if (!DDocStd::GetDocument(arg[1],D)) return 1;     
143 //     Handle(TPrsStd_AISViewer) viewer;
144 //     if (!TPrsStd_AISViewer::Find (D->Main(),viewer)) {
145 //       TCollection_AsciiString title;  
146 //       title.Prepend(arg[1]);   
147 //       title.Prepend("_"); 
148 //       title.Prepend("Document");  
149 //       Handle(V3d_Viewer) vw = ViewerTest_Tool::MakeViewer (title.ToCString()); 
150 //       Handle(AIS_InteractiveContext) IC = new AIS_InteractiveContext(vw);
151 //       A->SetViewer (D,IC);
152 //     } 
153 //     ViewerTest_Tool::InitViewerTest (viewer->GetInteractiveContext());
154 //     return 0;
155 //   }
156 //   cout << "DDocStd_InitViewer : Error" << endl;
157 //   return 1;
158 // }
159
160
161 //=======================================================================
162 //function : Open
163 //purpose  : 
164 //=======================================================================
165
166 static Standard_Integer DDocStd_Open (Draw_Interpretor& di,
167                                       Standard_Integer nb,
168                                       const char** a)
169 {   
170   if (nb == 3) {
171     TCollection_ExtendedString path (a[1]); 
172     Handle(TDocStd_Application) A;
173     if (!DDocStd::Find(A)) return 1;
174     Handle(TDocStd_Document) D;
175     Standard_Integer insession = A->IsInSession(path);
176     if (insession > 0) {  
177       di <<"document " << insession << "  is already in session" << "\n";
178       return 0;
179     }
180     PCDM_ReaderStatus theStatus = A->Open(path,D);
181     if (theStatus == PCDM_RS_OK && !D.IsNull()) {
182       Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D);
183       TDataStd_Name::Set(D->GetData()->Root(),a[2]);
184       Draw::Set(a[2],DD);
185       return 0; 
186     } else {
187       switch ( theStatus ) {
188       case PCDM_RS_AlreadyRetrieved: 
189       case PCDM_RS_AlreadyRetrievedAndModified: {
190         di << " already retrieved " << "\n" ;  
191         break;
192       }
193       case PCDM_RS_NoDriver: {
194         di << " could not retrieve , no Driver to make it " <<"\n" ;
195         break ;
196       }
197       case PCDM_RS_UnknownDocument:
198       case PCDM_RS_NoModel: {
199         di << " could not retrieve , Unknown Document or No Model " <<"\n";
200         break ; 
201       }
202       case PCDM_RS_TypeNotFoundInSchema:
203       case PCDM_RS_UnrecognizedFileFormat: {
204         di << " could not retrieve , Type not found or Unrecognized File Format" <<"\n";
205         break ;
206       }
207       case PCDM_RS_PermissionDenied: {
208         di << " could not retrieve , permission denied " << "\n" ;  
209         break;
210       }
211       default:
212         di << " could not retrieve " << "\n" ;  
213         break;
214       }
215       di << "DDocStd_Open : Error" << "\n";
216     }   
217   }
218   return 1;
219 }
220
221 //=======================================================================
222 //function : Save
223 //purpose  : 
224 //=======================================================================
225
226 static Standard_Integer DDocStd_Save (Draw_Interpretor& di,
227                                       Standard_Integer nb,
228                                       const char** a)
229 {  
230   if (nb == 2) {
231     Handle(TDocStd_Document) D;    
232     if (!DDocStd::GetDocument(a[1],D)) return 1;
233     Handle(TDocStd_Application) A;
234     if (!DDocStd::Find(A)) return 1;  
235     if (!D->IsSaved()) {
236       di << "this document has never been saved" << "\n";
237       return 0;
238     }
239     A->Save(D);
240     return 0; 
241   }
242   di << "DDocStd_Save : Error" << "\n";
243   return 1;
244 }
245
246 //=======================================================================
247 //function : SaveAs
248 //purpose  : 
249 //=======================================================================
250
251 static Standard_Integer DDocStd_SaveAs (Draw_Interpretor& di,
252                                         Standard_Integer nb,
253                                         const char** a)
254 {  
255   if (nb == 3) {
256     Handle(TDocStd_Document) D;    
257     if (!DDocStd::GetDocument(a[1],D)) return 1;  
258     TCollection_ExtendedString path (a[2]); 
259     Handle(TDocStd_Application) A;
260     if (!DDocStd::Find(A)) return 1;
261     PCDM_StoreStatus theStatus = A->SaveAs(D,path);
262     if (theStatus != PCDM_SS_OK ) {
263       switch ( theStatus ) {
264       case PCDM_SS_DriverFailure: {
265         di << " could not store , no driver found to make it " <<"\n" ;
266         break ;
267       }
268       case PCDM_SS_WriteFailure: {
269         di << " Write access failure " << "\n" ;  
270         break;
271       }
272       case PCDM_SS_Failure: {
273         di << " Write failure " << "\n" ;  
274       }
275       }
276       return 1;
277     } else {
278       return 0; 
279     }
280   }
281   di << "DDocStd_SaveAs : Error not enough argument" << "\n";
282   return 1;
283 }
284
285 //=======================================================================
286 //function : Close
287 //purpose  : 
288 //=======================================================================
289
290 static Standard_Integer DDocStd_Close (Draw_Interpretor& di,
291                                        Standard_Integer nb,
292                                        const char** a)
293 {   
294   if (nb == 2) {   
295     Handle(TDocStd_Document) D;   
296     if (!DDocStd::GetDocument(a[1],D)) return 1;
297     Handle(TDocStd_Application) A;
298     if (!DDocStd::Find(A)) return 1;
299     A->Close(D); 
300
301     Handle(Draw_Drawable3D) DD = Draw::Get(a[1],Standard_False);
302     dout.RemoveDrawable (DD);
303     Handle(Draw_Drawable3D) aDNull;
304     Draw::Set(a[1], aDNull);
305     return 0;
306   }
307   di << "DDocStd_Close : Error" << "\n";
308   return 1;
309 }
310
311 //=======================================================================
312 //function : IsInSession
313 //purpose  : 
314 //=======================================================================
315
316 static Standard_Integer DDocStd_IsInSession (Draw_Interpretor& di,
317                                              Standard_Integer nb,
318                                              const char** a)
319 {   
320   if (nb == 2) {   
321     Handle(TDocStd_Application) A;
322     if (!DDocStd::Find(A)) return 1;
323     di << A->IsInSession(a[1]);
324     return 0;
325   }  
326   di << "DDocStd_IsInSession : Error" << "\n";
327   return 1;
328 }
329
330  
331 //=======================================================================
332 //function : OSDPath
333 //purpose  : 
334 //=======================================================================
335
336 static Standard_Integer DDocStd_OSDPath (Draw_Interpretor& di,
337                                          Standard_Integer nb,
338                                          const char** a)
339 {   
340   if (nb == 2) { 
341     OSD_Path path (a[1]);
342     di << "Node      : " << path.Node().ToCString() << "\n";  
343     di << "UserName  : " << path.UserName().ToCString() << "\n"; 
344     di << "Password  : " << path.Password().ToCString() << "\n";  
345     di << "Disk      : " << path.Disk().ToCString() << "\n";
346     di << "Trek      : " << path.Trek().ToCString() << "\n"; 
347     di << "Name      : " << path.Name().ToCString() << "\n";  
348     di << "Extension : " << path.Extension().ToCString() << "\n";
349     return 0;
350   }
351   di << "DDocStd_OSDPath : Error" << "\n";
352   return 1;
353 }
354
355
356 //=======================================================================
357 //function : Path
358 //purpose  : 
359 //=======================================================================
360
361 static Standard_Integer DDocStd_Path (Draw_Interpretor& di,
362                                        Standard_Integer nb,
363                                        const char** a)
364 {   
365   if (nb == 2) { 
366     TDocStd_PathParser path (a[1]);
367     //cout << "Trek      : " << path.Trek() << endl;  
368     //cout << "Name      : " << path.Name() << endl; 
369     //cout << "Extension : " << path.Extension() << endl;
370     //cout << "Path      : " << path.Path() << endl;
371     TCollection_AsciiString TrekAsciiString(path.Trek().ToExtString(),'?');
372     TCollection_AsciiString NameAsciiString(path.Name().ToExtString(),'?');
373     TCollection_AsciiString ExtensionAsciiString(path.Extension().ToExtString(),'?');
374     TCollection_AsciiString PathAsciiString(path.Path().ToExtString(),'?');
375     di << "Trek      : " << TrekAsciiString.ToCString() << "\n";  
376     di << "Name      : " << NameAsciiString.ToCString() << "\n"; 
377     di << "Extension : " << ExtensionAsciiString.ToCString() << "\n";
378     di << "Path      : " << PathAsciiString.ToCString() << "\n";
379     return 0;
380   }
381   di << "DDocStd_Path : Error" << "\n";
382   return 1;
383 }
384
385 //=======================================================================
386 //function : AddComment
387 //purpose  : 
388 //=======================================================================
389 static Standard_Integer DDocStd_AddComment (Draw_Interpretor& di,
390                                             Standard_Integer nb,
391                                             const char** a)
392 {  
393   if (nb == 3) {
394     Handle(TDocStd_Document) D;    
395     if (!DDocStd::GetDocument(a[1],D)) return 1;  
396     TCollection_ExtendedString comment (a[2]); 
397 //    Handle(TDocStd_Application) A;
398 //    if (!DDocStd::Find(A)) return 1;
399 //    A->AddComment(D,comment);
400     D->AddComment(comment);
401     return 0; 
402   }
403   di << "DDocStd_AddComment : Wrong arguments number" << "\n";
404   return 1;
405 }
406
407 //=======================================================================
408 //function : PrintComments
409 //purpose  : 
410 //=======================================================================
411 static Standard_Integer DDocStd_PrintComments (Draw_Interpretor& di,
412                                                Standard_Integer nb,
413                                                const char** a)
414 {  
415   if (nb == 2) {
416     Handle(TDocStd_Document) D;    
417     if (!DDocStd::GetDocument(a[1],D)) return 1;  
418
419     TColStd_SequenceOfExtendedString comments;
420     D->Comments(comments);
421
422     for (int i = 1; i <= comments.Length(); i++)
423     {
424       //cout << comments(i) << endl;
425       TCollection_AsciiString commentAsciiString(comments(i).ToExtString(),'?');
426       di << commentAsciiString.ToCString() << "\n";
427     }
428
429     return 0; 
430   }
431   di << "DDocStd_PrintComments : Wrong arguments number" << "\n";
432   return 1;
433 }
434
435 //=======================================================================
436 //function : ApplicationCommands
437 //purpose  : 
438 //=======================================================================
439
440 void DDocStd::ApplicationCommands(Draw_Interpretor& theCommands) 
441 {
442   
443   static Standard_Boolean done = Standard_False;
444   if (done) return;
445   done = Standard_True;
446
447   const char* g = "DDocStd application commands";
448
449   // user application commands
450
451   
452   theCommands.Add("ListDocuments",
453                   "ListDocuments",
454                   __FILE__, DDocStd_ListDocuments, g);  
455
456   theCommands.Add("NewDocument",
457                   "NewDocument docname format",
458                   __FILE__, DDocStd_NewDocument, g);  
459
460   //theCommands.Add("InitViewer",
461         //        "InitViewer DOC",
462         //        __FILE__, DDocStd_InitViewer, g);
463
464   theCommands.Add("Open",
465                   "Open path docname",
466                   __FILE__, DDocStd_Open, g);   
467
468   theCommands.Add("SaveAs",
469                   "SaveAs DOC path",
470                   __FILE__, DDocStd_SaveAs, g);  
471
472   theCommands.Add("Save",
473                   "Save",
474                   __FILE__, DDocStd_Save, g);  
475
476   theCommands.Add("Close",
477                   "Close DOC",
478                   __FILE__, DDocStd_Close, g);   
479
480   theCommands.Add("IsInSession",
481                   "IsInSession path",
482                   __FILE__, DDocStd_IsInSession, g);  
483
484   theCommands.Add("OSDPath",
485                   "OSDPath string",
486                   __FILE__, DDocStd_OSDPath, g);  
487
488   theCommands.Add("Path",
489                   "Path string",
490                   __FILE__, DDocStd_Path, g);
491
492   theCommands.Add("AddComment",
493                   "AddComment Doc string",
494                   __FILE__, DDocStd_AddComment, g);
495
496   theCommands.Add("PrintComments",
497                   "PrintComments Doc",
498                   __FILE__, DDocStd_PrintComments, g);
499
500  // active document
501
502   //theCommands.Add("Active",
503         //        "Active [D]",
504         //        __FILE__, DDocStd_Active, g);  
505
506 }