Change Element Colour

We once looked at

changing the colour of model or detail curves
by
setting their category line style.

Here is another simple little question, on setting the colour of an element, with a clear and simple answer, also by Joe Ye:

Question: How can I programmatically change the colour of an annotation symbol, e.g. a tag, in a Revit project?

Answer: You can change the colour of individual elements in a specified view using the View class ProjColorOverrideByElement property.
It takes a collection of elements as an argument and overrides their projection lines’ colour in the given view.

Here is a VSTA code snippet showing how it can be used:


public void ChangeElementColor()
{
  Application app = this.ActiveUIDocument
    .Application.Application;
 
  UIDocument uidoc = this.ActiveUIDocument;
  Document doc = uidoc.Document;
 
  Color color = app.Create.NewColor();
  color.Blue = ( byte ) 150;
  color.Red = ( byte ) 200;
  color.Green = ( byte ) 200;
 
  Selection sel = uidoc.Selection;
 
  Reference ref1 = sel.PickObject(
    ObjectType.Element,
    "Pick element to change its colour" );
 
  Element elem = ref1.Element;
 
  List<ElementId> ids = new List<ElementId>( 1 );
  ids.Add( elem.Id );
 
  Transaction trans = new Transaction( doc );
  trans.Start( "ChangeColor" );
 
  doc.ActiveView.set_ProjColorOverrideByElement(
    ids, color );
 
  trans.Commit();
}

Many thanks to Joe for providing this solution!


Comments

