Skip to content

linkmaker

Handle creation of output window helper links.

Attributes

mlogger = get_logger(__name__) module-attribute

PROTOCOL_NAME = 'revit://outputhelpers?' module-attribute

MAX_INLINE_QUERY_LENGTH = 2000 module-attribute

APPEND_SELECTION_ONCLICK = 'onclick="var appendEvt=window.event||event;var appendLnk=this;if(appendEvt.ctrlKey){location.href=this.href+\'&append=true\';setTimeout(function(){appendLnk.style.backgroundColor="#f39c12";},0);return false;}return true;"' module-attribute

Functions:

Create link for given element ids.

This link is a special format link with revit:// scheme that is handled by the output window to select the provided element ids in current project. Scripts should not call this function directly. Creating clickable element links is handled by the output wrapper object through the :func:linkify method.

Ids that fit in a url (under :data:MAX_INLINE_QUERY_LENGTH characters) are embedded in the link itself. Longer lists are handed to register_ids and the link carries the token it returns, so any number of elements can be selected with one link.

Parameters:

Name Type Description Default
element_ids ElementId | list[ElementId]

single or multiple ids

required
contents str

label of the link. defaults to the list of ids, or to the element count when the ids are registered

None
register_ids callable

receives the comma separated id values and returns the token the output window resolves back to them. Only called when the ids do not fit in the url

None

Raises:

Type Description
ValueError

when an item is not an ElementId, or the ids do not fit in the url and no register_ids was given

Examples:

output = pyrevit.output.get_output()
for idx, elid in enumerate(element_ids):
    print('{}: {}'.format(idx+1, output.linkify(elid)))
Source code in pyrevitlib/pyrevit/output/linkmaker.py
def make_link(element_ids, contents=None, register_ids=None):
    """Create link for given element ids.

    This link is a special format link with revit:// scheme that is handled
    by the output window to select the provided element ids in current project.
    Scripts should not call this function directly. Creating clickable element
    links is handled by the output wrapper object through the :func:`linkify`
    method.

    Ids that fit in a url (under :data:`MAX_INLINE_QUERY_LENGTH` characters) are
    embedded in the link itself. Longer lists are handed to ``register_ids`` and
    the link carries the token it returns, so any number of elements can be
    selected with one link.

    Args:
        element_ids (ElementId | list[ElementId]): single or multiple ids
        contents (str): label of the link. defaults to the list of ids, or to
            the element count when the ids are registered
        register_ids (callable): receives the comma separated id values and
            returns the token the output window resolves back to them. Only
            called when the ids do not fit in the url

    Raises:
        ValueError: when an item is not an ElementId, or the ids do not fit in
            the url and no ``register_ids`` was given

    Examples:
        ```python
        output = pyrevit.output.get_output()
        for idx, elid in enumerate(element_ids):
            print('{}: {}'.format(idx+1, output.linkify(elid)))
        ```
    """
    get_elementid_value = get_elementid_value_func()
    try:
        try:
            strids = [safe_strtype(get_elementid_value(x)) for x in element_ids]
        except TypeError:
            strids = [safe_strtype(get_elementid_value(element_ids))]
    except AttributeError:
        raise ValueError("One or more items are not ElementIds")

    elementquery = ("element[]={}".format(strid) for strid in strids)

    reviturl = "&".join(elementquery)
    link_title = ", ".join(strids)

    if len(reviturl) >= MAX_INLINE_QUERY_LENGTH:
        if register_ids is None:
            raise ValueError(
                "{} element ids do not fit in one link and no register_ids "
                "was given".format(len(strids))
            )
        reviturl = "elements={}".format(register_ids(",".join(strids)))
        link_title = "{} elements".format(len(strids))

    base_link = 'href="{}{}{}&show={{}}"'.format(
        PROTOCOL_NAME, "&command=select&", reviturl
    )
    linkattrs_select = base_link.format("false") + " " + APPEND_SELECTION_ONCLICK
    linkattrs_show = base_link.format("true") + " " + APPEND_SELECTION_ONCLICK

    return DEFAULT_LINK.format(
        attrs_select=linkattrs_select,
        attrs_show=linkattrs_show,
        ids=contents or link_title,
        show_icon=LINK_SHOW_ICON,
    )