summaryrefslogtreecommitdiff
path: root/src/DDF/DDF_TransactionCommands.cxx
blob: 102f8d58b9d1fd872113b7da946b8e52334be54b (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
// File:	DDF_TransactionCommands.cxx
//      	---------------------------
// Author:	DAUTRY Philippe
//		<fid@fox.paris1.matra-dtv.fr>
// Copyright:	Matra Datavision 1997
// Version:	0.0
// History:	Version	Date		Purpose
//		0.0	Sep 30 1997	Creation



#include <DDF.hxx>
#include <DDF_Data.hxx>
#include <DDF_Transaction.hxx>
#include <DDF_TransactionStack.hxx>

#include <Draw.hxx>
#include <Draw_Appli.hxx>
#include <Draw_Drawable3D.hxx>
#include <Draw_Interpretor.hxx>

#include <TDF_Data.hxx>
#include <TDF_Delta.hxx>
#include <TDF_Transaction.hxx>

static DDF_TransactionStack DDF_TStack;
static Handle(TDF_Delta)    DDF_LastDelta;

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Transaction commands
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



//=======================================================================
//function : OpenTran
//purpose  : Opens a transaction
//=======================================================================

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

  Handle(TDF_Data) DF;
  if (DDF::GetDF (a[1], DF)) {
    Handle(DDF_Transaction) tr = new DDF_Transaction(DF);
    di<<"Open transaction # "<<tr->Open()<<" # "<<DF->Transaction()<<"\n";
    DDF_TStack.Push(tr);
  }
  return 0;
}


//=======================================================================
//function : AbortTran
//purpose  : Aborts a transaction
//=======================================================================

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

  Handle(TDF_Data) DF;
  if (DDF::GetDF (a[1], DF)) {
    if (DF->Transaction () > 0) {
      Handle(DDF_Transaction) tr = DDF_TStack.Top();
      di<<"Abort transaction # "<<tr->Transaction()<<" # "<<DF->Transaction()<<"\n";
      tr->Abort();
      DDF_TStack.Pop();
    }
    else {
      di<<"DDF_BasicCommands::AbortTran - No more transaction to abort"<<"\n";
    }
  }
  return 0;
}


//=======================================================================
//function : CommitTran
//purpose  : Commits a transaction
//=======================================================================

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

  Handle(TDF_Data) DF;
  if (DDF::GetDF (a[1], DF)) {
    if (DF->Transaction () > 0) {
      Handle(DDF_Transaction) tr = DDF_TStack.Top();
      di<<"Commit transaction # "<<tr->Transaction()<<" # "<<DF->Transaction()<<"\n";
      Standard_Boolean withDelta = Standard_False;
      if (n > 2) withDelta = (atoi(a[2]) != 0);
      DDF_LastDelta = tr->Commit(withDelta);
      DDF_TStack.Pop();
    }
    else {
      di<<"DDF_BasicCommands::CommitTran - No more transaction to commit"<<"\n";
    }
  }
  return 0;
}


//=======================================================================
//function : CurrentTran
//purpose  : Current transaction number.
//=======================================================================

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

  Handle(TDF_Data) DF;
  if (DDF::GetDF (a[1], DF)) {
    di<<"# "<<DF->Transaction()<<"\n";
    if (!DDF_TStack.IsEmpty())
      if (DF->Transaction() != DDF_TStack.Top()->Transaction())
	di<<"Transaction object said # "<<DDF_TStack.Top()->Transaction()<<"\n";
  }
  return 0;

}




// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Delta commands
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



//=======================================================================
//function : Undo
//purpose  : Undo
//=======================================================================

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

  Handle(TDF_Data) DF;
  if (DDF::GetDF (a[1], DF)) {
    Standard_Boolean withDelta = Standard_False;
    if (n > 2) withDelta = (atoi(a[2]) != 0);
    if (!DDF_LastDelta.IsNull()) {
      if (DF->IsApplicable(DDF_LastDelta)) {
	Handle(TDF_Delta) tmp = DF->Undo(DDF_LastDelta,withDelta);
	DDF_LastDelta = tmp;
      }
      else {
	di<<"Undo not applicable HERE and NOW."<<"\n";
	return 1;
      }
    }
    else {
      di<<"No undo to apply."<<"\n";
      return 1;
    }
  }
  else {
    di<<"Unknown DF."<<"\n";
    return 1;
  }
  return 0;
}


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




//=======================================================================
//function : TransactionCommands
//purpose  : 
//=======================================================================

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

  const char* g = "DF transaction and undo commands";

  // Transaction :
  // +++++++++++++
  theCommands.Add
    ("OpenTran",
     "Opens a transaction on a DF: OpenTran dfname",
     __FILE__, OpenTran, g);

  theCommands.Add
    ("AbortTran",
     "Aborts a transaction on a DF: AbortTran dfname",
     __FILE__, AbortTran, g);

  theCommands.Add
    ("CommitTran",
     "Commits a transaction on a DF with/without delta generation : CommitTran dfname [withDelta]",
     __FILE__, CommitTran, g);

  theCommands.Add
    ("CurrentTran",
     "Returns the current transaction number on a DF : CurrentTran dfname",
     __FILE__, CurrentTran, g);

  // Undo :
  // ++++++
  theCommands.Add
    ("DFUndo",
     " Undos last DF commit modifications: Undo dfname [withDelta]",
     __FILE__, Undo, g);


}