Zoom Extents of 8.5x11

I have an extension that cycles through all the existing tags, regex maps the tag name to a camera orientation, zooms extents, makes a scene, then publishes those scenes to Layout on 8.5"x11" paper.
This seems to be a very simple solution, except that my window has to be roughly 8.5"x11" or the zoom level is incorrect in LO. Would anyone care to share a more robust solution than zoom extents?

I have a solution for Ortho that involves knowing the size of what’s captured and setting the scale. But I haven’t been able to figure it out for perspective.

Thanks and Regards

Just learned how to paste code. This gives different results in LO depending on the aspect ratio of the SU window. Any ideas on how to get consistent results when viewing in perspective?

# simple_scenes.rb

module SimpleScenes
  def self.model
    Sketchup.active_model
  end
  def self.scenes
    model.pages
  end
  def self.view
    model.active_view
  end

  def self.add_lo_view bounds, page, scene_num, layer
    skp_model = Layout::SketchUpModel.new(model.path, bounds)
    skp_model.current_scene = scene_num
    page.document.add_entity(skp_model, layer, page)
  end

  def self.make_scenes
    scenes.to_a.each { |s| scenes.erase(s) }
    view.zoom_extents
    scenes.add 'reset'
   
    #... other scenes 
  end
  
  def self.send_scenes_to_layout
    model.save
    dirname = File.dirname model.path
    filename = File.basename(model.path, '.skp') + '.layout'
    lo_path = File.join(dirname, filename)

    paper_width = 11
    paper_height = 8.5
    lo_file = Layout::Document.new

    skp_layer = lo_file.layers.add('Model Views', false)

    lo_file.page_info.width = paper_width
    lo_file.page_info.height = paper_height
    lo_file.page_info.display_resolution = Layout::PageInfo::RESOLUTION_HIGH

    border = 0.25

    view_bounds = Geom::Bounds2d.new(border, border, paper_width - 2*border, paper_height - 2*border)

    add_lo_view(view_bounds, lo_file.pages.first, 1, skp_layer)

    #... add more views

    lo_file.save(lo_path)
    Sketchup.send_to_layout(lo_path)
  end

  make_scenes
  send_scenes_to_layout
end

Here is one method to change size of window

Thanks to @ eneroth3

I installed Eneroth Viewport Resizer and changed make_scenes to

  def self.make_scenes
    hWnd = Ene_ViewprtResizer::GetActiveWindow.call
    current_size = Ene_ViewprtResizer.get_size(hWnd)

    scenes.to_a.each { |s| scenes.erase(s) }
    Ene_ViewprtResizer.resize(true, [1100, 850])
    view.zoom_extents
    scenes.add 'reset'
   
    #... other scenes 
    if true
      Ene_ViewprtResizer::ShowWindow.call hWnd, 3 #SW_MAXIMIZE
    else
      Ene_ViewprtResizer.resize(true, current_size)
    end
  end

1 Like

The code I’m using has the same debilitating issue. I’d be glad if someone had a solution.

In my case I’m exporting a 2D image so I can control the aspect ratio, but the zoom is wrong depending on the viewport ratio.

Sketchup.active_model.active_view.write_image(f2.path, 2600, 1600, false, 0.0)

How about ?

  def write_view(path, width, height, antialias = true, compression = 1.0)
    model = Sketchup.active_model
    view  = model.active_view
    camera_old = view.camera
    aspect_old = camera_old.aspect_ratio
    aspect = width.to_f / height.to_f
    view.camera.aspect_ratio= aspect
    view.zoom_extents
    view.write_image(path, width, height, antialias, compression)
    UI.start_timer(0.5,false) {
      view.camera= camera_old
      view.camera.aspect_ratio= aspect_old
    }
  end
  
  write_view(f2.path, 2600, 1600, false, 0.0)
1 Like