25 responses to “Change Element Colour”

  1. Rudolf Honke Avatar
    Rudolf Honke

    Hi Jeremy !
    Is it possible to override the color of a room, despite the fact that there is an AreaScheme attached to the active View ?

  2. Hi, Jeremy, how are you?
    I used this code:

    ModelLine modelLine = doc.Create.NewModelCurve(line, NewSketchPlanePassLine(line)) as ModelLine;

    to create model Lines, but why they will disappear after shutting down the application? I want the model lines can be saved. How can I do it?

  3. Hi, Jeremy, how are you?
    Do you have any idea on my question? such as I need to refresh view after line creation or configurate some setting on Revit?
    If I can not find the solusion, my company will decide to use other software that I do not want to happen.

  4. Dear Rudolf,
    Is it possible to do through the user interface? If not, then there is probably no possibility through the API either.
    Cheers, Jeremy.

  5. Rudolf Honke Avatar
    Rudolf Honke

    Ah, of course.
    Thank you for answering this (redundant) question.
    It’s evident: you can’t do anything via API that cannot be done via GUI.*
    Thanks again,

    Rudolf

    except setting invisible parameters, for example

  6. Dear Junyu,
    If you create a model line in the database, then it is definitely saved. Of course you need to ensure that you have a valid transaction open, and commit it after the addition, or use automatic transaction mode.
    Every model line added to the Revit document is stored in the database and will remain there when the document is saved.
    There are indeed many user interface settings in Revit that affect the display of model lines, but that has nothing to do with the API.
    Cheers, Jeremy.

  7. Rudolf Honke Avatar
    Rudolf Honke

    Dear Junyu,
    perhaps you should make sure that your Revit is not in trial mode.
    You say that lines disappear after shutting down Revit.
    It says in the RevitAPI.chm that the Document.Save() method throws an exception when trying to invoke it in trial mode.
    So in this case nothing will be saved.
    Best regards,
    Rudolf

  8. goran.bubas@fer.hr Avatar
    goran.bubas@fer.hr

    Hello Jeremy,
    When I tried to debug your code i got this message:
    “starting a new transaction is not permitted…”
    Can you help me what could be the case?
    Regards,
    Goran.

  9. goran.bubas@fer.hr Avatar
    goran.bubas@fer.hr

    Hi Jeremy, again :)
    I did silly mistake and I managed to correct it by myself. your core works fine.
    I have one more question:
    If you have some element IDs, how would you do color change of that specific elements?
    Regards,
    Goran.

  10. Dear Goran,
    Congratulations on finding your mistake, and on making a silly mistake rather than a clever one. The latter are mostly much harder to discover and fix.
    Regarding changing the colour of specific elements, that is exactly the question answered by the sample code above.
    Cheers, Jeremy.

  11. goran.bubas@fer.hr Avatar
    goran.bubas@fer.hr

    Hello Jeremy,
    Tnx for answering me, and also for that email couple of days ago.
    What about changing surface color (not just projection lines) of an element (Revit 2011)?
    Regards,
    Goran.

  12. Hi there,
    is there a way to change de surface color of elements?
    We can do this in the UI, but I can’t figure out how to
    do this with the API…
    Thanks

  13. Dear Goran,
    From the UI, you can probably use the paint tool for this. There is no API access to that data in Revit 2011, though.
    Cheers, Jeremy.

  14. Dear Ben,
    New functionality in the Revit 2012 API provides access to the FaceSplitter class, and the Face HasRegions property and GetRegions method.
    These give at least read access to the surface data of an element created using the paint tool.
    I don’t know about creating the surface data programmatically, though.
    Cheers, Jeremy.

  15. Hi Jeremy,
    Having used ProjColorOverrideByElement to change the color. how do I remove the overide?
    Regards
    Paul

  16. Dear Paul,
    I would start by reading the documentation on the View.ProjColorOverrideByElement property in the Revit API help file RevitAPI.chm. It says:
    Field Value: The override color of the elements that are to be overridden. A null reference (Nothing in Visual Basic) indicates that no override should be applied.
    Is that what you are looking for?
    Cheers, Jeremy.

  17. Hi Jeremy,
    “Sometime you just can’t see the wood for the trees….”
    Setting to “IsNothing” works a treat.
    Cheers
    Paul

  18. Dear Paul,
    I am glad to hear it works.
    Cheers, Jeremy.

  19. Luc Van Asch Avatar
    Luc Van Asch

    Hi. I would like to apply a color scheme to the elements in a view, based on certain parameters – eg pricing (like a GIS legend). View.ProjColorOverrideByElement and View.CutColorOverrideByElement are nice, but any idea about something like FILLColorOverrideByElement. I was looking for it in the 2012 API, but in vain. I now create a temporary material with the desired color and apply it to the element(s) I want to color. It works, but it’s slow (causes the model to regenerate), especially when there’s al lot of elements to process. Any idea’s of how to speed the coloring of element surfaces up? Changing the FILLColorOverrideByElement is possible in the UI, so it should be around somewhere in the dll’s… Thanks in advance for any replies! Luc

  20. Dear Luc,
    I am not sure that the functionality you are looking for is accessible programmatically, or not completely.
    One thing I do now is that the Revit MEP API HVAC sample application uses a colour fill scheme to display air flow volume per room surface graphically.
    For that, it uses some kind of manually set up schedule, and the only thing done programmatically is to set the appropriate parameter value on each element. The parameter values are picked up be the schedule somehow and determine the fill colour in the graphics display.
    Cheers, Jeremy.

  21. Luc Van Asch Avatar
    Luc Van Asch

    Thanks for the reply, Jeremy. I’ve been on holidays and I’m currently working on another (Revit API) application, so it took me some time to read your reaction. I will definitely take a look at the MEP HVAC application you’ve mentioned. Does this come with the standard download of the 2012 API? Or do I have to search for it on ADN?
    Thank you.
    Luc

  22. Dear Luc,
    I just published the Revit MEP 2012 version of the ADN MEP HVAC sample code, so you can access it from here:
    http://thebuildingcoder.typepad.com/blog/2011/08/mep-sample-code-for-revit-2012.html
    Previous versions and more documentation are available from the AU web site.
    Cheers, Jeremy.

  23. Manish Avatar
    Manish

    hi jeremy
    I want to know how to change color of elbow element
    Code:
    Color color = _rvtDoc.Application.Create.NewColor();
    color.Blue = 255;
    color.Red = 0;
    color.Green = 0;
    Transaction trans = TransStart(_rvtDoc);
    _rvtDoc.ActiveView.set_ProjColorOverrideByElement(iElementIds, color);
    trans.commit();
    This code not work for Elbow element.

  24. Tim Berteotti Avatar
    Tim Berteotti

    Hi Jeremy,
    First, thank you for keeping this blog. It has been very helpful while I am learning to use the Revit API.
    I am trying to change the detail level of an element within a view using the SetElementOverrides method in Revit 2014. The OverrideGraphicSettings object includes a property for DetailLevel that I am able to read and write to successfully. However, these changes are not reflected in the view while other overrides are. I also tried to set the detail level through a view filter using the SetFilterOverrides method with the same results. Is this a situation where “if I can’t do it with the user interface, it can’t be done”, or am I missing something?
    Thanks!
    Tim

  25. Dear Tim,
    Thank you for your appreciation. I am very glad The Building Coder has proved helpful for you.
    I cannot tell off-hand whether the behaviour you describe is intentional or not.
    If the other overrides work fine and this one seems to change the property successfully but still has no effect, then it sounds like we should investigate it further.
    Can you provide a minimal reproducible case to test?
    http://thebuildingcoder.typepad.com/blog/about-the-author.html#1b
    Thank you!
    Cheers, Jeremy.

Leave a Reply to Tim BerteottiCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading