/
simple.js
187 lines (167 loc) · 4.13 KB
/
simple.js
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
/**
* Simple API, mainly designed for use in the ECMAScript console.
*/
/**
* \defgroup ecma_simple QCAD Simple API
* \ingroup ecma_scripts
*
* \brief This module defines the QCAD Simple API in ECMAScript.
* The QCAD Simple API is meant to simplify common tasks such as adding entities.
* To use the QCAD Simple API in your scripts, include the file simple.js in your
* script:
*
* \code
* include("simple.js");
* \endcode
*/
include("library.js");
include("input.js");
// internal:
__simpleUseOp = false;
__simpleOp = undefined;
__simpleDoc = undefined;
__simpleDi = undefined;
include("simple_create.js");
include("simple_info.js");
include("simple_input.js");
include("simple_modify.js");
include("simple_transaction.js");
include("simple_view.js");
/**
* Returns a pointer to the main application window (RMainWindowQt).
* \ingroup ecma_simple
*/
function getMainWindow() {
return RMainWindowQt.getMainWindow();
}
/**
* Returns the current RDocumentInterface or undefined.
* \ingroup ecma_simple
*/
function getDocumentInterface() {
var appWin = getMainWindow();
if (isNull(appWin)) {
qWarning("QCAD SIMPLE API: no main window");
return undefined;
}
return appWin.getDocumentInterface();
}
/**
* Returns the current RDocument or undefined.
* \ingroup ecma_simple
*/
function getDocument() {
var di = getDocumentInterface();
if (isNull(di)) {
qWarning("QCAD SIMPLE API: no document");
return undefined;
}
return di.getDocument();
}
/**
* Returns the current or last active RGraphicsView.
* \ingroup ecma_simple
*/
function getGraphicsView() {
var di = getDocumentInterface();
if (isNull(di)) {
return undefined;
}
return di.getLastKnownViewWithFocus();
}
/**
* \deprecated
*/
function createOffScreenDocument() {
return createDocument();
}
/**
* Creates an (off-screen) document. Such a document can be filled with
* objects and saved to disk, pasted into the current document, etc.
* \ingroup ecma_simple
*
* \return new empty RDocument with default settings
*/
function createDocument() {
return new RDocument(new RMemoryStorage(), new RSpatialIndexSimple());
}
/**
* Creates an (off-screen) document with a document interface that can be used
* to import / export to / from files or graphics scenes and views.
* \ingroup ecma_simple
*
* \return new empty RDocumentInterface with default settings
*/
function createDocumentInterface() {
return new RDocumentInterface(new RDocument(new RMemoryStorage(), createSpatialIndex()));
}
/**
* Disables the main application window to prevent user input.
* \ingroup ecma_simple
*/
function disableInput() {
getMainWindow().enabled = false;
return getMainWindow().enabled;
}
/**
* Enables the main application window to prevent user input.
* \ingroup ecma_simple
*/
function enableInput() {
getMainWindow().enabled = true;
return getMainWindow().enabled;
}
/**
* Returns true if user input is enabled.
* \ingroup ecma_simple
*/
function isInputEnabled() {
return getMainWindow().enabled;
}
/**
* Prints a warning to stdout.
* \ingroup ecma_simple
*/
function warning(msg) {
if (isFunction(warning.handler)) {
return warning.handler(msg);
}
print("WARNING: " + msg);
}
/**
* Keeps the user interface up to date during long operations.
* User input must be disabled using disableInput before calling update.
* \ingroup ecma_simple
*/
function update() {
if (isInputEnabled()) {
warning("User input must be disabled before calling update");
return false;
}
var ret = true;
try {
QCoreApplication.processEvents(QEventLoop.ExcludeSocketNotifiers, 100);
} catch (e) {
qWarning("error: " + e);
ret = false;
}
return ret;
}
/**
* Sleeps for the indicated time in milliseconds.
* \ingroup ecma_simple
*/
function sleep(d) {
if (isInputEnabled()) {
warning("User input must be disabled before calling sleep");
return false;
}
var t = new QTime();
t.start();
while (t.elapsed() < d) {
if (!update()) {
return false;
}
}
return true;
}