Exemple #1
0
    def create_push_button(self, button_name, asm_location, class_name,
                           icon_path='',
                           tooltip='', tooltip_ext='', tooltip_video='',
                           ctxhelpurl=None,
                           avail_class_name=None,
                           update_if_exists=False, ui_title=None):
        if self.contains(button_name):
            if update_if_exists:
                existing_item = self._get_component(button_name)
                try:
                    # Assembly and Class info of current active
                    # script button can not be updated.
                    if button_name != EXEC_PARAMS.command_name:
                        rvtapi_obj = existing_item.get_rvtapi_object()
                        rvtapi_obj.AssemblyName = asm_location
                        rvtapi_obj.ClassName = class_name
                        if avail_class_name:
                            rvtapi_obj.AvailabilityClassName = avail_class_name
                except Exception as asm_update_err:
                    logger.debug('Error updating button asm info: {} '
                                 '| {}'.format(button_name, asm_update_err))

                existing_item.set_tooltip(tooltip)
                existing_item.set_tooltip_ext(tooltip_ext)
                if tooltip_video:
                    existing_item.set_tooltip_video(tooltip_video)

                existing_item.set_contexthelp(ctxhelpurl)

                if ui_title:
                    existing_item.set_title(ui_title)

                if not icon_path:
                    logger.debug('Icon not set for {}'.format(button_name))
                else:
                    try:
                        existing_item.set_icon(icon_path, icon_size=ICON_LARGE)
                    except PyRevitUIError as iconerr:
                        logger.error('Error adding icon for {} '
                                     '| {}'.format(button_name, iconerr))
                existing_item.activate()
            else:
                raise PyRevitUIError('Push button already exits and update '
                                     'is not allowed: {}'.format(button_name))
        else:
            logger.debug('Parent does not include this button. Creating: {}'
                         .format(button_name))
            try:
                button_data = UI.PushButtonData(button_name, button_name,
                                             asm_location, class_name)
                if avail_class_name:
                    button_data.AvailabilityClassName = avail_class_name
                if not self.itemdata_mode:
                    ribbon_button = \
                        self.get_rvtapi_object().AddItem(button_data)
                    new_button = _PyRevitRibbonButton(ribbon_button)
                else:
                    new_button = _PyRevitRibbonButton(button_data)

                if ui_title:
                    new_button.set_title(ui_title)

                if not icon_path:
                    logger.debug('Parent ui item is a panel and '
                                 'panels don\'t have icons.')
                else:
                    logger.debug('Creating icon for push button {} '
                                 'from file: {}'.format(button_name,
                                                        icon_path))
                    try:
                        new_button.set_icon(icon_path, icon_size=ICON_LARGE)
                    except PyRevitUIError as iconerr:
                        logger.error('Error adding icon for {} from {} '
                                     '| {}'.format(button_name,
                                                   icon_path,
                                                   iconerr))

                new_button.set_tooltip(tooltip)
                new_button.set_tooltip_ext(tooltip_ext)
                if tooltip_video:
                    new_button.set_tooltip_video(tooltip_video)

                new_button.set_contexthelp(ctxhelpurl)

                new_button.set_dirty_flag()
                self._add_component(new_button)

            except Exception as create_err:
                raise PyRevitUIError('Can not create button '
                                     '| {}'.format(create_err))
Exemple #2
0
 def set_contexthelp(self, ctxhelpurl):
     if ctxhelpurl:
         ch = UI.ContextualHelp(UI.ContextualHelpType.Url, ctxhelpurl)
         self.get_rvtapi_object().SetContextualHelp(ch)