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 Examples
	module CenteringModel

		#centering model.
		def self.centering_model
			# at the top
	        done = Sketchup.active_model.get_attribute('Shuojin', 'model_moved')
	        if done
	        	UI.messagebox('This model is centered already!')


            else
				#centering the model by preset distance.
				model = Sketchup.active_model
				vec =  Geom::Point3d.new(186764.218159,167965.596006,188.011513).vector_to(ORIGIN)
				model.entities.transform_entities(vec,model.entities.to_a)

				 stp_r, stp_g, stp_b = vec.to_a.entries
				    # then
				    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

				# adding a tag
	            Sketchup.active_model.set_attribute('Shuojin', 'model_moved', true)

	            UI.messagebox('This model is centered!')

            end


        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 Examples
  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