[code] Example "Move to Ground Plane" context menu command


#1

Continuing the discussion from How to set entities to a specific elevation (z):

Here is an example script that does this.
(It is just too simple to warrant the rigmarole of publishing an extension for it.)

  1. The top level namespace module name is just a generic “Author”.
    Change it to something unique for production use.

  2. If you wish to disable the UI redraw during the command,
    change the @@disable_ui variable to true.

move_to_ground_plane.rb (903 Bytes)

This is what the file looks like for those not wanting to download the file:

# encoding: UTF-8

module Author; end
module Author::MoveToGroundPlane

  extend self

  MENU_TEXT ||= "Move to Ground Plane"

  @@loaded ||= false
  @@disable_ui ||= false

  def move_to_ground_plane(ents)
    model = Sketchup.active_model
    model.start_operation(MENU_TEXT,@@disable_ui)
      ents.each do |ent|
        ent.transform!([0,0,-ent.transformation.origin.z])
      end
    model.commit_operation
  end

  if !@@loaded

    UI.add_context_menu_handler do |popup|

      sel = Sketchup.active_model.selection
      unless sel.empty?
        ents = sel.grep(Sketchup::Group) + sel.grep(Sketchup::ComponentInstance)
        unless ents.empty?
          popup.add_item(MENU_TEXT) { move_to_ground_plane(ents) }
        end
      end

    end # context menu handler registrar

    @@loaded = true

  end # run once at first load

end # extension module

How to set entities to a specific elevation (z)
#2

Note that in the above example, if the object is below the ground plane, it will be moved up to the ground plane.

This is because the example negates the object’s current z coordinate for the z transformation vector,
which is already negative, so -(-z) == +z for the movement vector.


#3

I’m a bit thick with ruby code, can I just copy the file move_to_ground_plane.rb to my plugin folder. Would it works?


#4

Yes it should, but you will not be able to turn if on and off with the Extension Manager.
(It would need an extension registrar script for that.)

(Likely Eneroth3 or sdmitch will add a command like this to one of their existing extensions.)


#5

Finally something short and functional for me to try to understand. I have long dreamed of understanding Ruby extensions, but with only an AutoLisp, GDL and Hypertalk (old Apple Hypercard) background it is a totally different world. Thanks a lot, Dan


#6

HA!!! Thanks Dan, it works.:+1::grin:


#7

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.