How to create a FilteredElementCollector in C#

Hello
,

i stuck, in the structure i think, AI is missleading


using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;

namespace MyDraxl
{
    public class Doors
    {
        public static IList<Element> GetAllDoors(Document doc)
        {
            // Collecting door elements in the entire document
            FilteredElementCollector collector = new FilteredElementCollector(doc)
                                                    .OfCategory(BuiltInCategory.OST_Doors)
                                                    .WhereElementIsNotElementType();

            // Convert the collector to a list of elements
            IList<Element> doorElements = collector.ToElements();

            // Return the collected doors as a list of elements
            return doorElements;
        }
    }
}


Where i get stucked ?

i want just listed my doors in the current project

These are the changes I would make to your code

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using Revit.Elements;

namespace MyDraxl
{
    public class Doors
    {
        // hide the overall class so you don't get the node Doors.Doors
        private Doors(){}

       //better to change the code to just get the current document, instead of having an input
        public static IList<Element> GetAllDoors(bool refresh)
        {
            // get the current document
            Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
            // Collecting door elements in the entire document
            FilteredElementCollector collector = new FilteredElementCollector(doc)
                                                    .OfCategory(BuiltInCategory.OST_Doors)
                                                    .WhereElementIsNotElementType();

            // Convert the collector to a list of elements
            IList<Element> doorElements = collector.ToElements();

            // Return the collected doors as a list of elements
            return doorElements;
        }
    }
}
5 Likes

A recent experience with ZT helped me understand the need to hide some objects (variables, methods, classes)

I finally partly understood why there were so many non-public objects in the Dynamo API

a ninja dynamo Zero Touch Node coder :laughing:

3 Likes

@john_pierson ,

revit does not exist, which module or reference is missing?

@c.poupin ,

i want only the current document, nothing else, even chatGPT is talking about anything

@Draxl_Andreas
add this

using RevitServices.Persistence;

Visual Studio should suggest this as a fix

1 Like

DynamoVisualProgramming.Revit nuget package

2 Likes


no

it is the latest version of visual studio

the example “this” i got from RevitAPI page…

Need to add this package(s)

image

2 Likes

@john_pierson ,

here is a issue about Net.8

i did not even run revit, because i have no solution for now

I think that is ignorable for the time being as I reference those same ones with Rhythm

1 Like

@john_pierson

:drum::drum::drum:

solution trail and error no red underlines … i will see

@john_pierson

it is not exactly what i want but ok


using Autodesk.Revit.DB;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
// using Revit.Elements;
using RevitServices.Persistence;

namespace MyDraxl
{
    public class Doors
    {
        // hide the overall class so you don't get the node Doors.Doors
        private Doors() { }

        //better to change the code to just get the current document, instead of having an input
        public static IList<Element> GetAllDoors(bool refresh = true)
        {
            // get the current document
            Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
            // Collecting door elements in the entire document
            FilteredElementCollector collector = new FilteredElementCollector(doc)
                                                    .OfCategory(BuiltInCategory.OST_Doors)
                                                    .WhereElementIsNotElementType();

            // Convert the collector to a list of elements
            IList<Element> doorElements = collector.ToElements();

            // Return the collected doors as a list of elements
            return doorElements;
        }
    }
}

That is great progress. The last part is to Wrap or Cast the element to their Revit.Elements.Element counterpart.

That is the ToDSType(true) portion of C# Dynamo nodes

@john_pierson

So i can not set to private to hide the input, the node itself disappears … hmm

// hide the overall class so you don't get the node Doors.Doors
private Doors() { }

//better to change the code to just get the current document, instead of having an input
private static IList<Element> GetAllDoors(bool refresh = true)

I Typically handle this (wrapping) with something called a Lambda in C#:

4 Likes

Don’t set that one to private. Just the first part. Apologies if I messed that up.

It should read as follows:

public static IList<Element> GetAllDoors(bool refresh = true)
1 Like

@john_pierson ,

enough talk… for tonight

Thank you

2 Likes

@john_pierson ,

it works, but i am not fully aware what i do… :wink:

using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Dynamo.Graph.Nodes;
using RevitServices.Persistence;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using Revit.Elements;

namespace MyDraxl
{
    public class Doors
    {
        // hide the overall class so you don't get the node Doors.Doors
        private Doors() { }

        //better to change the code to just get the current document, instead of having an input
        public static List<global::Revit.Elements.Element> GetAllDoors(bool refresh = true)
        {
            // get the current document
            Autodesk.Revit.DB.Document doc = DocumentManager.Instance.CurrentDBDocument;
            // Collecting door elements in the entire document

            FilteredElementCollector collector = new FilteredElementCollector(doc);
            List<global::Revit.Elements.Element> elems =
                new List<global::Revit.Elements.Element>(collector.OfCategory(BuiltInCategory.OST_Doors).ToElements()
                    .Select(e => e.ToDSType(false)));

            return elems;

          
        }
    }
}

this sentence, what i am doing here:

public static List<global::Revit.Elements.Element> GetAllDoors(bool refresh = true)

In that sentence, you are saying that it is okay to have the method public, but the overall class should not be

1 Like