-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcad_compare_2_files.html
More file actions
388 lines (363 loc) · 16.4 KB
/
Copy pathcad_compare_2_files.html
File metadata and controls
388 lines (363 loc) · 16.4 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
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
<html>
<head>
<link rel="icon" href="./favicon.ico" />
<link rel="stylesheet" type="text/css" href="./global.css" />
<link rel="stylesheet" type="text/css" href="./iconfont/iconfont.css" />
<link rel="stylesheet" type="text/css" href="./iconfont/iconfont2.css" />
<style>
body {
font-size: 0;
}
#myCanvas1,
#myCanvas2 {
width: calc(50% - 1px);
height: 100%;
margin: 0;
padding: 0;
display: inline-block;
overflow: hidden;
}
.split {
display: inline-block;
width: 1px;
height: 100%;
margin: 0;
padding: 0;
background-color: #eee;
}
/* Upload button styles – keep in sync with upload_dxf.html */
.upload-btn {
margin-top: 2em;
display: inline-block;
}
.upload-btn button {
width: 0.1px;
height: 0.1px;
opacity: 0;
}
.upload-btn label {
color: #353535;
background: gray;
border: 1px solid #353535;
border-radius: 3px;
/*transition: ease 0.2s background-color;*/
font-size: 1rem;
font-weight: 700;
text-overflow: ellipsis;
white-space: nowrap;
cursor: pointer;
display: inline-block;
overflow: hidden;
padding: 0.625rem 1.25rem;
}
.upload-btn label:hover {
background: #DDD;
}
.upload-btn svg {
width: 1em;
height: 1em;
vertical-align: middle;
fill: currentColor;
margin-top: -0.25em;
margin-right: 0.25em;
}
#syncCamera {
position: absolute;
top: 16px;
left: 50%;
transform: translateX(-50%);
min-width: 32px;
min-height: 32px;
padding: 4px 6px;
font-size: 18px; /* icon size */
line-height: 1;
border-radius: 6px;
border: 1px solid #409effaa;
background-color: rgba(64, 158, 255, 0.4);
color: #fff;
cursor: pointer;
}
</style>
</head>
<body>
<div id="app" class="app">
<div id="myCanvas1"></div>
<div class="split"></div>
<div id="myCanvas2"></div>
</div>
<!-- Use an icon-style button to indicate synced/unsynced state (unlocked = unsynced, locked = synced) -->
<button id="syncCamera" title="Click to toggle camera sync">🔓</button>
<!-- Left upload area, centered over left viewer -->
<div style="position: absolute; top: 10px; left: 25%; transform: translateX(-50%); opacity: 0.6; pointer-events: none;">
<div style="pointer-events: auto;">
<div id="currentFileNameLeft" style="margin-top: 4px; color: #eeeeee; font-size: 14px; text-align: center;"></div>
<div class="upload-btn" id="uploadBtnLeft" style="text-align: center;">
<button id="uploadModelFileLeft" type="button">Click to upload dwg/dxf/pdf file(s)</button>
<label for="uploadModelFileLeft" title="Choose one or more dwg/dxf/pdf files to load into left viewer">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17">
<path
d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z">
</path>
</svg>
<span>Upload left dwg/dxf/pdf</span>
</label>
</div>
</div>
</div>
<!-- Right upload area, centered over right viewer -->
<div style="position: absolute; top: 10px; left: 75%; transform: translateX(-50%); opacity: 0.6; pointer-events: none;">
<div style="pointer-events: auto;">
<div id="currentFileNameRight" style="margin-top: 4px; color: #eeeeee; font-size: 14px; text-align: center;"></div>
<div class="upload-btn" id="uploadBtnRight" style="text-align: center;">
<button id="uploadModelFileRight" type="button">Click to upload dwg/dxf/pdf file(s)</button>
<label for="uploadModelFileRight" title="Choose one or more dwg/dxf/pdf files to load into right viewer">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17">
<path
d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z">
</path>
</svg>
<span>Upload right dwg/dxf/pdf</span>
</label>
</div>
</div>
</div>
<script>
// Auto-detect environment and set import map for x-viewer packages
(function () {
const wl = window.location;
const isDevEnv = false;
const resolvePath = (pkgName) =>
isDevEnv
? `../packages/${pkgName}/dist/index.esm.js`
: `https://cdn.jsdelivr.net/npm/@x-viewer/${pkgName}@latest/dist/index.esm.js`;
const corePath = resolvePath("core");
const pluginsPath = resolvePath("plugins");
const uiPath = resolvePath("ui");
const importMap = document.createElement("script");
importMap.type = "importmap";
importMap.textContent = JSON.stringify({
imports: {
"@x-viewer/core": corePath,
"@x-viewer/plugins": pluginsPath,
"@x-viewer/ui": uiPath,
},
});
document.head.appendChild(importMap);
})();
</script>
<script type="module">
import {
Viewer2d,
THREE,
} from "@x-viewer/core";
import {
AxisGizmoPlugin,
BottomBarPlugin,
Viewer2dToolbarPlugin,
LayerManagerPlugin,
ToolbarMenuId,
LocalDxfUploader,
MeasurementPlugin,
ScreenshotPlugin,
Settings2dPlugin,
} from "@x-viewer/plugins";
const language = "en"; // "en" or "zh"
const viewerCfg1 = {
containerId: "myCanvas1",
language,
enableSpinner: true,
enableProgressBar: true,
enableLayoutBar: true,
enableLocalCache: false,
};
const viewerCfg2 = { ...viewerCfg1, containerId: "myCanvas2" };
const viewer1 = new Viewer2d(viewerCfg1);
const viewer2 = new Viewer2d(viewerCfg2);
window.viewer1 = viewer1;
window.viewer2 = viewer2;
// Set fonts for both viewers so they can display dwg/dxf correctly
const fontFiles = ["./libs/fonts/hztxt.shx", "./libs/fonts/simplex.shx"];
await viewer1.setFont(fontFiles);
await viewer2.setFont(fontFiles);
// Basic plugins on both viewers
new AxisGizmoPlugin(viewer1, { ignoreZAxis: true });
new BottomBarPlugin(viewer1);
new MeasurementPlugin(viewer1);
new Settings2dPlugin(viewer1, { language, visible: false });
new AxisGizmoPlugin(viewer2, { ignoreZAxis: true });
new BottomBarPlugin(viewer2);
new MeasurementPlugin(viewer2);
new Settings2dPlugin(viewer2, { language, visible: false });
const menuConfig1 = {
[ToolbarMenuId.Fullscreen]: {
visible: false,
},
[ToolbarMenuId.Screenshot]: {
visible: false,
},
[ToolbarMenuId.Markup]: {
visible: false,
},
[ToolbarMenuId.MarkupVisibility]: {
visible: false,
},
[ToolbarMenuId.Layers]: {
onActive: () => {
console.log("[Toolbar]", "Activate Layers");
if (!window.layerManager1) {
window.layerManager1 = new LayerManagerPlugin(window.viewer1);
window.layerManager1.addEventListener("visibilitychange", (visible) => {
const isActive = toolbar1.isActive(ToolbarMenuId.Layers);
// do this check to avoid recursive call
if (isActive !== visible) {
toolbar1.setActive(ToolbarMenuId.Layers, visible);
}
});
}
window.layerManager1.setVisible(true);
},
onDeactive: () => {
console.log("[Toolbar]", "Deactivate Layers");
window.layerManager1.setVisible(false);
},
},
};
const menuConfig2 = {
[ToolbarMenuId.Fullscreen]: {
visible: false,
},
[ToolbarMenuId.Screenshot]: {
visible: false,
},
[ToolbarMenuId.Markup]: {
visible: false,
},
[ToolbarMenuId.MarkupVisibility]: {
visible: false,
},
[ToolbarMenuId.Layers]: {
onActive: () => {
console.log("[Toolbar]", "Activate Layers");
if (!window.layerManager2) {
window.layerManager2 = new LayerManagerPlugin(window.viewer2);
window.layerManager2.addEventListener("visibilitychange", (visible) => {
const isActive = toolbar2.isActive(ToolbarMenuId.Layers);
// do this check to avoid recursive call
if (isActive !== visible) {
toolbar2.setActive(ToolbarMenuId.Layers, visible);
}
});
}
window.layerManager2.setVisible(true);
},
onDeactive: () => {
console.log("[Toolbar]", "Deactivate Layers");
window.layerManager2.setVisible(false);
},
},
};
const toolbar1 = new Viewer2dToolbarPlugin(viewer1, { language, menuConfig: menuConfig1 });
const toolbar2 = new Viewer2dToolbarPlugin(viewer2, { language, menuConfig: menuConfig2 });
window.toolbar1 = toolbar1;
window.toolbar2 = toolbar2;
// Uploaders for left and right viewers (similar to upload_dxf.html)
const pdfWorker = "libs/pdf/pdf.worker.min.js";
const uploader1 = new LocalDxfUploader(viewer1);
uploader1.setPdfWorker(pdfWorker);
uploader1.onSuccess = (event) => {
if (event && event.fileName) {
const nameDiv = document.getElementById("currentFileNameLeft");
if (nameDiv) {
if (nameDiv.textContent) {
nameDiv.textContent += " | ";
}
nameDiv.textContent += event.fileName;
}
}
};
uploader1.onError = (event) => {
if (event && event.fileName) {
alert(`Failed to load ${event.fileName}\n\r${event.error}`);
}
};
document.getElementById("uploadModelFileLeft").onclick = function () {
uploader1.openFileBrowserToUpload();
};
const uploader2 = new LocalDxfUploader(viewer2);
uploader2.setPdfWorker(pdfWorker);
uploader2.onSuccess = (event) => {
if (event && event.fileName) {
const nameDiv = document.getElementById("currentFileNameRight");
if (nameDiv) {
if (nameDiv.textContent) {
nameDiv.textContent += " | ";
}
nameDiv.textContent += event.fileName;
}
}
};
uploader2.onError = (event) => {
if (event && event.fileName) {
alert(`Failed to load ${event.fileName}\n\r${event.error}`);
}
};
document.getElementById("uploadModelFileRight").onclick = function () {
uploader2.openFileBrowserToUpload();
};
// Toggle sync of camera/viewport between two Viewer2d instances
const syncCameraBtn = document.getElementById("syncCamera");
let enableSyncCamera = false;
let syncCamera = false;
const syncCameraControls = (srcControls, tarControls, targetZoom) => {
const focalOffset = new THREE.Vector3();
const eye = new THREE.Vector3();
const target = new THREE.Vector3();
srcControls.getPosition(eye);
srcControls.getTarget(target);
srcControls.getFocalOffset(focalOffset);
tarControls.setLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z);
tarControls.zoomTo(targetZoom);
tarControls.dispatchEvent({ type: "control" });
};
// Attach control/update listeners once, mimic DxfCompareHelper.initSyncCameraEvent
const control1 = viewer1.getCameraManager().cameraControls;
const control2 = viewer2.getCameraManager().cameraControls;
let operationTriggeredByControl2 = false;
control1.addEventListener("control", () => {
operationTriggeredByControl2 = false;
});
control1.addEventListener("update", () => {
if (!syncCamera || operationTriggeredByControl2) {
return;
}
syncCameraControls(control1, control2, viewer1.camera.zoom);
});
control2.addEventListener("control", () => {
operationTriggeredByControl2 = true;
});
control2.addEventListener("update", () => {
if (!syncCamera || !operationTriggeredByControl2) {
return;
}
syncCameraControls(control2, control1, viewer2.camera.zoom);
});
syncCameraBtn.onclick = function () {
enableSyncCamera = !enableSyncCamera;
// just toggle flag; actual sync happens in control/update listeners
syncCamera = enableSyncCamera;
// when turning on, align right viewer to left viewer once
if (enableSyncCamera) {
syncCameraControls(control1, control2, viewer1.camera.zoom);
}
// Use lock icon to indicate state: locked = synced, unlocked = unsynced
if (enableSyncCamera) {
syncCameraBtn.textContent = "🔒";
syncCameraBtn.title = "Click to unsync cameras";
} else {
syncCameraBtn.textContent = "🔓";
syncCameraBtn.title = "Click to sync cameras";
}
};
</script>
</body>
</html>