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