Model and Detail Curve Colour

We have had several questions on how to change the colour of model or detail curves.
Here is an answer to this question from a recent case handled by Joe:

Question: I am drawing a detail arc using the Revit API DetailArc class and
trying to change its properties like colour, graphic style etc.
How can this be achieved?

Answer: You can change the detail arc line colour by changing the colour of the detail curve category’s line style.

Here is The Building Coder sample command

CmdDetailCurves

demonstrating the creation of a few detail curves and enhanced to modify the line colour of their graphics style:


Application app = commandData.Application;
Document doc = app.ActiveDocument;
View view = doc.ActiveView;
 
// Create a geometry line
 
XYZ startPoint = new XYZ( 0, 0, 0 );
XYZ endPoint = new XYZ( 10, 10, 0 );
 
Line geomLine = app.Create.NewLine(
  startPoint, endPoint, true );
 
// Create a geometry arc
 
XYZ end0 = new XYZ( 1, 0, 0 );
XYZ end1 = new XYZ( 10, 10, 10 );
XYZ pointOnCurve = new XYZ( 10, 0, 0 );
 
Arc geomArc = app.Create.NewArc(
  end0, end1, pointOnCurve );
 
// Create a geometry plane
 
XYZ origin = new XYZ( 0, 0, 0 );
XYZ normal = new XYZ( 1, 1, 0 );
 
Plane geomPlane = app.Create.NewPlane(
  normal, origin );
 
// Create a sketch plane in current document
 
SketchPlane sketch = doc.Create.NewSketchPlane(
  geomPlane );
 
// Create a DetailLine element using the 
// newly created geometry line and sketch plane
 
DetailLine line = doc.Create.NewDetailCurve(
  view, geomLine ) as DetailLine;
 
// Create a DetailArc element using the 
// newly created geometry arc and sketch plane
 
DetailArc arc = doc.Create.NewDetailCurve(
  view, geomArc ) as DetailArc;
 
// Change detail curve colour.
// Initially, this only affects the newly 
// created curves. However, when the view 
// is refreshed, all detail curves will 
// be updated.
 
GraphicsStyle gs = arc.LineStyle as GraphicsStyle;
 
gs.GraphicsStyleCategory.LineColor
  = new Color( 250, 10, 10 );
 
return CmdResult.Succeeded;

Note that you need to ensure that detail curves are visible in the view that you are working in in order to see the changes taking effect.

Also note that the newly defined colour will be applied to all detail curves after the view is refreshed or closed and reopened.
It is not possible to change one single detail line’s colour individually in the long run.

Here are a couple of detail curves drawn in the default drawing before running this command:

Default detail curves

Here are the new detail curves added by the command with their modified line colour:

Detail curves with modified settings

When the view is refreshed or closed and reopened, all detail lines will adopt the new colour.

Here is
version 1.1.0.58
of the complete Building Coder sample source code and Visual Studio solution including the updated version of this command.

Many thanks to Joe for handling this case!


Comments

