Skip to content

framework

Provide access to DotNet Framework.

Examples:

from pyrevit.framework import Assembly, Windows

Attributes

ASSEMBLY_FILE_TYPE = 'dll' module-attribute

ASSEMBLY_FILE_EXT = '.dll' module-attribute

ipy_assmname = '{prefix}IronPython'.format(prefix=eng.EnginePrefix) module-attribute

ipy_dllpath = op.join(eng.EnginePath, ipy_assmname + ASSEMBLY_FILE_EXT) module-attribute

wpf_assmname = '{prefix}IronPython.Wpf'.format(prefix=eng.EnginePrefix) module-attribute

wpf_dllpath = op.join(eng.EnginePath, wpf_assmname + ASSEMBLY_FILE_EXT) module-attribute

sqlite3_assmname = '{prefix}IronPython.SQLite'.format(prefix=eng.EnginePrefix) module-attribute

sqlite3_dllpath = op.join(eng.EnginePath, sqlite3_assmname + ASSEMBLY_FILE_EXT) module-attribute

Functions:

to_clr_list(item_type, iterable)

Build a List[item_type] from any Python iterable, on either engine.

List[T](some_python_list) is the obvious spelling and it only works on IronPython. Under pythonnet the single-argument constructor cannot convert a Python list to IEnumerable<T], so overload resolution falls through to List(int capacity) and raises::

TypeError: No method matches given arguments for List`1..ctor: (<class 'list'>)

which surfaces from the Revit API call that consumed the list rather than from here, so the traceback points at the caller and hides the cause. Filling the list through Add needs no conversion and behaves the same on both engines.

Parameters:

Name Type Description Default
item_type type

The CLR element type, e.g. DB.FilterRule.

required
iterable iterable

Values to add, in order.

required

Returns:

Type Description

System.Collections.Generic.List[item_type]

Source code in pyrevitlib/pyrevit/framework.py
def to_clr_list(item_type, iterable):
    """Build a ``List[item_type]`` from any Python iterable, on either engine.

    ``List[T](some_python_list)`` is the obvious spelling and it only works on
    IronPython. Under pythonnet the single-argument constructor cannot convert a
    Python list to ``IEnumerable<T]``, so overload resolution falls through to
    ``List(int capacity)`` and raises::

        TypeError: No method matches given arguments for List`1..ctor: (<class 'list'>)

    which surfaces from the Revit API call that consumed the list rather than
    from here, so the traceback points at the caller and hides the cause. Filling
    the list through ``Add`` needs no conversion and behaves the same on both
    engines.

    Args:
        item_type (type): The CLR element type, e.g. ``DB.FilterRule``.
        iterable (iterable): Values to add, in order.

    Returns:
        System.Collections.Generic.List[item_type]
    """
    clr_list = List[item_type]()
    for item in iterable:
        clr_list.Add(item)
    return clr_list

get_type(fw_object)

Return CLR type of an object.

Source code in pyrevitlib/pyrevit/framework.py
def get_type(fw_object):
    """Return CLR type of an object."""
    return clr.GetClrType(fw_object)

get_dll_file(assembly_name)

Return path to given assembly name.

Source code in pyrevitlib/pyrevit/framework.py
def get_dll_file(assembly_name):
    """Return path to given assembly name."""
    addin_file = op.join(BIN_DIR, assembly_name + ".dll")
    if op.exists(addin_file):
        return addin_file

add_reference_to_file(asm_file)

Make a .NET assembly file available for import on any engine.

Engine-portable replacement for IronPython's clr.AddReferenceToFileAndPath, which does not exist under CPython/pythonnet. Accepts a path with or without the .dll extension.

Parameters:

Name Type Description Default
asm_file str

path to the assembly file

required

Returns:

Type Description
Assembly | None

the loaded assembly; None under IronPython

Note: IronPython's clr.AddReferenceToFileAndPath appends the assembly's own folder to sys.path on every call, so referencing several assemblies from the same folder would otherwise list that folder once per assembly. #3687.

Source code in pyrevitlib/pyrevit/framework.py
def add_reference_to_file(asm_file):
    """Make a .NET assembly file available for import on any engine.

    Engine-portable replacement for IronPython's
    ``clr.AddReferenceToFileAndPath``, which does not exist under
    CPython/pythonnet. Accepts a path with or without the ``.dll``
    extension.

    Args:
        asm_file (str): path to the assembly file

    Returns:
        (Assembly | None): the loaded assembly; None under IronPython

    Note: IronPython's ``clr.AddReferenceToFileAndPath`` appends the
    assembly's own folder to ``sys.path`` on every call, so referencing
    several assemblies from the same folder would otherwise list that
    folder once per assembly. #3687.
    """
    if IRONPY:
        assembly = clr.AddReferenceToFileAndPath(asm_file)
        dedupe_sys_path()
        return assembly
    # assembly names can contain dots (IxMilia.Dxf), so detect the
    # extension by suffix, not by splitext
    if not op.isfile(asm_file) and not asm_file.lower().endswith(ASSEMBLY_FILE_EXT):
        asm_file += ASSEMBLY_FILE_EXT
    return Assembly.LoadFrom(asm_file)

get_current_thread_id()

Return manageed thread id of current thread.

Source code in pyrevitlib/pyrevit/framework.py
def get_current_thread_id():
    """Return manageed thread id of current thread."""
    return System.Threading.Thread.CurrentThread.ManagedThreadId