Pages.add with local camera settings

I want to automatically create TopView scenes for the selected components.
If I were to do it manually I would:

Set Camera Parralel Projection >> Open object instance >> Select Top in Views toolbar >> Zoom Extents >> Add Scene.

I have implemented this logic in a Ruby script.
But the result is not the same as performing the operations manually in SketchUp.
The camera location created with the Ruby script is not what I expected.

mod = Sketchup.active_model
selected_objects = mod.selection.to_a

# Parralel Projection
mod.active_view.camera.perspective = false

selected_objects.each{ |object|
  # Open the instance
  instance_path = Sketchup::InstancePath.new([object])
  mod.active_path = instance_path
  # Set Top view
  Sketchup.send_action("viewTop:")
  # Zoom Extents
  mod.active_view.zoom_extents
  # Add new page in model
  mod.pages.add(object.name + "_TOP") 
  # Close the instance
  mod.active_path = nil
}

How can I set the Top View in the local object environment?
I’m probably overlooking something…

Sketchup.send_action is not synchronous. The successive Ruby statements can be executed before the view is done changing.

To speed things up, temporarily switch off scene transitions.

Secondly I don’t think I’d do it the way you’ve done it (ie, using the send_action.)
Before the iteration, I would setup a camera looking straight down and set it to the view:

center = model.bounds.center
cam = Sketchup::Camera.new(
  center.offset([0,0,model.bounds.depth]), center, Y_AXIS, false
)
view.camera= cam

During the iteration I would zoom(object) instead of entering in the object’s edit context.

1 Like

Thanks @DanRathbun for your comment :smiley:

In the overall script I already turned off the scene transitions so that it improves the speed.
I didn’t realize / read that Sketchup.send_a is not synchronous :sweat_smile:
That caused the main problem.

It was indeed successful by setting the camera properly.

tr = mod.axes.transformation
view = mod.active_view
center = mod.bounds.center
new_cam = Sketchup::Camera.new(
   center.offset([0,0,mod.bounds.depth]), center, object.transformation.yaxis.transform(tr), false
)
view.camera = new_cam
view.zoom_extents
1 Like