Search code examples
autodesk-forgeautodesk-viewer

How can I make it so Edit2D lines do not resize when viewer is zoomed in and out?


Long story short: I want to set Edit2D polyline tool's line width to 6" based on calibration sizing and have it stay that size in the viewer no matter the camera zoom.


I'm using the edit2d library to allow drawing. I need to be able to set the line width for the polyline tool and have it stay at that width similar to how markups stay a set size when drawn. The default functionality of the edit2d polyline tool is for the line to be resized on camera changes and so it grows and shrinks depending on zoom.

I tried setting edit2DTools.polylineTool.style.isScreenSpace = false; which works, however, trying to set the specific size is difficult as it ends up being a decimal less then 1 and I can't find a correlation from the calibration, page size, etc. to allow me to dynamically set the size the same on different models.


I also found this in the Edit2D Snapper's code, but I can't figure out what's happening here to replicate it on the polyline tool. It seems to be doing what I want to do.

Edit2D Snapper

Any help or ideas at all would be greatly appreciated!


Solution

  • This code seems to be working for me:

    async function setLineWidth(viewer) {
      let mt = await viewer.loadExtension("Autodesk.Measure");
      if (!mt.sharedMeasureConfig.calibrationFactor) {
        console.log("Has not been calibrated yet");
        return;
      }
    
      let edit2d = viewer.getExtension('Autodesk.Edit2D');
    
      let pt1 = {x:0, y:0, z:0}, pt2 = {x:6, y:0, z:0};
      viewer.model.pageToModel(pt1, pt2, 0 /*viewport id*/, true /*reverse*/);
      let style = edit2d.defaultTools.polylineTool.style;
      style.lineWidth = (pt2.x - pt1.x) / mt.calibration.scaleFactor;
    }

    Here is a video showing it in action: https://youtu.be/DIaKugvQdm4

    As you can see first the lineWidth of the polylineTool is not what you want, but after setting what 6" should be in the drawing, I can set it to the same width and all is fine.

    Here is a zip file with the html including the JavaScript code and the test PDFs I used: https://github.com/adamenagy/Container/blob/master/Edit2dTest.zip