summaryrefslogtreecommitdiff
path: root/src/DDocStd/DDocStd_DocumentCommands.cxx
blob: 69a0ed4fae61c4f6722e71683feb1984713c1907 (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
// File:	DDocStd_DocumentCommands.cxx
// Created:	Wed Mar  1 14:06:39 2000
// Author:	Denis PASCAL
//		<dp@dingox.paris1.matra-dtv.fr>


#include <DDocStd.hxx>

#include <DDF.hxx>
#include <Draw.hxx>
#include <Draw_Interpretor.hxx>
#include <TDocStd_XLinkTool.hxx>
#include <TDF_Reference.hxx>
#include <TDocStd_Document.hxx>
#include <CDM_Document.hxx>
#include <TDocStd_Modified.hxx>
#include <TDF_Label.hxx>
#include <DDocStd_DrawDocument.hxx>
#include <TDocStd_Document.hxx>
#include <TDocStd_XLink.hxx>
#include <TDocStd_XLinkRoot.hxx>
#include <TDocStd_XLinkIterator.hxx>
#include <TDocStd_Application.hxx>
#include <Draw.hxx>
#include <Draw_Interpretor.hxx>
#include <TDF_MapIteratorOfLabelMap.hxx>
#include <Plugin.hxx>
#include <TCollection_ExtendedString.hxx>
#include <TDF_Tool.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDF_Tool.hxx>
//
// pour propagate
//
#include <TDocStd_XLinkTool.hxx>

#include <OSD_Function.hxx>
#include <OSD_SharedLibrary.hxx>
#include <OSD_LoadMode.hxx>

//typedef Standard_Integer (* DFBROWSER_CALL)(const Handle(TDocStd_Document)&);
//static DFBROWSER_CALL gDFunc = 0;

//=======================================================================
//function : Main
//purpose  : Main (DOC)
//=======================================================================

static Standard_Integer DDocStd_Main (Draw_Interpretor& di,Standard_Integer nb, const char** a)
{
  if (nb == 2) {
    Handle(TDocStd_Document) DOC;   
    if (!DDocStd::GetDocument(a[1],DOC)) return 1;
    DDocStd::ReturnLabel(di,DOC->Main());
    return 0;
  }   
  di << "DDocStd_Main : Error" << "\n";
  return 1;  
}


//=======================================================================
//function : Format
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_Format (Draw_Interpretor& di,Standard_Integer n, const char** a)
{ 
  Handle(TDocStd_Document) D;
  if (n == 2) {
    if (!DDocStd::GetDocument(a[1],D)) return 1;    
    //cout << "FORMAT : " << D->StorageFormat() << endl;
    di << "FORMAT : ";
    Standard_SStream aStream;
    D->StorageFormat().Print(aStream);
    di << aStream;
    di << "\n";
    return 0;
  }  
  if (n == 3) {
    if (!DDocStd::GetDocument(a[1],D)) return 1;    
    D->ChangeStorageFormat(a[2]);  
    return 0;
  }  
  di << "DDocStd_Format : Error" << "\n";
  return 1;
}

//=======================================================================
//function : Copy "Copy DOC entry XDOC xentry",
//=======================================================================

static Standard_Integer DDocStd_Copy (Draw_Interpretor& di,Standard_Integer n, const char** a)
{
  if (n == 5) {  
    Handle(TDocStd_Document) DOC, XDOC;  
    if (!DDocStd::GetDocument(a[1],DOC)) return 1; 
    if (!DDocStd::GetDocument(a[3],XDOC)) return 1;
    TDF_Label L,XL;
    if (!DDocStd::Find(DOC,a[2],L)) return 1; 
    if (!DDocStd::Find(XDOC,a[4],XL)) return 1;
    TDocStd_XLinkTool XLinkTool;
    XLinkTool.Copy(L,XL);
    if (!XLinkTool.IsDone()) {
      di << "DDocStd_XLinkTool : not done" << "\n";
    }
    return 0;
  }  
  di << "DDocStd_XLinkTool : Error" << "\n";
  return 1; 
}


//=======================================================================
//function : CopyWithLink "Copy DOC entry XDOC xentry",
//=======================================================================

static Standard_Integer DDocStd_CopyWithLink (Draw_Interpretor& di,Standard_Integer n, const char** a)
{  
  if (n == 5) {  
    Handle(TDocStd_Document) DOC, XDOC;  
    if (!DDocStd::GetDocument(a[1],DOC)) return 1; 
    if (!DDocStd::GetDocument(a[3],XDOC)) return 1;   
    TDF_Label L,XL;
    if (!DDocStd::Find(DOC,a[2],L)) return 1; 
    if (!DDocStd::Find(XDOC,a[4],XL)) return 1;
    TDocStd_XLinkTool XLinkTool;
    XLinkTool.CopyWithLink(L,XL);
    if (!XLinkTool.IsDone()) {
      di << "DDocStd_CopyWithLink : not done" << "\n";
    }
    return 0;
  }  
  di << "DDocStd_CopyWithLink : Error" << "\n";
  return 1; 
}

//=======================================================================
//function : UpdateLink (D,[xrefentry])
//=======================================================================

static Standard_Integer DDocStd_UpdateLink (Draw_Interpretor& di,Standard_Integer nb, const char** a)
{  
  if (nb == 2 || nb == 3) {  
    Handle(TDocStd_Document) DOC;  
    if (!DDocStd::GetDocument(a[1],DOC)) return 1; 
    Handle(TDF_Reference) REF;
    TDocStd_XLinkTool XLinkTool;  
    if (nb == 3) {
      if (!DDocStd::Find(DOC,a[2],TDF_Reference::GetID(),REF)) return 1;
      XLinkTool.UpdateLink(REF->Label());
      if (!XLinkTool.IsDone()) {
	di << "DDocStd_UpdateXLink : not done" << "\n";
      }
    }
    else {
      for (TDocStd_XLinkIterator xit (DOC); xit.More(); xit.Next()) {
	XLinkTool.UpdateLink(xit.Value()->Label());
	if (!XLinkTool.IsDone()) {
	  di << "DDocStd_UpdateXLink : not done" << "\n";
	}
      }
    }
    return 0;
  }
  di << "DDocStd_UpdateXLink : Error" << "\n";
  return 1; 
}

//=======================================================================
//function : UndoLimit
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_UndoLimit (Draw_Interpretor& di,Standard_Integer n, const char** a)
{
  if (n < 2) return 1;
  
  Handle(TDocStd_Document) D;
  if (!DDocStd::GetDocument(a[1],D)) return 1;
  
  if (n > 2) {
    Standard_Integer lim = atoi(a[2]);
    D->SetUndoLimit(lim);
  }
  
  // display current values
  di << D->GetUndoLimit() << " ";
  di << D->GetAvailableUndos() << " ";
  di << D->GetAvailableRedos();
  return 0;
}

//=======================================================================
//function : Undo, Redo
//purpose  : Undo (DOC)
//=======================================================================

static Standard_Integer DDocStd_Undo (Draw_Interpretor& di,Standard_Integer n, const char** a)
{
  if (n < 2) return 1;
  
  Handle(TDocStd_Document) D;
  if (!DDocStd::GetDocument(a[1],D)) return 1;
  
  Standard_Integer i,step = 1;
  if (n > 2) {
    step = atoi(a[2]);
  }

  // test if the command was undo or redo
  Standard_Boolean undo = a[0][0] == 'U';

  for (i = 1; i <= step; i++) {
    if (undo) {
      if (!D->Undo()) di << "Undo not done" << "\n";
    }
    else {
      if (!D->Redo()) di << "Redo not done" << "\n";
    }
  }
  
  return 0;
}

//=======================================================================
//function : NewCommand
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_NewCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
{
  if (n < 2) return 1;
  Handle(TDocStd_Document) D;
  if (!DDocStd::GetDocument(a[1],D)) return 1;
  D->NewCommand();
  return 0;
}

//=======================================================================
//function : OpenCommand
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_OpenCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
{
  if (n < 2) return 1;

  Handle(TDocStd_Document) D;
  if (!DDocStd::GetDocument(a[1],D)) return 1;
  D->OpenCommand();
  return 0;
}

//=======================================================================
//function : AbortCommand
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_AbortCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
{
  if (n < 2) return 1;
  Handle(TDocStd_Document) D;
  if (!DDocStd::GetDocument(a[1],D)) return 1;
  D->AbortCommand();
  return 0;
}

//=======================================================================
//function : CommitCommand
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_CommitCommand(Draw_Interpretor& /*di*/,Standard_Integer n, const char** a)
{
  if (n < 2) return 1;
  Handle(TDocStd_Document) D;
  if (!DDocStd::GetDocument(a[1],D)) return 1;
  D->CommitCommand();
  return 0;
}


//=======================================================================
//function : DDocStd_DumpDocument
//purpose  : DumpDocument (DOC)
//=======================================================================

static Standard_Integer DDocStd_DumpDocument (Draw_Interpretor& di,
					      Standard_Integer nb, 
					      const char** arg) 
{   
  if (nb == 2) {   
    Handle(TDocStd_Document) D;       
    if (!DDocStd::GetDocument(arg[1],D)) return 1;     
    di << "\n"; 
    // document name
    if (D->IsSaved()) 
      di << "DOCUMENT      : " << TCollection_AsciiString(D->GetName(),'?').ToCString();
    else 
      di << "DOCUMENT      : " << "not saved";
    di << "\n";
    // format
    //cout << "FORMAT        : " << D->StorageFormat();
    di << "FORMAT        : ";
    Standard_SStream aStream;
    D->StorageFormat().Print(aStream);
    di << aStream;
    di << "\n";
    // command
    di << "COMMAND       : ";
    if (D->HasOpenCommand()) di << " Is Open";
#ifndef DEB
    else di << " Is Not Open";
#else
    else " Is Not Open"; // ???
#endif
    // undo
    di << "UNDO          :"; 
    di << " limit :" << D->GetUndoLimit();
    di << " undos :" << D->GetAvailableUndos() << " ";  
    di << " redos :" << D->GetAvailableRedos(); 
    di << "\n";
    // cout << "CURRENT :";   
    //     TCollection_AsciiString string;  
    //     TDF_Tool::Entry(D->CurrentLabel(),string); 
    //     cout << string;
    //     cout << endl;
    // modified  
    di << "MODIFIED      : ";
    if (D->IsModified()) di << "true";
    else di << "false";  
    di << "\n"; 
    if (!TDocStd_Modified::IsEmpty(D->Main())) {
      di << "MODIFICATIONS : ";     
      TDF_MapIteratorOfLabelMap it (D->GetModified()); 
      if (!it.More()) di << "VALID" << "\n"; 
      else {
	TCollection_AsciiString string;  
	for (;it.More();it.Next()) { 
	  TDF_Tool::Entry(it.Key(),string);
	  di << string.ToCString() << " "; 
	}
	di << "\n";
      } 
    }
    return 0;
  } 
  di << "DDocStd_DumpDocument : Error" << "\n";
  return 1;
}

//=======================================================================
//function : SetModified
//purpose  : Set modifications in a document
//=======================================================================

static Standard_Integer DDocStd_SetModified (Draw_Interpretor& di,Standard_Integer n, const char** a)
{
  if (n > 2) {
    Handle(TDocStd_Document) D;
    if (!DDocStd::GetDocument(a[1],D)) return 1;
    TDF_Label L;
    for (Standard_Integer i = 2; i < n; i++) {
      if (DDocStd::Find(D,a[i],L)) D->SetModified(L);
    }
    return 0; 
  }
  di << "DDocStd_SetModified : Error" << "\n";
  return 1;
}

//=======================================================================
//function : Propagate
//purpose  : 
//=======================================================================

static Standard_Integer DDocStd_Propagate (Draw_Interpretor& di,Standard_Integer /*n*/, const char** /*a*/)
{
//   if (n == 2) {
//     Handle(TDocStd_Document) D;
//     if (!DDocStd::GetDocument(a[1],D)) return 1;
//     if (D->IsValid()) {
//       cout << "the document is valid" << endl;
//       return 0;
//     }
//     Handle(TDesign_Function) F; 
//     if (!D->Main().FindAttribute(TDesign_Function::GetID(),F)) {
//       cout << "no function found at main" << endl;
//       return 0;
//     }
//     TFunction_Logbook mdf (Standard_True);   
//     for (TDF_MapIteratorOfLabelMap it (D->GetModified());it.More();it.Next()) {
//       mdf.SetTouched(it.Key());
//     }  
//     F->Execute(mdf);
//     D->PurgeModified();
//     return 0;
//   }
  di << "DDocStd_Propagate : not implemented" << "\n";
  return 1;
}

//=======================================================================
//function : DocumentCommands
//purpose  : 
//=======================================================================

void DDocStd::DocumentCommands(Draw_Interpretor& theCommands) 
{
  
  static Standard_Boolean done = Standard_False;
  if (done) return;
  done = Standard_True;
  
  
  const char* g = "DDocStd commands";
  
  // Data Framework Access
  
  theCommands.Add("Main","Main (DOC)",
  		  __FILE__, DDocStd_Main, g);  

  
  // DUMP
  
  theCommands.Add ("Format", 
                   "Format (DOC, [format])",
		   __FILE__, DDocStd_Format, g); 

  theCommands.Add ("DumpDocument", 
                   "DumpDocument (DOC)",
		   __FILE__, DDocStd_DumpDocument, g);   

  // XREF

  theCommands.Add("Copy","Copy DOC entry XDOC xentry",
		  __FILE__, DDocStd_Copy, g);  

  theCommands.Add("CopyWithLink","CopyWithLink DOC entry XDOC xentry",
		  __FILE__, DDocStd_CopyWithLink, g);  

  theCommands.Add("UpdateLink","UpdateLink DOC [entry]",
		  __FILE__, DDocStd_UpdateLink, g);  


  // UNDO/REDO

  theCommands.Add("UndoLimit","UndoLimit DOC (Value), return UndoLimit Undos Redos",
		  __FILE__, DDocStd_UndoLimit, g);
  
  theCommands.Add("Undo","Undo DOC (steps = 1)",
		  __FILE__, DDocStd_Undo, g);
  
  theCommands.Add("Redo","Redo DOC (steps = 1)",
		  __FILE__, DDocStd_Undo, g);
  
  theCommands.Add("NewCommand","NewCommand DOC",
		  __FILE__, DDocStd_NewCommand, g);  

  theCommands.Add("OpenCommand","OpenCommand DOC",
		  __FILE__, DDocStd_OpenCommand, g);  

  theCommands.Add("AbortCommand","AbortCommand DOC",
		  __FILE__, DDocStd_AbortCommand, g);    

  theCommands.Add("CommitCommand","CommitCommand DOC",
		  __FILE__, DDocStd_CommitCommand, g);    


  // Modif and Propagation

  theCommands.Add("SetModified","SetModified DOC Label1 Label2 ....",
 		  __FILE__, DDocStd_SetModified, g);

  theCommands.Add("Propagate","Propagate DOC",
		  __FILE__, DDocStd_Propagate, g);

}