def find_loaded_command(command_unique_name): """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_loaded_command('pyRevitCorepyRevitpyRevittoolsReload') >>> command_instance = cmd() >>> command_instance.Execute() # Provide commandData, message, elements Args: command_unique_name (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 from pyrevit.coreutils import find_loaded_asm for loaded_assm_name in sessioninfo.get_loaded_pyrevit_assemblies(): loaded_assm = find_loaded_asm(loaded_assm_name) if loaded_assm: for pyrvt_type in loaded_assm[0].GetTypes(): if pyrvt_type.FullName == command_unique_name: return pyrvt_type return None
def _cleanup_cache_files(): """ Upgrades local files and settings per this commit changes. commit message: Organized data files per Revit version in appdata folder commit hash: 44dd765c0f662f3faf5a40b92e3c5173804be37f """ cache_file_exts = ['pickle', 'json', 'log', 'pym'] for cache_file_ext in cache_file_exts: for cache_file_path in appdata.list_data_files(file_ext=cache_file_ext, universal=True): appdata.garbage_data_file(cache_file_path) # Cleanup Universal Dll files if get_revit_instance_count() == 1: for asm_file_path in appdata.list_data_files(file_ext='dll', universal=True): if not find_loaded_asm(asm_file_path, by_location=True): appdata.garbage_data_file(asm_file_path)
def _produce_ui_linkbutton(ui_maker_params): """ Args: ui_maker_params (UIMakerParams): Standard parameters for making ui item """ parent_ui_item = ui_maker_params.parent_ui parent = ui_maker_params.parent_cmp linkbutton = ui_maker_params.component ext_asm_info = ui_maker_params.asm_info if not linkbutton.is_supported: return None if linkbutton.beta_cmd and not ui_maker_params.create_beta_cmds: return None if not linkbutton.command_class: return None mlogger.debug('Producing button: %s', linkbutton) try: linked_asm_list = find_loaded_asm(linkbutton.assembly) if not linked_asm_list: return None linked_asm = linked_asm_list[0] parent_ui_item.create_push_button( linkbutton.name, linked_asm.Location, _make_full_class_name(linked_asm.GetName().Name, linkbutton.command_class), linkbutton.icon_file or parent.icon_file, _make_button_tooltip(linkbutton), _make_button_tooltip_ext(linkbutton, ext_asm_info.name), None, None, None, update_if_exists=True, ui_title=_make_ui_title(linkbutton)) return parent_ui_item.button(linkbutton.name) except PyRevitException as err: mlogger.error('UI error: %s', err.msg) return None
def _get_reference_file(ref_name): # First try to find the dll in the project folder addin_file = _get_addin_dll_file(ref_name) if addin_file: return addin_file # Then try to find the dll in windows SDK if FRAMEWORK_DIRS: fw_module_file = _get_framework_module(ref_name) if fw_module_file: return fw_module_file # Lastly try to find location of assembly if already loaded loaded_asm = find_loaded_asm(ref_name) if loaded_asm: return loaded_asm[0].Location # if not worked raise critical error logger.critical( 'Can not find required reference assembly: {}'.format(ref_name))
def _get_reference_file(ref_name): # First try to find the dll in the project folder addin_file = framework.get_dll_file(ref_name) if addin_file: load_asm_file(addin_file) return addin_file # Lastly try to find location of assembly if already loaded loaded_asm = find_loaded_asm(ref_name) if loaded_asm: return loaded_asm[0].Location # Then try to find the dll in windows SDK if DOTNET_TARGETPACK_DIRS: fw_module_file = _get_framework_module(ref_name) if fw_module_file: return fw_module_file # if not worked raise critical error mlogger.critical('Can not find required reference assembly: %s', ref_name)
def find_all_commands(category_set=None, cache=True): global pyrevit_extcmdtype_cache if cache and pyrevit_extcmdtype_cache: 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() all_exported_type_names = [x.Name for x in all_exported_types] 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
def _get_csharp_cmd_asm(cmd_component): """ Args: cmd_component (pyrevit.extensions.genericcomps.GenericUICommand): Returns: """ script_path = cmd_component.get_full_script_address() source = read_source_file(script_path) script_hash = get_str_hash(source)[:HASH_CUTOFF_LENGTH] command_assm_file_id = '{}_{}'\ .format(script_hash, cmd_component.unique_name) # check to see if compiled c# command assembly is already loaded compiled_assm_list = find_loaded_asm(command_assm_file_id, by_partial_name=True) if len(compiled_assm_list) > 0: return compiled_assm_list[0] # if not already loaded, check to see if the assembly file exits compiled_assm_path = \ appdata.is_data_file_available(file_id=command_assm_file_id, file_ext=ASSEMBLY_FILE_TYPE) if compiled_assm_path: return load_asm_file(compiled_assm_path) # else, let's compile the script and make the types command_assm_file = \ appdata.get_data_file(file_id=command_assm_file_id, file_ext=ASSEMBLY_FILE_TYPE) logger.debug('Compiling script {} to {}'.format(cmd_component, command_assm_file)) compiled_assm_path = compile_csharp([script_path], command_assm_file, reference_list=_get_references()) return load_asm_file(compiled_assm_path)
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
def _get_reference_file(ref_name): mlogger.debug('Searching for dependency: %s', ref_name) # First try to find the dll in the project folder addin_file = framework.get_dll_file(ref_name) if addin_file: load_asm_file(addin_file) return addin_file mlogger.debug('Dependency is not shipped: %s', ref_name) mlogger.debug('Searching for dependency in loaded assemblies: %s', ref_name) # Lastly try to find location of assembly if already loaded loaded_asm = find_loaded_asm(ref_name) if loaded_asm: return loaded_asm[0].Location mlogger.debug('Dependency is not loaded: %s', ref_name) mlogger.debug('Searching for dependency in installed frameworks: %s', ref_name) # Then try to find the dll in windows installed framework files if DOTNET_DIR: fw_module_file = _get_framework_module(ref_name) if fw_module_file: return fw_module_file mlogger.debug('Dependency is not installed: %s', ref_name) mlogger.debug('Searching for dependency in installed frameworks sdks: %s', ref_name) # Then try to find the dll in windows SDK if DOTNET_TARGETPACK_DIRS: fw_sdk_module_file = _get_framework_sdk_module(ref_name) if fw_sdk_module_file: return fw_sdk_module_file # if not worked raise critical error mlogger.critical('Can not find required reference assembly: %s', ref_name)
raise compile_err def _get_base_classes_asm(): if appdata.is_data_file_available(file_id=BASE_TYPES_ASM_FILE_ID, file_ext=ASSEMBLY_FILE_TYPE): return load_asm_file(BASE_TYPES_ASM_FILE) else: return _generate_base_classes_asm() if not EXEC_PARAMS.doc_mode: # compile or load the base types assembly # see it the assembly is already loaded BASE_TYPES_ASM = None assm_list = find_loaded_asm(BASE_TYPES_ASM_NAME) if assm_list: BASE_TYPES_ASM = assm_list[0] else: # else, let's generate the assembly and load it BASE_TYPES_ASM = _get_base_classes_asm() CMD_EXECUTOR_TYPE = find_type_by_name(BASE_TYPES_ASM, CMD_EXECUTOR_TYPE_NAME) CMD_AVAIL_TYPE = find_type_by_name(BASE_TYPES_ASM, CMD_AVAIL_TYPE_NAME) CMD_AVAIL_TYPE_CATEGORY = find_type_by_name(BASE_TYPES_ASM, CMD_AVAIL_TYPE_NAME_CATEGORY) CMD_AVAIL_TYPE_SELECTION = find_type_by_name( BASE_TYPES_ASM, CMD_AVAIL_TYPE_NAME_SELECTION) else: BASE_TYPES_ASM = CMD_EXECUTOR_TYPE = CMD_AVAIL_TYPE = None
def _is_pyrevit_ext_already_loaded(ext_asm_name): logger.debug( 'Asking Revit for previously loaded package assemblies: {}'.format( ext_asm_name)) return len(find_loaded_asm(ext_asm_name))
def cleanup_assembly_files(): if get_revit_instance_count() == 1: for asm_file_path in appdata.list_data_files(file_ext='dll'): if not find_loaded_asm(asm_file_path, by_location=True): appdata.garbage_data_file(asm_file_path)