0

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.

2
  • I think I have worked out the reason why this is happening, but I'm still not sure why. I have a second viewer instance that also runs in the background, and if I stop the second viewer instance from initialising, then the snapper icon appears on this viewer. but I'm not sure why the other one would stop it? Commented Feb 28 at 14:36
  • Would you be able to provide a minimal, working sample project that could be used to reproduce the issue? Maybe something based on this? aps.autodesk.com/blog/publicly-share-models-customized-viewer Commented Mar 7 at 14:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.