forked from KeanW/AcadZMachine
/
AutoCADIONodeMap.cs
398 lines (336 loc) · 8.02 KB
/
AutoCADIONodeMap.cs
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
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using ZLR.VM;
namespace AcadZMachine
{
class AutoCADIONodeMap : IZMachineIO
{
[DllImport(
"AcJsCoreStub.crx", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "acjsInvokeAsync")]
extern static private int acjsInvokeAsync(
string name, string jsonArgs
);
private string _suppliedCommandFile;
private Editor _ed;
private short _curWin = 0;
private bool _showProgress;
private List<string> _non0 = new List<string>();
private Point2d _position = Point2d.Origin;
private string _lastMove = "", _lastLoc = "";
private ushort _curLoc = 0;
const int locIdx = 1;
const int scoIdx = 52;
const int movIdx = 119;
public AutoCADIONodeMap(
Editor ed, bool showProgress = false, string commandFile = null
)
{
_suppliedCommandFile = commandFile;
_ed = ed;
_showProgress = showProgress;
}
public void SetLocation(ushort s)
{
_curLoc = s;
}
public string ReadLine(
string initial, int time, TimedInputCallback callback,
byte[] terminatingKeys, out byte terminator
)
{
if (_non0.Count == 199)
{
_lastLoc = _non0[locIdx];
var sb = new StringBuilder();
sb.AppendFormat(
"{{\"id\":\"{0}\",\"name\":\"{1}\",\"dir\":\"{2}\"}}",
_curLoc, _lastLoc, _lastMove.ToUpper()
);
acjsInvokeAsync("setloc", sb.ToString());
}
terminator = 13;
var prompt =
_non0.Count == 199 && _showProgress ?
String.Format(
"\n<Location: {0}, Score {1}, Moves {2}> ",
_non0[locIdx],
_non0[scoIdx],
_non0[movIdx]
) : "";
var pso = new PromptStringOptions(prompt);
pso.AllowSpaces = true;
var pr = _ed.GetString(pso);
if (pr.Status == PromptStatus.OK && pr.StringResult == "-")
{
pr = _ed.GetString(pso);
}
_lastMove = pr.Status == PromptStatus.OK ? pr.StringResult : "";
_non0.Clear();
return _lastMove;
}
public short ReadKey(
int time, TimedInputCallback callback, CharTranslator translator
)
{
/*
short ch;
do
{
ConsoleKeyInfo info = Console.ReadKey();
ch = translator(info.KeyChar);
} while (ch == 0);
*/
_ed.WriteMessage("\nReadKey called.\n");
return 0;
}
public void PutCommand(string command)
{
// nada
}
public void PutChar(char ch)
{
PutString(ch.ToString());
}
public void PutString(string str)
{
if (_curWin == 0)
//_ed.WriteMessage("{0}[{1}]", str, _curWin);
_ed.WriteMessage(str);
else
_non0.Add(str);
}
public void PutTextRectangle(string[] lines)
{
foreach (string str in lines)
PutString(str);
}
public bool Buffering
{
get { return false; }
set { /* nada */ }
}
public bool Transcripting
{
get { return false; }
set { /* nada */ }
}
public void PutTranscriptChar(char ch)
{
// not implemented
}
public void PutTranscriptString(string str)
{
// not implemented
}
public System.IO.Stream OpenSaveFile(int size)
{
// not implemented
return null;
}
public System.IO.Stream OpenRestoreFile()
{
// not implemented
return null;
}
public System.IO.Stream OpenAuxiliaryFile(
string name, int size, bool writing
)
{
// not implemented
return null;
}
public System.IO.Stream OpenCommandFile(bool writing)
{
string filename;
if (_suppliedCommandFile != null)
{
filename = _suppliedCommandFile;
_suppliedCommandFile = null;
}
else
{
do
{
var prompt =
String.Format(
"Enter the name of a command file to {0}",
writing ? "record" : "play back"
);
filename = _ed.GetFilename(prompt, !writing);
if (String.IsNullOrWhiteSpace(filename))
return null;
if (writing)
{
// If the file exists, prompt to overwrite it
if (File.Exists(filename))
{
var prompt2 =
String.Format("\"{0}\" exists. Are you sure", filename);
var overWrite = _ed.GetYesOrNo(prompt2, false);
if (overWrite.HasValue && overWrite.Value)
break;
}
else
break;
}
else
{
// The file must already exist
if (File.Exists(filename))
break;
}
}
while (true);
}
return new FileStream(filename,
writing ? FileMode.Create : FileMode.Open,
writing ? FileAccess.Write : FileAccess.Read
);
}
public void SetTextStyle(TextStyle style)
{
// nada
}
public void SplitWindow(short lines)
{
// nada
}
public void SelectWindow(short num)
{
_curWin = num;
}
public void EraseWindow(short num)
{
// nada
}
public void EraseLine()
{
// nada
}
public void MoveCursor(short x, short y)
{
// nada
}
public void GetCursorPos(out short x, out short y)
{
x = 1;
y = 1;
}
public void SetColors(short fg, short bg)
{
// nada
}
public short SetFont(short num)
{
return 0;
}
public void PlaySoundSample(
ushort number, SoundAction action, byte volume,
byte repeats, SoundFinishedCallback callback
)
{
// nada
}
public void PlayBeep(bool highPitch)
{
// nada
}
public bool ForceFixedPitch
{
get { return false; }
set { /* nada */ }
}
public bool BoldAvailable
{
get { return false; }
}
public bool ItalicAvailable
{
get { return false; }
}
public bool FixedPitchAvailable
{
get { return false; }
}
public bool VariablePitchAvailable
{
get { return false; }
}
public bool ScrollFromBottom
{
get { return false; }
set { /* nada */ }
}
public bool GraphicsFontAvailable
{
get { return false; }
}
public bool TimedInputAvailable
{
get { return false; }
}
public bool SoundSamplesAvailable
{
get { return false; }
}
public byte WidthChars
{
get { return 80; }
}
public short WidthUnits
{
get { return 80; }
}
public byte HeightChars
{
get { return 25; }
}
public short HeightUnits
{
get { return 25; }
}
public byte FontHeight
{
get { return 1; }
}
public byte FontWidth
{
get { return 1; }
}
public event EventHandler SizeChanged
{
add { /* nada */ }
remove { /* nada */ }
}
public bool ColorsAvailable
{
get { return false; }
}
public byte DefaultForeground
{
get { return 9; }
}
public byte DefaultBackground
{
get { return 2; }
}
public UnicodeCaps CheckUnicode(char ch)
{
return UnicodeCaps.CanInput | UnicodeCaps.CanPrint;
}
public bool DrawCustomStatusLine(
string location, short hoursOrScore, short minsOrTurns, bool useTime
)
{
return false;
}
}
}