How to get a instance by a Value?

Hello,

i used already ChatGPT or should i say CheatGPT :wink:

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

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

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

KR

Andreas

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

3 Likes

Hi,
look at this nugget of an article by Mr. Poupin C


Since you work the fec.

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 :wink:

cordially
christian.stan

2 Likes

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 :slight_smile: )

5 Likes

@Mike.Buttery ,

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

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

KR

Andreas

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

2 Likes

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

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

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

1 Like

Now you can start chaining with logical filters

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

1 Like