Here is a quickie for the weekend. The following code illustrates how to iterate all the layer groups and their child nodes, then how to isolate a specific layer based on its name similarly to a user clicking on that layer from the Layers panel in UI.
Below is the signature of the function used to isolate a layer:
1 /** 2 * Set visibility for a single layer, or for all layers. 3 * 4 * Not yet implemented for 3D. 5 * 6 * @param {?Array} nodes - An array of layer nodes, 7 * or a single layer node, or null for all layers 8 * 9 * @param {boolean} visible - true to show, false to hide 10 * 11 * @param {boolean=} [isolate] - true to isolate the layer 12 */ 13 Autodesk.Viewing.Viewer3D.prototype.setLayerVisible = 14 function (nodes, visible, isolate)
I am implementing my code as an extension so it can be very easily reused or tested on your side:
1 /////////////////////////////////////////////////////////////////////////////// 2 // Layers viewer extension 3 // by Philippe Leefsma, January 2015 4 // 5 /////////////////////////////////////////////////////////////////////////////// 6 AutodeskNamespace("Autodesk.ADN.Viewing.Extension"); 7 8 Autodesk.ADN.Viewing.Extension.Layers = function ( 9 viewer, 10 options) { 11 12 Autodesk.Viewing.Extension.call( 13 this, 14 viewer, 15 options); 16 17 var _self = this; 18 19 var _viewer = viewer; 20 21 _self.load = function () { 22 23 var root = _viewer.model.getLayersRoot(); 24 25 if(root == null) { 26 27 console.log("No layer information..."); 28 return; 29 } 30 31 console.log(root); 32 33 for (var i = 0; i < root.childCount; i++) { 34 35 var group = root.children[i]; 36 37 console.log(group); 38 39 for (var j = 0; j < group.childCount; j++) { 40 41 var layer = group.children[j]; 42 43 if (layer.name === 44 "ORN-A-B-01-BDYGRD-XX-XXXX-MAS|A-Cols") { 45 46 _viewer.setLayerVisible( 47 [layer], 48 true, 49 true); 50 } 51 } 52 } 53 54 console.log( 55 'Autodesk.ADN.Viewing.Extension.Layers loaded'); 56 57 return true; 58 }; 59 60 _self.unload = function () { 61 62 console.log( 63 'Autodesk.ADN.Viewing.Extension.Layers unloaded'); 64 65 return true; 66 }; 67 }; 68 69 Autodesk.ADN.Viewing.Extension.Layers.prototype = 70 Object.create(Autodesk.Viewing.Extension.prototype); 71 72 Autodesk.ADN.Viewing.Extension.Layers.prototype.constructor = 73 Autodesk.ADN.Viewing.Extension.Layers; 74 75 Autodesk.Viewing.theExtensionManager.registerExtension( 76 'Autodesk.ADN.Viewing.Extension.Layers', 77 Autodesk.ADN.Viewing.Extension.Layers);
Layer groups are being dumped into the browser debug window as illustrated in that screenshot:
You can download the code from the following link: Autodesk.ADN.Viewing.Extension.Layers
Hello Phillippe, thanks for your post.
I am trying to use it in my app but I am facing an issue in this line:
var root = _viewer.model.getLayersRoot();
Basically the "model" property is null.
Am I missing something?
Posted by: Fpontes79 | 02/05/2015 at 08:52 AM
Do you have any geometry in your model? Are you sure that model is null or is it the result "root"?
This code only works for 2d drawings, getLayersRoot return null for 3d models...
Posted by: Philippe Leefsma | 02/05/2015 at 09:08 AM