You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于.NET的Autodesk Forge应用开发:查询BIM360模型参数值

基于C#/.NET开发BIM360模型参数查询Forge应用的步骤

一、前期准备

  • 注册Forge开发者账号,创建应用时勾选Data Management和Model Derivative权限,记录生成的Client ID和Client Secret
  • 确保BIM360项目中已上传目标Revit模型,后续需通过Data Management API获取模型的URN(需做URL安全的Base64编码)

二、核心开发流程

1. 实现2-legged身份认证

服务器端调用Forge API需先获取访问令牌,用C#的HttpClient实现:

using System.Net.Http;
using System.Collections.Generic;
using System.Text.Json;

public async Task<string> GetForgeToken(string clientId, string clientSecret)
{
    var client = new HttpClient();
    var formData = new Dictionary<string, string>
    {
        {"grant_type", "client_credentials"},
        {"scope", "data:read model:read"},
        {"client_id", clientId},
        {"client_secret", clientSecret}
    };
    var response = await client.PostAsync("https://developer.api.autodesk.com/authentication/v2/token", 
        new FormUrlEncodedContent(formData));
    response.EnsureSuccessStatusCode();
    var tokenData = await response.Content.ReadFromJsonAsync<Dictionary<string, object>>();
    return tokenData["access_token"].ToString();
}

2. 获取模型元数据与参数

拿到令牌后,调用Model Derivative API获取模型结构和属性:

  • 先调用GET {urn}/metadata获取模型层级节点的GUID
  • 再调用GET {urn}/metadata/{nodeGuid}/properties获取指定节点的参数

示例代码片段:

public async Task<Dictionary<string, object>> GetModelProperties(string urn, string nodeGuid, string token)
{
    var client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
    var url = $"https://developer.api.autodesk.com/modelderivative/v2/designdata/{urn}/metadata/{nodeGuid}/properties";
    var response = await client.GetAsync(url);
    response.EnsureSuccessStatusCode();
    return await response.Content.ReadFromJsonAsync<Dictionary<string, object>>();
}

3. 前端交互实现

在ASP.NET的Razor View中添加按钮,通过AJAX触发后端接口调用并展示参数:

<button id="queryParamsBtn">查询模型参数</button>
<div id="paramsResult"></div>

<script>
document.getElementById('queryParamsBtn').addEventListener('click', async () => {
    try {
        const response = await fetch('/api/forge/getmodelparams');
        const params = await response.json();
        let resultHtml = '<ul>';
        for (const [key, value] of Object.entries(params)) {
            resultHtml += `<li><strong>${key}</strong>: ${value}</li>`;
        }
        resultHtml += '</ul>';
        document.getElementById('paramsResult').innerHTML = resultHtml;
    } catch (err) {
        document.getElementById('paramsResult').innerHTML = '<p>查询失败,请检查配置</p>';
    }
});
</script>

4. 后端接口封装

在ASP.NET Controller中创建接口,整合令牌获取与参数查询逻辑:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

[ApiController]
[Route("api/forge")]
public class ForgeController : ControllerBase
{
    private readonly IConfiguration _config;

    public ForgeController(IConfiguration config)
    {
        _config = config;
    }

    [HttpGet("getmodelparams")]
    public async Task<IActionResult> GetModelParams()
    {
        var clientId = _config["Forge:ClientId"];
        var clientSecret = _config["Forge:ClientSecret"];
        var modelUrn = "替换为你的URL安全Base64格式URN";
        var targetNodeGuid = "替换为目标构件的节点GUID";

        try {
            var token = await GetForgeToken(clientId, clientSecret);
            var properties = await GetModelProperties(modelUrn, targetNodeGuid, token);
            return Ok(properties);
        } catch (Exception ex) {
            return BadRequest(new { message = ex.Message });
        }
    }

    // 插入之前的GetForgeToken和GetModelProperties方法
}

三、关键注意事项

  • URN需做URL安全Base64编码:将原始URN转Base64后,把+替换为-、/替换为_,去掉末尾的=
  • 若模型未完成格式转换,需先调用Model Derivative的POST job接口,将Revit模型转换为可提取属性的格式
  • 增加异常捕获逻辑,处理令牌过期、权限不足、模型未找到等API错误

内容的提问来源于stack exchange,提问作者Tom Boudniatski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 13:10:31