Skip to content

files

Helper functions for working with revit files.

Attributes

mlogger = get_logger(__name__) module-attribute

Functions

cleanup_backups(main_revitfile)

Remove all incremental saves of the given Revit file.

Source code in pyrevitlib/pyrevit/revit/files.py
def cleanup_backups(main_revitfile):
    """Remove all incremental saves of the given Revit file."""
    file_dir = op.dirname(main_revitfile)
    fname, fext = op.splitext(op.basename(main_revitfile))
    backup_fname = re.compile(r'{}\.\d\d\d\d\.{}'.format(fname,
                                                         fext.replace('.', '')))
    for f in os.listdir(file_dir):
        if backup_fname.findall(f):
            file_path = op.join(file_dir, f)
            try:
                os.remove(file_path)
            except Exception as osremove_err:
                mlogger.error('Error backup file cleanup on: %s | %s',
                              file_path, osremove_err)

correct_text_encoding(filename)

Convert encoding of text file generated by Revit to UTF-8.

Parameters:

Name Type Description Default
filename str

file path

required
Source code in pyrevitlib/pyrevit/revit/files.py
def correct_text_encoding(filename):
    """Convert encoding of text file generated by Revit to UTF-8.

    Args:
        filename (str): file path
    """
    fcontent = None
    if coreutils.check_encoding_bom(filename, bom_bytes=codecs.BOM_UTF16):
        with codecs.open(filename, 'r', 'utf_16_le') as oldf:
            fcontent = oldf.readlines()
    elif coreutils.check_encoding_bom(filename, bom_bytes=codecs.BOM_UTF8):
        with codecs.open(filename, 'r', 'utf_8') as oldf:
            fcontent = oldf.readlines()

    with codecs.open(filename, 'w', 'utf-8') as newf:
        newf.writelines(fcontent)

read_text(filepath)

Safely read text files with Revit encoding.

Source code in pyrevitlib/pyrevit/revit/files.py
def read_text(filepath):
    """Safely read text files with Revit encoding."""
    with codecs.open(filepath, 'r', 'utf_16_le') as text_file:
        return text_file.read()

write_text(filepath, contents)

Safely write text files with Revit encoding.

Source code in pyrevitlib/pyrevit/revit/files.py
def write_text(filepath, contents):
    """Safely write text files with Revit encoding."""
    # if 'utf_16_le' is specified, codecs will not write the BOM
    with codecs.open(filepath, 'w', 'utf_16') as text_file:
        text_file.write(contents)

get_file_info(filepath)

Returns file information.

Parameters:

Name Type Description Default
filepath str

file path

required

Returns:

Type Description
RevitModelFile

file information

Source code in pyrevitlib/pyrevit/revit/files.py
def get_file_info(filepath):
    """Returns file information.

    Args:
        filepath (str): file path

    Returns:
        (RevitModelFile): file information
    """
    return TargetApps.Revit.RevitModelFile(filepath)