mirror of
https://github.com/yeicor-3d/yet-another-cad-viewer.git
synced 2025-12-20 06:27:04 +01:00
fixed tool/selection init, and some bounding box issues
This commit is contained in:
@@ -4,9 +4,9 @@ export const settings = {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
// new URL('../../assets/fox.glb', import.meta.url).href,
|
// new URL('../../assets/fox.glb', import.meta.url).href,
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
new URL('../../assets/logo.glb', import.meta.url).href,
|
// new URL('../../assets/logo.glb', import.meta.url).href,
|
||||||
// Websocket URLs automatically listen for new models from the python backend
|
// Websocket URLs automatically listen for new models from the python backend
|
||||||
// "ws://192.168.1.132:32323/"
|
"ws://192.168.1.132:32323/"
|
||||||
],
|
],
|
||||||
displayLoadingEveryMs: 1000, /* How often to display partially loaded models */
|
displayLoadingEveryMs: 1000, /* How often to display partially loaded models */
|
||||||
checkServerEveryMs: 100, /* How often to check for a new server */
|
checkServerEveryMs: 100, /* How often to check for a new server */
|
||||||
|
|||||||
@@ -180,9 +180,9 @@ function toggleHighlightNextSelection() {
|
|||||||
|
|
||||||
function toggleShowBoundingBox() {
|
function toggleShowBoundingBox() {
|
||||||
showBoundingBox.value = !showBoundingBox.value;
|
showBoundingBox.value = !showBoundingBox.value;
|
||||||
|
updateBoundingBox();
|
||||||
}
|
}
|
||||||
|
|
||||||
let viewerFound = false
|
|
||||||
let firstLoad = true;
|
let firstLoad = true;
|
||||||
let hasListeners = false;
|
let hasListeners = false;
|
||||||
let cameraChangeWaiting = false;
|
let cameraChangeWaiting = false;
|
||||||
@@ -205,41 +205,29 @@ let onCameraChange = () => {
|
|||||||
};
|
};
|
||||||
setTimeout(waitingHandler, 100); // Wait for the camera to stop moving
|
setTimeout(waitingHandler, 100); // Wait for the camera to stop moving
|
||||||
};
|
};
|
||||||
watch(() => props.viewer, (viewer) => {
|
let onViewerReady = (viewer) => {
|
||||||
console.log('Viewer changed', viewer)
|
|
||||||
if (!viewer) return;
|
if (!viewer) return;
|
||||||
// props.viewer.elem may not yet be available, so we need to wait for it
|
// props.viewer.elem may not yet be available, so we need to wait for it
|
||||||
viewer.onElemReady((elem) => {
|
viewer.onElemReady((elem) => {
|
||||||
if (viewerFound) return;
|
|
||||||
viewerFound = true;
|
|
||||||
if (hasListeners) return;
|
if (hasListeners) return;
|
||||||
hasListeners = true;
|
hasListeners = true;
|
||||||
elem.addEventListener('mouseup', selectionListener);
|
elem.addEventListener('mouseup', selectionListener);
|
||||||
elem.addEventListener('mousedown', selectionMoveListener); // Avoid clicking when dragging
|
elem.addEventListener('mousedown', selectionMoveListener); // Avoid clicking when dragging
|
||||||
elem.addEventListener('load', () => {
|
elem.addEventListener('load', () => {
|
||||||
console.log('Model loaded')
|
|
||||||
if (firstLoad) {
|
if (firstLoad) {
|
||||||
toggleShowBoundingBox();
|
toggleShowBoundingBox();
|
||||||
firstLoad = false;
|
firstLoad = false;
|
||||||
}
|
} else {
|
||||||
updateBoundingBox();
|
updateBoundingBox();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
console.log(elem)
|
|
||||||
if (elem.loaded) {
|
|
||||||
console.log('Model already loaded')
|
|
||||||
if (firstLoad) {
|
|
||||||
toggleShowBoundingBox();
|
|
||||||
firstLoad = false;
|
|
||||||
}
|
|
||||||
updateBoundingBox();
|
|
||||||
}
|
|
||||||
elem.addEventListener('camera-change', onCameraChange);
|
elem.addEventListener('camera-change', onCameraChange);
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
|
if (props.viewer) onViewerReady(props.viewer);
|
||||||
|
else watch(() => props.viewer, onViewerReady);
|
||||||
|
|
||||||
let {sceneDocument}: { sceneDocument: ShallowRef<Document> } = inject('sceneDocument');
|
let {sceneDocument}: { sceneDocument: ShallowRef<Document> } = inject('sceneDocument');
|
||||||
|
|
||||||
|
|
||||||
let boundingBoxLines: { [points: string]: number } = {}
|
let boundingBoxLines: { [points: string]: number } = {}
|
||||||
|
|
||||||
function updateBoundingBox() {
|
function updateBoundingBox() {
|
||||||
@@ -305,19 +293,23 @@ function updateBoundingBox() {
|
|||||||
if (matchingLine) {
|
if (matchingLine) {
|
||||||
boundingBoxLinesToRemove = boundingBoxLinesToRemove.filter((l) => l !== lineCacheKey);
|
boundingBoxLinesToRemove = boundingBoxLinesToRemove.filter((l) => l !== lineCacheKey);
|
||||||
} else {
|
} else {
|
||||||
boundingBoxLines[lineCacheKey] = props.viewer?.addLine3D(from, to,
|
let newLineId = props.viewer.addLine3D(from, to,
|
||||||
to.clone().sub(from).length().toFixed(1) + "mm", {
|
to.clone().sub(from).length().toFixed(1) + "mm", {
|
||||||
"stroke": "rgb(" + color.join(',') + ")",
|
"stroke": "rgb(" + color.join(',') + ")",
|
||||||
"stroke-width": "2"
|
"stroke-width": "2"
|
||||||
});
|
});
|
||||||
|
if (newLineId) {
|
||||||
|
boundingBoxLines[lineCacheKey] = newLineId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Remove the lines that are no longer needed
|
// Remove the lines that are no longer needed
|
||||||
for (let lineLocator of boundingBoxLinesToRemove) {
|
for (let lineLocator of boundingBoxLinesToRemove) {
|
||||||
props.viewer?.removeLine3D(boundingBoxLines[lineLocator]);
|
if (props.viewer.removeLine3D(boundingBoxLines[lineLocator])) {
|
||||||
delete boundingBoxLines[lineLocator];
|
delete boundingBoxLines[lineLocator];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function toggleShowDistances() {
|
function toggleShowDistances() {
|
||||||
showDistances.value = !showDistances.value;
|
showDistances.value = !showDistances.value;
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ class Line3DData {
|
|||||||
centerTextSize?: [number, number]
|
centerTextSize?: [number, number]
|
||||||
}
|
}
|
||||||
|
|
||||||
let nextLineId = 0;
|
let nextLineId = 1; // Avoid 0 (falsy!)
|
||||||
let lines = ref<{ [id: number]: Line3DData }>({});
|
let lines = ref<{ [id: number]: Line3DData }>({});
|
||||||
|
|
||||||
function positionToHotspot(position: Vector3): string {
|
function positionToHotspot(position: Vector3): string {
|
||||||
@@ -59,8 +59,8 @@ function addLine3D(p1: Vector3, p2: Vector3, centerText: string = "",
|
|||||||
lineAttrs: { [key: string]: string } = {
|
lineAttrs: { [key: string]: string } = {
|
||||||
"stroke-width": "2",
|
"stroke-width": "2",
|
||||||
"stroke": "red",
|
"stroke": "red",
|
||||||
}): number {
|
}): number | null {
|
||||||
if (!scene.value) return -1;
|
if (!scene.value) return null
|
||||||
let id = nextLineId++;
|
let id = nextLineId++;
|
||||||
let hotspotName1 = 'line' + id + '_start';
|
let hotspotName1 = 'line' + id + '_start';
|
||||||
let hotspotName2 = 'line' + id + '_end';
|
let hotspotName2 = 'line' + id + '_end';
|
||||||
@@ -80,14 +80,15 @@ function addLine3D(p1: Vector3, p2: Vector3, centerText: string = "",
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeLine3D(id: number) {
|
function removeLine3D(id: number): boolean {
|
||||||
if (!scene.value) return;
|
if (!scene.value || !(id in lines.value)) return false;
|
||||||
scene.value.removeHotspot(new Hotspot({name: 'line' + id + '_start'}));
|
scene.value.removeHotspot(new Hotspot({name: 'line' + id + '_start'}));
|
||||||
lines.value[id].startHotspot.parentElement.remove()
|
lines.value[id].startHotspot.parentElement.remove()
|
||||||
scene.value.removeHotspot(new Hotspot({name: 'line' + id + '_end'}));
|
scene.value.removeHotspot(new Hotspot({name: 'line' + id + '_end'}));
|
||||||
lines.value[id].endHotspot.parentElement.remove()
|
lines.value[id].endHotspot.parentElement.remove()
|
||||||
delete lines.value[id];
|
delete lines.value[id];
|
||||||
scene.value.queueRender() // Needed to update the hotspots
|
scene.value.queueRender() // Needed to update the hotspots
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onCameraChange() {
|
function onCameraChange() {
|
||||||
|
|||||||
Reference in New Issue
Block a user