SketchUp Ruby API and 2 Point Perspective Expertise Needed

The name of the dictionary "JcB" should have been read as an example. (It is John’s initials.)
You really should use your own unique dictionary name. Usually developers use a combination of their company namespace and the plugin name …
Ex:

return if Sketchup.active_model.get_attribute('YangSuoJin_ModelMover', 'move_done')

The return statement in Ruby “returns” from a method to the place that called the method.

The first time run, your should see "new_file" output to the Console.
You can change that to something more descriptive, if you wish. Or you could use a message box.
See: UI.messagebox

1 Like

Very good to know! Thanks Dan.

1 Like

@john_drivenupthewall

Final Code with Full Function:

require 'sketchup.rb'

module JohnDanYangshuojin
  module CenteringModel

    # Centering model on xy plane of the world axes but preserving the camera setting saved with all scenes.
    def self.centering_model

      # Centering the model on xy plane by the projection of its geometric center.
      model = Sketchup.active_model
      center = model.bounds.center
      vec =  Geom::Point3d.new(center.x,center.y,0).vector_to(ORIGIN)
      model.entities.transform_entities(vec,model.entities.to_a)

        stp_r, stp_g, stp_b = vec.to_a.entries

          # Preserving the camera setting in each scenes.
          pages = Sketchup.active_model.pages
          pages.each do |page|
          # camera
          cam = page.camera
          # camera target
          target = cam.target
          # camera eye
          eye = cam.eye
          # camera up
          up = cam.up
          # get arrays
          e0  = eye.to_a
          t0  = target.to_a
          # use standard ruby on target position
          t = t0.zip([stp_r, stp_g, stp_b]).map(&:sum)
          # eye positions
          e = e0.zip([stp_r, stp_g, stp_b]).map(&:sum)
          # use the same up
          cam.set(e, t, up)
          end #pages.each

      # Message about the job done.
      UI.messagebox('This model is centered!')

    end # centering_model

    #Add menu item.
    unless file_loaded?(__FILE__)

    # Add to extension menu.
    menu = UI.menu('Plugins')

    #Menu name.
    menu.add_item('Centering Model'){
      self.centering_model
    }

    file_loaded(__FILE__)
    end

  end # module CenteringModel

end # module JohnDanYangshuojin
  1. Your top level namespace module should be unique, probably Shuojin, and not Examples as this is too generic and likely to clash with someone’s else’s example utility plugin.

  2. Offsetting the model by a specific “hard-coded” distance does not set a good example. Not all models will have the same boundingbox so they’ll need to each be offset by the vector from their bottom center to the ORIGIN (as we showed above several times.)

  3. Your indentation is not correct and it’s 4 spaces. Ruby language uses 2 space indents. (The 4 space indents cause the need for excessive horizontal scrolling of code block in forum posts.)

1 Like

Thanks Dan! I will definitely do clean up and repost. I do see your comments would benefit hugely, hugely in the long run.

2 Likes

Dan,

Code refined based on your instruction. I wonder how you guy’s code gets color and format?

See this pinned post at top of Developers category …

1 Like