Save image to html

Hi,

To save an image from the view in sketchup you have the following code:

 model = Sketchup.active_model
 entities = model.active_entities

 UI.messagebox "Now Lets Write the Image"
 view = model.active_view
 # Puts in SketchUp install directory by default
 status = view.write_image "test.jpg"
 keys = {
   :filename => "c:/tmp/write_image.jpg",
   :width => 1920,
   :height => 1080,
   :antialias => true,
   :compression => 0.9,
   :transparent => false
 }
 model = Sketchup.active_model
 view = model.active_view
 view.write_image keys

Is there a way to get this without saving in a html webdialog? So you have some sort of preview idea?

thanks

Saving in a html webdialog? I’m not sure I fully understand that question.
You want to preview the result without using a WebDialog? (Why do you not want to use a WebDialog?)

In your example snippet you save two times - why is that?

Hi thomthom,

Sorry for my english. Yes I want to use the webdialog to show the result. But without saving it first.

The code I sent came from the sketchup developer site. Forgot to delete the first rules.

You can’t display an image without first making a file to display - even if it’s temporary…
So use something a bit like the first example, to make a temporary image file.
Then with some simple html show that file in a SketchUp webdialog.

But you don’t even need to do that…
Make the temporary image file, then use something like:
UI.openURL("file::///#{full_path_to_image}")
It will then open that image in whatever your system has set up to display that image type - jpg, png etc

You can send the data as base64 to the webdialog - and use a data URI to display the image: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

However, there is no API method to get an image of the view without saving. So you have to use some form of temp file for that. But if you want to delete the temp file as quickly as possible you can read it, delete the temp file… Then base64 encode the content and pass it into the webdialog which inserts it as a data URI.

in it’s simplest form you just add an img tag with some sizing css…


model = Sketchup.active_model
entities = model.active_entities
view = model.active_view

img = '/tmp/test.jpg'

view.write_image(img)

html = %Q[
<html>
<head>
<style>
img {
  height:10cm;
  width:10cm;
}
</style>
</head><body><div class="container">
        <img src="#{img}" alt="#{File.basename(img, '.jpg')}"></div>
</body></html>
]

dlg = UI::WebDialog.new('My Image', false, '', 400, 400, 100, 100, false)
dlg.set_html(html)
dlg.show

john

1 Like

Working :slight_smile: thanks