Forge Viewer中選取模型中所有物件

透過View and data API和javascript選取模型中所有物件

 首先透過viewer獲得物件樹

第一行程式碼的viewer為初始化Forge Viewer時假設的變數,因使用者命名而異

var instanceTree = viewer.model.getData().instanceTree;
var rootId = this.rootId = instanceTree.getRootId();

舉例:以Viewer and Data API初始化Viewer,範例將初始化的viewer命名為_viewer

 var viewerElement = document.getElementById('MyViewerDiv');// placeholder in HTML to stick the viewer

 var  _viewer = new Autodesk.Viewing.Private.GuiViewer3D(viewerElement, {});

 //nogui
 // _viewer = new Autodesk.Viewing.Viewer3D(viewerElement, {});

 

透過rootId即可利用下列函式獲得模型所有物件的dbid陣列

function getAlldbIds (rootId) {
	var alldbId = [];
	if (!rootId) {
		return alldbId;
	}
	var queue = [];
	queue.push(rootId);
	while (queue.length > 0) {
		var node = queue.shift();
		alldbId.push(node);
		instanceTree.enumNodeChildren(node, function(childrenIds) {
			queue.push(childrenIds);
		});
	}
	return alldbId;
}

 

參考:

http://adndevblog.typepad.com/cloud_and_mobile/2015/12/select-all-elements-in-the-viewer-with-view-and-data-api-with-javascript.html

遇到問題最可怕的不是被問倒,而是自己選擇逃避