Donate Now Goal amount for this year: 2500 USD, Received: 2174 USD (87%)

Results 1 to 8 of 8

Thread: Revit SDK 2012 Retrieve Filtered Elements example

  1. #1
    Junior Member
    Join Date
    December 16, 2010
    Posts
    14
    Current Local Time
    04:29 PM

    Revit SDK 2012 Retrieve Filtered Elements example

    I am trying to follow the "Retrieve Filter Elements" sample file from the API Developer Guide. Basically it is a copy what is on the page exercise, but there is enough left out (which I am sure any programmer would know) to leave me in the dust. If someone could explain what is missing from this code to make it functional, I would appreciate it. I know at least part of the problem is in identifying the active document, but I just don't know what is missing. The build error says the name 'uiDoc' does not exist in the current context. The sample file is simply finding and displaying all the door ids in an open file.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Autodesk.Revit.UI;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.UI.Selection;
    
    namespace FilteredElements
    {
        [Autodesk.Revit.Attributes.Transaction(TransactionMode.Automatic)]
        public class Class1 : IExternalCommand
        {
    
            
            public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                {
    
                    UIDocument uidoc = commandData.Application.ActiveUIDocument;
    
                    ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));
                    ElementCategoryFilter doorsCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
    
                    LogicalAndFilter doorInstancesFilter = new LogicalAndFilter(familyInstanceFilter, doorsCategoryFilter);
    
                    FilteredElementCollector collector = new FilteredElementCollector(uiDoc);
    
                    ICollection<ElementId> doors = collector.WherePasses(doorInstancesFilter).ToElementIds();
    
                    String prompt = "The ids of the doors in the current document are: ";
                    foreach (ElementId id in doors)
                    {
                        prompt += "n\t" + id.IntegerValue;
                    }
    
                    TaskDialog.Show("Revit", prompt);
    
                }
            }
                
    
        }
    }
    Last edited by Munkholm; April 27th, 2011 at 06:43 PM. Reason: BB Formatted CODE

  2. #2
    Junior Member
    Join Date
    December 16, 2010
    Posts
    14
    Current Local Time
    04:29 PM

    Slight improvement...

    Seems to be getting closer, but am getting an error saying not all code paths return a value in relation to the FilteredElements.Class1.Execute.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Autodesk.Revit.UI;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.UI.Selection;
    
    namespace FilteredElements
    {
        [Autodesk.Revit.Attributes.Transaction(TransactionMode.Automatic)]
        public class Class1 : IExternalCommand
        {
    
            
            public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                try
                {
    
                    Document doc = commandData.Application.ActiveUIDocument.Document;
    
    
                    ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));
                    ElementCategoryFilter doorsCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
    
                    LogicalAndFilter doorInstancesFilter = new LogicalAndFilter(familyInstanceFilter, doorsCategoryFilter);
    
                    FilteredElementCollector collector = new FilteredElementCollector(doc);
    
                    ICollection<ElementId> doors = collector.WherePasses(doorInstancesFilter).ToElementIds();
    
                    String prompt = "The ids of the doors in the current document are: ";
                    foreach (ElementId id in doors)
                    {
                        prompt += "n\t" + id.IntegerValue;
                    }
    
                    TaskDialog.Show("Revit", prompt);
    
    
                }
                catch
                {
                    message = "unexpected exception thrown.";
                    return Autodesk.Revit.UI.Result.Failed;
                }
            }
                
        }
    }
    Last edited by Nelson; April 27th, 2011 at 07:08 PM. Reason: Updating code

  3. #3
    Administrator Munkholm's Avatar
    Join Date
    December 7, 2010
    Location
    Kingdom of Denmark
    Posts
    4,225
    Current Local Time
    06:29 PM
    Hi Nelson, and welcome to the Forum

    Sorry, can´t offer any help on the SDK, but here´s a little tip on using BB Codes on the forum:

    At the beginning and the end of the code you are posting, add the [ code ] [ /code ] tags (without spaces) - Just to ensure that your code stays formatted as intended

  4. #4
    Junior Member
    Join Date
    December 16, 2010
    Posts
    14
    Current Local Time
    04:29 PM
    Thanks. Noticed you edited it and did the same to my second post.

  5. #5
    Member joseguia's Avatar
    Join Date
    March 8, 2011
    Location
    Dallas, TX
    Posts
    102
    Current Local Time
    11:29 AM
    Are you using the VSTA Macro Manager, or are you using Visual Studio?
    Last edited by joseguia; April 27th, 2011 at 09:05 PM.

  6. #6
    Junior Member
    Join Date
    December 16, 2010
    Posts
    14
    Current Local Time
    04:29 PM
    Visual Studio 2010 Express

  7. #7
    Member krispy5's Avatar
    Join Date
    December 21, 2010
    Posts
    68
    Current Local Time
    03:29 AM
    You need to return a value after the try/catch block... just add a return Autodesk.Revit.UI.Result.Succeeded; statement after the catch curly braces:
    Code:
    catch
    {
        message = "unexpected exception thrown.";
        return Autodesk.Revit.UI.Result.Failed;
    }
    return Autodesk.Revit.UI.Result.Succeeded;

  8. #8
    Junior Member
    Join Date
    December 16, 2010
    Posts
    14
    Current Local Time
    04:29 PM
    That did it! Thanks!

Similar Threads

  1. Revit 2012- tutoriel:éléments (parts)
    By jbenoit44 in forum Tutoriels, trucs et astuces en français
    Replies: 4
    Last Post: January 25th, 2012, 06:45 PM
  2. retrieve the slab thickness via a parameter ?
    By jh75 in forum Structure - General Questions
    Replies: 4
    Last Post: May 26th, 2011, 04:44 AM
  3. Revit 2012- éléments (parts) épisode 2
    By jbenoit44 in forum Tutoriels, trucs et astuces en français
    Replies: 0
    Last Post: April 14th, 2011, 09:32 PM
  4. Revit 2012- nouveauté n°1: les éléments de construction
    By jbenoit44 in forum Forum Français
    Replies: 0
    Last Post: April 13th, 2011, 09:23 PM
  5. Default System over 50 Elements (in Revit Arch)
    By safoulkes in forum Architecture and General Revit Questions
    Replies: 4
    Last Post: January 10th, 2011, 10:33 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
  •