RevitAPI: Get/Create FilledRegionType
π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:
# Variables
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application
rvt_year = int(app.VersionNumber)
# Function
def get_filled_region(filled_region_name):
"""Function to get FilledRegionType by Name using Revit API Filters"""
# Create Filter
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)
# Get FilledRegionType
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.
# FilledRegionType Settings
color = Color(128,128,255)
lineweight = 2
masking = False
# Get Solid Pattern
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!
# Duplicate existing FilledRegionType
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.
# Set PatternΒ Β Β
new_region.BackgroundPatternId = ElementId(-1)Β Β #None
new_region.ForegroundPatternId = solid_pattern.Id
# Set ColourΒ Β Β
new_region.BackgroundPatternColor = color
new_region.ForegroundPatternColor = color
# MaskingΒ Β Β
new_region.IsMasking = masking
# LineWeightΒ Β Β
new_region.LineWeight = lineweight⨠Final Code
Here is the final code for pyRevit's .pushbutton to Get or Create new FilledRegionType.
# -*- coding: utf-8 -*-
__title__ = "Get/Create FilledRegion"
__author__ = "Erik Frits"
# β¦ββ¦ββββββββ¦ββββ¦ββββ
# βββββ βββ ββ β¦β β βββ
# β©β© β©β© ββββ©ββ β© βββ IMPORTS
# ==================================================
# Regular + Autodesk
from Autodesk.Revit.DB import *
# β¦ β¦ββββ¦βββ¦βββββ β¦ ββββββ
# βββββ ββ£β β¦βββ ββ£β β©ββ ββ£ βββ
# ββ β© β©β©βββ©β© β©ββββ©ββββββββ VARIABLES
# ==================================================
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'
# Get Solid Pattern
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"""
# Create Filter
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)
# Get Types
return FilteredElementCollector(doc).OfClass(FilledRegionType).WherePasses(my_filter).FirstElement()
def create_RegionType(name, color, masking = False, lineweight = 1):
#type: (str, Color, bool, int) -> FilledRegionType
"""Create FilledRegionType with Solid Pattern."""
random_filled_region = FilteredElementCollector(doc).OfClass(FilledRegionType).FirstElement()
new_region = random_filled_region.Duplicate(name)
# Set Solid Pattern
new_region.BackgroundPatternId = ElementId(-1)
new_region.ForegroundPatternId = solid_pattern.Id
# Set Colour
new_region.BackgroundPatternColor = color
new_region.ForegroundPatternColor = color
# Masking
new_region.IsMasking = masking
# LineWeight
new_region.LineWeight = lineweight
return new_region
# ββ¦βββββ¦βββ
# ββββ ββ£ββββ
# β© β©β© β©β©βββ MAIN
# ==================================================
# Try to get existing FilledRegions
region_A = get_filled_region(region_name_A)
region_B = get_filled_region(region_name_B)
with Transaction(doc,__title__) as t:
t.Start()
# Create new if does not exist
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 Names of Regions:
print(Element.Name.GetValue(region_A))
print(Element.Name.GetValue(region_B))