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

如何用C#通过Revit API为Revit族添加参数?报错求助

Revit族宏添加参数时文档再生失败错误的解决方法

问题说明

运行宏代码为Revit族添加参数时,触发以下错误:

System.Exception: Document regeneration failed. at
MacroModule.executeMacro_(MacroModule*, AString* MacroName) at
MacroModule.executeMacro_(MacroModule*, AString* ) at
UIMacroGeneralManager.runMacro(UIMacroGeneralManager*, MacroModule*
pModule, AString* macroName)

原宏代码如下:

public void FamilyInstanceParameters()
{
    Document document = this.ActiveUIDocument.ActiveView.Document;
    if (!document.IsFamilyDocument)
    {
        TaskDialog.Show("Info", "This is not a FamilyDocument");
    }
    else
    {
        try
        {
            Transaction transaction = new Transaction(document);
            transaction.Start("Param");

            // Get the family document category
            Family family = document.OwnerFamily;
            Category category = family.FamilyCategory;

            FamilyType familyType = document.FamilyManager.NewType("New Type A");
            document.FamilyManager.CurrentType = familyType;


            // Parameter group
            BuiltInParameterGroup builtInParamGroup = BuiltInParameterGroup.PG_IDENTITY_DATA;


            document.Regenerate();
            FamilyParameter familyParameter = document.FamilyManager
               .AddParameter("parameterName", builtInParamGroup, category, false);

            document.FamilyManager.Set(familyParameter, "parameterValue");

            transaction.Commit();
            transaction.Dispose();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}

错误原因及修复方案

核心问题

  1. 事务内调用Regenerate违规:Revit API不允许在事务执行过程中手动调用document.Regenerate(),这会打乱文档的事务状态,引发再生失败。
  2. AddParameter参数误用:AddParameter方法中代表参数类型的参数应该是ParameterType枚举值,原代码传入族的Category对象属于参数类型错误。

修复步骤

  • 移除事务内的document.Regenerate()调用,Revit会在事务提交时自动处理文档更新。
  • 将添加参数时的category参数替换为对应类型的ParameterType枚举(比如文本参数用ParameterType.Text)。
  • 用using语句管理事务,自动释放资源,避免手动调用Dispose()的潜在问题。

修正后的代码

public void FamilyInstanceParameters()
{
    Document document = this.ActiveUIDocument.ActiveView.Document;
    if (!document.IsFamilyDocument)
    {
        TaskDialog.Show("提示", "当前文档不是族文档");
        return;
    }

    using (Transaction transaction = new Transaction(document, "添加族参数"))
    {
        try
        {
            transaction.Start();

            // 创建新族类型并设为当前类型
            FamilyType familyType = document.FamilyManager.NewType("新类型A");
            document.FamilyManager.CurrentType = familyType;

            // 指定参数组与参数类型
            BuiltInParameterGroup paramGroup = BuiltInParameterGroup.PG_IDENTITY_DATA;
            ParameterType paramType = ParameterType.Text;

            // 添加族参数
            FamilyParameter familyParameter = document.FamilyManager
               .AddParameter("参数名称", paramGroup, paramType, false);

            // 设置参数值
            document.FamilyManager.Set(familyParameter, "参数值");

            transaction.Commit();
            TaskDialog.Show("成功", "族参数添加完成");
        }
        catch (Exception ex)
        {
            transaction.RollBack();
            TaskDialog.Show("错误", $"操作失败:{ex.Message}");
        }
    }
}

额外注意事项

  • 确保当前打开的是可编辑的族文档,部分系统族可能限制自定义参数添加。
  • 选择的参数组需匹配参数类型,避免出现参数显示异常。
  • 始终通过事务包裹所有修改文档的操作,保证操作的原子性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 14:55:21