天天看点

sketchup Ruby API学习笔记(六)——UI模块

作者:钢之炼筋术士

12 Module: UI UI模块

12.1 .add_context_menu_handler{|menu| ... } ⇒ Integer

# Right click on anything to see a Hello World item.
UI.add_context_menu_handler do |context_menu|
  context_menu.add_item("Hello World") {
    UI.messagebox("Hello world")
  }
end           
sketchup Ruby API学习笔记(六)——UI模块

右键菜单中增加了一个hello world按钮。点击后会弹框“hello world”

12.2 .beep⇒ nil

播放系统嘟嘟声的函数。但在我的机器上测试无任何效果。

12.3 .create_cursor(path, hot_x, hot_y) ⇒ Integer

设置光标。SU2020的光标文件夹改到了image目录下。

12.4 .inputbox(prompts, defaults, title) ⇒ Array<String>, false

12.4 .inputbox(prompts, defaults, list, title) ⇒ Array<String>, false

# With three params, it shows all text boxes:
prompts = ["What is your Name?", "What is your Age?", "Gender"]
defaults = ["Enter name", "", "Male"]
input = UI.inputbox(prompts, defaults, "Tell me about yourself.")

# With four params, it shows a drop down box for prompts that have
# pipe-delimited lists of options. In this case, the Gender prompt
# is a drop down instead of a text box.
prompts = ["What is your Name?", "What is your Age?", "Gender"]
defaults = ["Enter name", "", "Male"]
list = ["", "", "Male|Female"]
input = UI.inputbox(prompts, defaults, list, "Tell me about yourself.")           

输入框,返回Array(string)

12.5 .inspector_names⇒ Array<String>

inspectors = UI.inspector_names
#["Materials", "Components", "Styles", "Scenes", "Shadows", "Layers", "SoftenEdges", "Outliner", "Instructor", "Fog", "MatchPhoto"]           

inspector_names方法用于返回所有检查器的名称。“检查器”是可以在SketchUp中激活的各种浮动对话框窗口的另一个名称,例如“材质”窗口。

12.6 .menu(menu_name = "Plugins") ⇒ Sketchup::Menu

tool_menu = UI.menu("Tools")
tool_menu.add_item("Cheese Tool") {
  UI.messagebox("Cheese activated.")
}           
sketchup Ruby API学习笔记(六)——UI模块

12.7 .messagebox(message, type = MB_OK) ⇒ Integer

这里我上了段原文。前面是消息框格式,后面是返回值。

Creates a dialog box containing static text with a series of buttons for the user to choose from.

Valid message box types are:

  • MB_OK - Contains an OK button.
  • MB_OKCANCEL - Contains OK and Cancel buttons.
  • MB_ABORTRETRYIGNORE - Contains Abort, Retry, and Ignore buttons.
  • MB_YESNOCANCEL - Contains Yes, No, and Cancel buttons.
  • MB_YESNO - Contains Yes and No buttons.
  • MB_RETRYCANCEL - Contains Retry and Cancel buttons.
  • MB_MULTILINE - Contains and OK button.

Return values can be any of following:

  • IDOK
  • IDCANCEL
  • IDABORT
  • IDRETRY
  • IDIGNORE
  • IDYES
  • IDNO

In an MB_MULTILINE message box, the message is displayed as a multi-line message with scrollbars (as needed). MB_MULTILNE also allows a third string argument that will be used as the title for the messagebox.

result = UI.messagebox('Do you like cheese?', MB_YESNO)
if result == IDYES
  UI.messagebox('SketchUp likes cheese too!')
end

#选yes返回1,选no无返回:)           

12.8 .model_info_pages⇒ Array<String>

mypages = UI.model_info_pages
#["Animation", "Classifications", "Components", "Credits", "Dimensions", "File", "Geo-location", "Rendering", "Statistics", "Text", "Units"]           
sketchup Ruby API学习笔记(六)——UI模块

12.9 .openpanel(title, directory, filename) ⇒ String

chosen_image = UI.openpanel("Open SKP File", "c:/", "model.skp")
chosen_image = UI.openpanel("Open Image File", "c:/", "Image Files|*.jpg;*.png;||")
chosen_image = UI.openpanel("Open CAD File", "c:/", "DXF|*.dxf|DWG|*.dwg||")           

