0

我正在尝试使用 Autodesk forge API for C# 来获取集线器列表。这是我到目前为止所做的:

HubsApi api = new HubsApi();
Hubs hubs = api.GetHubs();

很简单。但是当我这样做时,我得到一个抱怨的异常,即无法从转换DynamicJsonResponseHubs. 我认为,这是因为我在响应字符串中收到了两个警告,因此它不再是集线器对象。警告如下所示:

"warnings":[  
        {  
           "Id":null,
           "HttpStatusCode":"403",
           "ErrorCode":"BIM360DM_ERROR",
           "Title":"Unable to get hubs from BIM360DM EMEA.",
           "Detail":"You don't have permission to access this API",
           "AboutLink":null,
           "Source":[  

           ],
           "meta":[  

           ]
        }

所有这些都包含在一个包含四个条目的字典中,其中只有两个是数据。但是,根据 Autodesk 的说法,可以忽略此警告。

所以在那之后我尝试在字典中转换它并且只选择数据条目

HubsApi api = new HubsApi();
DynamicJsonResponse resp = api.GetHubs();
DynamicDictionary hubs = (DynamicDictionary)resp.Dictionary["data"];

然后我遍历它:

for(int i = 0; i < hubs.Dictionary.Count && bim360hub == null; i++)
{
    string hub = hubs.Dictionary.ElementAt(i).ToString();
    [....]
}

但字符串hub也不是 json-hub。它是一个如下所示的数组:

[
  0,
  {
    "type": "hubs",
    "id": "****",
    "attributes": {...},
    "links": {...},
    "relationships": {...},
  }
]

数组中的第二个元素是我的集线器。我知道,我如何选择第二个元素。但是必须更容易获得集线器列表。在参考文献中的一个示例中,它似乎可以使用以下简单的两行代码:

HubsApi api = new HubsApi();
Hubs hubs = api.GetHubs();

任何想法,我如何设法获得我的集线器?

4

1 回答 1

1

首先,考虑使用这些方法的异步版本,避免使用非异步调用,因为它会导致桌面应用程序冻结(在连接时)或在 ASP.NET 上分配更多资源。

以下函数此示例的一部分,它列出了用户帐户下的所有集线器、项目和文件。这是一个很好的起点。请注意,它将集线器组织在一个TreeNode列表中,这与jsTree兼容。

private async Task<IList<TreeNode>> GetHubsAsync()
{
  IList<TreeNode> nodes = new List<TreeNode>();

  HubsApi hubsApi = new HubsApi();
  hubsApi.Configuration.AccessToken = AccessToken;

  var hubs = await hubsApi.GetHubsAsync();
  string urn = string.Empty;
  foreach (KeyValuePair<string, dynamic> hubInfo in new DynamicDictionaryItems(hubs.data))
  {
    string nodeType = "hubs";
    switch ((string)hubInfo.Value.attributes.extension.type)
    {
      case "hubs:autodesk.core:Hub":
        nodeType = "hubs";
        break;
      case "hubs:autodesk.a360:PersonalHub":
        nodeType = "personalhub";
        break;
      case "hubs:autodesk.bim360:Account":
        nodeType = "bim360hubs";
        break;
    }
    TreeNode hubNode = new TreeNode(hubInfo.Value.links.self.href, (nodeType == "bim360hubs" ? "BIM 360 Projects" : hubInfo.Value.attributes.name), nodeType, true);
    nodes.Add(hubNode);
  }

  return nodes;
}
于 2017-08-03T15:29:04.290 回答