Feb 1, 2023

RevitAPI: Get/Create FilledRegionType

There is no method to create new FilledRegionType. But it does not mean we can not duplicate existing one and adjust its parameters. Read More if you want to Create new 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))

Want to Learn More?

🔥 Amazing Revit API Resources!

Join Revit API

Newsletter Today!

Join Us!

which is already read by 4500+ people!

Get short Revit API Lessons and Tips directly in your mail!