打开“打开文件”页面

12.10 .openURL(url) ⇒ Boolean

status = UI.openURL("http://www.sketchup.com")
#打开网页           

12.11 .play_sound(filename) ⇒ nil

UI.play_sound "Plugins/mediadiscussion.wav"           

12.12 .preferences_pages⇒ Array<String>

prefs = UI.preferences_pages           
sketchup Ruby API学习笔记(六)——UI模块

12.13 .refresh_inspectors⇒ nil

告诉SketchUp刷新所有检查器,如组件浏览器和大纲视图。当您通过Ruby对文档进行更改后需要手动强制刷新时,这非常有用。通常,SketchUp会为您保持这些同步,但偶尔不会,例如当model.start_operation禁用了UI更新时。

UI.refresh_inspectors           

12.14 .refresh_toolbars⇒ nil

UI.refresh_toolbars           

12.15 .savepanel(title, directory, filename) ⇒ String

The savepanel method is used to display the Save dialog box. The path that is returned can then be used inside code to save out a text or image file. See the standard Ruby class File for examples of reading and writing from disk.

path_to_save_to = UI.savepanel("Save Image File", "c:\\", "Shapes.jpg")           

12.16 .scale_factor⇒ Float

显示器相关功能。

12.17 .select_directory(options = {}) ⇒ String, ...

# Default title and folder:
chosen_folder = UI.select_directory

# Custom dialog title:
chosen_folder = UI.select_directory(title: "Select Image Directory")

# Force a start folder:
chosen_folder = UI.select_directory(directory: "C:/images")

# Allow multiple items to the selected:
chosen_folder = UI.select_directory(select_multiple: true)

# Custom dialog title and force a start folder:
chosen_folder = UI.select_directory(
  title: "Select Image Directory",
  directory: "C:/images"
)           

12.18 .set_cursor(cursor_id) ⇒ Boolean

def onSetCursor
  UI.set_cursor(cursor_id)
end           

set_cursor方法用于将光标更改为具有给定光标id的新光标。有关使用任意光标创建自己的工具的详细信息,请参见UI.create_cursor和Tool类。

如果在标准SketchUp工具处于活动状态时调用此命令,则不会看到自定义光标,因为这些工具不断设置自己的光标以指示SketchUp的状态。

12.19 .set_toolbar_visible(name, visible) ⇒ Boolean

status = UI.set_toolbar_visible("Camera", true)           

设置工具条显示。测试失败,我还没搞明白。

12.20 .show_extension_manager⇒ nil

显示“扩展程序管理器”

12.21 .show_inspector(name) ⇒ Boolean

status = UI.show_inspector("Components")           

打开组件选项卡。测试成功,返回true。

12.22 .show_model_info(page_name) ⇒ Boolean

UI.show_model_info('Credits')
#返回true,弹出模型信息->版权信息           

12.23 .show_preferences(page_name) ⇒ Boolean

status = UI.show_preferences('GraphicsCard')
#返回true,弹出系统设置->openGL           

12.24 .start_timer(seconds, repeat = false) {|procedure| ... } ⇒ Integer

# Beep once after 10 seconds.
id = UI.start_timer(10, false) { UI.beep }           

12.25 .stop_timer(id) ⇒ nil

# Stop timer before it triggers.
id = UI.start_timer(10) { UI.beep }
UI.stop_timer(id)           

其实timer无所谓,我们可以使用ruby的time

12.26 .toolbar(name) ⇒ UI::Toolbar

toolbar = UI.toolbar('Test')           

12.27 .toolbar_names⇒ Array<String>

