0026940: Visualization, TKOpenGl - capping plane should be applied to connected struc...
[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>
35#include <TDocStd_PathParser.hxx>
7fd59977 36
0e93d9e5 37#include <AIS_InteractiveContext.hxx>
38#include <TPrsStd_AISViewer.hxx>
39#include <ViewerTest.hxx>
40#include <V3d_Viewer.hxx>
41
42#ifndef _WIN32
7fd59977 43extern Draw_Viewer dout;
44#else
45Standard_IMPORT Draw_Viewer dout;
46#endif
47
7fd59977 48//=======================================================================
49//function : ListDocuments
50//purpose :
51//=======================================================================
52
53static Standard_Integer DDocStd_ListDocuments (Draw_Interpretor& di,
54 Standard_Integer nb,
55 const char** /*a*/)
56{
57 if (nb == 1) {
58 Handle(TDocStd_Application) A;
59 if (!DDocStd::Find(A)) return 1;
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 }
78 di << "DDocStd_ListDocuments : Error" << "\n";
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);
99 di << "document (not handled by application) " << a[1] << " created" << "\n";
100 DDocStd::ReturnLabel(di,D->Main());
101 }
102 else di << a[1] << " is already a document" << "\n";
103 return 0;
104 }
105 if (nb == 3) {
106 if (!DDocStd::GetDocument(a[1],D,Standard_False)) {
107 Handle(TDocStd_Application) A;
108 if (!DDocStd::Find(A)) return 1;
109 A->NewDocument(a[2],D);
110 DD = new DDocStd_DrawDocument(D);
111 TDataStd_Name::Set(D->GetData()->Root(),a[1]);
112 Draw::Set(a[1],DD);
113 di << "document " << a[1] << " created" << "\n";
114 DDocStd::ReturnLabel(di,D->Main());
115 }
116 else di << a[1] << " is already a document" << "\n";
117 return 0;
118 }
119 di << "DDocStd_NewDocument : Error" << "\n";
120 return 1;
121}
122
7fd59977 123//=======================================================================
124//function : Open
125//purpose :
126//=======================================================================
127
128static Standard_Integer DDocStd_Open (Draw_Interpretor& di,
129 Standard_Integer nb,
130 const char** a)
131{
132 if (nb == 3) {
133 TCollection_ExtendedString path (a[1]);
134 Handle(TDocStd_Application) A;
135 if (!DDocStd::Find(A)) return 1;
136 Handle(TDocStd_Document) D;
137 Standard_Integer insession = A->IsInSession(path);
138 if (insession > 0) {
139 di <<"document " << insession << " is already in session" << "\n";
140 return 0;
141 }
15e8b082
M
142 PCDM_ReaderStatus theStatus = A->Open(path,D);
143 if (theStatus == PCDM_RS_OK && !D.IsNull()) {
7fd59977 144 Handle(DDocStd_DrawDocument) DD = new DDocStd_DrawDocument(D);
145 TDataStd_Name::Set(D->GetData()->Root(),a[2]);
146 Draw::Set(a[2],DD);
147 return 0;
148 } else {
149 switch ( theStatus ) {
15e8b082
M
150 case PCDM_RS_AlreadyRetrieved:
151 case PCDM_RS_AlreadyRetrievedAndModified: {
7fd59977 152 di << " already retrieved " << "\n" ;
153 break;
154 }
15e8b082 155 case PCDM_RS_NoDriver: {
7fd59977 156 di << " could not retrieve , no Driver to make it " <<"\n" ;
157 break ;
158 }
15e8b082
M
159 case PCDM_RS_UnknownDocument:
160 case PCDM_RS_NoModel: {
7fd59977 161 di << " could not retrieve , Unknown Document or No Model " <<"\n";
162 break ;
163 }
15e8b082
M
164 case PCDM_RS_TypeNotFoundInSchema:
165 case PCDM_RS_UnrecognizedFileFormat: {
7fd59977 166 di << " could not retrieve , Type not found or Unrecognized File Format" <<"\n";
167 break ;
168 }
15e8b082 169 case PCDM_RS_PermissionDenied: {
7fd59977 170 di << " could not retrieve , permission denied " << "\n" ;
171 break;
172 }
15e8b082
M
173 default:
174 di << " could not retrieve " << "\n" ;
175 break;
7fd59977 176 }
177 di << "DDocStd_Open : Error" << "\n";
178 }
179 }
180 return 1;
181}
182
183//=======================================================================
184//function : Save
185//purpose :
186//=======================================================================
187
188static Standard_Integer DDocStd_Save (Draw_Interpretor& di,
189 Standard_Integer nb,
190 const char** a)
191{
192 if (nb == 2) {
193 Handle(TDocStd_Document) D;
194 if (!DDocStd::GetDocument(a[1],D)) return 1;
195 Handle(TDocStd_Application) A;
196 if (!DDocStd::Find(A)) return 1;
197 if (!D->IsSaved()) {
198 di << "this document has never been saved" << "\n";
199 return 0;
200 }
201 A->Save(D);
202 return 0;
203 }
204 di << "DDocStd_Save : Error" << "\n";
205 return 1;
206}
207
208//=======================================================================
209//function : SaveAs
210//purpose :
211//=======================================================================
212
213static Standard_Integer DDocStd_SaveAs (Draw_Interpretor& di,
214 Standard_Integer nb,
215 const char** a)
216{
217 if (nb == 3) {
218 Handle(TDocStd_Document) D;
219 if (!DDocStd::GetDocument(a[1],D)) return 1;
220 TCollection_ExtendedString path (a[2]);
221 Handle(TDocStd_Application) A;
222 if (!DDocStd::Find(A)) return 1;
15e8b082
M
223 PCDM_StoreStatus theStatus = A->SaveAs(D,path);
224 if (theStatus != PCDM_SS_OK ) {
7fd59977 225 switch ( theStatus ) {
566f8441 226 case PCDM_SS_DriverFailure: {
227 di << "Error saving document: Could not store , no driver found to make it" << "\n";
228 break ;
229 }
230 case PCDM_SS_WriteFailure: {
231 di << "Error saving document: Write access failure" << "\n";
232 break;
233 }
234 case PCDM_SS_Failure: {
235 di << "Error saving document: Write failure" << "\n" ;
236 break;
237 }
238 case PCDM_SS_Doc_IsNull: {
239 di << "Error saving document: No document to save" << "\n";
240 break ;
241 }
242 case PCDM_SS_No_Obj: {
243 di << "Error saving document: No objects written" << "\n";
244 break;
245 }
246 case PCDM_SS_Info_Section_Error: {
247 di << "Error saving document: Write info section failure" << "\n" ;
248 break;
249 }
250 default:
251 break;
7fd59977 252 }
253 return 1;
254 } else {
255 return 0;
256 }
257 }
258 di << "DDocStd_SaveAs : Error not enough argument" << "\n";
259 return 1;
260}
261
262//=======================================================================
263//function : Close
264//purpose :
265//=======================================================================
266
0e93d9e5 267static Standard_Integer DDocStd_Close (Draw_Interpretor& /*theDI*/,
268 Standard_Integer theArgNb,
269 const char** theArgVec)
7fd59977 270{
0e93d9e5 271 if (theArgNb != 2)
272 {
273 std::cout << "DDocStd_Close : Error\n";
274 return 1;
275 }
24de79c3 276
0e93d9e5 277 Handle(TDocStd_Document) aDoc;
278 Standard_CString aDocName = theArgVec[1];
279 if (!DDocStd::GetDocument (aDocName, aDoc))
280 {
281 return 1;
7fd59977 282 }
0e93d9e5 283
284 TDF_Label aRoot = aDoc->GetData()->Root();
285 Handle(TPrsStd_AISViewer) aDocViewer;
286 if (TPrsStd_AISViewer::Find (aRoot, aDocViewer)
287 && !aDocViewer->GetInteractiveContext().IsNull())
288 {
289 Handle(V3d_Viewer) aViewer = aDocViewer->GetInteractiveContext()->CurrentViewer();
290 for (aViewer->InitDefinedViews(); aViewer->MoreDefinedViews(); aViewer->NextDefinedViews())
291 {
292 Handle(V3d_View) aView = aViewer->DefinedView();
293 ViewerTest::RemoveView (aView);
294 }
295 }
296
297 Handle(TDocStd_Application) aDocApp;
298 if (!DDocStd::Find (aDocApp))
299 {
300 return 1;
301 }
302
303 aDocApp->Close (aDoc);
304
305 Handle(Draw_Drawable3D) aDrawable = Draw::Get (aDocName, Standard_False);
306 dout.RemoveDrawable (aDrawable);
307 Draw::Set (theArgVec[1], Handle(Draw_Drawable3D)());
308 return 0;
7fd59977 309}
310
311//=======================================================================
312//function : IsInSession
313//purpose :
314//=======================================================================
315
316static 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
336static 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
361static 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//=======================================================================
389static 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//=======================================================================
411static 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
440void 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
7fd59977 450 theCommands.Add("ListDocuments",
451 "ListDocuments",
452 __FILE__, DDocStd_ListDocuments, g);
453
454 theCommands.Add("NewDocument",
455 "NewDocument docname format",
456 __FILE__, DDocStd_NewDocument, g);
457
7fd59977 458 theCommands.Add("Open",
459 "Open path docname",
460 __FILE__, DDocStd_Open, g);
461
462 theCommands.Add("SaveAs",
463 "SaveAs DOC path",
464 __FILE__, DDocStd_SaveAs, g);
465
466 theCommands.Add("Save",
467 "Save",
468 __FILE__, DDocStd_Save, g);
469
470 theCommands.Add("Close",
471 "Close DOC",
472 __FILE__, DDocStd_Close, g);
473
474 theCommands.Add("IsInSession",
475 "IsInSession path",
476 __FILE__, DDocStd_IsInSession, g);
477
478 theCommands.Add("OSDPath",
479 "OSDPath string",
480 __FILE__, DDocStd_OSDPath, g);
481
482 theCommands.Add("Path",
483 "Path string",
484 __FILE__, DDocStd_Path, g);
485
486 theCommands.Add("AddComment",
487 "AddComment Doc string",
488 __FILE__, DDocStd_AddComment, g);
489
490 theCommands.Add("PrintComments",
491 "PrintComments Doc",
492 __FILE__, DDocStd_PrintComments, g);
7fd59977 493}