Announcement

Collapse
No announcement yet.

Revit SDK 2012 Retrieve Filtered Elements example

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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 27, 2011, 06:43 PM. Reason: BB Formatted CODE

    #2
    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 27, 2011, 07:08 PM. Reason: Updating code

    Comment


      #3
      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 :beer:
      Klaus Munkholm
      "Do. Or do not. There is no try."

      Comment


        #4
        Thanks. Noticed you edited it and did the same to my second post.

        Comment


          #5
          Are you using the VSTA Macro Manager, or are you using Visual Studio?
          Last edited by joseguia; April 27, 2011, 09:05 PM.
          bim cad tech com

          Comment


            #6
            Visual Studio 2010 Express

            Comment


              #7
              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;
              http://krispcad.blogspot.com/

              Comment


                #8
                That did it! Thanks!

                Comment

                Related Topics

                Collapse

                Working...
                X