names = UI.toolbar_names
#["¦ᄌᄏ│ᆭチ", "¦ᄏモ¥ᄎモ", "¦ᄑ﾿￧ヤᄄ¥ナᆬ←ラᄄ", "¥ネニ￧ᄆᄏ¥ルᄄ", "¥ワᄚ￧ツᄍ", "¥ᄂᄃ¥ᄋᆬ¥ナᄋ←ロニ", "¥ᆴ゙¦ᄑモ¥ᄋᆬ¥ナᄋ", "¥ᄏᄎ￧ᆳム₩ヨᄑ¥ᄋᆬ", "₩ネᆰ←ンᄁ", "₩ᅠヌ¥ヌニ", "₩ᅠヌ│ᆴᄚ", "₩ᅠᄋ¥ᄐマ", "￧ロᄌ₩ワᄎ", "￧ᄏリ¥ロᄒ", "￧ᄐヨ│ᄒム", "│ᄃニ¥ロᄒ", "←リᄡ¥ᄑᄆ"]           

12.28 .toolbar_visible?(name) ⇒ Boolean

status = UI.toolbar_visible?("Camera")           

测试失败

12.29 Class: UI::Command 命令类

# You can add menu items as procedure blocks, as shown here, but
# you have no control over whether it is grayed out, for example.
UI.menu("Draw").add_item("My Procedure") {
  UI.messagebox("My Procedure")
}

# Better to create a command object.
cmd = UI::Command.new("Tester") {
  UI.messagebox("My Command")
}
cmd.menu_text = "My Command"
cmd.set_validation_proc {
  if Sketchup.active_model.selection.length == 0
    MF_GRAYED
  else
    MF_ENABLED
  end
}
UI.menu("Draw").add_item cmd           

12.29.1 .new(menutext) { ... } ⇒ UI::Command

UI.menu("Draw").add_separator

# Adds a Test submenu to the Draw menu where the Tester menu item appears
testmenu = UI.menu("Draw").add_submenu("Test")

# This menu item simply displays Hello World on the screen when clicked.
cmd = UI::Command.new("Tester") { UI.messagebox("Hello World") }
testmenu.add_item cmd           
sketchup Ruby API学习笔记(六)——UI模块
sketchup Ruby API学习笔记(六)——UI模块

12.29.2 #extension⇒ SketchupExtension?

extension = Sketchup.extensions['Sandbox Tools']
cmd = UI::Command.new("Tester") {}
cmd.extension = extension
p cmd.extension == extension
#在SU2020下,返回error           

注: 在SketchUp 2019之前,由于SketchUp初始化类的方式存在错误,无法对UI::Command进行子类。

12.29.3 #extension=(extension) ⇒ Object

注: 同上

12.29.4 #get_validation_proc⇒ Proc?

SU2022以上,略

12.29.5 #large_icon⇒ String大图标

toolbar = UI::Toolbar.new "Test"
# This command displays Hello World on the screen when clicked
cmd = UI::Command.new("Test") { UI.messagebox("Hello World") }
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
toolbar = toolbar.add_item cmd
toolbar.show
puts cmd.large_icon           
sketchup Ruby API学习笔记(六)——UI模块

12.29.6 #large_icon=(path) ⇒ String

toolbar = UI::Toolbar.new "Test"
# This command displays Hello World on the screen when clicked
cmd = UI::Command.new("Test") { UI.messagebox("Hello World") }
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
toolbar = toolbar.add_item cmd
toolbar.show           

和前面的例子几乎一样,就少了最后一行

12.29.7 #menu_text⇒ String

add_separator_to_menu("Draw")
# Adds a Test submenu to the Draw menu where the Tester menu item appears
testmenu = UI.menu("Draw").add_submenu("Test")
cmd = UI::Command.new("Tester") { UI.messagebox("Hello World") }
cmd.menu_text = "New String"
testmenu.add_item cmd
puts cmd.menu_text           
sketchup Ruby API学习笔记(六)——UI模块

12.29.8 #menu_text=(menuitem) ⇒ String

add_separator_to_menu("Draw")
# Adds a Test submenu to the Draw menu where the Tester menu item appears
testmenu = UI.menu("Draw").add_submenu("Test")
cmd = UI::Command.new("Tester") { UI.messagebox("Hello World") }
cmd.menu_text = "New String"
testmenu.add_item cmd           
sketchup Ruby API学习笔记(六)——UI模块

12.29.9 #proc⇒ Proc

SU2022

12.29.10 #set_validation_proc{ ... } ⇒ UI::Command

