# transformable_ci.rb - a ComponentInstance with easy, transform!-based transformations # from Edges to Rubies - The Complete SketchUp Tutorial # Copyright 2010, Martin Rinehart require 'sketchup' $radians_per_degree = Math::PI/180.0 class TransformableCI =begin A TransformableCI can do move, rotate and scale transformations. The developer creates a TransformableCI from a ComponentInstance and then applies move(), rotate() and/or scale() methods directly, without needing Transformation objects or methods. Internally, the transformations are performed with the ComponentInstance.transform!() method, recording changes on the undo stack and redrawing immediately. (Use a MovableCI if you do not want to record undo changes and view changes immediately .) This is documented in the tutorial's Chapter 16. =end attr_reader :inst, :trans def initialize( inst ) @inst = inst @trans = inst.transformation.to_a() end # of initialize def move( *args ) # Point3d or Vector3d or [r,g,b] arg = args[0] if args.length == 3 vec = Geom::Vector3d.new( args[0], args[1], args[2] ) xform = Geom::Transformation.new( vec ) @inst.transform!( xform ) elsif arg.is_a?( Array ) vec = Geom::Vector3d.new( args[0] ) xform = Geom::Transformation.new( vec ) @inst.transform!( xform ) elsif arg.is_a?( Geom::Vector3d ) xform = Geom::Transformation.new( args[0] ) @inst.transform!( xform ) elsif arg.is_a?( Geom::Point3d ) xform = inst.transformation().to_a() xform[12] = arg[0]; xform[13] = arg[1]; xform[14] = arg[2] @inst.transformation= xform else raise "move cannot handle " + args[0].to_s() end end # of move() def rotate( point, plane_or_axis, degrees ) axis = make_axis(plane_or_axis) degrees *= $radians_per_degree xform = Geom::Transformation.rotation( point, axis, degrees ) @inst.transform!( xform ) end # of rotate() def scale( *args ) case args.length when 1 then xform = Geom::Transformation.scaling( args[0] ) when 2 then xform = Geom::Transformation.scaling( args[0], args[1] ) when 3 then xform = Geom::Transformation.scaling( args[0], args[1], args[2] ) when 4 then xform = Geom::Transformation.scaling( args[0], args[1], args[2], args[3] ) end @inst.transform!( xform ) end # of scale() # support functions def make_axis( plane_or_axis ) if plane_or_axis.is_a?( String ) case plane_or_axis when 'rg' then axis = [0,0,1] when 'rb' then axis = [0,1,0] when 'gb' then axis = [1,0,0] else raise "Plane must be 'rg', 'rb', 'gb' or an axis." end else axis = plane_or_axis end return axis end # of make_axis() def inspect() return '#' end # of inspect() end # of class MovableCI # end of movable_ci.rb