1

I need to get and set a new value for a bunch of params into a model in Revit, so Im trying to create an Addin for it, so I can get a JSON file and implement into the model. I Tried this code to get a test param, but it keeps returning null. I created the param into the family on revit to try this. I dont know if this is the problem.

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 "";

        }
    }

In the same place that I get this code, I got another one that returns a built-in param, so I tried as I saw on the video, but it also return null on debug.

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;
    }
MackMag
  • 23
  • 4

1 Answers1

0

This works on my end. In your catch statement add a line to show your error:

#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 "";

            }
        }
    }
}
smaren
  • 1
  • 1