Search code examples
autodesk-viewerautodesk-navisworks

Converting navisworks section planes to forge


I am making a forge integration for Navisworks and am stuck on how to bring in the sectioning planes from Navisworks to Forge. I followed the instructions in this blog post to set the cut planes but I am not seeing them show up in the viewer. To set the planes I am pulling the SectionData from the Navisworks ActiveView and extracting the normal vector and distance from the returned json object, then passing the data via url parameters to my forge viewer. Code is below:

                  //set cut planes
                    if (planes.length > 0)
                    {
                        var three_planes = [];

                        var i;
                        for (i = 0; i < planes.length; i += 4)
                        {
                            var a = String(planes[i]);
                            var b = String(planes[i+1]);
                            var c = String(planes[i+2]);
                            var distance = String(planes[i+3]);
                            if (a.includes("E") || a.includes("e"))
                            {
                                a = "0.00";
                            }

                            if (b.includes("E") || b.includes("e"))
                            {
                                b = "0.00";
                            }

                            if (c.includes("E") || c.includes("e"))
                            {
                                c = "0.00";
                            }

                            if (distance.includes("E") || distance.includes("e"))
                            {
                                distance = "0.00";
                            }

                            var afloat = parseFloat(a);
                            var bfloat = parseFloat(b);
                            var cfloat = parseFloat(c);
                            var distancefloat = parseFloat(distance);
                             // create a THREE.Vector4
                              var vector4 = new THREE.Vector4(
                                afloat, bfloat,
                                cfloat, distancefloat);

                              three_planes.push(vector4);
                        }

                       viewer.setCutPlanes(three_planes);
                    }

Viewer state shows that the planes are there but can't see them. Are there any special coordinate transformations necessary to make this work?

EDIT: I tried the solution given below but I am still not seeing the cut planes in Forge. I've confirmed that my cut plane variables from Navis (afloat, bfloat, cfloat, distancefloat) are coming in correctly. My modified code is below. Any idea what I may be doing wrong?

 //set cut planes
                        if (planes.length > 0)
                        {
                            var three_planes = [];

                            var i;
                            for (i = 0; i < planes.length; i += 4)
                            {
                                var a = String(planes[i]);
                                var b = String(planes[i+1]);
                                var c = String(planes[i+2]);
                                var distance = String(planes[i+3]);
                                if (a.includes("E") || a.includes("e"))
                                {
                                    a = "0.00";
                                }

                                if (b.includes("E") || b.includes("e"))
                                {
                                    b = "0.00";
                                }

                                if (c.includes("E") || c.includes("e"))
                                {
                                    c = "0.00";
                                }

                                if (distance.includes("E") || distance.includes("e"))
                                {
                                    distance = "0.00";
                                }

                                var afloat = parseFloat(a);
                                var bfloat = parseFloat(b);
                                var cfloat = parseFloat(c);
                                var distancefloat = parseFloat(distance);

                                var forge_model_offset = NOP_VIEWER.model.getData().globalOffset;

                                //calculate exact distance in Forge Viewer
                                var dis_in_forge = (forge_model_offset.x * afloat + forge_model_offset.y * bfloat + forge_model_offset.z * cfloat) - 
                                                distancefloat;


                                //build the plane for Forge Viewer sectioning.

                                var vector4 = new THREE.Vector4(-afloat, -bfloat, -cfloat, -dis_in_forge);
                                three_planes.push(vector4);
                            }

                           NOP_VIEWER.setCutPlanes(three_planes);
                        }

EDIT 2: Got it working. Posting here in case anyone runs into this issue. setCutPlanes must be called late in the loading process. My code originally was calling it from onModelLoadSuccess, which did not work. But when I moved it to the TEXTURES_LOADED event it worked fine.


Solution

  • I did some investigations and found some tricks in the mapping the sectioning plane between Navisworks and Forge Viewer. This is a blog on the topic: https://forge.autodesk.com/blog/map-sectioning-plane-navisworks-forge-viewer-2

    the core code is

    //get offset of the model
    let forge_model_offset = NOP_VIEWER.model.getData().globalOffset
    
     // assume the param of Navisworks clip plane is available 
    //I copied from the XML file
    let navis_clip_plane = 
       {x:0.1368226246,y:0.4334009763,z:0.8907542664,d:886.2418151801}
    
    //calculate exact distance in Forge Viewer
    dis_in_forge =( forge_model_offset.x * navis_clip_plane.x  +   
                        forge_model_offset.y * navis_clip_plane.y + 
                        forge_model_offset.z * navis_clip_plane.z) - 
                    navis_clip_plane.d
    
    //build the plane for Forge Viewer sectioning.
    cutplanes = [
    new THREE.Vector4( -navis_clip_plane.x, 
                       -navis_clip_plane.y, 
                       - navis_clip_plane.z, -dis_in_forge) 
      ];
    
     //apply the plane to sectioning
     NOP_VIEWER.setCutPlanes(cutplanes)