Skip to content

Instantly share code, notes, and snippets.

@jimfoltz
Created March 22, 2014 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimfoltz/9699580 to your computer and use it in GitHub Desktop.
Save jimfoltz/9699580 to your computer and use it in GitHub Desktop.
# jf_attach_camera_to_sun.rb (C) jim.foltz@gmail.com
#
# Name: Sun Tool
#
module JF
module AttachCameraToSun
VERSION = '1.0.0'.freeze
class ShadowInfoObserver < Sketchup::ShadowInfoObserver
def register(tool)
@tool = tool
end
def onShadowInfoChanged(*args)
si = args[0]
if @tool
@tool.update()
else
raise "#{$0}Tool not registered."
end
end
end # class ShadowInfoObserver
class CameraToSunTool
def activate
@camera = Sketchup.active_model.active_view.camera
@mem = [@camera.eye, @camera.target, @camera.up]
@target = Sketchup.active_model.bounds.center
@shadow_observer = ShadowInfoObserver.new
@altitude = (@camera.eye - @target).length
@shadow_observer.register(self)
Sketchup.active_model.shadow_info.add_observer(@shadow_observer)
sel = Sketchup.active_model.selection
if sel.size > 0
bb = Geom::BoundingBox.new
sel.each do |entity|
if entity.respond_to?(:bounds)
bb.add(entity.bounds)
end
end
@target = bb.center
end
update
end
def enableVCB?
return true
end
def onUserText(text, view)
@altitude = text.to_l
update
end
# called when the ShadowInfo is changed
def update()
si = Sketchup.active_model.shadow_info
v = si["SunDirection"]
v.length = @altitude
t = si["ShadowTime"]
tz = si["TZOffset"]
t = t - (60 * 60 * tz)
ft = t.strftime("%m/%d/%Y %H:%M %p")
Sketchup.set_status_text("Attach Camera To Sun v#{VERSION} - Shadow Time: #{ft}", SB_PROMPT)
Sketchup.set_status_text("Distance", SB_VCB_LABEL)
Sketchup.set_status_text(v.length, SB_VCB_VALUE)
@pt = @target + v
Sketchup.active_model.active_view.invalidate
end
def deactivate(*args)
Sketchup.active_model.shadow_info.remove_observer(@shadow_observer)
@shadow_observer = nil
@camera.set(*@mem)
end
def resume(view)
@altitude = (@camera.eye - @target).length
Sketchup.set_status_text "Sun Tool", SB_PROMPT
update
end
def draw(view)
if @pt
@camera.set(@pt, @target, Z_AXIS)
end
end
end # SunTool
end # AttachCameraToSun
end # jf
UI.menu('Plugins').add_item("Attach Camera to Sun") { Sketchup.active_model.select_tool(JF::AttachCameraToSun::CameraToSunTool.new) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment