Search code examples
autodesk-forgeforge

How can I create section box in forge viewer


I wanted to build a section box inside forge viewer. I have the minx, maxX, minY, maxY through area marking. Also I have minZ and maxZ through level selection (levels[i].elevation – globalOffset.Z).

I want a similar functionality like the default Add Box button in Section Analysis but I want to add the min max co-ordinates Default Add Box Button Image

Can you please help me with this?


Solution

  • Here you go ~ Please refer to the following code snippet to calculate cut plane parameters for the viewer:

    const minPt = new THREE.Vector3( -82.73119354248047, -115.42709350585938, -31.42848777770996 ); //!<<< put your point here
    const maxPt = new THREE.Vector3( -0.0000020212402347397074, 0, 0 ); //!<<< put your point here
    
    const normals = [
        new THREE.Vector3(1, 0, 0), 
        new THREE.Vector3(0, 1, 0), 
        new THREE.Vector3(0, 0, 1),
        new THREE.Vector3(-1, 0, 0),
        new THREE.Vector3(0, -1, 0),
        new THREE.Vector3(0, 0, -1)
    ];
    
    const bbox = new THREE.Box3( minPt, maxPt );
    const cutPlanes = [];
    
    for( let i = 0; i < normals.length; i++ ) {
        const plane = new THREE.Plane( normals[i], -1 * maxPt.dot( normals[i] ) );
    
        // offset plane with negative normal to form an octant
        if( i > 2 ) {
            const ptMax = plane.orthoPoint( bbox.max );
            const ptMin = plane.orthoPoint( bbox.min );
            const size = new THREE.Vector3().subVectors( ptMax, ptMin );
            plane.constant -= size.length();
        }
    
        const n = new THREE.Vector4( plane.normal.x, plane.normal.y, plane.normal.z, plane.constant );
        cutPlanes.push( n );
    }
    
    viewer.setCutPlanes( cutPlanes );