Пример #1
0
        private bool OverrideGraphics(Autodesk.Revit.DB.View activeView)
        {
            bool result = false;

            try
            {
                //known issues: doesn't work with linked files and 3D Views
                //Creates the lists of ElementId to pass to the Projection Color Override by Element
                //those are just empty container at the moment
                FilteredElementCollector collector = new FilteredElementCollector(m_doc);

                List <ElementId> ids0 = new List <ElementId>();
                List <ElementId> ids1 = new List <ElementId>();
                List <ElementId> ids2 = new List <ElementId>();


                //It works fine for 2D views, building sections and elevations for example
                //it should work also with floor plans and even tilted Detail Views
                //but it wasn't my goal when I started
                //won't work for a 3D View
                int clip = activeView.get_Parameter(BuiltInParameter.VIEWER_BOUND_FAR_CLIPPING).AsInteger();
                //If the far clipping is not active the default depth is 10 feet and won't work correctly
                if (clip == 0)
                {
                    TaskDialog.Show("View Depth Override", "In order to use this macro far clipping must be activated.");
                }
                else
                {
                    //If the far clipping is active then the view depth can be subdivided into 3 segments:
                    //foreground (0)
                    //middle (1)
                    //background (2)
                    ids0 = IntegralCollection(activeView, 0);
                    ids1 = IntegralCollection(activeView, 1);
                    ids2 = IntegralCollection(activeView, 2);
                    //Just a check to handle some common errors, for instance not even one
                    //ElementId was found in the foreground view interval
                    if (ids0.Count == 0)
                    {
                        TaskDialog.Show("View Depth Override", "Something went wrong in the closer segment.\n\nPlease adjust the view depth to include some objects.");

                        ElementId e = new FilteredElementCollector(m_doc).OfCategory(BuiltInCategory.OST_Walls).FirstElement().Id;
                        ids0.Add(e);
                        ids1.Add(e);
                        ids2.Add(e);
                    }
                    else
                    {
                        //Again just a check to handle some common errors, in this case not even one
                        //ElementId was found in the background view interval
                        //because the view depth is too much rather then just enough to enclos
                        //the objects in the model
                        if (ids2.Count == 0)
                        {
                            TaskDialog.Show("View Depth Override", "Something went wrong in the farther segment to be overridden in Grey 192.\n\nPlease check that the view depth in the current view is just enough to include the objects you need.");
                            ElementId e = new FilteredElementCollector(m_doc).OfCategory(BuiltInCategory.OST_Walls).FirstElement().Id;
                            ids2.Add(e);
                            ids1.Add(e);
                        }
                    }
                }
                //Begins the transaction to override the elements
                using (Transaction t = new Transaction(m_doc, "View Depth Override"))
                {
                    t.Start();
                    while (ids0.Count > 0)
                    {
                        //Stores the color for the foreground
                        OverrideGraphicSettings gSettings = activeView.GetElementOverrides(ids0.First());
                        Color Color0 = gSettings.ProjectionLineColor;

                        if (ids1.Count != 0)
                        {
                            OverrideGraphicSettings gSettings1 = new OverrideGraphicSettings();
                            gSettings1.SetProjectionLineColor(new Color((byte)128, (byte)128, (byte)128));

                            foreach (ElementId eId in ids1)
                            {
                                activeView.SetElementOverrides(eId, gSettings1);
                            }
                        }
                        else
                        {
                            //Just a precaution, not sure it is really necessary
                            TaskDialog.Show("View Depth Override", "Something went wrong in the middle segment to be overridden in Grey 128.\n\nPlease check that the view depth in the current view is just enough to include the objects you need.");
                            break;
                        }
                        if (ids2.Count != 0)
                        {
                            OverrideGraphicSettings gSettings2 = new OverrideGraphicSettings();
                            gSettings2.SetProjectionLineColor(new Color((byte)192, (byte)192, (byte)192));

                            foreach (ElementId eId in ids2)
                            {
                                activeView.SetElementOverrides(eId, gSettings2);
                            }
                        }
                        else
                        {
                            //Overrides the background segment
                            TaskDialog.Show("View Depth Override", "Something went wrong in the farther segment to be overridden in Grey 192.\n\nPlease check that the view depth in the current view is just enough to include the objects you need.");
                            break;
                        }
                        //Resets the foreground color in case of objects overlapping
                        //foreground and middle segment
                        gSettings.SetProjectionLineColor(Color0);
                        foreach (ElementId eId in ids0)
                        {
                            activeView.SetElementOverrides(eId, gSettings);
                        }
                        break;
                    }
                    m_doc.Regenerate();
                    m_app.ActiveUIDocument.RefreshActiveView();
                    t.Commit();
                }
                result = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to override graphics.\n" + ex.Message, "Override Graphics - " + activeView.Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(result);
        }