# Create a command object.
cmd = UI::Command.new("Tester") {
  UI.messagebox("My Command")
}
cmd.menu_text = "My Command"
cmd.set_validation_proc {
  if Sketchup.active_model.selection.length == 0
    MF_GRAYED
  else
    MF_ENABLED
  end
}
UI.menu("Draw").add_item(cmd)
#当有active_model被selection时,按钮My Command生效,否则按钮失效。           
sketchup Ruby API学习笔记(六)——UI模块

12.29.11 #small_icon⇒ String

12.29.11 #small_icon=(path) ⇒ String

12.29.12 #status_bar_text⇒ String

toolbar = UI::Toolbar.new("Test")
# This toolbar tool simply displays Hello World on the screen
# when clicked
cmd = UI::Command.new("Tester") { UI.messagebox("Hello World") }
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
cmd.status_bar_text = "Testing the toolbars class."
toolbar = toolbar.add_item cmd
toolbar.show
puts cmd.status_bar_text           
sketchup Ruby API学习笔记(六)——UI模块

12.29.12 #status_bar_text=(text) ⇒ String

12.29.13 #tooltip⇒ String

12.29.13 #tooltip=(text) ⇒ String

toolbar = UI::Toolbar.new "Test"
# This command displays Hello World on the screen when clicked
cmd = UI::Command.new("Test") { UI.messagebox("Hello World") }
cmd.tooltip = "Hello World Tool"
cmd.status_bar_text = "Testing the toolbars class."
toolbar = toolbar.add_item cmd
toolbar.show
puts cmd.tooltip
#添加一行,第5行后,案例就清楚了。           
sketchup Ruby API学习笔记(六)——UI模块

12.30 Class: UI::HtmlDialog (HTML对话框)

12.30.1 #initialize(properties) ⇒ UI::HtmlDialog

dialog = UI::HtmlDialog.new(
{
  :dialog_title => "Dialog Example",
  :preferences_key => "com.sample.plugin",
  :scrollable => true,
  :resizable => true,
  :width => 600,
  :height => 400,
  :left => 100,
  :top => 100,
  :min_width => 50,
  :min_height => 50,
  :max_width =>1000,
  :max_height => 1000,
  :style => UI::HtmlDialog::STYLE_DIALOG
})
dialog.set_url("http://www.sketchup.com")
dialog.show
#Access denied           

Parameters:

  • properties
  • (Hash)
  • A hash containing the initial properties of the newly created dialog.

Options Hash (properties):

  • :dialog_title (String)
  • :preferences_key (String)
  • :scrollable (Boolean)
  • :resizable (Boolean) — default: true
  • :use_content_size (Boolean) — default: false
  • :width (Integer) — default: 250
  • :height (Integer) — default: 250
  • :left (Integer) — default: 0
  • :top (Integer) — default: 0
  • :min_width (Integer) — default: 0
  • :min_height (Integer) — default: 0
  • :max_width (Integer) — default: -1
  • :max_height (Integer) — default: -1
  • :style (Integer) — default: UI::HtmlDialog::STYLE_DIALOG

Version:

  • SketchUp 2017

12.30.2 #add_action_callback(callback_name) {|action_context, *args| ... } ⇒ Boolean

回调函数。

Examples:

Ruby Code

dialog.add_action_callback("say") { |action_context, param1, param2|
  puts "JavaScript said #{param1} and #{param2}"
}           

JavaScript

sketchup.say('Hello World', 42);           

JavaScript with callback

sketchup.say('Hello World', 42, {
  onCompleted: function() {
    console.log('Ruby side done.');
  }
});           

Returns true if action added successfully, false otherwise.

12.30.2 补充一个案例

sketchup Ruby API学习笔记(六)——UI模块
sketchup Ruby API学习笔记(六)——UI模块

12.30.3 #bring_to_front⇒ nil

dialog.bring_to_front           

置于最前

12.30.4 #center⇒ true

dialog.center           

置于中心

12.30.5 #close⇒ nil

dialog.close           

关闭对话框

12.30.6 #execute_script(script) ⇒ nil

js_command = "document.getElementById('id').innerHTML = '<b>Hi!</b>'"
dialog.execute_script(js_command)           

