Checking for users viewport settings?
-
A particular Ruby script I'm presently writing, assumes the user is in plan view, when he/she uses my script.
Is there a code snippet that I can add to my script, that does the following:
checks the current viewport setting, and re-orients if necessary the users viewport to a plan view , prior to activating the rest of my Ruby script? -
Look at http://code.google.com/apis/sketchup/docs/ourdoc/sketchup.html#send_action
Sketchup.send_action(xxx)
where xxx==
"viewTop:"
and then perhaps
"viewZoomExtents:"
or
"viewZoomToSelection:"
and probably before the zoom
model.active_view.camera.perspective=false
###(if model.active_view.camera.perspective?) -
You would want to check the vector of the camera direction. In plan view, it should be 0,0,-1. This snippet can be run from the web console:
model = Sketchup.active_model view = model.active_view cam = view.camera if cam.direction.normalize.inspect == (Geom::Vector3d.new 0,0,-1).inspect UI.messagebox "Plan view already!" else Sketchup.send_action("viewTop:") end
Thanks to TIG for the send_action idea. I rarely think about those things, and it simplified setting the top view camera significantly.
-
I was also doing some more digging into TIG's Stuff I modified the axo+iso.rb to include a plan option
(a lot of coding to get the same desired effect) ...... thanks for the inputdef plan model=Sketchup.active_model model.start_operation("plan") view=model.active_view camera=view.camera eye=Geom;;Point3d.new(0.0,0.0,1.0) target=Geom;;Point3d.new(0.0,0.0,0.0) up=Geom;;Vector3d.new(0.0, 0.1, 0.1) perspective=false newcamera=Sketchup;;Camera.new(eye,target,up,perspective) view.camera=newcamera Sketchup.send_action("viewZoomExtents;") puts model.commit_operation end
-
@chris fullmer said:
You would want to check the vector of the camera direction. In plan view, it should be 0,0,-1. This snippet can be run from the web console:
if cam.direction.normalize.inspect == (Geom::Vector3d.new 0,0,-1).inspect
No need to slow things by doing String comparison.
Remember SU defines extensions to the Ruby Array base class, so they are Comparable with Sketchup::Point3d and Sketchup::Vector3d class objects.
as in:
cam.direction.normalize==[0,0,-1]
returns true if normalized camera direction vector is vertical and in the negative (down) direction. -
Good to know Dan. I was not getting the vector object to compare to an array (user error I'm sure), which is why I added the inspect. I didn't quite realize that inspect would return a string. I'll keep that in mind.
-
@chris fullmer said:
I was not getting the vector object to compare to an array (user error I'm sure), ...
for the Ruby Array literal. Get's confusing when your coding in Js, Ruby, etc etc and the same time. -
My approach avoids the confusion of vectors, eye, target etc... Just use the API functions to set a plan view and zoom to what you want to see, switching off perspective as needed...
Advertisement