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

如何通过Design Automation发布BIM 360云模型新版本?求C#代码示例

BIM 360 Revit模型更新参数后发布新版本的C#实现

核心逻辑

完成Design Automation的WorkItem处理后,需要将修改后的模型从Forge存储桶上传回BIM 360项目,并通过Data Management API创建并发布新版本。

关键步骤及代码示例

1. 获取源版本的基础信息

先通过原版本ID拿到文件所在的项目、文件夹ID,确定新版本的上传位置:

public async Task<dynamic> GetSourceVersionInfo(string forgeToken, string sourceVersionId)
{
    using var client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", forgeToken);
    var response = await client.GetAsync($"https://developer.api.autodesk.com/data/v1/versions/{sourceVersionId}");
    response.EnsureSuccessStatusCode();
    return await response.Content.ReadFromJsonAsync<dynamic>();
}

2. 将处理后的文件上传到BIM 360

把Design Automation输出到存储桶的模型文件,上传到BIM 360对应文件夹:

public async Task<string> UploadProcessedFileToBim360(string forgeToken, string projectId, string folderId, string bucketKey, string objectName, string originalFileName)
{
    // 第一步:获取BIM 360上传授权
    using var authClient = new HttpClient();
    authClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", forgeToken);
    var uploadAuthRequest = new
    {
        json = new
        {
            objectKey = originalFileName,
            storage = new { storageType = "OSS", region = "US" }
        }
    };
    var authResponse = await authClient.PostAsJsonAsync(
        $"https://developer.api.autodesk.com/data/v1/projects/{projectId}/items:upload",
        uploadAuthRequest);
    authResponse.EnsureSuccessStatusCode();
    var authData = await authResponse.Content.ReadFromJsonAsync<dynamic>();
    var uploadUrl = authData.data.attributes.uploadUrl;
    var itemId = authData.data.id;

    // 第二步:从Forge存储桶下载文件并上传到BIM 360
    using var bucketClient = new HttpClient();
    bucketClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", forgeToken);
    var fileStream = await bucketClient.GetStreamAsync($"https://developer.api.autodesk.com/oss/v2/buckets/{bucketKey}/objects/{objectName}");
    
    using var uploadClient = new HttpClient();
    var uploadResponse = await uploadClient.PutAsync(uploadUrl, new StreamContent(fileStream));
    uploadResponse.EnsureSuccessStatusCode();

    return itemId;
}

3. 创建并发布新版本

基于上传的文件创建新版本,并直接设置为发布状态:

public async Task PublishNewVersion(string forgeToken, string projectId, string itemId, string sourceVersionId, string versionName)
{
    using var client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", forgeToken);
    var versionRequest = new
    {
        jsonapi = new { version = "1.0" },
        data = new
        {
            type = "versions",
            attributes = new
            {
                name = versionName,
                description = "参数更新后发布的新版本",
                extension = new
                {
                    type = "versions:autodesk.bim360:File",
                    version = "1.0",
                    data = new { state = "published" }
                }
            },
            relationships = new
            {
                item = new { data = new { type = "items", id = itemId } },
                parent = new { data = new { type = "versions", id = sourceVersionId } }
            }
        }
    };

    var response = await client.PostAsJsonAsync(
        $"https://developer.api.autodesk.com/data/v1/projects/{projectId}/versions",
        versionRequest);
    response.EnsureSuccessStatusCode();
}

4. 整合到回调流程

在Design Automation的回调接口中串联上述步骤:

public async Task<IActionResult> DesignAutomationCallback([FromBody] dynamic callbackPayload)
{
    string forgeToken = await GetValidForgeToken(); // 自行实现获取有效Token的逻辑
    string projectId = "你的BIM 360项目ID";
    string sourceVersionId = "原模型的版本ID";
    string bucketKey = "Design Automation输出文件所在的存储桶Key";
    string processedObjectName = "处理后的模型文件名(如sample_updated.rvt)";

    // 1. 获取源版本的文件夹信息
    var sourceVersionInfo = await GetSourceVersionInfo(forgeToken, sourceVersionId);
    string folderId = sourceVersionInfo.data.relationships.parent.data.id;
    string originalFileName = sourceVersionInfo.data.attributes.name;

    // 2. 上传处理后的文件到BIM 360
    string itemId = await UploadProcessedFileToBim360(forgeToken, projectId, folderId, bucketKey, processedObjectName, originalFileName);

    // 3. 创建并发布新版本
    await PublishNewVersion(forgeToken, projectId, itemId, sourceVersionId, originalFileName);

    return Ok("新版本发布完成");
}

注意事项

  • 确保Forge应用已开启Data Management和BIM 360 Docs权限,Token需包含data:write、data:create、bucket:read等必要权限范围。
  • 若处理大文件,需替换为分块上传逻辑,避免超时或内存溢出。
  • 保持新版本文件名与原文件一致,确保关联到同一个项目项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 05:43:22