Set Parameter not Working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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 RevitUIApplication 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.