BIM World
A Professional BIM Learning Platform


Revit API Tutorial: How to Replace View Styles Through Secondary Development

The goal is to replace graphics within exported view templates. Since view templates in Revit are also considered views, this approach applies universally across all views.

The process is straightforward. You can access style overrides in a view using OverrideGraphicSettings. However, careful filtering of the retrieved data is necessary.

The method for overriding by category is very similar and can be applied almost identically.

Below is a sample implementation:

        private class ViewGraphicOverride
        {
            static Document _doc;
            static View _view;
            static Category _category;

            public ViewGraphicOverride(Document document, View view, string categoryName)
            {
                _doc = document;
                _view = view;
                _category = _doc.Settings.Categories.get_Item(categoryName);
            }

            // Visibility status
            public string CategoryVisible
            {
                get
                {
                    return _view.GetVisibility(_category) ? "Yes" : "No";
                }
            }

            // Projection line weight
            public string ProjectionLineWeight
            {
                get
                {
                    string lineWeight = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    if (ogs.ProjectionLineWeight != -1) lineWeight = ogs.ProjectionLineWeight.ToString();
                    return lineWeight;
                }
            }

            // Projection line color
            public string ProjectionLineColor
            {
                get
                {
                    string lineColor = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    if (ogs.ProjectionLineColor.IsValid)
                    {
                        Color rgb = ogs.ProjectionLineColor;
                        lineColor = $"{rgb.Red},{rgb.Green},{rgb.Blue}";
                    }
                    return lineColor;
                }
            }

            // Projection line pattern name
            public string ProjectionLinePatternName
            {
                get
                {
                    string patternName = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    ElementId patternId = ogs.ProjectionLinePatternId;
                    if (patternId.IntegerValue != -1)
                    {
                        if (patternId.IntegerValue == -3000010)
                            patternName = "Solid Line";
                        else
                            patternName = LinePatternElement.GetLinePattern(_doc, patternId).Name;
                    }
                    return patternName;
                }
            }

            // Projection fill pattern visibility
            public string IsProjectionFillPatternVisible
            {
                get
                {
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    return ogs.IsProjectionFillPatternVisible ? "Yes" : "No";
                }
            }

            // Projection fill color
            public string ProjectionFillColor
            {
                get
                {
                    string fillColor = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    if (ogs.ProjectionFillColor.IsValid)
                    {
                        Color rgb = ogs.ProjectionFillColor;
                        fillColor = $"{rgb.Red},{rgb.Green},{rgb.Blue}";
                    }
                    return fillColor;
                }
            }

            // Projection fill pattern name
            public string ProjectionFillPatternName
            {
                get
                {
                    string fillPatternName = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    ElementId patternId = ogs.ProjectionFillPatternId;
                    if (patternId.IntegerValue != -1)
                        fillPatternName = _doc.GetElement(patternId).Name;
                    return fillPatternName;
                }
            }

            // Transparency level
            public int Transparency
            {
                get
                {
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    return ogs.Transparency;
                }
            }

            // Cut line weight
            public string CutLineWeight
            {
                get
                {
                    string lineWeight = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    if (ogs.CutLineWeight != -1) lineWeight = ogs.CutLineWeight.ToString();
                    return lineWeight;
                }
            }

            // Cut line color
            public string CutLineColor
            {
                get
                {
                    string lineColor = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    if (ogs.CutLineColor.IsValid)
                    {
                        Color rgb = ogs.CutLineColor;
                        lineColor = $"{rgb.Red},{rgb.Green},{rgb.Blue}";
                    }
                    return lineColor;
                }
            }

            // Cut line pattern name
            public string CutLinePatternName
            {
                get
                {
                    string patternName = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    ElementId patternId = ogs.CutLinePatternId;
                    if (patternId.IntegerValue != -1)
                    {
                        if (patternId.IntegerValue == -3000010)
                            patternName = "Solid Line";
                        else
                            patternName = LinePatternElement.GetLinePattern(_doc, patternId).Name;
                    }
                    return patternName;
                }
            }

            // Cut fill pattern visibility
            public string IsCutFillPatternVisible
            {
                get
                {
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    return ogs.IsCutFillPatternVisible ? "Yes" : "No";
                }
            }

            // Cut fill color
            public string CutFillColor
            {
                get
                {
                    string fillColor = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    if (ogs.CutFillColor.IsValid)
                    {
                        Color rgb = ogs.CutFillColor;
                        fillColor = $"{rgb.Red},{rgb.Green},{rgb.Blue}";
                    }
                    return fillColor;
                }
            }

            // Cut fill pattern name
            public string CutFillPatternName
            {
                get
                {
                    string fillPatternName = "<No Override>";
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    ElementId patternId = ogs.CutFillPatternId;
                    if (patternId.IntegerValue != -1)
                        fillPatternName = _doc.GetElement(patternId).Name;
                    return fillPatternName;
                }
            }

            // Halftone status
            public string Halftone
            {
                get
                {
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    return ogs.Halftone ? "Yes" : "No";
                }
            }

            // Detail level
            public string DetailLevel
            {
                get
                {
                    string detailLevel;
                    OverrideGraphicSettings ogs = _view.GetCategoryOverrides(_category.Id);
                    switch (ogs.DetailLevel)
                    {
                        case ViewDetailLevel.Coarse:
                            detailLevel = "Coarse";
                            break;
                        case ViewDetailLevel.Medium:
                            detailLevel = "Medium";
                            break;
                        case ViewDetailLevel.Fine:
                            detailLevel = "Fine";
                            break;
                        default:
                            detailLevel = "By View";
                            break;
                    }
                    return detailLevel;
                }
            }
        }
xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » Revit API Tutorial: How to Replace View Styles Through Secondary Development

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up