Results 1 to 5 of 5

Thread: Isolate Elements

  1. #1
    Member
    Join Date
    2011-04
    Posts
    8
    Login to Give a bone
    0

    Default Isolate Elements

    I would like to isolate some selected elements. But the View Object only provides function "Hide" and "Unhide". I try to hide all elements and after that, unhide elements I want to isolate. But there is one problem here, I don't know how to hide all elements (only object such as wall, floor, room, door.., but not objects such as View..) in the document. If I check the property "canbehidden" of elements, View is also the element I can hide.

    So, anyone has another way to isolate selected elements or suggests me a way to hide all elements?

    Thank you so much!

    Trang

  2. #2
    Member
    Join Date
    2011-04
    Posts
    8
    Login to Give a bone
    0

    Default Re: Isolate Elements

    After reviewing SDK and combining some codes from them, I could isolate selected elements eventually. However, it runs too slowly. Here is the sequence I have done.

    1. Apply SetVisibility(false) for all categories of document.

    2. Apply SetVisibility(true) for all categories of selected elements (ex. these categories called elemCats).

    3. Filter and Hide all elements those belong to elemCats.

    4. Unhide selected Elements.

    anyone has suggestion to make this process better?

    Following is codes:

    Code:
    public class Commands : IExternalCommand
        {
            private UIDocument m_document;    // the active document
            private Hashtable m_allCategories = new Hashtable(); // all categories name with its visibility
            private Hashtable m_categoriesWithName = new Hashtable(); // all categories with its name
           
           
            public Result Execute(
                ExternalCommandData commandData,
                ref string message,
                ElementSet elements)
            {
                //TODO: Add your code here
                try
                {
                    m_document = commandData.Application.ActiveUIDocument;    // the active document
                                    
    
                    // fill out the two table
                    foreach (Category category in m_document.Document.Settings.Categories)
                    {
                        if (category.get_AllowsVisibilityControl(m_document.Document.ActiveView))
                        {
                            m_allCategories.Add(category.Name, category.get_Visible(m_document.Document.ActiveView));
                            m_categoriesWithName.Add(category.Name, category);
                        }
                    }
    
                    ElementSet elems = m_document.Selection.Elements;
    
                    // hide all categories elements
                    foreach (Category cat in m_document.Document.Settings.Categories)
                    {
                        SetVisibility(false, cat.Name);
                    }
    
                    // set the visibility for the selection elements
                    foreach (Element elem in elems)
                    {
                        Category cat = elem.Category;
                        if (null != cat && !string.IsNullOrEmpty(cat.Name))
                        {
                            SetVisibility(true, cat.Name);
                        }
                    }
    
                    List<Element> allElems = new List<Element>();
    
                    foreach (Element elem in elems)
                    {                    
                        Category cat = elem.Category;
                        if (null != cat && !string.IsNullOrEmpty(cat.Name))
                        {
                            BuiltInCategory myCatEnum = (BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), cat.Id.ToString());
                            FilteredElementCollector collector=new FilteredElementCollector(m_document.Document).OfCategory(myCatEnum);
                            List<Element> elemList = collector.ToElements() as List<Element>;
                            foreach (Element e1 in elemList)
                            {
                                allElems.Add(e1);
                            }
                            
                        }
                        
                    }
                    ElementSet allElement = new ElementSet();
                    foreach (Element elem in allElems)
                    {
                        allElement.Insert(elem);
                    }
    
                    m_document.ActiveView.Hide(allElement);
                    m_document.ActiveView.Unhide(elems);  
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                    return Result.Failed;
                }
    
                //Must return some code
                return Result.Succeeded;
            }
    
            //-----------------------------------------------------------------------------------
            /// Set the visibility for the active view
            /// </summary>
            /// <returns>Return true if operation successed, or else, return false.</returns>
            public bool SetVisibility(bool visible, string name)
            {
                try
                {
                    Category cat = m_categoriesWithName[name] as Category;
                    m_document.Document.ActiveView.setVisibility(cat, visible);
                    //or cat.set_Visible(m_document.ActiveView, visible);
                    m_allCategories[cat.Name] = visible;
                }
                catch (Exception)
                {
                    return false;
                }
    
                return true;
            }
        }

  3. #3
    Member
    Join Date
    2010-06
    Location
    Brisbane, Australia
    Posts
    25
    Login to Give a bone
    0

    Default Re: Isolate Elements

    You could probably achieve a decent performance increase by reducing the number of times you call hide / unhide / setVisibility. Specifically, the first part of the code where it hides and then unhides elements by category seems redundant.

    I've used code similar to the following to do this sort of thing.

    Code:
                // Get the selected elements
                var selectedElements = myDocument.Selection.Elements.Cast<Element>().ToArray();
    
                // Use a collector for this particular view
                using (FilteredElementCollector collector = new FilteredElementCollector(myDocument, myView.Id))
                {
                    // Get all elements in the view excluding the selected elements
                    var elementsToHide = collector
                        .WhereElementIsNotElementType()
                        .Excluding(selectedElements.Select(e => e.Id).ToArray())
                        .ToElements();
    
                    // Populate an ElementSet with the elements to be hidden
                    using (var elementSet = new ElementSet())
                    {
                        foreach (var element in elementsToHide)
                        {
                            if (element.CanBeHidden(myView) && !element.IsHidden(myView))
                            {
                                elementSet.Insert(element);
                            }
                        }
    
                        // Hide the elements
                        myView.Hide(elementSet);
                    }
                }
    Note that you may need to consider relationships between elements. For example, in my case I was trying to isolate a hosted element and the hosted element was automatically hidden with it's host. In this case I excluded the host from the ElementSet being hidden, and then called myView.setVisibility(hostElement.Category, false) to hide the host without hiding it's hosted elements.

  4. #4
    Member
    Join Date
    2011-04
    Posts
    8
    Login to Give a bone
    0

    Default Re: Isolate Elements

    Thanks, msiebert. I will try with your code.

    Trang

  5. #5
    Member
    Join Date
    2011-02
    Posts
    4
    Login to Give a bone
    0

    Default Re: Isolate Elements

    Maybe the View.IsolateElementsTemporary method achieves exactly what you need more efficiently and with much less effort?

    Cheers,

    Jeremy

    - ADN -- http://adn.autodesk.com
    - ADN Open -- http://www.autodesk.com/adnopen
    - Autodesk Discussion Groups -- http://discussion.autodesk.com
    - The Building Coder -- http://thebuildingcoder.typepad.com

Similar Threads

  1. Replies: 0
    Last Post: 2014-03-06, 11:28 PM
  2. 2013: Turning off elements in linked file also turns off elements in host model
    By GCIPRIANO in forum Revit MEP - General
    Replies: 2
    Last Post: 2013-07-21, 11:38 PM
  3. Isolate elements export to CAD
    By diesellam in forum Revit Architecture - General
    Replies: 5
    Last Post: 2009-07-07, 11:16 PM
  4. Show Elements that have Graphics Overrides Applied in Reveal Hidden Elements Mode
    By revit.wishlist1942 in forum Revit Architecture - Wish List
    Replies: 0
    Last Post: 2008-03-04, 04:06 PM
  5. Detail Lines are model elements, how about annotation elements?
    By scowsert in forum Revit Architecture - General
    Replies: 5
    Last Post: 2007-08-03, 07:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •