require 'sketchup.rb'
##
## Rotate selected component instance 90 degrees around Z axis
##
##  Copyright (c) 2008 Clark Bremer (aka daiku)
##  Northern Lights Timber Framing
##  clarkb@northernlightstimberframing.com
##
## 5/17/08:  All 3 axes.  Input from Matt666
## 4/24/08: Use center as rotation point instead of CI origin.  Also works on groups now.
##
def sele(typ)
    mm = Sketchup.active_model
    ss = mm.selection
	ent = ss[0]
    return nil if ss.count != 1 
    return nil if not ent.typename == typ
    ent
end

def rotate90(ent, vecteur)
	#rp = Geom::Point3d.new(ent.bounds.center) 		#rotation point
	rv = Geom::Vector3d.new(0,0,1) if vecteur == "z"
	rv = Geom::Vector3d.new(0,1,0) if vecteur == "y"
	rv = Geom::Vector3d.new(1,0,0) if vecteur == "x"
	#ra = 90.degrees
	#rt = Geom::Transformation.rotation(rp, rv, ra) 
	ent.transform!(Geom::Transformation.rotation(Geom::Point3d.new(ent.bounds.center), rv, 90.degrees))
end

if( not file_loaded?("rot90.rb") )
    UI.add_context_menu_handler do |menu|
		if menu == nil then 
			UI.messagebox("Error setting context menu handler")
		else
			if (sel = sele "ComponentInstance") or (sel = sele "Group")
				sbm = menu.add_submenu("Rotate 90")
				sbm.add_item("Around Red") {rotate90 sel, "x"}
				sbm.add_item("Around Green") {rotate90 sel, "y"}
				sbm.add_item("Around Blue") {rotate90 sel, "z"}
			end
		end
        
	end
end

file_loaded("rot90.rb")