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

请求提供ACC账户2020-2023年Revit模型项目信息参数提取示例

提取ACC中Revit模型的项目信息参数(APS实现示例)

核心思路

要提取Revit模型的项目信息参数(如项目名称、编号、客户信息等),需通过Autodesk Platform Services(APS)的Data Management API筛选目标模型,结合Model Derivative API解析模型元数据并定位到Revit的「Project Information」元素,最终提取其参数。


步骤1:获取APS访问令牌

首先通过OAuth2认证获取access_token,用于后续API调用:

import requests

def get_access_token(client_id, client_secret):
    url = "https://developer.api.autodesk.com/authentication/v1/authenticate"
    payload = {
        "client_id": client_id,
        "client_secret": client_secret,
        "grant_type": "client_credentials",
        "scope": "data:read modelderivative:read"
    }
    response = requests.post(url, data=payload)
    if response.status_code == 200:
        return response.json()["access_token"]
    else:
        raise Exception(f"认证失败: {response.text}")

步骤2:筛选2020-2023年的Revit模型

调用Data Management API获取指定ACC项目下符合条件的.rvt文件,通过filter参数限定文件类型和创建时间范围:

def get_target_revit_models(access_token, project_id):
    url = f"https://developer.api.autodesk.com/data/v1/projects/{project_id}/items"
    headers = {"Authorization": f"Bearer {access_token}"}
    # 筛选条件:rvt文件 + 2020-2023年创建
    params = {
        "filter": "type:\"rvt\" AND createdTime:[2020-01-01T00:00:00Z TO 2023-12-31T23:59:59Z]",
        "limit": 100  # 分页获取,可根据实际调整
    }
    all_models = []
    while True:
        response = requests.get(url, headers=headers, params=params)
        if response.status_code != 200:
            break
        data = response.json()
        all_models.extend(data["data"])
        # 处理分页
        if "next" not in data.get("links", {}):
            break
        url = data["links"]["next"]["href"]
        params = {}  # 下一页链接已包含参数,无需重复传递
    return all_models

步骤3:翻译模型(若未完成翻译)

若目标模型尚未生成SVF格式的元数据,需调用Model Derivative API发起翻译:

import base64

def translate_revit_model(access_token, item_id):
    # 将item_id转为URN(base64编码并去除末尾等号)
    urn = base64.b64encode(item_id.encode()).decode().rstrip("=")
    url = "https://developer.api.autodesk.com/modelderivative/v2/designdata/job"
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json"
    }
    payload = {
        "input": {"urn": urn},
        "output": {"formats": [{"type": "svf", "views": ["2d", "3d"]}]}
    }
    response = requests.post(url, headers=headers, json=payload)
    return response.status_code in [200, 201]

步骤4:提取Project Information参数

模型翻译完成后,先获取模型元数据,定位到「Project Information」元素,再提取其所有参数:

def get_project_info_parameters(access_token, urn):
    # 获取模型元数据
    metadata_url = f"https://developer.api.autodesk.com/modelderivative/v2/designdata/{urn}/metadata"
    headers = {"Authorization": f"Bearer {access_token}"}
    response = requests.get(metadata_url, headers=headers)
    if response.status_code != 200:
        return None
    metadata = response.json()["data"]
    
    # 找到Project Information对应的guid
    project_info_guid = None
    for item in metadata:
        if item["name"] == "Project Information":
            project_info_guid = item["guid"]
            break
    if not project_info_guid:
        return None
    
    # 提取该元素的参数
    properties_url = f"https://developer.api.autodesk.com/modelderivative/v2/designdata/{urn}/metadata/{project_info_guid}/properties"
    props_response = requests.get(properties_url, headers=headers)
    if props_response.status_code == 200:
        return props_response.json()["data"]
    return None

整合执行示例

if __name__ == "__main__":
    CLIENT_ID = "你的APS客户端ID"
    CLIENT_SECRET = "你的APS客户端密钥"
    PROJECT_ID = "你的ACC项目ID"
    
    try:
        token = get_access_token(CLIENT_ID, CLIENT_SECRET)
        models = get_target_revit_models(token, PROJECT_ID)
        
        for model in models:
            item_id = model["id"]
            model_name = model["attributes"]["displayName"]
            print(f"处理模型: {model_name}")
            
            # 发起翻译请求
            translate_success = translate_revit_model(token, item_id)
            if not translate_success:
                print(f"模型翻译请求失败: {model_name}")
                continue
            
            # 轮询翻译状态(生产环境推荐)
            urn = base64.b64encode(item_id.encode()).decode().rstrip("=")
            manifest_url = f"https://developer.api.autodesk.com/modelderivative/v2/designdata/{urn}/manifest"
            translated = False
            for _ in range(10):
                manifest_resp = requests.get(manifest_url, headers={"Authorization": f"Bearer {token}"})
                if manifest_resp.status_code == 200:
                    status = manifest_resp.json()["status"]
                    if status == "success":
                        translated = True
                        break
                    elif status == "failed":
                        print(f"模型翻译失败: {model_name}")
                        break
                import time
                time.sleep(10)
            
            if not translated:
                continue
            
            # 提取项目信息参数
            project_params = get_project_info_parameters(token, urn)
            if project_params:
                print("项目信息参数:")
                for param in project_params["properties"]:
                    print(f"- {param['displayName']}: {param['value']}")
            else:
                print(f"未找到项目信息参数: {model_name}")
            print("---")
    except Exception as e:
        print(f"执行错误: {str(e)}")

关键说明

  • URN编码: Revit模型的item_id需转为base64编码并去除末尾的=符号,才能用于Model Derivative API调用。
  • 翻译状态轮询: 示例中通过循环轮询翻译状态,避免固定延时的不确定性,生产环境可根据需求调整轮询次数和间隔。
  • 权限范围: 确保APS应用的权限范围包含data:read和modelderivative:read,否则会触发权限错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 01:02:06