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

Revit API多类别添加共享参数失败问题求助

问题描述

已通过Revit API成功为单个类别添加共享参数,但使用for循环为多个类别添加该参数时失败,预期为墙、柱等类别的每个实例添加该共享参数。

单个类别添加参数的可行代码
UIDocument uidoc = this.Application.ActiveUIDocument;
Document doc = uidoc.Document;

String modulePath = doc.PathName;
String tit = doc.Title;

String paramFile = modulePath.Substring(0,modulePath.Length-47) + "\\addParameterTest.txt";

if (File.Exists(paramFile))
{
    File.Delete(paramFile);
}

FileStream fs = File.Create(paramFile);
fs.Close();

this.Application.Application.SharedParametersFilename = paramFile;

DefinitionFile parafile = this.Application.Application.OpenSharedParameterFile();

ExternalDefinitionCreationOptions externalDefinitionCreationOptions = new ExternalDefinitionCreationOptions("New Parameter", ParameterType.Text);

DefinitionGroup apiGroup = parafile.Groups.Create("Test");
Definition newSharedParamDef = apiGroup.Definitions.Create(externalDefinitionCreationOptions);

Category columnCat = doc.Settings.Categories.get_Item(BuiltInCategory.OST_StructuralColumns);

CategorySet categories = this.Application.Application.Create.NewCategorySet();
InstanceBinding binding = this.Application.Application.Create.NewInstanceBinding(categories);
if (columnCat.AllowsBoundParameters) {
    using (Transaction trn = new Transaction(doc,"add parameter")) {
        trn.Start();
        categories.Insert(columnCat);
        doc.ParameterBindings.Insert(newSharedParamDef, binding);
        trn.Commit();
    }
}
多类别循环添加参数的失败代码
UIDocument uidoc = this.Application.ActiveUIDocument;
Document doc = uidoc.Document;

String modulePath = doc.PathName;
String tit = doc.Title;

String paramFile = modulePath.Substring(0,modulePath.Length-47) + "\\addParameterTest.txt";

if (File.Exists(paramFile))
{
    File.Delete(paramFile);
}

FileStream fs = File.Create(paramFile);
fs.Close();

this.Application.Application.SharedParametersFilename = paramFile;

DefinitionFile parafile = this.Application.Application.OpenSharedParameterFile();

ExternalDefinitionCreationOptions externalDefinitionCreationOptions = new ExternalDefinitionCreationOptions("New Parameter", ParameterType.Text);

DefinitionGroup apiGroup = parafile.Groups.Create("Test");
Definition newSharedParamDef = apiGroup.Definitions.Create(externalDefinitionCreationOptions);

Categories cat = doc.Settings.Categories;

CategorySet categories = this.Application.Application.Create.NewCategorySet();

InstanceBinding binding = this.Application.Application.Create.NewInstanceBinding(categories);

using (Transaction trn = new Transaction(doc,"add parameter")) {
    trn.Start();
    foreach (Category element in cat) {
        if (element.AllowsBoundParameters)
        {
            categories.Insert(element);
            doc.ParameterBindings.Insert(newSharedParamDef, binding);
        }
    }   
    trn.Commit();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 10:49:56