【问题标题】:Revit Getting/Setting values from a paramRevit 从参数获取/设置值
【发布时间】:2021-07-20 19:25:21
【问题描述】:

我需要为 Revit 中的模型获取和设置一组参数的新值,因此我尝试为其创建一个插件,以便获取 JSON 文件并在模型中实现。我尝试使用此代码获取测试参数,但它一直返回 null。我在 revit 上将参数创建到家庭中以尝试此操作。不知道是不是这个问题。

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

        Element e = SelectElement(uidoc, doc);
        Parameter parameter = e.LookupParameter("comment_test");

        using(Transaction t = new Transaction(doc, "parameter"))
        {
            t.Start("param");
            try
            {
                parameter.Set("Test Comment");
            }
            catch { }
            t.Commit();
        }




        return Result.Succeeded;
    }

    //Select the element
    public Element SelectElement(UIDocument uidoc, Document doc)
    {
        Reference r = uidoc.Selection.PickObject(ObjectType.Element);
        Element el= uidoc.Document.GetElement(r);

        return el;
    }

    // Get the param value and return as string
    public string GetParameterValue(Parameter parameter)
    {
        switch (parameter.StorageType)
        {
            case StorageType.ElementId:
                return parameter.AsElementId().IntegerValue.ToString();

            case StorageType.Integer:
            case StorageType.None:
            case StorageType.Double:
                return parameter.AsValueString();

            case StorageType.String:
                return parameter.AsString();

            default:
                return "";

        }
    }

在我得到这段代码的同一个地方,我得到另一个返回内置参数的地方,所以我尝试了我在视频中看到的,但它在调试时也返回 null。

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

        Element e = SelectElement(uidoc, doc);
        Parameter parametro = e.get_Parameter(BuiltInParameter.ALL_MODEL_DESCRIPTION);
        using(Transaction t = new Transaction(doc, "parametro"))
        {
            t.Start("param");
            try
            {
                parametro.Set("Teste de Inserção de Comentário");
            }
            catch { }
            t.Commit();
        }

        return Result.Succeeded;
    }

【问题讨论】:

    标签: c# revit-api


    【解决方案1】:

    这对我有用。在你的 catch 语句中添加一行来显示你的错误:

    #region Namespaces
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Selection;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    
    #endregion
    
    namespace combineParameters
    {
        [Transaction(TransactionMode.Manual)]
        public class Command : IExternalCommand
        {
            public Result Execute(
                  ExternalCommandData commandData,
                  ref string message,
                  ElementSet elements)
            {
                UIApplication uiapp = commandData.Application;
                UIDocument uidoc = uiapp.ActiveUIDocument;
                Application app = uiapp.Application;
                Document doc = uidoc.Document;
    
                Element e = SelectElement(uidoc, doc);
                Parameter parameter = e.LookupParameter("Comments");
    
                using (Transaction t = new Transaction(doc, "parameter"))
                {
                    
                    try
                    {
                        t.Start();
                        parameter.Set("New");
                        t.Commit();
                    }
                    
                    
                    catch (Exception err)
                    {
                        TaskDialog.Show("Error", err.Message);
                        t.RollBack();
                    }
                  
                }
                return Result.Succeeded;
            }
    
            //Select the element
            public Element SelectElement(UIDocument uidoc, Document doc)
            {
                Reference r = uidoc.Selection.PickObject(ObjectType.Element);
                Element el = uidoc.Document.GetElement(r);
    
                return el;
            }
    
            // Get the param value and return as string
            public string GetParameterValue(Parameter parameter)
            {
                switch (parameter.StorageType)
                {
                    case StorageType.ElementId:
                        return parameter.AsElementId().IntegerValue.ToString();
    
                    case StorageType.Integer:
                    case StorageType.None:
                    case StorageType.Double:
                        return parameter.AsValueString();
    
                    case StorageType.String:
                        return parameter.AsString();
    
                    default:
                        return "";
    
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-23
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 2021-10-26
      相关资源
      最近更新 更多