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

如何在Autodesk Viewer中获取对象的ThemingColor(类似getThemingColor)

获取Autodesk Platform Services中对象的ThemingColor方案

目前Autodesk Platform Services(APS)Viewer的官方API确实没有提供直接的getThemingColor方法来获取对象关联的主题色,但可以通过以下两种方案实现需求:

方案1:自行维护主题色映射表

在调用setThemingColor或clearThemingColor时,同步维护一个全局的对象ID与主题色的映射关系,后续判断或获取时直接查询该映射表即可,这是最稳定且官方兼容的方案。

示例代码:

// 全局存储对象ID与主题色的映射
const themingColorRegistry = new Map();

// 封装设置主题色的方法,同步更新映射表
function applyThemingColor(viewer, dbId, color) {
    viewer.clearThemingColor(dbId);
    viewer.setThemingColor(dbId, color);
    themingColorRegistry.set(dbId, color);
}

// 封装清除主题色的方法,同步移除映射
function removeThemingColor(viewer, dbId) {
    viewer.clearThemingColor(dbId);
    themingColorRegistry.delete(dbId);
}

// 判断对象是否关联了主题色,或获取对应颜色
function checkThemingColor(dbId) {
    return themingColorRegistry.get(dbId); // 存在则返回Color对象,不存在返回undefined
}

// 使用示例
const targetDbId = 123;
const hasThemingColor = checkThemingColor(targetDbId) !== undefined;

方案2:访问Viewer扩展的内部状态(非官方兼容)

APS Viewer的主题色功能由Autodesk.Viewing.ThemingExtension提供,该扩展内部维护了一个颜色映射表。可以通过访问扩展的私有属性来获取对象的主题色,但需注意:私有属性不属于官方公开API,可能随Viewer版本更新发生变化,存在兼容性风险。

示例代码:

function getThemingColor(viewer, dbId) {
    const themingExtension = viewer.getExtension('Autodesk.Viewing.ThemingExtension');
    if (!themingExtension) return null;
    
    // 访问扩展内部的colorMap属性(私有属性,需谨慎使用)
    return themingExtension.colorMap.get(dbId) || null;
}

// 使用示例
const targetDbId = 123;
const color = getThemingColor(viewer, targetDbId);
const hasThemingColor = color !== null;

优先推荐方案1,避免依赖内部实现导致的潜在问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 06:07:42