REVIT API: Problem with the color changing of element by selecting it

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all,
I'm a newbie to Revit API and have very little programming knowledge. I wanted to create a Revit Add ins using Visual Studio 2019 and use it in Revit 2018.
I'm writing a program to change the color of the element by selecting it. (It's my first step and will go into advanced into later steps) The coding of the program as shown below which I took references from others.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
namespace ChangeElementColour
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string FAIL, ElementSet elements)
{
//Default setting: Get application and document objects
UIApplication uiapp = commandData.Application;
Document doc = uiapp.ActiveUIDocument.Document;
//Define a reference Object to accept the pick result
Reference pickedref = null;
//Pick an element to change colour
Selection sel = uiapp.ActiveUIDocument.Selection;
pickedref = sel.PickObject(ObjectType.Element, "Pick element to change its colour");
Element elem = doc.GetElement(pickedref);
//Create new colour
Color color = uiapp.ActiveUIDocument.Application.Application.Create.NewColor();
color.Blue = (byte)255;
color.Red = (byte)0;
color.Green = (byte)255;
//Make Transaction of new colour
List<ElementId> ids = new List<ElementId>(1);
ids.Add(elem.Id);
Transaction trans = new Transaction(doc);
trans.Start("ChangeColour");
OverrideGraphicSettings ogs = new OverrideGraphicSettings();
ogs.SetProjectionLineColor(color);
uiapp.ActiveUIDocument.Document.ActiveView.SetElementOverrides(ids, color);
trans.Commit();
return Result.Succeeded;
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The red highlighted words (ids, color) are the errors I'm facing for days and unable to solve it. Please kindly advice and help me in this.
Many thanks.