简体   繁体   English

无法在 SketchUp 中打开 a.html 文件 Ruby

[英]Unable to open a .html file in SketchUp Ruby

I am trying to create a tool which will open a.html file, however I need help with the piece of code which will open said.html file.我正在尝试创建一个可以打开 .html 文件的工具,但是我需要有关打开 said.html 文件的代码段的帮助。 I have this code below...我在下面有这段代码......

help_file = Sketchup.find_support_files("html", "Plugins")
if help_file
  # Print out the help_file full path
  UI.messagebox(help_file)

  # Open the help_file in a web browser
  UI.openURL("file://" + help_file)
else
  UI.messagebox("Failure")
end

The output from this piece of code is shown in the screenshot below.这段代码中的 output 显示在下面的屏幕截图中。

https://i.stack.imgur.com/ixLIT.png https://i.stack.imgur.com/ixLIT.png

That is what I had expected.这正是我所期望的。 No.html opened in Chrome either because there's two.html files. No.html 在 Chrome 中打开,因为有两个 .html 文件。 So now I take a step further and try to specify which.html file I would like opened.所以现在我更进一步,尝试指定要打开哪个 .html 文件。 I would like to open the 'basic.html' (it's a blank.html file so far) and I change my code accordingly (the first line to be specific).我想打开“basic.html”(到目前为止它是一个空白的 .html 文件)并相应地更改我的代码(第一行是特定的)。

help_file = Sketchup.find_support_files("basic.html", "Plugins")
if help_file
  # Print out the help_file full path
  UI.messagebox(help_file)

  # Open the help_file in a web browser
  UI.openURL("file://" + help_file)
else
  UI.messagebox("Failure")
end

Unfortunately, I didn't get the output I had hoped for.不幸的是,我没有得到我所希望的 output。 This is what I ended up with.这就是我最终得到的。

https://i.stack.imgur.com/4xyQT.png https://i.stack.imgur.com/4xyQT.png

the basic.html didn't open in Chrome either, which was a bummer. basic.html 也没有在 Chrome 中打开,这是一个无赖。

Below is what my files in the Plugins folder looks like, incase you wanted to see that.下面是我在 Plugins 文件夹中的文件的样子,如果您想查看的话。

https://i.stack.imgur.com/OW7xM.png https://i.stack.imgur.com/OW7xM.png

What is the problem I am facing?我面临的问题是什么?

How to test code below:如何测试下面的代码:

  1. Create.rb file (name it 'open-html.rb' for example) and copy & paste the code below.创建.rb 文件(例如将其命名为“open-html.rb”)并复制并粘贴下面的代码。 Then place the file in the SketchUp Plugins folder.然后将文件放在 SketchUp Plugins 文件夹中。
  2. Activate the tool inside Sketchup by going to Plugins or Extension Menu -> Open basic.html inside the plugins folder通过转到Plugins or Extension Menu激活 Sketchup 中的工具 - > Open basic.html inside the plugins folder
  3. Done.完毕。 If you have an HTML file named 'basic.html' inside the plugins folder then this script will open it.如果您在插件文件夹中有一个名为“basic.html”的 HTML 文件,那么此脚本将打开它。

Possible Solution #1可能的解决方案 #1

module DevName
  module PluginName
    class Main
      def activate
        @dlg = UI::HtmlDialog.new(html_properties_activate)

        plugins_folder = "file:///#{Sketchup.find_support_file('Plugins').gsub(/ /, '%20')}" #/
        html_file = File.join(plugins_folder, 'basic.html')
        @dlg.set_url(html_file)
        @dlg.show
        @dlg.center
      end

      def html_properties_activate
        {
          dialog_title: 'Dialog Example',
          preferences_key: 'com.sample.plugin',
          scrollable: true,
          resizable: true,
          width: 420,
          height: 320
        }
      end
    end
    unless defined?(@loaded)
      UI.menu('Plugins').add_item('Open basic.html inside the plugins folder') do
        Sketchup.active_model.select_tool(DevName::PluginName::Main.new)
      end
      @loaded = true
    end
  end
end

Possible Solution #2 (Better Solution)可能的解决方案#2 (更好的解决方案)

I prefer finding the location of 'basic.html' using the relative path of your '.rb' script and not using the 'Plugins' folder path.我更喜欢使用“.rb”脚本的相对路径而不是“插件”文件夹路径来查找“basic.html”的位置。

Also, when opening a local HTML file it is best to use .set_file instead of .set_url .此外,打开本地 HTML 文件时,最好使用.set_file而不是.set_url

module DevName
  module PluginName2
    class Main
      def activate
        @dlg = UI::HtmlDialog.new(html_properties_activate)
        path = File.dirname(__FILE__)
        html_file = File.join(path, '/basic.html')
        @dlg.set_file(html_file)
        @dlg.show
        @dlg.center
      end

      def html_properties_activate
        {
          dialog_title: 'Dialog Example',
          preferences_key: 'com.sample.plugin',
          scrollable: true,
          resizable: true,
          width: 420,
          height: 320
        }
      end
    end
    unless defined?(@loaded)
      UI.menu('Plugins').add_item('Open basic.html inside plugins folder Solution #2') do
        Sketchup.active_model.select_tool(DevName::PluginName2::Main.new)
      end
      @loaded = true
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM