6 Apr 2018

Toggle Sheet Layer Visibility

Though we already have an article on this topic and things did not change that much, I thought it could still be useful to show the exact code that can be used now to toggle the visibility of a given layer on a sheet.

function toggleLayer(layerName, viewer) {
    var root = viewer.impl.getLayersRoot();

    if (root == null) {
        console.log("No layer information...");
        return;
    }

    var toggleLayerSub = function(layer, layerName, viewer) {
        if (layer.name === layerName) {
            var visible = !viewer.isLayerVisible(layer);
            viewer.setLayerVisible(
                [layer], // array of layers
                visible, // visible
                false    // isolate
            );
        }
    }

    for (var i = 0; i < root.childCount; i++) {
        var layer = root.children[i];

        // We can also check inside layer groups 
        if (!layer.isLayer) {
            for (var j = 0; j < layer.childCount; j++) {
                toggleLayerSub(layer.children[j], layerName, viewer);
            }
        } else {
            toggleLayerSub(layer, layerName, viewer);
        }  
    }
}

 

Related Article

Posted By

Adam Nagy

Follow @AdamTheNagy Adam Nagy joined Autodesk back in 2005 and has been providing programming support, consulting, training and evangelism to external developers. He started his career in Budapest, then worked in Prague for 3 years and now lives in South England, UK. At the moment focusing on Inventor and Fusion 360, plus cloud and mobile related technologies. Adam has a degree in Software Engineering and has...