|
SketchUp Ruby API Reference |
This class contains methods to manipulate the current point of view of the model. Many of these methods are meant to be invoked within a tool instead drawing separate from a tool (such as with the entities.add_edges method).
Parent: Object
Methods: add_observer, animation=, average_refresh_time, camera, camera=, center, corner, draw, draw2d, draw_line, draw_lines, draw_points, draw_polyline, draw_text, drawing_color= , dynamic=, enable_autopan=, field_of_view, field_of_view=, guess_target, inference_locked? , inputpoint, invalidate, last_refresh_time, line_stipple= , line_width= , lock_inference, model, pick_helper, pickray, pixels_to_model, remove_observer, screen_coords, set_color_from_line, show_frame, tooltip=, vpheight, vpwidth, write_image, zoom, zoom_extents
Example Code: viewtests.rb,
viewtooltests.rb, animation.rb
The add_observer method is used to add an observer to the current object.
status = object.add_observer observer
observer - an observer
true if successful, false if unsuccessful.
The animation= method is used to set an animation that is displayed for a view. See Animation for details on how to create an animation object.
animation = view.animation = animation
animation - an Animation object
animation - the newly set Animation object
animation = ViewSpinner.new
model = Sketchup.active_model
view = model.active_view
anim = view.animation=animation
if (anim)
UI.messagebox anim
else
UI.messagebox "Failure"
end
The average_refresh_time is used to set the average time used to refresh the current model in the view. This can be used to estimate the frame rate for an animation.
time = view.average_refresh_time
time - the time in milliseconds
model = Sketchup.active_model
view = model.active_view
time = view.average_refresh_time
The camera method is used to retrieve the camera for the view.
camera = view.camera
camera - a Camera object
model = Sketchup.active_model
view = model.active_view
c = view.camera
The camera= method is used to set the camera for the view. If a transition time is given, then it will animate the transition from the current camera to the new one.
view.camera = camera
view.camera = camera, transition_time
camera - the new Camera object
transition_time - the transition time from the existing camera to the new camera
camera2 = Sketchup.Camera.new
model = Sketchup.active_model
view = model.active_view
status = view.camera=camera2
The center method is used to retrieve the coordinates of the center of the view in pixels.
center = view.center
center - the center of the view
model = Sketchup.active_model
view = model.active_view
c = view.center
The corner method is used to retrieve the coordinates of one of the corners of the view. The argument is an index between 0 and 3 that identifies which corner you want. This method returns an array with two integers which are the coordinates of the corner of the view in the view space. If the view uses a Camera with a fixed aspect ratio, then the corners are the corners of the viewing are of the camera which might be different than the actual corners of the view itself.
point = view.corner index
index - a value between (or including) 0 and 3 identifying the corner whose coordinate you want to retrieve.
point - a Point3d object
model = Sketchup.active_model
view = model.active_view
pt = view.corner 0
The draw method is used to do basic drawing. This method can only be called from within the draw method of a tool that you implement in Ruby.
view = view.draw mode, pts
mode - the item you are going to draw: GL_LINES, GL_LINE_LOOP, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_POLYGON
pts - an array of Point3d objects
view - a View object
point6 = Geom::Point3d.new 0,0,0
point7 = Geom::Point3d.new 100,0,0
view.line_width=10
status = view.draw GL_LINES, [point6, point7]
The draw2d method is used to draw in screen space (using 2D screen coordinates) instead of 3D space.
view = view.draw2d openglenum, points
openglenum - an OpenGL enumerator (unsigned integer). See comments.
points - an array of point3d objects (or several individual point3D objects). These Point3D objects are in screen space, not 3D space. The X value corresponds to the number of pixels from the left edge of the drawing area. The Y value corresponds to the number of pixels down from the top of the drawing area. The Z value is not used.
The openglnum value can be any of the following: GL_POINTS, GL_LINES, GL_LINE_LOOP, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_POLYGON.
view - returns the View object.
The draw_line method is used to draw disconnected lines. This is an alias for draw_lines. See also draw_lines.
view = view.draw_line point1, point2, point3, point4, ...
point1, point2, poinit3, point4... - an even number of Point3d objects
view - a View object
This method is usually invoked within the draw method of a tool.
The draw_lines method is used to draw disconnected lines.
view = view.draw_lines point1, point2, point3, point4, ...
view = view.draw_lines pts
point1, point2, poinit3, point4... - an even number of Point3d objects
pts - an array of Point3d objects
view - a View object
You must have an even number of points. This method is usually invoked within the draw method of a tool.
point4 = Geom::Point3d.new 0,0,0
point5 = Geom::Point3d.new 100,100,100
# returns a view
status = view.drawing_color="red"
status = view.draw_lines point4, point5
This method is used to draw points.
view = view.draw_points pts, <pointsize>, <pointstyle>, <pointcolor>
pts - an array of Point3d objects
<pointsize> - an optional size of the point in pixels
<pointstyle> - an optional style of the point. 1 = open square, 2 = filled square, 3 = "+", 4 = "X", 5 = "*", 6 = open triangle, 7 = filled triangle.
<pointcolor> - an optional color of the point
view - a View object
This method is usually invoked within the draw method of a tool.
point3 = Geom::Point3d.new 0,0,0
# returns a view
status = view.draw_points point3, 10, 1, "red"
The draw_polyline method is used to draw a series of connected line segments from pt1 to pt2 to pt3, and so on.
view = view.draw_polyline pt1, pt2, pt3, pt4, ...
view = view.draw_polyline pts
point1, point2, poinit3, point4... - an even number of Point3d objects
pts - an array of Point3d objects
view - a View object
This method is usually invoked within the draw method of a tool.
point12 = Geom::Point3d.new 0,0,0
point13 = Geom::Point3d.new 10,10,10
point14 = Geom::Point3d.new 20,20,20
point15 = Geom::Point3d.new 30,30,30
status = view.draw_polyline point12, point13, point14, point15
This method is used to draw text on the screen.
view = view.draw_text point "text"
point - a Point3d object
"text" - the text string to draw
This method is usually invoked within the draw method of a tool.
point16 = Geom::Point3d.new 0,0,0
status = view.draw_text point16, "This is a test"
The drawing_color method is used to set the color that is used for drawing to the view.
view = view.drawing_color = color
color - a color
view - a View object
This method is usually invoked within the draw method of a tool.
point17 = Geom::Point3d.new 0,0,0
point18 = Geom::Point3d.new 100,0,0
view.line_width=10
status = view.set_color_from_line point17, point18
status = view.draw_lines point17, point18
The dynamic= method allows you to degrade performance, while improving performance, when a model is large and view refresh time is slow.
value = view.dynamic=value
value - 0 is false, 1 is true, >1 is
See also camera.rb which is part of the film and stage ruby scripts.
view.dyamic=3
The field_of_view method is used to add an observer to the current object.
The field_of_view= method is used to add an observer to the current object.
The enable_autopan= method allows you to allow the drawing window to pan when you drag the mouse beyond the boundary.
status = view.enable_autopan = status
status - true if you want to enable autopanning. false if you do not want to enable autopanning.
status - true if autopanning is set to true, false if it is set to false.
view.enable_autopan=false
The guess_target method is used to guess at what the user is looking at when you have a perspective view.
target = view.guess_target
target - a Point3d object representing the point in the model that the user is likely interested in.
This method is useful when writing a viewing tool. See also camera.rb which is part of the film and stage ruby scripts.
target = view.guess_target
The inference_locked? method is used to determine if inference locking is on for the view.
status = view.inference_locked?
status - true if locked, false if unlocked
model = Sketchup.active_model
view = model.active_view
status = view.inference_locked?
The inputpoint method is used to retrieve an input point.
inputpoint = view.inputpoint x, y inputpoint = view.inputpoint x, y, inputpoint1
x - a x value
y - a y value
inputpoint1 - an InputPoint object
This will normally be used inside one of the mouse event handling methods in a tool. Usually, it is preferable to create the InputPoint first and then use the pick method on it.
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face pts
edge = entities[0]
vertex = edge.end
point = vertex.position
view = model.active_view
ip = view.inputpoint point[0], point[1]
The invalidate method is used to refresh the view.
view = view.invalidate
view - the invalidated View object
model = Sketchup.active_model
view = model.active_view
status = view.invalidate
The last_refresh_time method is used to retrieve the time for the last full view refresh.
time = view.last_refresh_time
time - time in milliseconds
model = Sketchup.active_model
view = model.active_view
t = view.last_refresh_time
The line_stipple= method is used to set the line pattern to use for drawing.
view = view.line_stipple = pattern
pattern - a stipple pattern. See ConstructionLine.stipple for valid stipples.
view - the View object
This method is usually invoked within the draw method of a tool.
point8 = Geom::Point3d.new 0,0,0
point9 = Geom::Point3d.new 100,100,100
# Stipple types documented in ConstructionLine class
view.line_stipple = "-.-"
view = view.draw_lines point8, point9
The line_width= method is used to set the line width to use for drawing. The value is a Double indicating the desired width in pixels.
view = view.line_width = width
width - the width in pixels
view - a View object
This method is usually invoked within the draw method of a tool.
point10 = Geom::Point3d.new 0,0,0
point11 = Geom::Point3d.new 100,100,100
view.line_width=10
view = view.draw_lines point10, point11
The lock_inference method is used to lock or unlock an inference.
view = view.lock_inference
view = view.lock_inference inputpoint
view = view.lock_inference inputpoint1, inputpoint2
inputpoint, inputpoint2 - InputPoint objects
view - a View object
This method will typically be called from inside a tool class when the user presses the shift key.
With no arguments it unlocks all inferences. With one or two arguments, it locks the inference based on the given InputPoint(s).
model = Sketchup.active_model
view = model.active_view
s = view.lock_inference
UI.messagebox $!.message
status = view.inference_locked?
The model method is used to retrieve the model for the current view.
model = view.model
model - the model for this view
model = Sketchup.active_model
view = model.active_view
m = view.model
The pick_helper method is used to retrieve a pick helper for the view. See the PickHelper class for information on pick helpers.
pickhelper = view.pick_helper
pickhelper = view.pick_helper x, y
pickhelper = view.pick_helper x, y, aperture
x - a x value
y - a y value
aperture - a radius in pixels that defines what items will be picked.
pickhelper - a PickHelper object
With no arguments, this returns an initialized PickHelper.
If you supply x and y, they are screen coordinates of the point at which you want to do a pick. Typically, there will be points that were passed to a method on a tool class.
The optional aperture lets you set how big a pick aperture (in pixels) you want to use for allowing things to be picked. The default uses the standard pick aperture used for SketchUp picking.
model = Sketchup.active_model
view = model.active_view
ph = view.pick_helper
The pickray method is used to retrieve a ray passing through a given screen position in the viewing direction.
ray = view.pickray x, y
ray - a ray
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face pts
edge = entities[0]
vertex = edge.end
point = vertex.position
view = model.active_view
ray = view.pickray point[0], point[1]
The pixels_to_model method is used to compute a model size from a pixel size at a given point.
size = view.pixels_to_model pixels, point
pixels - the pixel size
point - a Point3d object where the size will be calculated from
size - the model size
This method is useful for deciding how big to draw something based on a desired size in pixels.
The remove_observer method is used to remove an observer from the current object.
status = object.remove_observer observer
observer - an observer
true if successful, false if unsuccessful.
The screen_coords method is used to retrieve the screen coordinates of the given point on the screen.
point3d = view.screen_coords point3d
point3d - a Point3d object.
point3d - the screen coordinates.
Set the drawing color for the view based on the direction of a line that you want to draw.
view = view.set_color_from_line point1, point2
point1, point2 - Point3d objects representing the line you want to draw
view - a View object
This method is usually invoked within the draw method of a tool.
point17 = Geom::Point3d.new 0,0,0
point18 = Geom::Point3d.new 100,0,0
view.line_width=10
status = view.set_color_from_line point17, point18
status = view.draw_lines point17, point18
The show_frame method is used to show a frame of an Animation object in the current view.
status = view.show_frame <delay>
<delay> - an optional delay in seconds.
You can supply an optional delay in seconds to wait before showing the next frame. This can be useful to control the speed at which the animation runs.
view.show_frame
Set a tooltip to display in the view. This is useful for displaying tooltips in a tool that you write in Ruby.
tooltip = view.tooltip = string
string - the string tooltip
tooltip - the new tooltip string
model = Sketchup.active_model
view = model.active_view
# Returns a string tool tip
tt = view.tooltip="This is a Test"
The vpheight method is used to retrieve the height of the viewport for the view.
height = view.vpheight
height - the height of the viewport in pixels.
model = Sketchup.active_model
view = model.active_view
height = view.vpheight
The vpwidth method is used to retrieve the width of the viewport for the view.
width = view.vpwidth
width - the width of the viewport in pixels.
model = Sketchup.active_model
view = model.active_view
width = view.vpwidth
The write_image method is used to write the current view to an image file.
status - view.write_image filename, <width>, <height>, <antialias>, <compressionFactor>
filename - the filename for the saved image
<width> - width in pixels
<height> - height in pixels
<antialias> -
<compressionFactor> -
All arguments except for the filename are optional.
If you specify a width without specifying a height, or if you give zero for either the width or height, then the other dimension is computed to preserve the aspect ratio of the view.
If antialias is specified, it should be either true or false.
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face pts
UI.messagebox "Now Lets Write the Image"
view = model.active_view
# Puts in SketchUp install directory by default
status = view.write_image "test.jpg"
The zoom method is used to zoom in or out by some zoom factor.
view = view.zoom factor view = view.zoom selection view = view.zoom entities view = view.zoom array_of_entities
factor - a Zoom factor from 1 or larger
view - the zoomed View object
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face pts
UI.messagebox "Now Lets Zoom"
view = model.active_view
newview = view.zoom 3
The zoom_extents method is used to zoom so that the entire model fills the view.
view = view.zoom_extents
view - the zoomed View object
depth = 100
width = 100
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face pts
UI.messagebox "Now Lets Zoom Extents"
view = model.active_view
newview = view.zoom_extents
|
SketchUp Ruby API Reference: View |
© Google Inc. 2007 sketchup.google.com |