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

Revit 2024 API无法设置图纸/视图共享参数值的解决方案咨询

Revit 2024 API共享参数赋值问题

我通过Revit 2024 API创建了一个共享参数,并将其绑定到Sheets(图纸)、Views(视图)和Schedules(明细表)类别。后续尝试为实例设置该共享参数值时,Schedules实例可正常赋值,但Sheets或Views实例抛出异常,提示共享参数为只读。我了解到部分内置类型的共享参数赋值存在限制,但因明细表可正常操作,想咨询是否有针对图纸和视图的解决办法。

以下是创建共享参数及设置值的代码:

private void CreateSharedParameter(UIApplication uiApp, Document doc, string groupName, string paramName)
{
    string sharedParamFile = @"C:\Program Files\Autodesk\OrganizationSharedParams.txt"; 

    if (!File.Exists(sharedParamFile))
    {
        File.Create(sharedParamFile).Dispose();
    }

    // Set the shared parameter file
    uiApp.Application.SharedParametersFilename = sharedParamFile;

    // Open the shared parameter file
    DefinitionFile defFile = uiApp.Application.OpenSharedParameterFile();
    if (defFile == null)
    {
        TaskDialog.Show("Error", "Shared parameter file could not be opened.");
        return;
    }

    DefinitionGroup defGroup = defFile.Groups.get_Item(groupName) ?? defFile.Groups.Create(groupName);

    // Create the definition
    ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions(paramName, SpecTypeId.String.Text)
    {
        Visible = true,
        UserModifiable = true
    };

    Definition definition = defGroup.Definitions.get_Item(paramName) ?? defGroup.Definitions.Create(options);

    // Bind to ViewSheet category
    Category sheetCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Sheets);
    Category viewCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Views);
    Category scheduleCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Schedules);

    CategorySet catSet = uiApp.Application.Create.NewCategorySet();
    catSet.Insert(sheetCat);
    catSet.Insert(viewCat);
    catSet.Insert(scheduleCat);

    // Instance binding (vs. Type binding)
    InstanceBinding binding = uiApp.Application.Create.NewInstanceBinding(catSet);

    // Add parameter to the document
    BindingMap bindingMap = doc.ParameterBindings;

    using (Transaction tx = new Transaction(doc, "Add Shared Parameter"))
    {
        tx.Start();
        if (!bindingMap.Contains(definition))
        {
            bindingMap.Insert(definition, binding, GroupTypeId.IdentityData);
        }
        tx.Commit();
    }
}

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
    Document doc = commandData.Application.ActiveUIDocument.Document;

    CreateSharedParameter(commandData.Application, doc, "ViewOrganization", "ProjectBrowser.Group");

    // Get all View instances in the document
    FilteredElementCollector viewCollector = new FilteredElementCollector(doc)
        .OfClass(typeof(View))
        .WhereElementIsNotElementType();

    // Get all ViewSchedule instances in the document
    FilteredElementCollector scheduleCollector = new FilteredElementCollector(doc)
        .OfClass(typeof(ViewSchedule))
        .WhereElementIsNotElementType();

    // Get all ViewSheet instances in the document
    FilteredElementCollector sheetCollector = new FilteredElementCollector(doc)
        .OfClass(typeof(ViewSheet))
        .WhereElementIsNotElementType();

    using (Transaction acTrans = new Transaction(doc, "SetSharedParamValue"))
    {
        acTrans.Start();

        foreach (Element element in scheduleCollector)
        {
            Parameter group = element.LookupParameter("Organization.ProjectBrowser.Group");
            if (group != null)
            {
                string disValue = group.HasValue ? group.AsValueString() : "";
                Parameter sharedGroup = element.LookupParameter("Organization.ProjectBrowser.Group1");
                if (sharedGroup != null)
                {
                    sharedGroup.Set(disValue); // will throw an exception if it's ViewSheet or View
                }
            }

            Parameter subGroup = element.LookupParameter("Organization.ProjectBrowser.Subgroup");
            if (subGroup != null)
            {
                string disValue = subGroup.HasValue ? subGroup.AsValueString() : "";
                Parameter sharedSubGroup = element.LookupParameter("Organization.ProjectBrowser.Subgroup1");
                if (sharedSubGroup != null)
                {
                    sharedSubGroup.Set(disValue);
                }
            }
        }

        acTrans.Commit();
    }

    return Result.Succeeded;
}

问题原因及解决思路

核心原因

Revit API中,View(视图)和ViewSheet(图纸)这类内置元素类型,直接绑定共享参数到OST_Views、OST_Sheets顶层类别时,会受到Revit内部的只读限制——这类元素的部分参数区域被系统锁定,常规实例绑定无法突破该限制。而ViewSchedule(明细表)不属于此类严格限制的内置元素,因此可以正常赋值。

可行解决办法

1. 绑定到视图具体子类别

不要直接绑定到OST_Views顶层类别,而是绑定到其下的具体子类别,比如平面图、立面图、3D视图等,图纸类别可直接保留。修改绑定代码如下:

// 替换原CategorySet创建逻辑
CategorySet catSet = uiApp.Application.Create.NewCategorySet();

// 绑定图纸类别
catSet.Insert(doc.Settings.Categories.get_Item(BuiltInCategory.OST_Sheets));

// 绑定具体视图子类别,而非OST_Views顶层
catSet.Insert(doc.Settings.Categories.get_Item(BuiltInCategory.OST_FloorPlanViews));
catSet.Insert(doc.Settings.Categories.get_Item(BuiltInCategory.OST_ElevationViews));
catSet.Insert(doc.Settings.Categories.get_Item(BuiltInCategory.OST_3DViews));
catSet.Insert(doc.Settings.Categories.get_Item(BuiltInCategory.OST_Schedules));

2. 用TypeBinding替代InstanceBinding(场景受限)

如果参数不需要每个实例单独赋值,同类型视图/图纸可共用一个值,可切换为类型绑定:

// 替换原InstanceBinding创建代码
TypeBinding binding = uiApp.Application.Create.NewTypeBinding(catSet);

注意:部分视图类型的参数仍可能存在编辑限制,需测试验证。

3. 确认参数可编辑属性

确保创建共享参数时,ExternalDefinitionCreationOptions的UserModifiable设为true(你的代码已设置,可再次确认):

ExternalDefinitionCreationOptions options = new ExternalDefinitionCreationOptions(paramName, SpecTypeId.String.Text)
{
    Visible = true,
    UserModifiable = true // 必须开启,否则绑定正确也无法编辑
};

4. 通过Definition查找参数(避免名称冲突)

LookupParameter可能因名称冲突返回错误参数,建议通过共享参数的Definition直接查找:

// 保存或重新获取共享参数的Definition引用
Definition sharedParamDef = defGroup.Definitions.get_Item(paramName);
Parameter sharedGroup = element.get_Parameter(sharedParamDef);
if (sharedGroup != null && !sharedGroup.IsReadOnly)
{
    sharedGroup.Set(disValue);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 00:16:06