12.30.7 #get_content_size⇒ Array(Integer, Integer)?

width, height = dialog.get_content_size           

12.30.8 #get_position⇒ Array(Integer, Integer)?

left, top = dialog.get_position           

12.30.9 #get_size⇒ Array(Integer, Integer)?

width, height = dialog.get_size           

12.30.10 #set_can_close⇒ Boolean

dialog.set_can_close { false }           

12.30.11 #set_content_size(width, height) ⇒ nil

dialog.set_content_size(600, 400)           

12.30.12 #set_file(filename) ⇒ nil

dialog.set_file("c:/mypage.html")           

12.30.13 #set_html(html_string) ⇒ nil

html = '<b>Hello world!</b>'
dialog.set_html(html)
dialog.show()           

12.30.14 #set_on_closed⇒ Boolean

dialog.set_on_closed { save_selection }           

#set_on_closed方法用于附加一个将在对话框已经处于关闭过程中时执行的块,在此块内执行任何最后一分钟的操作,例如保存当前状态。

12.30.15 #set_position(left, top) ⇒ true

dialog.set_position(100, 50)           

12.30.16 #set_size(width, height) ⇒ true

dialog.set_size(320, 240)           

12.30.17 #set_url(url) ⇒ nil

dialog.set_url("https://www.sketchup.com")           

12.30.18 #show⇒ nil

dialog.show           

12.30.19 #show_modal⇒ nil

dialog.show_modal           

12.30.20 #visible?⇒ Boolean

if dialog.visible?
  dialog.bring_to_front
else
  dialog = UI::HtmlDialog.new
  dialog.set_url("https://www.sketchup.com")
  dialog.show
end           

12.31 Class: UI::Notification

通知类,测试不成功。

12.32 Class: UI::Toolbar 工具条类

toolbar = UI::Toolbar.new "Test"
# This toolbar icon simply displays Hello World on the screen
cmd = UI::Command.new("Test") {
  UI.messagebox "Hello World"
}
cmd.small_icon = "ToolPencilSmall.png"
cmd.large_icon = "ToolPencilLarge.png"
cmd.tooltip = "Test Toolbars"
cmd.status_bar_text = "Testing the toolbars class"
cmd.menu_text = "Test"
toolbar = toolbar.add_item cmd
toolbar.show           

12.32.1 .new(toolbarname) ⇒ UI::Toolbar

toolbar = UI::Toolbar.new "Test"           

12.32.2 #add_item(command) ⇒ UI::Toolbar

toolbar = toolbar.add_item command
#会报错command指代cmd = UI::Command.new("Test") {}           

12.32.3 #add_separator(arg) ⇒ UI::Toolbar

add_separator方法用于向工具栏添加行分隔符。

toolbar = toolbar.add_separator           

12.32.4 #count⇒ Integer

number = toolbar.count
#返回按钮数量           

12.32.5 #each{|command| ... } ⇒ nil

toolbar.each { | item |
  puts item
}
#返回
#<UI::Command:0x00000271d7b25110>
#<UI::Command:0x00000271d7b25048>
#<UI::Command:0x00000271d7b24fa8>
#<UI::Command:0x00000271d7b24f08>
#<UI::Command:0x00000271d7b24e68>
#<UI::Command:0x00000271d7b24dc8>           

12.32.6 #get_last_state⇒ Boolean

state = toolbar.get_last_state           

get_last_state方法用于确定工具栏在SketchUp的上一个会话中是隐藏的还是可见的。

有效状态为1表示可见,0表示隐藏,-1表示以前从未显示。

12.32.7 #hide⇒ nil

toolbar.hide           

12.32.8 #length⇒ Integer

toolbar.length
#6           

12.32.9 #name⇒ String

puts toolbar.name
#test           

12.32.10 #restore⇒ nil

恢复方法用于将工具栏重新定位到其以前的位置,并在未隐藏时显示。

toolbar.restore           

12.32.11 #show⇒ nil

toolbar.show           

12.32.12 #size⇒ Integer

toolbar.size
#6           

12.32.13 #visible?⇒ Boolean

visible = toolbar.visible?
#true           

12.33 Class: UI::WebDialog

已弃用

继续阅读