πIntroduction
If you are reading this, then you have a question in your mind:
How to create a new FilledRegionType in Revit API?
You might have noticed that there are no methods in Revit API Docs to do that.
However, it does not mean we canβt take existing one and duplicate it. Then you can change necessary properties, so it's indeed a new FilledRegionType.
So let me take you through all these steps to write a code to: Get Existing FilledRegionType or Create a new one if it doesn't exist.
π Get Existing FilledRegionType by Name
First, let's check if desired FilledRegionType already exists. This will help you avoid duplicates and unwanted errors in your projects.
Here is a code snippet to do that:
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application
rvt_year = int(app.VersionNumber)
def get_filled_region(filled_region_name):
"""Function to get FilledRegionType by Name using Revit API Filters"""
param_id = ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME)
pvp = ParameterValueProvider(param_id )
condition = FilterStringEquals()
if rvt_year < 2022:
fRule = FilterStringRule(pvp, condition, filled_region_name, True)
else:
fRule = FilterStringRule(pvp, condition, filled_region_name)
my_filter = ElementParameterFilter(fRule)
return FilteredElementCollector(doc).OfClass(FilledRegionType)\
.WherePasses(my_filter).FirstElement()
π‘Tip: You can change Evaluator to FilterStringContain instead of FilterStringEquals.
π Duplicate existing FilledRegionType
If we can't find the FilledRegionType, let's make our own! Hereβs how in a few simple steps:
Define desired Pattern, Colour, Lineweight and Masking settings.
Get existing FilledRegionType and Duplicate it
Change parameters as you need
I am going to walk you through these steps.
1οΈβ£ Get Solid Pattern
Letβs begin by getting the right pattern for our new FilledRegionType.
In my case I will look for Solid pattern, but you can write your own logic to get desired pattern.
color = Color(128,128,255)
lineweight = 2
masking = False
all_pats = FilteredElementCollector(doc).OfClass(FillPatternElement).ToElements()
all_solid = [pat for pat in all_pats if pat.GetFillPattern().IsSolidFill]
solid_pattern = all_solid [0]
2οΈβ£ Get random FilledRegionType and Duplicate
The next step is to get a random FilledRegionType and Duplicate it.
I am going to get a random FilledRegionTypes with FilteredElementCollector. Since we are going to change all settings it does not matter which one we take. However, it's important to provide unique new name.
π‘ NB! You will get an error if you are trying to duplicate FilledRegionType with an existing new name, be aware of that!
new_name = βNew Regionβ
random_filled_region = FilteredElementCollector(doc).OfClass(FilledRegionType).FirstElement()
new_region = random_filled_region.Duplicate(new_name)
Now we have our new FilledRegionType with desired name and we can start changing its settings.
3οΈβ£ Apply new Settings to FilledRegionType
The last step is to change settings, and itβs not complicated.
All these settings are readable and writable properties, so itβs going to be very easy to set new values.
new_region.BackgroundPatternId = ElementId(-1)
new_region.ForegroundPatternId = solid_pattern.Id
new_region.BackgroundPatternColor = color
new_region.ForegroundPatternColor = color
new_region.IsMasking = masking
new_region.LineWeight = lineweight
β¨ Final Code
Here is the final code for pyRevit's .pushbutton to Get or Create new FilledRegionType.
__title__ = "Get/Create FilledRegion"
__author__ = "Erik Frits"
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application
rvt_year = int(app.VersionNumber)
region_name_A = 'New Region A'
region_name_B = 'New Region B'
all_patterns = FilteredElementCollector(doc).OfClass(FillPatternElement).ToElements()
all_solid_patterns = [pat for pat in all_patterns if pat.GetFillPattern().IsSolidFill]
solid_pattern = all_solid_patterns[0]
def get_filled_region(filled_region_name):
"""Function to get FireWall Types based on FamilyName"""
pvp = ParameterValueProvider(ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME))
condition = FilterStringEquals()
fRule = FilterStringRule(pvp, condition, filled_region_name, True) if rvt_year < 2022 \
else FilterStringRule(pvp, condition, filled_region_name)
my_filter = ElementParameterFilter(fRule)
return FilteredElementCollector(doc).OfClass(FilledRegionType).WherePasses(my_filter).FirstElement()
def create_RegionType(name, color, masking = False, lineweight = 1):
"""Create FilledRegionType with Solid Pattern."""
random_filled_region = FilteredElementCollector(doc).OfClass(FilledRegionType).FirstElement()
new_region = random_filled_region.Duplicate(name)
new_region.BackgroundPatternId = ElementId(-1)
new_region.ForegroundPatternId = solid_pattern.Id
new_region.BackgroundPatternColor = color
new_region.ForegroundPatternColor = color
new_region.IsMasking = masking
new_region.LineWeight = lineweight
return new_region
region_A = get_filled_region(region_name_A)
region_B = get_filled_region(region_name_B)
with Transaction(doc,__title__) as t:
t.Start()
if not region_A: region_A = create_RegionType(name = region_name_A, color = Color(255,128,128))
if not region_B: region_B = create_RegionType(name = region_name_B, color = Color(128,128,255))
t.Commit()
print(Element.Name.GetValue(region_A))
print(Element.Name.GetValue(region_B))