Creating a Fill Pattern with Python

Good evening everyone,
sorry this is the first time I try to write on the forum, I warn that my English is not perfect but I will try to explain myself.
Today I started taking a look at Python and loved it. So I’m trying to learn by doing. I have not found packages that allow me to create a FillPattern. So I thought I’d try, this is the result:

import clr
clr.AddReference(‘RevitNodes’)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

Import DocumentManager and TransactionManager

clr.AddReference(‘RevitServices’)
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

clr.AddReference(‘RevitAPI’)
import Autodesk
from Autodesk.Revit.DB import *

import sys
pyt_path = r’C:\Program Files (x86)\IronPython 2.7\Lib’
sys.path.append(pyt_path)
import System

UIunit = Document.GetUnits(doc).GetFormatOptions(UnitType.UT_Length).DisplayUnits

patternName = IN[0]

FillPattern = FillPattern(patternName,FillPatternTarget.Drafting,FillPatternHostOrientation.ToView)
FillPatern = FillPattern(UnitUtils.ConvertToInternalUnits(45,UIunit),UnitUtils.ConvertToInternalUnits(3,UIunit))

#Create a fillpattern element
TransactionManager.Instance.EnsureInTransaction(doc)
FillPatternElement = FillPatternElement.Create(doc, FillPattern)
TransactionManager.Instance.TransactionTaskDone()
OUT=FillPatternElement

I’m sure it is obvious to many why this doesn’t work.
Hoping someone can get me going in the right direction.
Thanks for your time

This may be a useful reference: Create Hatch Pattern from Selection

1 Like

take a look at pyRevit - https://www.notion.so/pyRevit-bd907d6292ed4ce997c46e84b6ef67a0

Thanks,

but in this way you create a pat file and not a Fillpattern.element which is my goal

A really useful tool thanks.

But I wanted to understand what was wrong with my script. I would like to create a node that allows you to create a FillPattern.element given the properties.

These lines may be the issue:

FillPattern = FillPattern(patternName,FillPatternTarget.Drafting,FillPatternHostOrientation.ToView)
FillPatern = FillPattern(UnitUtils.ConvertToInternalUnits(45,UIunit),UnitUtils.ConvertToInternalUnits(3,UIunit))

FillPattern is a class in the Revit API, but you are also naming your instance FillPattern. This will work for your first fill pattern, but once you have redefined the name “FillPattern”, attempting to construct a FillPattern later on will result in a TypeError:

TypeError: FillPattern is not callable

I prefer to use snake_case for all of my Python variables, reserving PascalCase for custom classes (if any), so that it will not conflict with anything in the Revit API.

Even after resolving the namespace conflict, you will still have an error as (Double, Double) is not one of the overloads shown in the API documentation.

Here is a way you can create a FillPatternElement:

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

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

doc = DocumentManager.Instance.CurrentDBDocument

pattern_name = IN[0]

ut_length = doc.GetUnits().GetFormatOptions(UnitType.UT_Length).DisplayUnits
ut_degrees = DisplayUnitType.DUT_DECIMAL_DEGREES

# Create fill pattern
target = FillPatternTarget.Drafting
orientation = FillPatternHostOrientation.ToView
angle = UnitUtils.ConvertToInternalUnits(45, ut_degrees)
spacing = UnitUtils.ConvertToInternalUnits(3, ut_length)
pattern = FillPattern(pattern_name, target, orientation, angle, spacing)

# Create fill pattern element
TransactionManager.Instance.EnsureInTransaction(doc)
element = FillPatternElement.Create(doc, pattern)
TransactionManager.Instance.TransactionTaskDone()

OUT = element
1 Like

Perfect! Thanks a lot!!! Now I’ll try to implement your script! Thank you

If I can get something, can I share it here?