Set the active model to front view and zoom it align to screen center with ruby

Hi all. I wonder if there is a quick ruby code could do the flowing steps:

  1. Set the front view of an active model.
  2. Then align the model’s center to screen center.
  3. Last, zoom out or in the camera to fit the model into a fixed square region(ex.1024*1024) in the center of the screen.
  4. The better result is that it could save the current view into a png file.

From:


To:

Thanks for your time.

That is the way of thinking when a human user does it.

The way you would do it with the API is to use math to calculate the desired camera position/direction/field of view in one step and apply it in one step.

…maybe you can check this:

https://extensions.sketchup.com/en/content/eneroth-view-memory

https://extensions.sketchup.com/en/content/eneroth-relative-camera

Sketchup.send_action( "viewFront:" )

Either …

Sketchup.send_action( "viewZoomExtents:" )

… or …

Sketchup.send_action( "viewZoomToSelection:" )

See the Team extension …
http://extensions.sketchup.com/en/content/safe-frames

… or …

Sketchup::View#zoom() method

Sketchup::View#write_image() method

Thanks, Dan. I have completed what I need.

1 Like

Thanks, dezmo. I wanna write it by myself, coz it’s really quick code…

Yes, for step 3, need to do some math calculation. Thanks for your advice, and I have completed this.