The HtmlDialog bug!

My Code:

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.myweb.com")
dialog.add_action_callback("say") { |action_context, url|
  definition = Sketchup.active_model.definitions.load_from_url url,DownLoadHandler.new
  if definition
    Sketchup.active_model.place_component definition,false
  end
}
dialog.show

This code can work on Windows System,but can’t work on MacOS!
when on MacOS ,the Sketchup will suspended!

you haven’t provided ‘workable’ code for either platform…

here’s an example that runs on a mac and downloads one of @Cotty’s models from this forum…

class DownLoadHandler

  attr :error

  def onPercentChange(percent)
    Sketchup::set_status_text("LOADING: #{percent}%")
  end

  def cancelled?
    # You could, for example, show a messagebox after X seconds asking if the
    # user wants to cancel the download. If this method returns true, then
    # the download cancels.
    return false
  end

  def onSuccess
    Sketchup::set_status_text('')
  end

  def onFailure(error_message)
    self.error = error_message
    Sketchup::set_status_text('')
  end

end

def handle_download(url)
	model = Sketchup.active_model
	load_handler = DownLoadHandler.new
	definition = model.definitions.load_from_url(url, load_handler)

	if definition
		Sketchup.active_model.place_component definition,false
	else
		puts "Error: #{load_handler.error}"
	end
end

dialog = UI::HtmlDialog.new(
{
  :dialog_title => "Dialog Example",
  :preferences_key => '',
  :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
})

# using one of @cottys models as an example...
dialog.set_url('https://forums.sketchup.com/t/cottys-gallery-images/17043/149')

dialog.show

@url = nil

dialog.add_action_callback("say") { |action_context, url|
  @url = url
  p @url
  # dialog.close
}

script = %Q[
  document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM fully loaded and parsed");
  });

var skp = document.getElementsByClassName('attachment')[0].getAttribute('href');

sketchup.say(skp)]

      wait4url = UI.start_timer(1.0, true) do
        if @url.nil?
          dialog.execute_script(script)
        else
          p 'have url'
          UI.stop_timer(wait4url)
          handle_download(@url)
        end # if nil
      end # wait

if your website has an onclick(sketchup.say(url)) event, then you should be able to comment out the dialog.execute_script(script) and use the click event instead…

john

Thank you john_drivenupthewall!
I use this code and it work !

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.myweb.com")
dialog.add_action_callback("say") { |action_context, url|
 UI.start_timer(1.0, false) {
  definition = Sketchup.active_model.definitions.load_from_url url,DownLoadHandler.new
  if definition
    Sketchup.active_model.place_component definition,false
  end
}
dialog.show

1 Like

I’m having a very similar problem and can’t seem to get past it. It ONLY affects HTML Dialog and Mac OS. I’m testing in both SU Make 2017 and SU 2018 Pro on desktop.

Here’s the important chunk of code I’m running:

class DownloadHandler
    attr :error

    def initialize(name, source)
        @name = name
        @source = source
        message = "Loading #{@name} from #{@source}..."
        Sketchup::set_status_text(message)
    end

    def onPercentChange(p)
        puts('percent changed...')
        Sketchup::set_status_text("Loading #{@name} from #{@source}: " + p.to_i.to_s + "%")
    end

    def cancelled?
        return false
    end

    def onSuccess
        puts("done!")
        Sketchup::set_status_text('')
    end

    def onFailure(error_message)
        puts "on failure"
        message = "Error loading #{@name}:\n" + error_message
        self.error = message
        puts(message)
        Sketchup::set_status_text('')
    end
end

def place_component(url)

    Sketchup.active_model.start_operation("Download Component")

    model = Sketchup.active_model
    definitions = model.definitions

    if (url)
        load_handler = DownloadHandler.new(name, url)

        puts('after defining load handler - before calling load_from_url')

        componentdefinition = definitions.load_from_url(url, load_handler)

        puts(componentdefinition)
    else
        componentdefinition = definitions.load name
    end

    return if not componentdefinition

    model.place_component(componentdefinition, false)

    puts('commit')
    Sketchup.active_model.commit_operation
end

def on_load_comp(dlg, params)
    url = "http://projectdomainname.com/download/fffdf2eb9bcbc2f40f310c42fc7995f42dfda9ed/aaaaaaaaaaa/00:11:22:33:44/ModelName.skp"
    place_component(name, hash, cat, url)
end

I’ve stripped out a bunch here of course but this is the important stuff.

When I attempt to download using Windows I have no issue at all. But using a Mac, I get the pinwheel and it never stops loading. I’ve spent many hours trying to debug this. Can’t seem to get anywhere.

To be very clear, it crashes on the “definitions.load_from_url(url, load_handler)” line. The puts above it does echo out in the ruby console. But then it stalls out SketchUp completely.

I have just about everything I can find. Would appreciate some help.

Did you try wrapping your operation in a single loop UI.timer as in the previous post to yours ?


Secondly, I think there are issues with #load_from_url, #load and #place_component with regard to operations.

We discussed some of this previous … In this post there is link to the official issue tracker where we discuss issues with some methods that break undo operations, …

Cancel multiple methods with a single undo in SketchUp - #2 by DanRathbun

… and also in which I posted a link to rigmarole tool clone here …