search all elements containing sepecific string i.e. ABC

search all elements containing sepecific string i.e. ABC

Ning_Zhou
Advocate Advocate
1,695 Views
12 Replies
Message 1 of 13

search all elements containing sepecific string i.e. ABC

Ning_Zhou
Advocate
Advocate

i'm trying to write utility tool to search all elements, pretty much everything, which contain specific string, mostly will be name, i.e. material name, type name, parameter name, etc.

including annotation symbol, view(port), diamension(style), text(style), tag, etc.

no need to search parameter value.

any easy way to make sure everything are included?

0 Likes
Accepted solutions (1)
1,696 Views
12 Replies
Replies (12)
Message 2 of 13

Moustafa_K
Collaborator
Collaborator

Hi

I believe the best way is to loop through Builtin Parameter of the element. I have quickly written this code "Not Tested" but will deliver the idea.

all I done  is

 

1- get a list of built in parameter

2- try and catch get each type value from the element.

3- if value equalls the string you need then you found what you want

 

       var bips = Enum.GetValues(typeof(BuiltInParameter));
          
        foreach (BuiltInParameter item in bips)
            {
                Parameter p = null;
                try
                {

                    p = myelement.get_Parameter(item);     
                    if (string.IsNullOrEmpty(p.AsString())) continue;

                    if(p.AsString() == searchstring) 
{
// do something
break; } } catch (Exception) { // this parameter does not exist for this type of element. } }

but this will take a long time unless you filter the search by a definition Name or something. The wide filter the more time... I hope that guides you to the right path.

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 3 of 13

Ning_Zhou
Advocate
Advocate

thanks mostafa90 for your quick reply, I did quick test, seems it's a bit slow, and only cover parameter side of element, I used the following code, seems working OK, but don't know if it's 100% covered.

 

foreach (Element e in all)
{
  if (e.Name.Contains("ABC"))
  {
   searched.Add(e);
   continue;
  }
  foreach (Parameter p in e.Parameters)
  {
   string n = p.AsString();
   if (string.IsNullOrEmpty(n)) continue;
   if (n.Contains("ABC"))
   {
    searched.Add(e);
    continue;
   }
  }
  // anything else?
}

0 Likes
Message 4 of 13

Moustafa_K
Collaborator
Collaborator
 foreach (Parameter p in e.Parameters)

the above statement in your loop will call the parameters each time from the element and will not iterate to go through one by one. this will cause that some of the parameters might be missed, for that reason I used this statement 

 

 var bips = Enum.GetValues(typeof(BuiltInParameter));
Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 5 of 13

Ning_Zhou
Advocate
Advocate
I iterate all elements in Revit database so its related parameters will be covered.

in your code, myelement is any one of elements in Revit databse or all elements?
0 Likes
Message 6 of 13

Moustafa_K
Collaborator
Collaborator

myelement is referring to one of the elements in the project that you need to check. so it can be an item variable in a loop of elements. I am Glad that resolved the problem

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 7 of 13

jeremytammik
Autodesk
Autodesk

Dear Ning and Moustafa,

 

You have not searched The Building Coder!

 

 

I hope this helps.

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 8 of 13

Ning_Zhou
Advocate
Advocate

thanks Jeremy for the links, I looked at your plugins before even though not fully tried, and seems to me it's only for searching parameter values, what i'm trying to achieve is to search everything in Revit database except parameter value, i.e. family name, family type name, material name, annotation stuff, etc., not a specific value assigned to element whether it's parameter or not.

 

the reason is to audit project templates and/or models from various sources in order to find out or even rename prefixed stuff (i.e. ABC_wall).

0 Likes
Message 9 of 13

matthew_taylor
Advisor
Advisor

Hi Ning,

If you just want types and families with names containing a string, you probably want to go for something like this:


Dim elementsTypesContainingText As IEnumerable(Of DB.Element) = New DB.FilteredElementCollector(doc).WhereElementIsElementType.Where(Function(e) PassesMyFilter(e, "ABC")) Dim familiesContainingText As IEnumerable(Of DB.Element) = New DB.FilteredElementCollector(doc).OfClass(GetType(DB.Family)).Where(Function(e) PassesMyFilter(e, "ABC"))
'Helper function: Private Function PassesMyFilter(e As DB.Element, matchStr As String) As Boolean If e.Name.Contains(matchStr, StringComparison.InvariantCultureIgnoreCase) Then Return True End If Return False End Function 'Helper module Public Module ExtensionMethods ''' <summary> ''' Usage: mystring.Contains(myStringToCheck, StringComparison.OrdinalIgnoreCase) ''' </summary> ''' <param name="source"></param> ''' <param name="toCheck"></param> ''' <param name="comp"></param> ''' <returns></returns> ''' <remarks></remarks> <Extension()> _ Public Function Contains(ByVal source As String, toCheck As String, comp As StringComparison) As Boolean Return source.IndexOf(toCheck, comp) >= 0 End Function End Module

 

You can always limit which categories to filter for also.

 

 

-Matt


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes
Message 10 of 13

Ning_Zhou
Advocate
Advocate

thanks Matthew for your coding, does it work for material names, etc.?

 

ironically, my following coding seems work but i'm not 100% sure if i covered everything.

foreach (Element e in all)
{
  if (e.Name.Contains("ABC"))
  {
   searched.Add(e);
   continue;
  }

}

0 Likes
Message 11 of 13

Moustafa_K
Collaborator
Collaborator
Hello
I thought you wuestion resolved. Can you explain why you have a doubt that the search loop is not covered 100%??
Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 12 of 13

Ning_Zhou
Advocate
Advocate

yes, i thought it's solved too, thanks everyone for suggestions.

0 Likes
Message 13 of 13

matthew_taylor
Advisor
Advisor
Accepted solution
Best to mark the solution for others then. 🙂

Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
0 Likes