I have been trying to use the built in Snapper tool functionality to snap to various 3D objects, I have mainly followed this guide: https://aps.autodesk.com/blog/snappy-viewer-tools. I have gone through some of the suggestions I have found on here already to try and get it working but with no success. Where the handleMouseMove event is within this custom component I have added some extra bits from other posts to try and get it working. All other code in my trial is the same as the snappy-viewer-tools I have linked above.
handleMouseMove(event) {
if (!this.active) {
return false;
}
// this.snapper.indicator.clearOverlays();
const viewer_pos = this.viewer.container.getBoundingClientRect();
const worldCoordinate = this.viewer.impl.hitTest(
event.clientX - viewer_pos.x,
event.clientY - viewer_pos.y
);
// console.log("worldCoordinate", worldCoordinate);
if (worldCoordinate === null) return;
// const hitTestResult = this.viewer.impl.snappingHitTest(
// worldCoordinate.point.x,
// worldCoordinate.point.y,
// worldCoordinate.point.z
// );
// console.log("hitTestResult", hitTestResult);
// if (hitTestResult === null) return;
// this.snapper.snapping3D(hitTestResult);
this.snapper.snapping3D(worldCoordinate);
const result = this.snapper.getSnapResult();
// this.snapper.indicator.render();
// console.log("isSnapped?", this.snapper.isSnapped());
if (this.snapper.isSnapped()) {
const result = this.snapper.getSnapResult();
// this.snapper.indicator.render();
// console.log("result", result);
const { SnapType } = Autodesk.Viewing.MeasureCommon;
// console.log("snapType ====", SnapType);
switch (result.geomType) {
case SnapType.SNAP_VERTEX:
case SnapType.SNAP_MIDPOINT:
case SnapType.SNAP_INTERSECTION:
case SnapType.SNAP_CIRCLE_CENTER:
case SnapType.RASTER_PIXEL:
console.log("Snapped to vertex", result.geomVertex);
this.snapper.indicator.render(); // Show indicator when snapped to a vertex
// this._update(result.geomVertex);
break;
case SnapType.SNAP_EDGE:
case SnapType.SNAP_CIRCULARARC:
case SnapType.SNAP_CURVEDEDGE:
console.log("Snapped to edge", result.geomEdge);
this.snapper.indicator.render();
break;
case SnapType.SNAP_FACE:
case SnapType.SNAP_CURVEDFACE:
console.log("Snapped to face", result.geomFace);
this.snapper.indicator.render();
break;
default:
// Do not snap to other types
break;
}
}
return false;
}
Since adding the hit test with the world coordinated I have been getting into the if statement of isSnapped(). I have tried the this.snapper.indicator.render() at several different places to try to render the indicator for what I am snapping to with no success, and the cursor itself does not snap to any objects either. I am getting the console logs for when I mouse over each of the different snap types, but no snapping happens and no icon appears either. Am I missing something? The Viewer is also built using Autodesk.Viewing.AggregatedView() in case this makes any difference, but I couldn't find anywhere that made it sound like it should.