How do you reverse a section plane in Ruby API?


#1

What is the command line to reverse a section plane?


#2

SectionPlane.get_plane
Reverse the plane and apply it back to the sectionplane with SectionPlane.set_plane


#3

Sorry, how do you reverse the plane?


#4

The section plane has the same properties as a geometrical plane, which can be oriented and located with a vector and a position. The vector and position can be transformed into an equation with 4 coefficients. The SectionPlane class includes a “get_plane” method (as @guy_wydouw showed) that returns an array with 4 equation coefficients. The array values can be thought of as:

plane_definition = [ x, y, z, d ]

The first 3 array elements are the coordinate data for the vector that represents the arrow direction when you select the section plane. The last one is the distance of the plane from the origin (position 0,0,0). The distance “d” can be negative, indicating that the origin is behind the plane. Both the vector and the distance need to be reversed, so all values need to be set negative. Given a Ruby variable “section_plane” referencing the desired section plane, then the following worked for me at the command line:

plane_def = section_plane.get_plane
section_plane.set_plane  [ -plane_def.x, -plane_def.y, -plane_def.z, -plane_def.last ]

I’m making use of the fact that the Sketchup team has modified the array class so that the first three elements of any array can be accessed with x, y and z.


#5

Another example, where sect_plane is a reference to the object, and just uses the array object returned from the get_plane method, without creating another literal array object:

sect_plane.set_plane( sect_plane.get_plane.map!{|i|-i} )

see: Array#map!


#6

Here is an example of a Ruby 2.0+ refinement:


module Author::Refine::SectionPlane
  refine Sketchup::SectionPlane do
    def reverse
      set_plane( get_plane.map!{|i|-i} )
    end
    alias_method(:invert,:reverse)
  end
end

using Author::Refine::SectionPlane

module Author::SomeNiftyPlugin

  def self::do_nifty()
    sel = Sketchup::active_model.selection
    return false if sel.empty?
    sp = sel.grep(Sketchup::SectionPlane).first
    return false if sp.nil?
    sp.reverse
  end

end

The Author namespace module is a frivolous holder for an actual namespace module name. (Ie, you replace it with your own module name.)