Esempio n. 1
0
def find_all_commands(category_set=None, cache=True):
    global pyrevit_extcmdtype_cache  #pylint: disable=W0603
    if cache and pyrevit_extcmdtype_cache:  #pylint: disable=E0601
        pyrevit_extcmds = pyrevit_extcmdtype_cache
    else:
        pyrevit_extcmds = []
        for loaded_assm_name in sessioninfo.get_loaded_pyrevit_assemblies():
            loaded_assm = coreutils.find_loaded_asm(loaded_assm_name)
            if loaded_assm:
                all_exported_types = loaded_assm[0].GetTypes()

                for pyrvt_type in all_exported_types:
                    tname = pyrvt_type.FullName
                    availtname = pyrvt_type.Name \
                                 + COMMAND_AVAILABILITY_NAME_POSTFIX
                    pyrvt_availtype = None

                    if not tname.endswith(COMMAND_AVAILABILITY_NAME_POSTFIX)\
                            and LOADER_BASE_NAMESPACE not in tname:
                        for exported_type in all_exported_types:
                            if exported_type.Name == availtname:
                                pyrvt_availtype = exported_type

                        pyrevit_extcmds.append(
                            PyRevitExternalCommandType(pyrvt_type,
                                                       pyrvt_availtype))
        if cache:
            pyrevit_extcmdtype_cache = pyrevit_extcmds

    # now check commands in current context if requested
    if category_set:
        return [
            x for x in pyrevit_extcmds
            if x.is_available(category_set=category_set,
                              zerodoc=HOST_APP.uidoc is None)
        ]
    else:
        return pyrevit_extcmds
Esempio n. 2
0
def find_pyrevitcmd(pyrevitcmd_unique_id):
    """Searches the pyRevit-generated assemblies under current session for
    the command with the matching unique name (class name) and returns the
    command type. Notice that this returned value is a 'type' and should be
    instantiated before use.

    Example:
        >>> cmd = find_pyrevitcmd('pyRevitCorepyRevitpyRevittoolsReload')
        >>> command_instance = cmd()
        >>> command_instance.Execute() # Provide commandData, message, elements

    Args:
        pyrevitcmd_unique_id (str): Unique name for the command

    Returns:
        Type for the command with matching unique name
    """
    # go through assmebles loaded under current pyRevit session
    # and try to find the command
    logger.debug(
        'Searching for pyrevit command: {}'.format(pyrevitcmd_unique_id))
    for loaded_assm_name in sessioninfo.get_loaded_pyrevit_assemblies():
        logger.debug('Expecting assm: {}'.format(loaded_assm_name))
        loaded_assm = coreutils.find_loaded_asm(loaded_assm_name)
        if loaded_assm:
            logger.debug('Found assm: {}'.format(loaded_assm_name))
            for pyrvt_type in loaded_assm[0].GetTypes():
                logger.debug('Found Type: {}'.format(pyrvt_type))
                if pyrvt_type.FullName == pyrevitcmd_unique_id:
                    logger.debug(
                        'Found pyRevit command in {}'.format(loaded_assm_name))
                    return pyrvt_type
            logger.debug('Could not find pyRevit command.')
        else:
            logger.debug('Can not find assm: {}'.format(loaded_assm_name))

    return None