0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / DDocStd / DDocStd_MTMCommands.cxx
CommitLineData
b311480e 1// Created on: 2002-11-26
2// Created by: Vladimir ANIKIN
973c2be1 3// Copyright (c) 2002-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 <TDocStd_MultiTransactionManager.hxx>
18#include <DDocStd_DrawDocument.hxx>
19#include <TDocStd_Document.hxx>
20#include <Draw.hxx>
21#include <DDF_Browser.hxx>
22#include <TDF_Label.hxx>
23#include <TDF_Tool.hxx>
24#include <TDF_AttributeIterator.hxx>
25#include <TDataStd_TreeNode.hxx>
26#include <TDataStd_Integer.hxx>
27#include <TDataStd_Real.hxx>
28#include <TDataStd_Name.hxx>
f767df4e 29#include <TDataStd_Comment.hxx>
30#include <TDataStd_AsciiString.hxx>
31#include <TDataStd_IntegerArray.hxx>
7fd59977 32#include <TDataStd_RealArray.hxx>
f767df4e 33#include <TDataStd_ByteArray.hxx>
7fd59977 34#include <TNaming_NamedShape.hxx>
35#include <TDataStd_UAttribute.hxx>
36#include <TopoDS_Shape.hxx>
37#include <Draw_Interpretor.hxx>
f767df4e 38#include <TDF_Reference.hxx>
7fd59977 39
40//=======================================================================
41//function : mtmCreate
42//purpose : creates new new multiple transactions' manager
43//=======================================================================
44
45static Handle(TDocStd_MultiTransactionManager) sMultiTransactionManager = 0;
46
47static int mtmCreate (Draw_Interpretor& /*di*/, int n, const char** a)
48{
49 if(!sMultiTransactionManager.IsNull())
50 sMultiTransactionManager->SetUndoLimit(0);
51
52 sMultiTransactionManager = new TDocStd_MultiTransactionManager();
53 if(n > 1)
91322f44 54 sMultiTransactionManager->SetUndoLimit(Draw::Atoi(a[1]));
7fd59977 55 return 0;
56}
57
58//=======================================================================
59//function : mtmAddDocument
60//purpose : adds a document to the transactions manager
61//=======================================================================
62
63static int mtmAddDocument (Draw_Interpretor& di, int n, const char** a)
64{
65 if(sMultiTransactionManager.IsNull()) {
586db386 66 di << "Error : manager is not initialised\n";
7fd59977 67 return 1;
68 }
69 if(n > 1) {
70 Handle(DDocStd_DrawDocument) aDrawDoc =
71 Handle(DDocStd_DrawDocument)::DownCast(Draw::Get(a[1]));
72 if(aDrawDoc.IsNull()) {
586db386 73 di << "Error : wrong document name\n";
7fd59977 74 return 1;
75 }
76 sMultiTransactionManager->AddDocument(aDrawDoc->GetDocument());
77 }
78 else {
586db386 79 di << "Error : document name is not defined\n";
7fd59977 80 return 1;
81 }
82 return 0;
83}
84
85//=======================================================================
86//function : mtmOpenTransaction
87//purpose : opens new transaction
88//=======================================================================
89
90static int mtmOpenTransaction (Draw_Interpretor& di, int /*n*/, const char** /*a*/)
91{
92 if(sMultiTransactionManager.IsNull()) {
586db386 93 di << "Error : manager is not initialised\n";
7fd59977 94 return 1;
95 }
96 sMultiTransactionManager->OpenCommand();
97 return 0;
98}
99
100//=======================================================================
101//function : mtmCommitTransaction
102//purpose : commits last opened transaction
103//=======================================================================
104
105static int mtmCommitTransaction (Draw_Interpretor& di, int n, const char** a)
106{
107 if(sMultiTransactionManager.IsNull()) {
586db386 108 di << "Error : manager is not initialised\n";
7fd59977 109 return 1;
110 }
111 if(n > 1)
112 sMultiTransactionManager->CommitCommand(a[1]);
113 else
114 sMultiTransactionManager->CommitCommand();
115 return 0;
116}
117
118//=======================================================================
119//function : mtmAbortTransaction
120//purpose : aborts last opened transaction
121//=======================================================================
122
123static int mtmAbortTransaction (Draw_Interpretor& di, int /*n*/, const char** /*a*/)
124{
125 if(sMultiTransactionManager.IsNull()) {
586db386 126 di << "Error : manager is not initialised\n";
7fd59977 127 return 1;
128 }
129 sMultiTransactionManager->AbortCommand();
130 return 0;
131}
132
133//=======================================================================
134//function : mtmDump
135//purpose : dumps state of the multiple transaction manager
136//=======================================================================
137
138static int mtmDump (Draw_Interpretor& di, int /*n*/, const char** /*a*/)
139{
140 if(sMultiTransactionManager.IsNull()) {
586db386 141 di << "Error : manager is not initialised\n";
7fd59977 142 return 1;
143 }
586db386 144 di << "*** Dump of MTM ***\n";
04232180 145 //sMultiTransactionManager->DumpTransaction(std::cout);
7fd59977 146 Standard_SStream aStream;
147 sMultiTransactionManager->DumpTransaction(aStream);
148 di << aStream;
586db386 149 di << "\n*** End ***\n";
7fd59977 150 return 0;
151}
152
153//=======================================================================
154//function : mtmUndo
155//purpose : undos last transaction
156//=======================================================================
157
158static int mtmUndo (Draw_Interpretor& di, int /*n*/, const char** /*a*/)
159{
160 if(sMultiTransactionManager.IsNull()) {
586db386 161 di << "Error : manager is not initialised\n";
7fd59977 162 return 1;
163 }
164 sMultiTransactionManager->Undo();
165 return 0;
166}
167
168//=======================================================================
169//function : mtmRedo
170//purpose : redos last transaction
171//=======================================================================
172
173static int mtmRedo (Draw_Interpretor& di, int /*n*/, const char** /*a*/)
174{
175 if(sMultiTransactionManager.IsNull()) {
586db386 176 di << "Error : manager is not initialised\n";
7fd59977 177 return 1;
178 }
179 sMultiTransactionManager->Redo();
180 return 0;
181}
182
183//=======================================================================
184//function : mtmNestedMode
185//purpose : redos last transaction
186//=======================================================================
187
188static int mtmNestedMode (Draw_Interpretor& di, int n, const char** a)
189{
190 if(sMultiTransactionManager.IsNull()) {
586db386 191 di << "Error : manager is not initialised\n";
7fd59977 192 return 1;
193 }
194 Standard_Boolean aMode = Standard_False;
195 if(n > 1) {
91322f44 196 aMode = Draw::Atoi(a[1]) ? Standard_True : Standard_False;
7fd59977 197 }
198 sMultiTransactionManager->SetNestedTransactionMode(aMode);
199 return 0;
200}
201
202//=======================================================================
203//function : XAttributeValue
204//purpose :
205//=======================================================================
206
207static Standard_Integer XAttributeValue (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
208{
1c8fc6be 209 if ( argc < 4 ) { di << "ERROR: Too few args\n"; return 1; }
210 Handle(DDF_Browser) browser = Handle(DDF_Browser)::DownCast (Draw::GetExisting (argv[1]));
211 if ( browser.IsNull() ) { std::cout << "Syntax error: Not a browser: " << argv[1] << "\n"; return 1; }
7fd59977 212
213 TDF_Label lab;
214 TDF_Tool::Label(browser->Data(),argv[2],lab);
1c8fc6be 215 if ( lab.IsNull() ) { di << "Syntax error: label is Null: " << argv[2] << "\n"; return 1; }
7fd59977 216
91322f44 217 Standard_Integer num = Draw::Atoi ( argv[3] );
7fd59977 218 TDF_AttributeIterator itr(lab,Standard_False);
219 for (Standard_Integer i=1; itr.More() && i < num; i++) itr.Next();
220
1c8fc6be 221 if ( ! itr.More() ) { di << "Syntax error: Attribute #" << num << " not found\n"; return 1; }
7fd59977 222
223 const Handle(TDF_Attribute)& att = itr.Value();
f767df4e 224 if ( att->IsKind(STANDARD_TYPE(TDataStd_TreeNode)) )
7fd59977 225 {
7fd59977 226 Handle(TDataStd_TreeNode) TN = Handle(TDataStd_TreeNode)::DownCast(att);
227 TCollection_AsciiString ref;
228 if ( TN->HasFather() )
229 {
230 TDF_Tool::Entry ( TN->Father()->Label(), ref );
f767df4e 231 di << " ==> " << ref.ToCString();
7fd59977 232 }
233 else
234 {
f767df4e 235 di << " <== (" << ref.ToCString();
7fd59977 236 Handle(TDataStd_TreeNode) child = TN->First();
237 while ( ! child.IsNull() )
238 {
239 TDF_Tool::Entry ( child->Label(), ref );
240 if ( child != TN->First() ) di << ", ";
241 di << ref.ToCString();
242 child = child->Next();
243 }
244 di << ")";
245 }
246 }
f767df4e 247 else if ( att->IsKind(STANDARD_TYPE(TDF_Reference)) )
248 {
249 Handle(TDF_Reference) val = Handle(TDF_Reference)::DownCast ( att );
250 TCollection_AsciiString ref;
251 TDF_Tool::Entry ( val->Get(), ref );
252 di << "==> " << ref.ToCString();
253 }
7fd59977 254 else if ( att->IsKind(STANDARD_TYPE(TDataStd_Integer)) )
255 {
256 Handle(TDataStd_Integer) val = Handle(TDataStd_Integer)::DownCast ( att );
257 TCollection_AsciiString str ( val->Get() );
258 di << str.ToCString();
259 }
260 else if ( att->IsKind(STANDARD_TYPE(TDataStd_Real)) )
261 {
262 Handle(TDataStd_Real) val = Handle(TDataStd_Real)::DownCast ( att );
263 TCollection_AsciiString str ( val->Get() );
264 di << str.ToCString();
265 }
f767df4e 266 else if ( att->IsKind(STANDARD_TYPE(TDataStd_Name)) )
7fd59977 267 {
268 Handle(TDataStd_Name) val = Handle(TDataStd_Name)::DownCast ( att );
269 TCollection_AsciiString str ( val->Get(), '?' );
270 di << str.ToCString();
271 }
f767df4e 272 else if ( att->IsKind(STANDARD_TYPE(TDataStd_Comment)) )
273 {
274 Handle(TDataStd_Comment) val = Handle(TDataStd_Comment)::DownCast ( att );
275 TCollection_AsciiString str ( val->Get(), '?' );
276 di << str.ToCString();
277 }
278 else if ( att->IsKind(STANDARD_TYPE(TDataStd_AsciiString)) )
279 {
280 Handle(TDataStd_AsciiString) val = Handle(TDataStd_AsciiString)::DownCast ( att );
281 TCollection_AsciiString str ( val->Get(), '?' );
282 di << str.ToCString();
283 }
284 else if ( att->IsKind(STANDARD_TYPE(TDataStd_IntegerArray)) )
285 {
286 Handle(TDataStd_IntegerArray) val = Handle(TDataStd_IntegerArray)::DownCast ( att );
287 for ( Standard_Integer j=val->Lower(); j <= val->Upper(); j++ )
288 {
289 if ( j > val->Lower() ) di << ", ";
290 TCollection_AsciiString str ( val->Value(j) );
291 di << str.ToCString();
292 }
293 }
294 else if ( att->IsKind(STANDARD_TYPE(TDataStd_RealArray)) )
7fd59977 295 {
296 Handle(TDataStd_RealArray) val = Handle(TDataStd_RealArray)::DownCast ( att );
297 for ( Standard_Integer j=val->Lower(); j <= val->Upper(); j++ )
298 {
299 if ( j > val->Lower() ) di << ", ";
300 TCollection_AsciiString str ( val->Value(j) );
301 di << str.ToCString();
302 }
303 }
f767df4e 304 else if ( att->IsKind(STANDARD_TYPE(TDataStd_ByteArray)) )
305 {
306 Handle(TDataStd_ByteArray) val = Handle(TDataStd_ByteArray)::DownCast ( att );
307 for ( Standard_Integer j=val->Lower(); j <= val->Upper(); j++ )
308 {
309 if ( j > val->Lower() ) di << ", ";
310 TCollection_AsciiString str ( val->Value(j) );
311 di << str.ToCString();
312 }
313 }
314 else if ( att->IsKind(STANDARD_TYPE(TNaming_NamedShape)) )
7fd59977 315 {
316 Handle(TNaming_NamedShape) val = Handle(TNaming_NamedShape)::DownCast ( att );
317 TopoDS_Shape S = val->Get();
318 di << S.TShape()->DynamicType()->Name();
319 if ( ! S.Location().IsIdentity() ) di << "(located)";
320 }
321
322 return 0;
323}
324
325//=======================================================================
326//function : mtmRemoveDocument
327//purpose : removes a document from the transactions manager
328//=======================================================================
329
330static int mtmRemoveDocument (Draw_Interpretor& di, int n, const char** a)
331{
332 if(sMultiTransactionManager.IsNull()) {
586db386 333 di << "Error : manager is not initialised\n";
7fd59977 334 return 1;
335 }
336 if(n > 1) {
337 Handle(DDocStd_DrawDocument) aDrawDoc =
338 Handle(DDocStd_DrawDocument)::DownCast(Draw::Get(a[1]));
339 if(aDrawDoc.IsNull()) {
586db386 340 di << "Error : wrong document name\n";
7fd59977 341 return 1;
342 }
343 sMultiTransactionManager->RemoveDocument(aDrawDoc->GetDocument());
344 }
345 else {
586db386 346 di << "Error : document name is not defined\n";
7fd59977 347 return 1;
348 }
349 return 0;
350}
351
352//=======================================================================
353//function : MTMCommands
354//purpose :
355//=======================================================================
356
357void DDocStd::MTMCommands(Draw_Interpretor& theCommands)
358{
359 static Standard_Boolean done = Standard_False;
360 if (done) return;
361 done = Standard_True;
362
363 const char* g = "MTM test commands";
364
365 theCommands.Add("mtmCreate",
366 "\t [undo limit] creates new new multiple transactions' manager",
367 __FILE__, mtmCreate, g);
368
369 theCommands.Add("mtmAdd",
370 "\t <document name> adds a document to the transactions' manager",
371 __FILE__, mtmAddDocument, g);
372
373 theCommands.Add("mtmRemove",
374 "\t <document name> removes a document from the transactions' manager",
375 __FILE__, mtmRemoveDocument, g);
376
377 theCommands.Add("mtmOpen",
378 "\t opens new transaction",
379 __FILE__, mtmOpenTransaction, g);
380
381 theCommands.Add("mtmCommit",
382 "\t [<transaction name>] commits last opened transaction",
383 __FILE__, mtmCommitTransaction, g);
384
385 theCommands.Add("mtmAbort",
386 "\t aborts last opened transaction",
387 __FILE__, mtmAbortTransaction, g);
388
389 theCommands.Add("mtmDump",
390 "\t dumps state of the multiple transactions' manager",
391 __FILE__, mtmDump, g);
392
393 theCommands.Add("mtmUndo",
394 "\t undos last transaction",
395 __FILE__, mtmUndo, g);
396
397 theCommands.Add("mtmRedo",
398 "\t redos last transaction",
399 __FILE__, mtmRedo, g);
400
401 theCommands.Add("mtmNestedMode",
402 "\t [0/1] sets nested mode if 1 and usets if 0 (default 0)",
403 __FILE__, mtmNestedMode, g);
404
405 theCommands.Add ("XAttributeValue", "Doc label #attribute: internal command for browser",
406 __FILE__, XAttributeValue, g);
407
408}