13 responses to “Model and Detail Curve Colour”

  1. Hi Jeremy,
    Is it possible to change also the Material category of a single GraphicStyle as you did with the LineColor category?
    You did for DetailCurve derived elements. What about ModelCurve derived elements? Will this procedure work same way it did for DetailCurve?
    Regards,
    Fernando.

  2. Hi Fernando,
    It is possible to change the Material property of category, which is retrieved by GraphicsStyleCategory.
    The way to change detail line color is applicable to model curve.
    Please see below code that I tested.
    public class ChangeModelLine : IExternalCommand
    {
    public IExternalCommand.Result Execute(ExternalCommandData commandData,
    ref string message, ElementSet elements)
    {
    Autodesk.Revit.Application app = commandData.Application;
    Document doc = app.ActiveDocument;
    XYZ end0 = new XYZ(-1, 0, 0);
    XYZ end1 = new XYZ(1, 0, 0);
    XYZ pointOnCurve = new XYZ(0, 1, 0);
    Arc geomArc = commandData.Application.Create.NewArc(end0, end1, pointOnCurve);
    ModelArc arc = doc.Create.NewModelCurve(geomArc, doc.ActiveView.SketchPlane) as ModelArc;
    GraphicsStyle gs = arc.LineStyle as GraphicsStyle;
    gs.GraphicsStyleCategory.LineColor = new Color(100, 100, 200);
    //change to a new material
    //get a material
    ElementIterator ei = doc.get_Elements(typeof(MaterialSteel));
    Material mat = null;
    while (ei.MoveNext())
    {
    mat = ei.Current as Material;
    if (null != mat)
    {
    if (mat.Name.Equals(“Metal – Steel”))
    break;
    }
    }
    gs.GraphicsStyleCategory.Material = mat;
    return IExternalCommand.Result.Succeeded;
    }
    }
    Regards,
    Joe

  3. Mille grazie, Joe! Cheers, Jeremy.

  4. Stanley Audrey Avatar
    Stanley Audrey

    Is it possible to add new line style to the category? I see the function CanAddSubCategory (boolean) but I don’t know how to create new line style/subcategory.
    Thanks
    Stanley Audrey

  5. Dear Stanley Audrey,
    Sure it is. Have a look at the Categories.NewSubcategory method in the Revit API help file. It adds a new subcategory to the Revit document. Samples on using it in C# and VB are provided. As far as I can tell from them, they execute in the family document context. I don’t know whether you can also add a category in the project document context.
    Cheers, Jeremy.

  6. Christian Degen Avatar
    Christian Degen

    Hello Jeremy
    Why is the normal in this example 1,1,0 and not 0,0,1 ?
    Did I miss something?
    Thanks
    Christian Degen

  7. Dear Christian,
    Thank you very much for this way overdue question!
    Hey guys, why did nobody else ask this before?
    This is an error, pure and simple, which came from copying someone else’s code.
    I discovered it when new error checks and messages were added in Revit 2011, and updated the sample command accordingly:
    http://thebuildingcoder.typepad.com/blog/2010/05/detail-curve-must-indeed-lie-in-plane.html
    The best thing to do, obviously, is to download the very most recent version of The Building Coder sample code, which is currently version 2011.0.81.0 provided in
    http://thebuildingcoder.typepad.com/blog/2010/11/access-to-sketch-and-sketch-plane.html
    Thank you again for noticing and pointing this out!
    Cheers, Jeremy.

  8. LeeJaeYoung Avatar
    LeeJaeYoung

    HI, I am LeeJaeYOung. Korean.
    I study api in Revit 2013.
    I study according to your source code.
    error occurs.
    I’m sorry to incorrect English.
    using System;
    using System.Collections;
    using System.Windows.Forms;
    using Autodesk;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.DB.Structure;
    using System.IO;
    using Autodesk.Revit.Creation;
    namespace DetailArc
    {
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Automatic)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    public class Class1
    {
    public class DetailArc : IExternalCommand
    {
    public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
    {
    // Application app = commandData.Application;
    // Document doc = app.ActiveDocument;
    // View view = doc.ActiveView;
    Autodesk.Revit.UI.UIApplication app = commandData.Application;
    UIDocument document = app.ActiveUIDocument;
    Autodesk.Revit.DB.Document doc = document.Document;
    Autodesk.Revit.DB.View view = document.Document.ActiveView;
    // Create a geometry line
    XYZ startPoint = new XYZ(0, 0, 0);
    XYZ endPoint = new XYZ(10, 10, 0);
    Line geomLine = app.Application.Create.NewLine(startPoint, endPoint, true);
    // Create a geometry arc
    XYZ end0 = new XYZ(1, 0, 0);
    XYZ end1 = new XYZ(10, 10, 10);
    XYZ pointOnCurve = new XYZ(10, 0, 0);
    Arc geomArc = app.Application.Create.NewArc(end0, end1, pointOnCurve);
    // Create a geometry plane
    XYZ origin = new XYZ(0, 0, 0);
    XYZ normal = new XYZ(1, 1, 0);
    Plane geomPlane = app.Application.Create.NewPlane(
    normal, origin);
    // Create a sketch plane in current document
    SketchPlane sketch = doc.Create.NewSketchPlane(geomPlane);
    // Create a DetailLine element using the
    // newly created geometry line and sketch plane
    DetailLine line = doc.Create.NewDetailCurve(view, geomLine) as DetailLine;
    // Create a DetailArc element using the
    // newly created geometry arc and sketch plane
    // DetailArc arc = doc.Create.NewDetailCurve(view, geomArc) as DetailArc;
    DetailArc arc = doc.Create.NewDetailCurve(view, geomArc) as DetailArc;
    // Change detail curve colour.
    // Initially, this only affects the newly
    // created curves. However, when the view
    // is refreshed, all detail curves will
    // be updated.
    GraphicsStyle gs = arc.LineStyle as GraphicsStyle;
    gs.GraphicsStyleCategory.LineColor
    = new Color(250, 10, 10);
    return CmdResult.Succeeded;
    }
    }
    }
    }
    error
    Error 1 Cannot convert type ‘Autodesk.Revit.DB.DetailCurve’ to ‘DetailArc.Class1.DetailArc’ via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion c:\users\team\documents\visual studio 2010\Projects\DetailArc\DetailArc\Class1.cs 70 33 DetailArc
    Error 2 ‘DetailArc.Class1.DetailArc’ does not contain a definition for ‘LineStyle’ and no extension method ‘LineStyle’ accepting a first argument of type ‘DetailArc.Class1.DetailArc’ could be found (are you missing a using directive or an assembly reference?) c:\users\team\documents\visual studio 2010\Projects\DetailArc\DetailArc\Class1.cs 78 40 DetailArc
    Error 3 The name ‘CmdResult’ does not exist in the current context c:\users\team\documents\visual studio 2010\Projects\DetailArc\DetailArc\Class1.cs 83 24 DetailArc

  9. Dear LeeJaeYoung,
    I would suggest you simply grab the most up-to-date version of The Building Coder sample code, instead of trying to work with this possibly obsolete version.
    The latest update was version 2013.0.99.4 and is provided here:
    http://thebuildingcoder.typepad.com/blog/2012/10/slab-boundary-revisited.html
    Cheers, Jeremy.

  10. LeeJaeYoung Avatar
    LeeJaeYoung

    thank you
    as your teaching i knew detailline
    happy new year

  11. Dear LeeJaeYoung,
    Thank you for your appreciation.
    Happy New Year to you too!
    Cheers, Jeremy.

  12. Mina Maher Avatar
    Mina Maher

    hi, first thank you for these great tutorials.
    i wanted to ask, when i try to get the Graphics Style Category of imported AutoCAD Geometry Line it returns null for the category but on revit they still has visible colors and in visibility options they have categories with layer names from AutoCAD so how to filter them by color or change their color

  13. Dear Mina,
    Thank you for your query and appreciation.
    I am very glad you like it :-)
    There are a large number of different methods and levels at which you can set graphics attributes in Revit:
    http://thebuildingcoder.typepad.com/blog/2013/08/attributes-relationships-and-other-stuff.html#6
    I would suggest exploring your model using RevitLookup or other more in-depth methods to determine where the required attributes are defined:
    http://thebuildingcoder.typepad.com/blog/2013/11/intimate-revit-database-exploration-with-the-python-shell.html
    Cheers, Jeremy.

Leave a Reply to LeeJaeYoungCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading