Exemplo n.º 1
0
        /// <summary>
        /// Paint solid by AVF
        /// </summary>
        /// <param name="solid">Solid to be painted</param>
        /// <param name="view">The view that shows solid</param>
        private void PaintSolid(Solid solid, View view)
        {
            String viewName         = view.Name;
            SpatialFieldManager sfm = SpatialFieldManager.GetSpatialFieldManager(view);

            if (sfm == null)
            {
                sfm = SpatialFieldManager.CreateSpatialFieldManager(view, 1);
            }

            if (m_schemaId != -1)
            {
                IList <int> results = sfm.GetRegisteredResults();

                if (!results.Contains(m_schemaId))
                {
                    m_schemaId = -1;
                }
            }

            // set up the display style
            if (m_schemaId == -1)
            {
                AnalysisResultSchema resultSchema1 = new AnalysisResultSchema("PaintedSolid " + viewName, "Description");

                AnalysisDisplayStyle displayStyle = AnalysisDisplayStyle.CreateAnalysisDisplayStyle(this.RevitDoc, "Real_Color_Surface" + viewName, new AnalysisDisplayColoredSurfaceSettings(), new AnalysisDisplayColorSettings(), new AnalysisDisplayLegendSettings());

                resultSchema1.AnalysisDisplayStyleId = displayStyle.Id;

                m_schemaId = sfm.RegisterResult(resultSchema1);
            }

            // get points of all faces in the solid
            FaceArray faces = solid.Faces;
            Transform trf   = Transform.Identity;

            foreach (Face face in faces)
            {
                int                  idx     = sfm.AddSpatialFieldPrimitive(face, trf);
                IList <UV>           uvPts   = null;
                IList <ValueAtPoint> valList = null;
                ComputeValueAtPointForFace(face, out uvPts, out valList, 1);

                FieldDomainPointsByUV pnts = new FieldDomainPointsByUV(uvPts);
                FieldValues           vals = new FieldValues(valList);
                sfm.UpdateSpatialFieldPrimitive(idx, pnts, vals, m_schemaId);
            }
        }
Exemplo n.º 2
0
        private static AnalysisDisplayStyle GetDefaultAnalysisDisplayStyle(Document doc)
        {
            var defaultDisplayStyle = FilterDefaultSpaceSyntaxAnalysisDisplayStyle(doc);

            AnalysisDisplayStyle analysisDisplayStyle = null;

            if (defaultDisplayStyle.Count() == 0)
            {
                analysisDisplayStyle = CreateDefaultSpaceSyntaxAnalysisDisplayStyle(doc);
            }
            else
            {
                analysisDisplayStyle = defaultDisplayStyle.Cast <AnalysisDisplayStyle>().ElementAt(0);
            }

            return(analysisDisplayStyle);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Retrieve or set up the attenuation
        /// display style for the given view.
        /// </summary>
        static void SetAvfDisplayStyle(View view)
        {
            Document doc = view.Document;

            ElementId id = AnalysisDisplayStyle.FindByName(
                doc, _displayStyleName);

            if (ElementId.InvalidElementId == id)
            {
                using (Transaction t = new Transaction(doc))
                {
                    t.Start("Create AVF Display Style");

                    AnalysisDisplayColoredSurfaceSettings
                        coloredSurfaceSettings = new
                                                 AnalysisDisplayColoredSurfaceSettings();

                    coloredSurfaceSettings.ShowGridLines = false;

                    AnalysisDisplayColorSettings colorSettings
                        = new AnalysisDisplayColorSettings();

                    colorSettings.MinColor = new Color(0, 255, 0);
                    colorSettings.MaxColor = new Color(255, 0, 0);

                    AnalysisDisplayLegendSettings legendSettings
                        = new AnalysisDisplayLegendSettings();

                    legendSettings.ShowLegend = true;

                    AnalysisDisplayStyle analysisDisplayStyle
                        = AnalysisDisplayStyle.CreateAnalysisDisplayStyle(
                              doc, _displayStyleName, coloredSurfaceSettings,
                              colorSettings, legendSettings);

                    view.AnalysisDisplayStyleId
                        = analysisDisplayStyle.Id;

                    t.Commit();
                }
            }
        }
Exemplo n.º 4
0
        private static void CreateAVFDisplayStyle(Document doc, Autodesk.Revit.DB.View view)
        {
            Transaction t = new Transaction(doc, "Create AVF style");

            t.Start();
            AnalysisDisplayColoredSurfaceSettings coloredSurfaceSettings = new AnalysisDisplayColoredSurfaceSettings();

            coloredSurfaceSettings.ShowGridLines = true;
            AnalysisDisplayColorSettings  colorSettings  = new AnalysisDisplayColorSettings();
            AnalysisDisplayLegendSettings legendSettings = new AnalysisDisplayLegendSettings();

            legendSettings.ShowLegend = false;
            string name = "Paint Solid";
            AnalysisDisplayStyle analysisDisplayStyle = new FilteredElementCollector(doc).OfClass(typeof(AnalysisDisplayStyle)).Cast <AnalysisDisplayStyle>().FirstOrDefault(q => q.Name == name);

            if (analysisDisplayStyle == null)
            {
                analysisDisplayStyle = AnalysisDisplayStyle.CreateAnalysisDisplayStyle(doc, name, coloredSurfaceSettings, colorSettings, legendSettings);
            }

            view.AnalysisDisplayStyleId = analysisDisplayStyle.Id;
            t.Commit();
        }
Exemplo n.º 5
0
        public static ElementId CreateAnalysisScheme(List <AnalysisDisplayColorEntry> colours, Document doc, string name, bool showLegend)
        {
            var result = ElementId.InvalidElementId;

            var colouredSurfaceSettings = new AnalysisDisplayColoredSurfaceSettings();

            colouredSurfaceSettings.ShowContourLines = true;
            colouredSurfaceSettings.ShowGridLines    = false;

            var colourSettings = new AnalysisDisplayColorSettings();

            colourSettings.MaxColor = colours.Last().Color;
            colourSettings.MinColor = colours.First().Color;
            colourSettings.SetIntermediateColors(colours);
            colourSettings.ColorSettingsType = AnalysisDisplayStyleColorSettingsType.GradientColor;

            if (AnalysisDisplayStyle.IsNameUnique(doc, name, null))
            {
                var ads = AnalysisDisplayStyle.CreateAnalysisDisplayStyle(
                    doc,
                    name,
                    colouredSurfaceSettings,
                    colourSettings,
                    new AnalysisDisplayLegendSettings());
                if (!showLegend)
                {
                    ads.GetLegendSettings().ShowLegend = false;
                }
                result = ads.Id;
            }
            else
            {
                result = AnalysisDisplayStyle.FindByName(doc, name);
            }

            return(result);
        }
Exemplo n.º 6
0
        private static Element createSolidDisplayStyle(Document doc, string name)
        {
            AnalysisDisplayColoredSurfaceSettings style = new AnalysisDisplayColoredSurfaceSettings();

            style.ShowGridLines = true;

            AnalysisDisplayColorSettings colors = new AnalysisDisplayColorSettings();

            colors.ColorSettingsType = AnalysisDisplayStyleColorSettingsType.GradientColor;
            colors.MinColor          = new Color((byte)255, (byte)165, (byte)0);
            colors.MaxColor          = new Color((byte)0, (byte)0, (byte)255); // orange to blue

            // we need to create it
            Element pc =
                AnalysisDisplayStyle.CreateAnalysisDisplayStyle(doc, name,
                                                                style,
                                                                colors,
                                                                new AnalysisDisplayLegendSettings()
            {
                ShowLegend = false
            });

            return(pc);
        }
Exemplo n.º 7
0
        /// <summary>
        ///   We setup our preferred style for displaying the results
        /// </summary>
        /// <remarks>
        ///   This is to make it easier to run this sample on any document.
        ///   We create a gradient-like style (unless it already exists)
        ///   and register it with the given view. Then we set it as the
        ///   default analysis stile in that view.
        /// </remarks>
        ///
        private void SetupDisplayStyle(Autodesk.Revit.DB.View view)
        {
            const string         styleName = "SDK2014-AL Style";
            AnalysisDisplayStyle ourStyle  = null;

            // check if we are already using our preferred display style

            if (ElementId.InvalidElementId != view.AnalysisDisplayStyleId)
            {
                ourStyle = view.Document.GetElement(view.AnalysisDisplayStyleId) as AnalysisDisplayStyle;
                if (ourStyle.Name == styleName)
                {
                    return;
                }
            }

            // Look if the style exist at all in the document

            FilteredElementCollector collector = new FilteredElementCollector(view.Document);
            ICollection <Element>    allStyles = collector.OfClass(typeof(AnalysisDisplayStyle)).ToElements();

            foreach (Element elem in allStyles)
            {
                if (elem.Name == styleName)
                {
                    using (Transaction trans = new Transaction(view.Document))
                    {
                        trans.Start("Change Analysis Display Style");
                        view.AnalysisDisplayStyleId = elem.Id;
                        trans.Commit();
                        return;
                    }
                }
            }

            // we do not have out style yet - let's create it

            // a) grid lines
            AnalysisDisplayColoredSurfaceSettings surface = new AnalysisDisplayColoredSurfaceSettings();

            surface.ShowGridLines = true;

            // b) colors
            AnalysisDisplayColorSettings colors = new AnalysisDisplayColorSettings();
            Color orange = new Color(255, 205, 0);
            Color green  = new Color(0, 255, 0);

            colors.MinColor = orange;
            colors.MaxColor = green;

            // c) the legend
            AnalysisDisplayLegendSettings legend = new AnalysisDisplayLegendSettings();

            legend.NumberOfSteps       = 10;
            legend.Rounding            = 0.1;
            legend.ShowDataDescription = false;
            legend.ShowLegend          = false;

            // creation of a style needs to be in a transaction
            using (Transaction trans = new Transaction(view.Document))
            {
                trans.Start("Set Analysis Display Style");
                ourStyle = AnalysisDisplayStyle.CreateAnalysisDisplayStyle(view.Document, styleName, surface, colors, legend);
                view.AnalysisDisplayStyleId = ourStyle.Id;
                trans.Commit();
            }
        }
        /// <summary>
        /// Paint a solid in a new named view
        /// </summary>
        /// <param name="s">solid</param>
        /// <param name="viewName">Given the name of view</param>
        public void PaintSolid(Solid s, String viewName)
        {
            View view;

            if (!viewNameList.Contains(viewName))
            {
                view      = m_doc.Create.NewView3D(new XYZ(1, 1, 1));
                view.Name = viewName;
                viewNameList.Add(viewName);
            }
            else
            {
                view = (((new FilteredElementCollector(m_doc).
                          OfClass(typeof(View))).Cast <View>()).
                        Where(e => e.Name == viewName)).First <View>();
            }

            SpatialFieldManager sfm = SpatialFieldManager.GetSpatialFieldManager(view);

            if (sfm == null)
            {
                sfm = SpatialFieldManager.CreateSpatialFieldManager(view, 1);
            }

            if (SchemaId != -1)
            {
                IList <int> results = sfm.GetRegisteredResults();

                if (!results.Contains(SchemaId))
                {
                    SchemaId = -1;
                }
            }

            if (SchemaId == -1)
            {
                AnalysisResultSchema resultSchema1 = new AnalysisResultSchema("PaintedSolid" + viewName, "Description");

                AnalysisDisplayStyle displayStyle = AnalysisDisplayStyle.CreateAnalysisDisplayStyle(
                    m_doc,
                    "Real_Color_Surface" + viewName,
                    new AnalysisDisplayColoredSurfaceSettings(),
                    new AnalysisDisplayColorSettings(),
                    new AnalysisDisplayLegendSettings());

                resultSchema1.AnalysisDisplayStyleId = displayStyle.Id;

                SchemaId = sfm.RegisterResult(resultSchema1);
            }

            FaceArray faces = s.Faces;
            Transform trf   = Transform.Identity;

            foreach (Face face in faces)
            {
                int idx = sfm.AddSpatialFieldPrimitive(face, trf);

                IList <UV>           uvPts   = null;
                IList <ValueAtPoint> valList = null;
                ComputeValueAtPointForFace(face, out uvPts, out valList, 1);

                FieldDomainPointsByUV pnts = new FieldDomainPointsByUV(uvPts);

                FieldValues vals = new FieldValues(valList);

                sfm.UpdateSpatialFieldPrimitive(idx, pnts, vals, SchemaId);
            }
        }
Exemplo n.º 9
0
        public bool DisplayDefaultSettings()
        {
            bool result = false;

            try
            {
                comboBoxStyle.Items.Clear();
                comboBoxUnits.Items.Clear();

                FilteredElementCollector collector     = new FilteredElementCollector(doc);
                List <Element>           displayStyles = collector.OfClass(typeof(AnalysisDisplayStyle)).ToElements().ToList();

                if (displayStyles.Count > 0)
                {
                    foreach (Element element in displayStyles)
                    {
                        AnalysisDisplayStyle style = element as AnalysisDisplayStyle;

                        AnalysisDisplayColoredSurfaceSettings coloredSurface;
                        try { coloredSurface = style.GetColoredSurfaceSettings(); }
                        catch { coloredSurface = null; }

                        AnalysisDisplayMarkersAndTextSettings markersText;
                        try { markersText = style.GetMarkersAndTextSettings(); }
                        catch { markersText = null; }

                        if (null != coloredSurface)
                        {
                            styleDictionary.Add(style.Name, "Colored Surface");
                        }
                        else if (null != markersText)
                        {
                            styleDictionary.Add(style.Name, "Markers And Text");
                        }

                        comboBoxStyle.Items.Add(style.Name);
                    }

                    if (comboBoxStyle.Items.Count > 0)
                    {
                        if (null != currentSetting.DisplayStyle)
                        {
                            for (int i = 0; i < comboBoxStyle.Items.Count; i++)
                            {
                                if (comboBoxStyle.Items[i].ToString() == currentSetting.DisplayStyle)
                                {
                                    comboBoxStyle.SelectedIndex = i;
                                }
                            }
                        }
                        else
                        {
                            comboBoxStyle.SelectedIndex = 0;
                        }
                    }

                    comboBoxUnits.Items.Add("none");
                    comboBoxUnits.Items.Add("feet");
                    comboBoxUnits.Items.Add("inches");
                    comboBoxUnits.Items.Add("meters");
                    comboBoxUnits.Items.Add("square feet");
                    comboBoxUnits.Items.Add("square meters");
                    comboBoxUnits.Items.Add("cubic feet");
                    comboBoxUnits.Items.Add("cubic meters");

                    if (null != currentSetting.Units)
                    {
                        for (int i = 0; i < comboBoxUnits.Items.Count; i++)
                        {
                            if (comboBoxUnits.Items[i].ToString() == currentSetting.Units)
                            {
                                comboBoxUnits.SelectedIndex = i;
                            }
                        }
                    }
                    else
                    {
                        comboBoxUnits.SelectedIndex = 0;
                    }
                    result = true;
                }
                else
                {
                    MessageBox.Show("An analysis display style is required for the HOK AVF tool to work.\nGo to Additional Settings in the Manage Tab and select Analysis Display Style.  Then re-run the HOK AVF Tool.",
                                    "Missing Display Style", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    result = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to display default settings.\n" + ex.Message, "DisplayStyleManager:DisplayDefaultSettings", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                result = false;
            }
            return(result);
        }
Exemplo n.º 10
0
 private static bool testDocNeedsInitializing(Document doc)
 {
     // does it already exist?  look for the latest kind...
     return(AnalysisDisplayStyle.FindByName(doc, "SolidView").IntegerValue < 0);
 }
Exemplo n.º 11
0
        public override Value Evaluate(FSharpList <Value> args)
        {
            AnalysisDisplayStyle ads = CreateDisplayStyle();

            return(Value.NewContainer(ads));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Set analysis display style to switch off grid lines
        /// and use greyscale values in active view.
        /// </summary>
        void SetAnalysisDisplayStyle(Document doc)
        {
            AnalysisDisplayStyle analysisDisplayStyle;

            const string styleName
                = "Revit Webcam Display Style";

            // extract existing display styles with specific name

            FilteredElementCollector a
                = new FilteredElementCollector(doc);

            IList <Element> elements = a
                                       .OfClass(typeof(AnalysisDisplayStyle))
                                       .Where(x => x.Name.Equals(styleName))
                                       .Cast <Element>()
                                       .ToList();

            if (0 < elements.Count)
            {
                // use the existing display style

                analysisDisplayStyle = elements[0]
                                       as AnalysisDisplayStyle;
            }
            else
            {
                // create new display style:

                // coloured surface settings:

                AnalysisDisplayColoredSurfaceSettings
                    coloredSurfaceSettings
                    = new AnalysisDisplayColoredSurfaceSettings();

                coloredSurfaceSettings.ShowGridLines = false;

                // color settings:

                AnalysisDisplayColorSettings colorSettings
                    = new AnalysisDisplayColorSettings();

                colorSettings.MaxColor = new Color(255, 255, 255);
                colorSettings.MinColor = new Color(0, 0, 0);

                // legend settings:

                AnalysisDisplayLegendSettings legendSettings
                    = new AnalysisDisplayLegendSettings();

                legendSettings.NumberOfSteps       = 10;
                legendSettings.Rounding            = 0.05;
                legendSettings.ShowDataDescription = false;
                legendSettings.ShowLegend          = true;

                //// extract legend text:

                //a = new FilteredElementCollector( doc );

                //elements = a
                //  .OfClass( typeof( TextNoteType ) )
                //  .Where( x => x.Name == "LegendText" )
                //  .Cast<Element>()
                //  .ToList();

                //if( 0 < elements.Count )
                //{
                //  // if LegendText exists, use it for this display style

                //  TextNoteType textType = elements[0] as TextNoteType;

                //  // warning CS0618:
                //  // SetTextTypeId(ElementId, Document) is obsolete:
                //  // this method will be obsolete from 2012.

                //  legendSettings.SetTextTypeId( textType.Id, doc );
                //}

                // Create the analysis display style:

                var transaction = new Transaction(doc);
                transaction.Start("Create AnalysisDisplayStyle");
                try
                {
                    analysisDisplayStyle = AnalysisDisplayStyle
                                           .CreateAnalysisDisplayStyle(
                        doc, styleName, coloredSurfaceSettings,
                        colorSettings, legendSettings);

                    // assign the display style to the active view

                    doc.ActiveView.AnalysisDisplayStyleId
                        = analysisDisplayStyle.Id;

                    transaction.Commit();
                }
                catch
                {
                    transaction.RollBack();
                    throw;
                }
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Set analysis display style to switch off grid lines
        /// and use greyscale values in active view.
        /// </summary>
        void SetAnalysisDisplayStyle(Document doc)
        {
            AnalysisDisplayStyle analysisDisplayStyle;

            const string styleName
                = "Revit Webcam Display Style";

            // extract existing display styles with specific name

            FilteredElementCollector a
                = new FilteredElementCollector(doc);

            IList <Element> elements = a
                                       .OfClass(typeof(AnalysisDisplayStyle))
                                       .Where(x => x.Name.Equals(styleName))
                                       .Cast <Element>()
                                       .ToList();

            if (0 < elements.Count)
            {
                // use the existing display style

                analysisDisplayStyle = elements[0]
                                       as AnalysisDisplayStyle;
            }
            else
            {
                // create new display style:

                // coloured surface settings:

                AnalysisDisplayColoredSurfaceSettings
                    coloredSurfaceSettings
                    = new AnalysisDisplayColoredSurfaceSettings();

                coloredSurfaceSettings.ShowGridLines = false;

                // color settings:

                AnalysisDisplayColorSettings colorSettings
                    = new AnalysisDisplayColorSettings();

                colorSettings.MaxColor = new Color(255, 255, 255);
                colorSettings.MinColor = new Color(0, 0, 0);

                // legend settings:

                AnalysisDisplayLegendSettings legendSettings
                    = new AnalysisDisplayLegendSettings();

                legendSettings.NumberOfSteps       = 10;
                legendSettings.Rounding            = 0.05;
                legendSettings.ShowDataDescription = false;
                legendSettings.ShowLegend          = true;

                // extract legend text:

                a = new FilteredElementCollector(doc);

                elements = a
                           .OfClass(typeof(TextNoteType))
                           .Where(x => x.Name == "LegendText")
                           .Cast <Element>()
                           .ToList();

                if (0 < elements.Count)
                {
                    // if LegendText exists, use it for this display style

                    TextNoteType textType = elements[0] as TextNoteType;

                    legendSettings.SetTextTypeId(textType.Id, doc);
                }

                // create the analysis display style:

                analysisDisplayStyle = AnalysisDisplayStyle
                                       .CreateAnalysisDisplayStyle(
                    doc, styleName, coloredSurfaceSettings,
                    colorSettings, legendSettings);
            }

            // assign the display style to the active view

            doc.ActiveView.AnalysisDisplayStyleId
                = analysisDisplayStyle.Id;
        }
Exemplo n.º 14
0
        private bool IsViewForAnalysis(Autodesk.Revit.DB.View activeView)
        {
            var result = false;

            try
            {
                if (activeView.ViewType == ViewType.FloorPlan)
                {
                    AnalysisDisplayStyle displayStyle = null;

                    var collector        = new FilteredElementCollector(m_doc);
                    var displayStyles    = collector.OfClass(typeof(AnalysisDisplayStyle)).ToElements().Cast <AnalysisDisplayStyle>().ToList();
                    var viewAnlysisStyle = from style in displayStyles where style.Name == "View Analysis" select style;
                    if (viewAnlysisStyle.Any())
                    {
                        displayStyle = viewAnlysisStyle.First();
                    }
                    else
                    {
                        var coloredSurfaceSettings = new AnalysisDisplayColoredSurfaceSettings();
                        coloredSurfaceSettings.ShowGridLines = false;

                        var colorSettings = new AnalysisDisplayColorSettings();
                        var yellow        = new Color(255, 255, 0);
                        var blue          = new Color(0, 128, 255);
                        var colorEntries  = new List <AnalysisDisplayColorEntry> {
                            new AnalysisDisplayColorEntry(blue)
                        };
                        colorSettings.SetIntermediateColors(colorEntries);

                        colorSettings.MinColor          = blue;
                        colorSettings.MaxColor          = yellow;
                        colorSettings.ColorSettingsType = AnalysisDisplayStyleColorSettingsType.SolidColorRanges;

                        var legendSettings = new AnalysisDisplayLegendSettings
                        {
                            NumberOfSteps       = 1,
                            ShowDataDescription = false,
                            ShowLegend          = false
                        };

                        using (var trans = new Transaction(m_doc))
                        {
                            trans.Start("Create an Analysis Display Style");
                            try
                            {
                                displayStyle = AnalysisDisplayStyle.CreateAnalysisDisplayStyle(m_doc, "View Analysis", coloredSurfaceSettings, colorSettings, legendSettings);
                                trans.Commit();
                            }
                            catch (Exception ex)
                            {
                                Log.AppendLog(LogMessageType.EXCEPTION, ex.Message);
                                trans.RollBack();
                            }
                        }
                    }

                    if (null != displayStyle)
                    {
                        using (var trans = new Transaction(m_doc))
                        {
                            trans.Start("Set Display Style");
                            try
                            {
                                m_doc.ActiveView.AnalysisDisplayStyleId = displayStyle.Id;
                                trans.Commit();
                                result = true;
                            }
                            catch (Exception ex)
                            {
                                Log.AppendLog(LogMessageType.EXCEPTION, ex.Message);
                                trans.RollBack();
                                result = false;
                            }
                        }
                    }
                    else
                    {
                        result = false;
                    }
                }
                else
                {
                    result = false;
                    MessageBox.Show("A floor plan should be an active view for the view anlaysis.\n", "View Analysis - Active View", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to get information of the active view.\n" + ex.Message, "View Analysis - Active View", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(result);
        }
Exemplo n.º 15
0
        public static void ActivateAnalysisDisplayStyle(Document doc)
        {
            AnalysisDisplayStyle analysisDisplayStyle = GetDefaultAnalysisDisplayStyle(doc);

            SetAnalysisDisplayStyle(doc, analysisDisplayStyle);
        }
Exemplo n.º 16
0
        public void SetCO2eAnalysisDisplayStyle(View view)
        {
            var t = new Transaction(view.Document);

            t.Start("SetCO2eAnalysisDisplayStyle");

            AnalysisDisplayStyle analysisDisplayStyle = null;
            // Look for an existing analysis display style with a specific name
            var collector1 = new FilteredElementCollector(view.Document);
            ICollection <Element> collection =
                collector1.OfClass(typeof(AnalysisDisplayStyle)).ToElements();
            var displayStyle = from element in collection
                               where element.Name == "CO2e Display Style 1"
                               select element;

            // If display style does not already exist in the document, create it
            if (displayStyle.Count() == 0)
            {
                var coloredSurfaceSettings = new AnalysisDisplayColoredSurfaceSettings {
                    ShowGridLines = false
                };
                var colorSettings = new  AnalysisDisplayColorSettings();
                var deepRed       = new Color(166, 0, 0);
                var red           = new Color(255, 44, 01);
                var orange        = new Color(255, 179, 0);
                var green         = new Color(0, 253, 0);
                var lightGreen    = new Color(128, 255, 12);
                colorSettings.MaxColor = deepRed;
                colorSettings.SetIntermediateColors(new List <AnalysisDisplayColorEntry>
                {
                    new AnalysisDisplayColorEntry(green),
                    new AnalysisDisplayColorEntry(orange),
                    new AnalysisDisplayColorEntry(red)
                });
                colorSettings.MinColor = lightGreen;
                var legendSettings = new AnalysisDisplayLegendSettings
                {
                    NumberOfSteps       = 10,
                    Rounding            = 0.05,
                    ShowDataDescription = false,
                    ShowLegend          = true
                };
                var collector2 = new FilteredElementCollector(view.Document);
                ICollection <Element> elementCollection = collector2.OfClass(typeof(TextNoteType)).ToElements();
                var textElements = from element in collector2
                                   where element.Name == "LegendText"
                                   select element;

                // if LegendText exists, use it for this Display Style
                if (textElements.Count() > 0)
                {
                    var textType =
                        textElements.Cast <TextNoteType>().ElementAt(0);
                    legendSettings.SetTextTypeId(textType.Id, view.Document);
                }
                analysisDisplayStyle = AnalysisDisplayStyle.CreateAnalysisDisplayStyle(view.Document,
                                                                                       "CO2e Display Style 1", coloredSurfaceSettings, colorSettings, legendSettings);
            }
            else
            {
                analysisDisplayStyle =
                    displayStyle.Cast <AnalysisDisplayStyle>().ElementAt(0);
            }
            // now assign the display style to the view
            view.AnalysisDisplayStyleId = analysisDisplayStyle.Id;

            t.Commit();
        }