-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathEmbeded_CAD.html
More file actions
326 lines (293 loc) · 10.9 KB
/
Embeded_CAD.html
File metadata and controls
326 lines (293 loc) · 10.9 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<title>BREP API Example: Embeded CAD</title>
<link rel="stylesheet" href="./example.css" />
<style>
#cad-host {
margin-top: 10px;
border: 1px solid #dde3f5;
border-radius: 10px;
height: 760px;
overflow: hidden;
background: #fff;
}
#cad-status {
margin-top: 10px;
font-size: 13px;
color: #384359;
}
.control-grid input[type="text"],
.control-grid select {
width: 100%;
height: 36px;
border: 1px solid #ccd5ec;
border-radius: 8px;
padding: 0 8px;
background: #fff;
}
</style>
</head>
<body>
<main>
<header class="example-header">
<div>
<h1>Embeded CAD</h1>
<p class="lead">
Mounts the full iframe-based <code>CadEmbed</code> API and controls history/model actions from the host page.
</p>
</div>
<nav class="example-nav">
<a href="./index.html">All Examples</a>
<a href="../index.html">BREP CAD App</a>
<a href="https://github.com/mmiscool/BREP/blob/master/apiExamples/Embeded_CAD.html" target="_blank" rel="noopener noreferrer">Source on GitHub</a>
</nav>
</header>
<div class="row">
<button id="btn-create">Create CAD Iframe</button>
<button id="btn-destroy" disabled>Destroy CAD</button>
<button id="btn-state" disabled>Get State</button>
<button id="btn-history" disabled>Get History JSON</button>
<button id="btn-sample" disabled>Load Sample Cube</button>
<button id="btn-reset" disabled>Reset Model</button>
<button id="btn-css" disabled>Apply CSS</button>
</div>
<div class="control-grid">
<label class="toggle" for="viewer-only">
<input id="viewer-only" type="checkbox" />
Viewer-only Mode
</label>
<label class="toggle" for="sidebar-expanded">
<input id="sidebar-expanded" type="checkbox" checked />
Sidebar Expanded
</label>
<div>
<label for="model-path">Model Path (optional)</label>
<input id="model-path" type="text" placeholder="examples/part-name" />
</div>
<div>
<label for="model-source">Model Source</label>
<select id="model-source">
<option value="local" selected>local</option>
<option value="github">github</option>
<option value="mounted">mounted</option>
</select>
</div>
<div>
<label for="model-repo">Repo / Mount ID</label>
<input id="model-repo" type="text" placeholder="owner/repo or mount-id" />
</div>
<div>
<label for="model-branch">Branch</label>
<input id="model-branch" type="text" placeholder="main" />
</div>
</div>
<label for="css-input">Custom CSS injected into iframe:</label>
<textarea id="css-input">#main-toolbar {
background: rgba(8, 14, 24, 0.92) !important;
}</textarea>
<div id="cad-status">CAD iframe not created.</div>
<div id="cad-host"></div>
<hr class="divider" />
<h2>State</h2>
<pre id="state-output">(No state yet)</pre>
<h2>History JSON</h2>
<pre id="history-output">(No history exported yet)</pre>
</main>
<script type="module">
import { CadEmbed } from '../dist-kernel/brep-kernel.js';
const btnCreate = document.getElementById('btn-create');
const btnDestroy = document.getElementById('btn-destroy');
const btnState = document.getElementById('btn-state');
const btnHistory = document.getElementById('btn-history');
const btnSample = document.getElementById('btn-sample');
const btnReset = document.getElementById('btn-reset');
const btnCss = document.getElementById('btn-css');
const viewerOnlyInput = document.getElementById('viewer-only');
const sidebarExpandedInput = document.getElementById('sidebar-expanded');
const modelPathInput = document.getElementById('model-path');
const modelSourceInput = document.getElementById('model-source');
const modelRepoInput = document.getElementById('model-repo');
const modelBranchInput = document.getElementById('model-branch');
const cssInput = document.getElementById('css-input');
const statusEl = document.getElementById('cad-status');
const hostEl = document.getElementById('cad-host');
const stateOutput = document.getElementById('state-output');
const historyOutput = document.getElementById('history-output');
let cad = null;
const sampleCubeHistory = {
features: [
{
type: 'Primitive Cube',
inputParams: {
id: 'sample_cube_1',
sizeX: 24,
sizeY: 18,
sizeZ: 14,
transform: {
position: [0, 0, 0],
rotationEuler: [0, 0, 0],
scale: [1, 1, 1],
},
boolean: {
targets: [],
operation: 'NONE',
},
},
persistentData: {},
timestamp: null,
},
],
idCounter: 1,
expressions: '//Examples:\nx = 10 + 6;\ny = x * 2;',
pmiViews: [],
metadata: {},
assemblyConstraints: [],
assemblyConstraintIdCounter: 0,
};
const setStatus = (text) => {
statusEl.textContent = text;
};
const setButtons = (mounted) => {
btnCreate.disabled = mounted;
btnDestroy.disabled = !mounted;
btnState.disabled = !mounted;
btnHistory.disabled = !mounted;
btnSample.disabled = !mounted;
btnReset.disabled = !mounted;
btnCss.disabled = !mounted;
viewerOnlyInput.disabled = mounted;
};
const currentModelRequest = () => {
const modelPath = String(modelPathInput.value || '').trim();
if (!modelPath) return null;
const source = String(modelSourceInput.value || 'local').trim() || 'local';
const repoFull = String(modelRepoInput.value || '').trim();
const branch = String(modelBranchInput.value || '').trim();
const out = { modelPath, source };
if (repoFull) out.repoFull = repoFull;
if (branch) out.branch = branch;
return out;
};
const renderState = (state, label = 'State updated') => {
stateOutput.textContent = JSON.stringify(state || {}, null, 2);
const features = Number(state?.featureCount || 0);
const modelName = state?.model?.name || '(unsaved/new)';
setStatus(`${label}. Features: ${features}. Model: ${modelName}`);
};
const createCad = async () => {
if (cad) return;
const initialModel = currentModelRequest();
cad = new CadEmbed({
mountTo: hostEl,
viewerOnlyMode: viewerOnlyInput.checked,
sidebarExpanded: sidebarExpandedInput.checked,
cssText: cssInput.value,
initialModel,
onReady: (state) => {
renderState(state, 'CAD ready');
},
onHistoryChanged: (state) => {
renderState(state, `History changed (${state?.reason || 'update'})`);
},
});
await cad.mount();
setButtons(true);
const state = await cad.getState();
renderState(state, 'CAD iframe mounted');
};
const destroyCad = async () => {
if (!cad) return;
await cad.destroy();
cad = null;
setButtons(false);
setStatus('CAD iframe destroyed.');
stateOutput.textContent = '(No state yet)';
historyOutput.textContent = '(No history exported yet)';
};
const refreshState = async () => {
if (!cad) return;
const state = await cad.getState();
renderState(state, 'State fetched');
};
const exportHistory = async () => {
if (!cad) return;
const json = await cad.getPartHistoryJSON();
historyOutput.textContent = json || '(History is empty)';
setStatus(`History exported (${json ? json.length : 0} chars).`);
};
const loadSampleCube = async () => {
if (!cad) return;
await cad.setPartHistory(sampleCubeHistory);
await refreshState();
setStatus('Sample cube history loaded.');
};
const resetModel = async () => {
if (!cad) return;
await cad.reset();
await refreshState();
setStatus('Model reset complete.');
};
const applyCss = async () => {
if (!cad) return;
await cad.setCss(cssInput.value);
await cad.setSidebarExpanded(sidebarExpandedInput.checked);
setStatus('Custom CSS and sidebar state applied.');
};
btnCreate.addEventListener('click', () => {
createCad().catch((error) => {
console.error(error);
setStatus(`Failed to create CAD iframe: ${error?.message || String(error)}`);
});
});
btnDestroy.addEventListener('click', () => {
destroyCad().catch((error) => {
console.error(error);
setStatus(`Failed to destroy CAD iframe: ${error?.message || String(error)}`);
});
});
btnState.addEventListener('click', () => {
refreshState().catch((error) => {
console.error(error);
setStatus(`Failed to get state: ${error?.message || String(error)}`);
});
});
btnHistory.addEventListener('click', () => {
exportHistory().catch((error) => {
console.error(error);
setStatus(`Failed to export history: ${error?.message || String(error)}`);
});
});
btnSample.addEventListener('click', () => {
loadSampleCube().catch((error) => {
console.error(error);
setStatus(`Failed to load sample history: ${error?.message || String(error)}`);
});
});
btnReset.addEventListener('click', () => {
resetModel().catch((error) => {
console.error(error);
setStatus(`Failed to reset model: ${error?.message || String(error)}`);
});
});
btnCss.addEventListener('click', () => {
applyCss().catch((error) => {
console.error(error);
setStatus(`Failed to apply CSS: ${error?.message || String(error)}`);
});
});
sidebarExpandedInput.addEventListener('change', () => {
if (!cad) return;
cad.setSidebarExpanded(sidebarExpandedInput.checked)
.catch((error) => {
console.error(error);
setStatus(`Failed to set sidebar state: ${error?.message || String(error)}`);
});
});
setButtons(false);
</script>
</body>
</html>