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

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

Anonymous
Not applicable
1,209 Views
1 Reply
Message 1 of 2

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

Anonymous
Not applicable

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.

0 Likes
1,210 Views
1 Reply
Reply (1)
Message 2 of 2

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Anonymous ,

 

check the below link

https://www.revitapidocs.com/2015/a6f1ced3-1f1c-dd42-c0ca-f15f301d1cad.htm 

 

In the link, you will find that the SetElementOverride method takes elementid and overrideGraphicssettings as parameters, not List<ElementID> and color.

 

so to change element color try using the below code

Color color = new Color(255,0,255);
                
                IList<ElementId> ids =new List<ElementId>();

                OverrideGraphicSettings ogs = new OverrideGraphicSettings();
                ogs.SetProjectionLineColor(color);

                foreach(ElementId eid in ids)
                {
                    doc.ActiveView.SetElementOverrides(eid, ogs);
                }

 

I hope this helps.

 

 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes