# How to get a instance by a Value?

**URL:** <https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194>\
**Category:** Developers\
**Created:** [November 4, 2023, 12:03pm UTC](https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194 "2023-11-04T12:03:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Draxl\_Andreas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/draxl_andreas/32/145744_2.png) [@Draxl\_Andreas](https://forum.dynamobim.com/u/Draxl_Andreas)\
**Post date:** [November 4, 2023, 12:03pm UTC](https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194/1 "2023-11-04T12:03:02Z")

</div>

Hello,

i used already ChatGPT or should i say CheatGPT 😉

i think the result is missleading, and it seems to be AI is inventing stuff…

```auto
import clr
import sys
import System

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

def get_family_symbol_by_name(doc, name):
    param_id = BuiltInParameter.ALL_MODEL_FAMILY_NAME
    param_value_provider = ParameterValueProvider(ElementId(param_id))
    equals_rule = FilterStringEquals()
    filter_rule = FilterStringRule(param_value_provider, equals_rule, name, False)
    filter = ElementParameterFilter(filter_rule)

    fec = FilteredElementCollector(doc)
    fec.OfCategory(BuiltInCategory.OST_FamilySymbols).WhereElementIsElementType().WherePasses(filter)

    if fec.GetElementCount() == 1:
        symbol = fec.FirstElement()
        if not symbol.IsActive:
            symbol.Activate()
            doc.Regenerate()
        return symbol
    return None

name = IN[0]
_instance = get_family_symbol_by_name(doc,name)

OUT = _Instance

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/c/2ce4b507d580ca2d64cee029c49150271d86d5b6.jpeg)

actually i tried to “translate” from C# to Python

> **[Revit API Docs - Code Samples](https://www.revitapidocs.com/code/)**
>
> Online Documentation for Autodesk's Revit API: 2015, 2016, 2017, 2017.1, 2018

KR

Andreas

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [November 4, 2023, 12:34pm UTC](https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194/2 "2023-11-04T12:34:45Z")

</div>

This might be a more useful starting point: [https://github.com/Amoursol/dynamoPython/blob/master/revitAPI/filteredElementCollectorByParameter.py](https://github.com/Amoursol/dynamoPython/blob/master/revitAPI/filteredElementCollectorByParameter.py)

---

<div class="post-metadata">

**Author:** ![christian.stan](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/christian.stan/32/166296_2.png) [@christian.stan](https://forum.dynamobim.com/u/christian.stan)\
**Post date:** [November 4, 2023, 1:25pm UTC](https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194/3 "2023-11-04T13:25:27Z")

</div>

Hi,  
look at this nugget of an [article](https://voltadynabim.blogspot.com/2020/04/dynamopython-selectionner-avec-lapi.html) by Mr. Poupin C

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/4/945fc488024a679fa2269a948010dc9e168f8127.png)  
Since you work the fec.

```auto
Nom=IN[0]

fami=FilteredElementCollector(doc).OfClass(FamilyInstance).WhereElementIsNotElementType().ToElements()

a=[i for i in fami if i.Name==Nom]

OUT = a,a[0]

```

I’m struggling with sketchplanes 😉

cordially  
christian.stan

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [November 4, 2023, 1:52pm UTC](https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194/4 "2023-11-04T13:52:00Z")

</div>

I think ChatGPT is going to be more harmful in the long run because the answers will seem plausible, yet they will have fundamental flaws or straight out fabrications, frustrating learners and experts alike. Alternately Github co-pilot is actually worthwhile as it only works by line or small code snippet and is context aware

When I want to get something specific, especially by matching a parameter I will tend to use the FEC - however FECs (this has a different meaning if you’re Irish) can be simple, or as frustrating as a calculator using Reverse Polish Notation

**EDIT** Updated code - see comment below  
Start with with `pfr = ParameterFilterRuleFactory.CreateEqualsRule(ElementId(BuiltInParameter.SYMBOL_NAME_PARAM), name)`. I used to roll my own filters but once I found the rule factory it has become a lot easier - use you BIP of choice here SYMBOL\_FAMILY\_NAME\_PARAM, etc

Drop into `filter = ElementParameterFilter(pfr)`

and finally `types = FilteredElementCollector(doc).OfClass(FamilyInstance).WherePasses(filter).FirstElement()` or `ToElements()`

I’m not at my workstation so I’m working from memory - this should get you closer to what you want to achieve (or use Christian’s code 🙂 )

---

<div class="post-metadata">

**Author:** ![Draxl\_Andreas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/draxl_andreas/32/145744_2.png) [@Draxl\_Andreas](https://forum.dynamobim.com/u/Draxl_Andreas)\
**Post date:** [November 5, 2023, 6:32pm UTC](https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194/5 "2023-11-05T18:32:31Z")

</div>

@Mike.Buttery ,

i tried it but, why need i element id ? can i just search via string ?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/c/dcb47e176b19720b737829e1f11bae6589f39688.jpeg)

```auto
parameter_id = ElementId(21714163)

name = "WDB"

case_sensitive = False  

pfr = ParameterFilterRuleFactory.CreateEqualsRule(parameter_id, name, case_sensitive)

filter = ElementParameterFilter(pfr)

types = FilteredElementCollector(doc).OfClass(FamilyInstance).WherePasses(filter).FirstElement()

OUT = pfr, filter, types

```

> **[CreateEqualsRule Method (ElementId, String, Boolean)](https://www.revitapidocs.com/2017/2a4943e9-4efe-7b79-b1ea-8b85c253001d.htm)**
>
> Online Documentation for Autodesk's Revit API: 2015, 2016, 2017, 2017.1, 2018

KR

Andreas

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [November 5, 2023, 7:58pm UTC](https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194/7 "2023-11-05T19:58:07Z")

</div>

Check the link I posted - it is a functional example of how to select elements via parameter value.

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [November 5, 2023, 11:17pm UTC](https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194/8 "2023-11-05T23:17:03Z")

</div>

The `CreateEqualsRule()` method takes a parameter id. For example, using the following code we can see that all parameters have an associated id

```python
OUT = (
    [id for id in System.Enum.GetValues(BuiltInParameter)],
    FilteredElementCollector(doc).OfClass(SharedParameterElement).ToElementIds(),
)

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/a/da5e25f4fc4bd0da1d9cb3428a3934d2b941d497.png)

My code was a little misleading - we need to get the parameter id as an `ElementId` so the revised code would be…  
`ParameterFilterRuleFactory.CreateEqualsRule(ElementId(BuiltInParameter.SYMBOL_NAME_PARAM), name, False)`

With results

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/7/d76cfa1d8fac5c4c1a9ea822504515021101797f.png)

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [November 5, 2023, 11:39pm UTC](https://forum.dynamobim.com/t/how-to-get-a-instance-by-a-value/95194/9 "2023-11-05T23:39:01Z")

</div>

Now you can start chaining with logical filters

```python
spa = ParameterFilterRuleFactory.CreateSharedParameterApplicableRule("Family Thumbnail")
hvp = ParameterFilterRuleFactory.CreateHasValueParameterRule(ElementId(2289313))

spaf = ElementParameterFilter(spa)
hvpf = ElementParameterFilter(hvp)

laf = LogicalAndFilter(spaf, hvpf)

with_thumbnail = FilteredElementCollector(doc).WherePasses(laf).ToElements()

OUT = with_thumbnail

```

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/7/779ecff7196b68db3447b24063fc026005527bcc.png)
