简体   繁体   English

Autodesk Forge如何从名称中获取双价

[英]Autodesk Forge How to get dbid from name

I have an extension which loads a docking panel with a text field and a button. 我有一个扩展程序,该扩展程序加载带有文本字段和按钮的停靠面板。 The functionality of this button would be to display the DB-ID of the item name given in the text field. 此按钮的功能是显示在文本字段中给定的项目名称的DB-ID。

Something like: Rubber = 2130 类似于:橡胶= 2130

where Rubber is the input and 2130(db-id) will be the output 其中Rubber是输入,2130(db-id)将是输出

How can I achieve this? 我该如何实现?

Thanks in advance. 提前致谢。

Would suggest using .search() method, which is a supported way: 建议使用.search()方法,这是一种受支持的方法:

viewer.search('Rubber', function(dbIds){
  // here the dbIds is a list of dbIds, you can handle it
  callback(dbIds); // handle the results async
}, function(error){
  // handle errors here...
}, ['name'] /* this array indicates the filter: search only on 'Name'*/
)

And look here on how to improve performance on search. 并在这里查看如何提高搜索效果。

There is no such API available currently, but a workaround can be used in this case. 当前没有此类API,但是在这种情况下可以使用解决方法。 It's shown as the following: 显示如下:

//-- For the element type or element category like "Floor [" or "Floor"

var it = viewer.model.getData().instanceTree;
var strIdx = it.nodeAccess.strings.indexOf( "Floor [" );
// var strIdx = it.nodeAccess.strings.indexOf( "Floor" );
var nameIdx = it.nodeAccess.names.indexOf( strIdx );
for( var key in it.nodeAccess.dbIdToIndex ) {
  if( it.nodeAccess.dbIdToIndex[key] === nameIdx ) console.log( key )
}


//-- For element name like "Floor[766598]":
var it = viewer.model.getData().instanceTree;
var eidIndex = it.nodeAccess.nameSuffixes.indexOf( 766598 );
for( let key in it.nodeAccess.dbIdToIndex ) { 
  if( it.nodeAccess.dbIdToIndex[key] === eidIndex ) console.log( key ) 
}

PS Since this is just a workaround, not the formal solution. PS由于这只是一种解决方法,而不是正式的解决方案。 You would have to use it at your own risk. 您必须自己承担使用风险。

If you want to avoid using the search() method, you could also create your own Object that maps the name to ID's. 如果要避免使用search()方法,则还可以创建自己的Object来将名称映射到ID。 This is a temporary workaround until Autodesk implements its own method. 在Autodesk实现自己的方法之前,这是一个临时解决方法。

var instanceTree = viewer.model.getData().instanceTree; // Get model tree
var allDbIds = Object.keys(instanceTree.nodeAccess.names); //get array of all ID's
var forgeNameMap = {} //Empty array for mapping names
allDbIds.map(function(id) { 
    forgeNameMap[instanceTree.getNodeName(id)] = id
})

let rubberID = forgeNameMap["Rubber"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM