Set Parameter not Working

Set Parameter not Working

igor.barcelosC54YD
Enthusiast Enthusiast
366 Views
4 Replies
Message 1 of 5

Set Parameter not Working

igor.barcelosC54YD
Enthusiast
Enthusiast

Hello, after quite a lot of research in this forum I'm having some troubles for set a parameter value for a Revit element. I quite new to Revit Api using c# but I will explain my situation. 

 

  • First I tried to update a parameter value using a button with an IExternalCommand.
    For example, for the elementId = 536767 I can modify the parameter "Commentaires" setting "New Comments" as follows and that works like a charm. 


    namespace DEF4Revit
    {
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

    public class ImportDB : IExternalCommand

    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {

    //Get the Current Session / Project from Revit

    UIApplication uiApp = commandData.Application;


    //Get the Current Document from the Current Session
    Document doc = uiApp.ActiveUIDocument.Document;


    int idInt = 536767;

    ElementId myId = new ElementId(idInt);
    Element myElement = doc.GetElement(myId);

    using (Transaction trans = new Transaction(doc, "Import Data"))
    {
    try
    {
    trans.Start();

    Parameter parameter = myElement.LookupParameter("Commentaires");
    parameter.Set("New Comments");

     

    trans.Commit();

    }
    catch (Exception e)
    {
    Debug.Print(e.Message);
    }
    }
    TaskDialog.Show("Impor Data", "Data successfuly imported");
    return Result.Succeeded;
    }

    }
    }

  • But I now I want to exactly the same thing but not using a button (IExternalCommand) but with a function (Receive) that is called after button click. 

    namespace DEF4Revit
    {
    [Transaction(TransactionMode.Manual)]
    public class Connector
    {

    public UIDocument Uidoc { get; }
    public Document Doc { get; }
    public Connector(UIDocument _Uidoc)
    {
    Uidoc = _Uidoc;
    }


    public List<DEFElement> Receive(string elementList)
    {

    Document Doc = Uidoc.Document;
    Selection selection = Uidoc.Selection;


    ICollection<ElementId> selectedIds = selection.GetElementIds();

    MessageBox.Show(elementList);
    List<DEFElement> _elementList = JsonConvert.DeserializeObject<List<DEFElement>>(elementList);
    List<DEFElement> list = new List<DEFElement>();

    string message = "";


    foreach (DEFElement element in _elementList)
    {
    list.Add(element);
    message = message + " " + element.name;
    }
    MessageBox.Show(message);

     


    // Update the element //

    foreach (ElementId id in selectedIds)
    {
    MessageBox.Show("The id of the element is " + id);

    Element element = Doc.GetElement(id);


    string elementName = element.Name;

    MessageBox.Show("The name is " + elementName);

    Parameter param = element.LookupParameter("Commantaires");

    using (Transaction trans = new Transaction(Doc,"Set parameter"))
    {
    try
    {
    trans.Start();

    param.Set("New Comments");

    trans.Commit();
    }
    catch (Exception err)
    {
    TaskDialog.Show("Error", "Commit not Worked");

    }

    }

    }


    return list;

    }

    }
    }
  • The second method doesn't work at all. Nothing is changed in the element. Any ideas what I'm missing here ? 
    Thank you all. 

 

 

 

 

 

0 Likes
367 Views
4 Replies
Replies (4)
Message 2 of 5

ridaabderrahmane
Enthusiast
Enthusiast

Hi there,

the two methods are exactly the same, you just made a typo in the second method:

Parameter param = element.LookupParameter("Commantaires");

"Commentaires" instead of "Commantaires".

In general it is a bad idea to use the LookupParameter function.

Try instead to to use the built in parameter enumeration : element.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).Set(""New Comments");

 

This way you are sure that you didn't make a typo in the name of the parameter, and if you add a question mark before the Set function you will make sure that the set os done only if the parameter is not null.

element.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)?.Set(""New Comments");

 

Hope this helps.

0 Likes
Message 3 of 5

igor.barcelosC54YD
Enthusiast
Enthusiast

Hi, thanks for your response. 

I fixed the parameter name and I've tried your suggestions but I got the same result. The modification is not done... The button that I'm using is in a WTF page, I wonder if that's the problem.. Any other ideas ? 

0 Likes
Message 4 of 5

RPTHOMAS108
Mentor
Mentor

I would go back to the SDK samples and ask yourself questions such as is your interaction modal or modeless i.e. in your button press handler how you are getting a valid context outside of IExternalCommand?

0 Likes
Message 5 of 5

mhannonQ65N2
Collaborator
Collaborator

Is it throwing an exception? selection.GetElementIds() should throw an exception if it is called without valid API context. There would also be an exception if param is null (which would be the case before you fixed the typo). If no exception is being thrown, it is likely that Receive isn't getting called.

0 Likes