How to pass a variable string to ElementClassFilter?

How to pass a variable string to ElementClassFilter?

kctangcl
Participant Participant
612 Views
5 Replies
Message 1 of 6

How to pass a variable string to ElementClassFilter?

kctangcl
Participant
Participant

Instead of hardcoding like: new ElementClassFilter(typeof(ViewSchedule)), I would like to use a variable string "ViewSchedule". How to convert the string to the class name?

0 Likes
Accepted solutions (1)
613 Views
5 Replies
Replies (5)
Message 2 of 6

Mohamed_Arshad
Advisor
Advisor

Hi @kctangcl    

 

   You can't be able to pass simple string to get Type or class, you need to give the assembly qualified name to get the type if the string. Kindly check the below code snippet and URL for additional information.

 

Reference Code Snippet

            string className = typeof(ViewSchedule).AssemblyQualifiedName;
            Type type = Type.GetType(className);

            ElementClassFilter elementClassFilter = new ElementClassFilter(type);

Useful Link

https://stackoverflow.com/questions/4876683/c-sharp-convert-dynamic-string-to-existing-class 
https://learn.microsoft.com/en-us/dotnet/api/system.type.assemblyqualifiedname?view=net-8.0&redirect... 

 

I don't know why you're using sting instead of class, if you going with string then my suggestion is to maintain all class Dictionary<string,string> () to get the type faster.

 

Hope this will Helps 🙂 

 


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 3 of 6

kctangcl
Participant
Participant

Thank you!

i want to use string variables because I want to state the settings like ElementCategory, ElementClass, ElementParameters, etc. in an Excel or txt file, and let the programme reads the settings. The users can change the settings but not the programme.

The Reference Code Snippet provided by you still requires to hardcode the class name ViewSchedule. In that case, I may as well just use it directly.

Using a Dictionary would require me to know all the key-value pairs of possible classes. I do not know how to get a list of valid class names.

The simplest but not complete solution would then be using the following for the already known and required classes:

if (classname == "ViewSchedule")

{

ElementClassFilter elementClassFilter = new ElementClasssFilter(typeof(ViewSchedule));

}

else if ....

 

By the way, Revit Python Wrapper gives the flexibility:

 

Coerces a class or class reference to a Class.

>>> from rpw.utils.coerce import to_class
>>> to_class('Wall')
[ DB.Wall ]
>>> to_class(Wall)
[ DB.Wall ]

 

0 Likes
Message 4 of 6

Mohamed_Arshad
Advisor
Advisor

Hi @kctangcl  

 

  As you told Python has ability to get class from string, C# we need to provide a Valid Name. My suggestion is to use category name in Text file, so that it will easy for data transfer


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 5 of 6

kctangcl
Participant
Participant

I do not know the list of ElementClass not served by ElementGategory.

 

The following post suggests not to use it unless I know what to use!

https://forums.autodesk.com/t5/revit-api-forum/collector-ofclass-typeof-toelement/td-p/10914596 

 

0 Likes
Message 6 of 6

kctangcl
Participant
Participant
Accepted solution

My solution:

       

        using System.Reflection;

        private Type getClass(string className)
        {
            // initialise dictionary
            Dictionary<string, Type> classDict = new Dictionary<string, Type>();
            // initialise list of arrays
            List<Type[]> typeArrayList = new List<Type[]>();
            // load assembly by name and get types there
            // only these two assemblies contain namespace AutodeskRevit.DB
            typeArrayList.Add(Assembly.Load("RevitAPI").GetTypes());
            typeArrayList.Add(Assembly.Load("RevitAPIExtData").GetTypes());
            // loop through each array in list
            foreach (Type[] typeArray in typeArrayList)
            {
                // loop through each type in array
                foreach (Type type in typeArray)
                {
                    // check if the type is a class and belongs to the specified namespace
                    if (type.IsClass && type.Namespace == "Autodesk.Revit.DB")
                    {
                        // check if key not already exists
                        if (!classDict.ContainsKey(type.Name))
                        {
                            // add key-value pair of type to dictionary 
                            classDict.Add(type.Name, type);
                        }
                    }
                }
            }
            return classDict[className];
        }

 

use as:

new ElementClassFilter(getClass("ViewSchedule"))

for hardcoded:

new ElementClassFilter(typeof(ViewSchedule))

 

Some classes contained in the dictionary are not relevant. However, the codes do serve the purpose of converting the class name in string to class name in Type.

0 Likes