Class: Sketchup::View
| Relationships | |
| Inherits: | Object | 
Overview
This class contains methods to manipulate the current point of view of the model. The drawing methods here (draw_line, draw_polyline, etc) are meant to be invoked within a tool’s Tool.draw method. Calling them outside Tool.draw will have no effect.
You access the View by calling the Model.active_view method.
Instance Attribute Summary
- 
    
      #animation=(animation)  
    
    writeonly
    The #animation= method is used to set an animation that is displayed for a view. 
- 
    
      #camera  ⇒ Sketchup::Camera 
    
    rw
    The camera method is used to retrieve the camera for the view. 
- 
    
      #camera=(camera)  
    
    rw
    The #camera= method is used to set the camera for the view. 
- 
    
      #drawing_color=(color)  ⇒ Sketchup::View 
    
    writeonly
    The drawing_color method is used to set the color that is used for drawing to the view. 
- 
    
      #dynamic=(value)  ⇒ Boolean 
    
    writeonly
    deprecated
    Deprecated.
      This method is no longer doing anything. 
- 
    
      #field_of_view  ⇒ Float 
    
    rw
    The field_of_view method is used get the view’s field of view setting, in degrees. 
- 
    
      #field_of_view=(fov)  ⇒ Numeric 
    
    rw
    The field_of_view= method is used set the view’s field of view setting, in degrees. 
- 
    
      #inference_locked?  ⇒ Boolean 
    
    readonly
    The inference_locked? method is used to determine if inference locking is on for the view. 
- 
    
      #line_stipple=(pattern)  ⇒ Sketchup::View 
    
    writeonly
    The line_stipple= method is used to set the line pattern to use for drawing. 
- 
    
      #line_width=(width)  ⇒ Integer 
    
    writeonly
    The line_width= method is used to set the line width to use for drawing. 
- 
    
      #tooltip=(string)  ⇒ String 
    
    writeonly
    Seta tooltip to display in the view.
Instance Method Summary
- 
    
      #add_observer(observer)  ⇒ Boolean 
    
    The add_observer method is used to add an observer to the current object. 
- 
    
      #average_refresh_time  ⇒ Float 
    
    The average_refresh_time is used to set the average time used to refresh the current model in the view. 
- 
    
      #center  ⇒ Geom::Point3d 
    
    The center method is used to retrieve the coordinates of the center of the view in pixels. 
- 
    
      #corner(index)  ⇒ Array(Integer, Integer) 
    
    The corner method is used to retrieve the coordinates of one of the corners of the view. 
- 
    
      #draw(openglenum, points)  ⇒ Sketchup::View 
    
    The #draw method is used to do basic drawing. 
- 
    
      #draw2d(openglenum, points)  ⇒ Sketchup::View 
    
    The #draw2d method is used to draw in screen space (using 2D screen coordinates) instead of 3D space. 
- 
    
      #draw_lines(points, ...)  ⇒ Sketchup::View 
    
    The draw_lines method is used to draw disconnected lines. 
- 
    
      #draw_lines(points, ...)  ⇒ Sketchup::View 
    
    The draw_lines method is used to draw disconnected lines. 
- 
    
      #draw_points(points, size = 6, style = 3, color = 'black')  ⇒ Sketchup::View 
    
    This method is used to draw points. 
- 
    
      #draw_polyline(points, ...)  ⇒ Sketchup::View 
    
    The draw_polyline method is used to draw a series of connected line segments from pt1 to pt2 to pt3, and so on. 
- 
    
      #draw_text(point, text)  ⇒ Sketchup::View 
    
    This method is used to draw text on the screen and is usually invoked within the draw method of a tool. 
- 
    
      #graphics_engine  ⇒ Symbol 
    
    The #graphics_engine method is used query the type of the graphics engine that’s currently used by this view. 
- 
    
      #guess_target(*args)  ⇒ Geom::Point3d 
    
    The guess_target method is used to guess at what the user is looking at when you have a perspective view. 
- 
    
      #inputpoint(x, y, inputpoint1)  ⇒ Sketchup::InputPoint 
    
    The inputpoint method is used to retrieve an input point. 
- 
    
      #invalidate  ⇒ Sketchup::View 
    
    The invalidate method is used mark the view as in need of a redraw. 
- 
    
      #last_refresh_time(*args)  ⇒ Float 
    
    The last_refresh_time method is used to retrieve the time for the last full view refresh. 
- #load_texture(image_rep) ⇒ Integer
- 
    
      #lock_inference  ⇒ Sketchup::View 
    
    The #lock_inference method is used to lock or unlock an inference. 
- 
    
      #model  ⇒ Sketchup::Model 
    
    The model method is used to retrieve the model for the current view. 
- 
    
      #pick_helper  ⇒ Sketchup::PickHelper 
    
    The pick_helper method is used to retrieve a pick helper for the view. 
- 
    
      #pickray(screen_point)  ⇒ Array(Geom::Point3d, Geom::Vector3d) 
    
    The pickray method is used to retrieve a ray passing through a given screen position in the viewing direction. 
- 
    
      #pixels_to_model(pixels, point)  ⇒ Float 
    
    The pixels_to_model method is used to compute a model size from a pixel size at a given point. 
- 
    
      #refresh  ⇒ Sketchup::View 
    
    The refresh method is used to immediately force a redraw of the view. 
- 
    
      #release_texture(texture_id)  ⇒ Boolean 
    
    Releases a texture loaded via #load_texture, freeing up it’s memory. 
- 
    
      #remove_observer(observer)  ⇒ Boolean 
    
    The remove_observer method is used to remove an observer from the current object. 
- 
    
      #screen_coords(point3d)  ⇒ Geom::Point3d 
    
    The screen_coords method is used to retrieve the screen coordinates of the given point on the screen. 
- 
    
      #set_color_from_line(point1, point2)  ⇒ Sketchup::View 
    
    Setthe drawing color for the view based on the direction of a line that you want to draw.
- 
    
      #show_frame(delay)  ⇒ Sketchup::View 
    
    The show_frame method is used to show a frame of an Animationobject in the current view.
- 
    
      #text_bounds(point, text, options = {})  ⇒ Geom::Bounds2d 
    
    This method is used to compute the bounds of the text when using #draw_text. 
- 
    
      #vpheight  ⇒ Integer 
    
    The vpheight method is used to retrieve the height of the viewport for the view. 
- 
    
      #vpwidth  ⇒ Integer 
    
    The vpwidth method is used to retrieve the width of the viewport for the view. 
- 
    
      #write_image(filename, width = view.vpwidth, height = view.vpheight, antialias = false)  ⇒ Boolean 
    
    The #write_image method is used to write the current view to an image file. 
- 
    
      #zoom(zoom_or_ents)  ⇒ Sketchup::View 
    
    The zoom method is used to zoom in or out by some zoom factor. 
- 
    
      #zoom_extents  ⇒ Sketchup::View 
    
    The zoom_extents method is used to zoom to the extents about the entire model, as if the user has selected the zoom extents command from the menu. 
Instance Attribute Details
#animation=(animation) (writeonly)
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.
#camera ⇒ Sketchup::Camera (rw)
The camera method is used to retrieve the camera for the view.
    
      #camera=(camera)   (rw)
      #camera=(camera_and_transition)  
    
  
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.
    #drawing_color=(color)  ⇒ View  (writeonly)
  
The drawing_color method is used to set the color that is used for drawing to the view.
This method is usually invoked within the draw method of a tool.
    #dynamic=(value)  ⇒ Boolean  (writeonly)
  
This method is no longer doing anything.
The dynamic= method allows you to degrade visual quality while improving performance when a model is large and view refresh time is slow. For example, if you were using a Ruby script to animate the camera through a large scene, you may want to set dynamic to true during that time.
See also camera.rb which is part of the film and stage ruby scripts.
    #field_of_view  ⇒ Float  (rw)
  
The field_of_view method is used get the view’s field of view setting, in degrees.
#field_of_view=(fov) ⇒ Numeric (rw)
The field_of_view= method is used set the view’s field of view setting, in degrees.
    #inference_locked?  ⇒ Boolean  (readonly)
  
The inference_locked? method is used to determine if inference locking is on for the view.
    #line_stipple=(pattern)  ⇒ View  (writeonly)
  
The line_stipple= method is used to set the line pattern to use for drawing. The stipple pattern is given as a string. Valid strings are:
"." (Dotted Line),
"-" (Short Dashes Line),
"_" (Long Dashes Line),
"-.-" (Dash Dot Dash Line),
"" (Solid Line).This method is usually invoked within the draw method of a tool.
    #line_width=(width)  ⇒ Integer  (writeonly)
  
As of SU2017 this will automatically scale the line width by the same factor as UI.scale_factor.
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.
This method is usually invoked within the draw method of a tool.
#tooltip=(string) ⇒ String (writeonly)
Set a tooltip to display in the view. This is useful for displaying tooltips in a tool that you write in Ruby.
Instance Method Details
    #add_observer(observer)  ⇒ Boolean 
  
The add_observer method is used to add an observer to the current object.
    #average_refresh_time  ⇒ Float 
  
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.
#center ⇒ Geom::Point3d
The center method is used to retrieve the coordinates of the center of the view in pixels. It is returned as an array of 2 values for x and y.
    #corner(index)  ⇒ Array(Integer, Integer) 
  
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.
The index numbers are as follows:
- 0: top left,
- 1: top right,
- 2: bottom left,
- 3: bottom right.
    
      #draw(openglenum, points)  ⇒ View 
      #draw(openglenum, *points)  ⇒ View 
      #draw(openglenum, points, **options)  ⇒ View 
      #draw(openglenum, *points, **options)  ⇒ View 
    
  
View 
      #draw(openglenum, *points)  ⇒ View 
      #draw(openglenum, points, **options)  ⇒ View 
      #draw(openglenum, *points, **options)  ⇒ View 
    If you draw outside the model bounds you need to implement Tool#getExtents which returns a bounding box large enough to include the points you draw. Otherwise your drawing will be clipped.
The #draw method is used to do basic drawing. This method can only be called from within the Tool#draw method of a tool that you implement in Ruby.
The following constants are all OpenGL terms and have been externalized to Ruby. Here is a summary of their meanings:
- GL_POINTS
- 
Treats each vertex as a single point. Vertex n defines point n. N points are drawn. 
- GL_LINES
- 
Treats each pair of vertices as an independent line segment. Vertices 2n-1 and 2n define line n. N/2 lines are drawn. 
- GL_LINE_STRIP
- 
Draws a connected group of line segments from the first vertex to the last. Vertices n and n+1 define line n. N-1 lines are drawn. 
- GL_LINE_LOOP
- 
Draws a connected group of line segments from the first vertex to the last, then back to the first. Vertices n and n+1 define line n. The last line, however, is defined by vertices N and 1. N lines are drawn. 
- GL_TRIANGLES
- 
Treats each triplet of vertices as an independent triangle. Vertices 3n-2, 3n-1, and 3n define triangle n. N/3 triangles are drawn. 
- GL_TRIANGLE_STRIP
- 
Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. For odd n, vertices n, n+1, and n+2 define triangle n. For even n, vertices n+1, n, and n+2 define triangle n. N-2 triangles are drawn. 
- GL_TRIANGLE_FAN
- 
Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. Vertices 1, n+1, and n+2 define triangle n. N-2 triangles are drawn. 
- GL_QUADS
- 
Treats each group of four vertices as an independent quadrilateral. Vertices 4n-3, 4n-2, 4n-1, and 4n define quadrilateral n. N/4 quadrilaterals are drawn. 
- GL_QUAD_STRIP
- 
Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair. Vertices 2n-1, 2n, 2n+2, and 2n+1 define quadrilateral n. N/2-1 quadrilaterals are drawn. Note that the order in which vertices are used to construct a quadrilateral from strip data is different from that used with independent data. 
- GL_POLYGON
- 
Draws a single, convex polygon. Vertices 1 through N define this polygon. 
    
      #draw2d(openglenum, points)  ⇒ View 
      #draw2d(openglenum, *points)  ⇒ View 
      #draw2d(openglenum, points, **options)  ⇒ View 
      #draw2d(openglenum, *points, **options)  ⇒ View 
    
  
View 
      #draw2d(openglenum, *points)  ⇒ View 
      #draw2d(openglenum, points, **options)  ⇒ View 
      #draw2d(openglenum, *points, **options)  ⇒ View 
    The #draw2d method is used to draw in screen space (using 2D screen coordinates) instead of 3D space.
The second parameter is an ::Array of ::Geom::Point3d objects (or several individual ::Geom::Point3d objects). These ::Geom::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.
    
      #draw_lines(points, ...)  ⇒ View 
      #draw_lines(points)  ⇒ View 
    
  
View 
      #draw_lines(points)  ⇒ View 
    The draw_lines method is used to draw disconnected lines.
You must have an even number of points. This method is usually invoked within the draw method of a tool.
    
      #draw_lines(points, ...)  ⇒ View 
      #draw_lines(points)  ⇒ View 
    
  
View 
      #draw_lines(points)  ⇒ View 
    The draw_lines method is used to draw disconnected lines.
You must have an even number of points. This method is usually invoked within the draw method of a tool.
    #draw_points(points, size = 6, style = 3, color = 'black')  ⇒ View 
  
This method is used to draw points.
This method is usually invoked within the draw method of a tool.
    
      #draw_polyline(points, ...)  ⇒ View 
      #draw_polyline(points)  ⇒ View 
    
  
View 
      #draw_polyline(points)  ⇒ View 
    The draw_polyline method is used to draw a series of connected line segments from pt1 to pt2 to pt3, and so on.
This method is usually invoked within the draw method of a tool.
    
      #draw_text(point, text)  ⇒ View 
      #draw_text(point, text, options = {})  ⇒ View 
    
  
View 
      #draw_text(point, text, options = {})  ⇒ View 
    Under Windows the font name must be less than 32 characters - due to system limitations.
As of SU2017 this will automatically scale the font-size by the same factor as UI.scale_factor.
The font size is platform dependent. On Windows the method expects points, where on Mac it’s pixels. See “Cross Platform Font Size” example for details.
This method is used to draw text on the screen and is usually invoked within the draw method of a tool.
The TextVerticalAlignCenter option will align the text to the center of the height of the first line, not the whole boundingbox of the text. To align around the full bounds of the text, use #text_bounds to compute the desired alignment.
The vertical alignment can vary between fonts and platforms. It’s recommended to test different fonts and find one that fits well across both platforms for your purposes.
Example of different vertical alignment and text bounds:

    #graphics_engine  ⇒ Symbol 
  
The #graphics_engine method is used query the type of the graphics engine that’s currently used by this view.
#guess_target(*args) ⇒ Geom::Point3d
The guess_target method is used to guess at what the user is looking at when you have a perspective view.
This method is useful when writing a viewing tool. See also camera.rb which is part of the film and stage ruby scripts.
#inputpoint(x, y, inputpoint1) ⇒ Sketchup::InputPoint
The inputpoint method is used to retrieve an input point.
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.
    #invalidate  ⇒ View 
  
This is the preferred method to update the viewport. Use this before trying to use #refresh.
The invalidate method is used mark the view as in need of a redraw.
    #last_refresh_time(*args)  ⇒ Float 
  
The last_refresh_time method is used to retrieve the time for the last full view refresh.
    #load_texture(image_rep)  ⇒ Integer 
  
Avoid loading and releasing textures within the Tool#draw event as that is not efficient.
SketchUp 2020.0-2022.0: To conserve resources on the user’s machine, textures can be loaded only when there is a Ruby tool on the tool stack. Make sure to release the texture when it’s no longer needed. Any textures not already released when the last Ruby tool on the tool stack is removed will be automatically released by SketchUp. As of SketchUp 2023.0 this automatic cleanup was removed to allow Overlays to draw textures.
    
      #lock_inference  ⇒ View 
      #lock_inference(inputpoint)  ⇒ View 
      #lock_inference(inputpoint, inputpoint2)  ⇒ View 
    
  
View 
      #lock_inference(inputpoint)  ⇒ View 
      #lock_inference(inputpoint, inputpoint2)  ⇒ View 
    The #lock_inference method is used to lock or unlock an inference.
This method will typically be called from inside a tool class when the user presses the shift key or arrow keys.
With no arguments it unlocks all inferences. With one argument it locks inference based on that passed InputPoint‘s entities, e.g. along a Edge’s line or a Face‘s plane. With two arguments, it locks inference along an axis.
#model ⇒ Sketchup::Model
The model method is used to retrieve the model for the current view.
    
      #pick_helper  ⇒ Sketchup::PickHelper 
      #pick_helper(x, y, aperture = 0)  ⇒ Sketchup::PickHelper 
    
  
The pick_helper method is used to retrieve a pick helper for the view. See the PickHelper class for information on pick helpers.
This call returns an initialized PickHelper.
    
      #pickray(screen_point)  ⇒ Array(Geom::Point3d, Geom::Vector3d) 
      #pickray(x, y)  ⇒ Array(Geom::Point3d, Geom::Vector3d) 
    
  
The pickray method is used to retrieve a ray passing through a given screen position in the viewing direction.
    #pixels_to_model(pixels, point)  ⇒ Float 
  
As of SU2017 this will automatically scale the pixel-size by the same factor as UI.scale_factor.
The pixels_to_model method is used to compute a model size from a pixel size at a given point.
This method is useful for deciding how big to draw something based on a desired size in pixels.
    #refresh  ⇒ View 
  
This method might impact performance and if used incorrectly cause instability or crashes. Don’t use this unless you have verified that you cannot use #invalidate instead.
The refresh method is used to immediately force a redraw of the view.
    #release_texture(texture_id)  ⇒ Boolean 
  
Releases a texture loaded via #load_texture, freeing up it’s memory. It’s good practice to do so whenever there is no longer any need for the resource.
For example, when your tool deactivates you probably want to release your resources as you don’t know if your tool will be used again.
    #remove_observer(observer)  ⇒ Boolean 
  
The remove_observer method is used to remove an observer from the current object.
#screen_coords(point3d) ⇒ Geom::Point3d
The screen_coords method is used to retrieve the screen coordinates of the given point on the screen.
The x and y values returned correspond to the x and y screen coordinates. Ignore the z values. If the referenced point is not in the current viewport, the x and/or y value may be negative.
    #set_color_from_line(point1, point2)  ⇒ View 
  
Set the drawing color for the view based on the direction of a line that you want to draw. These colors will match the axes colors in the SketchUp model (typically blue for straight up and down, etc.)
This method is usually invoked within the draw method of a tool.
    #show_frame(delay)  ⇒ View 
  
The show_frame method is used to show a frame of an Animation object in the current view.
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.
#text_bounds(point, text, options = {}) ⇒ Geom::Bounds2d
Under Windows the font name must be less than 32 characters - due to system limitations.
This method is used to compute the bounds of the text when using #draw_text. The bounds are not a tight fit around the top and bottom as they include varying amount of line spacing depending on the font used.
The TextVerticalAlignCenter option will align the text to the center of the height of the first line, not the whole boundingbox of the text.
Example of different vertical alignment and text bounds:

    #vpheight  ⇒ Integer 
  
The vpheight method is used to retrieve the height of the viewport for the view.
    #vpwidth  ⇒ Integer 
  
The vpwidth method is used to retrieve the width of the viewport for the view.
    
      #write_image(filename, width = view.vpwidth, height = view.vpheight, antialias = false)  ⇒ Boolean 
      #write_image(options)  ⇒ Boolean 
      #write_image(options)  ⇒ Boolean 
    
  
Boolean 
      #write_image(options)  ⇒ Boolean 
      #write_image(options)  ⇒ Boolean 
    The #write_image method is used to write the current view to an image file.
Supported file types are ‘.png`, .jpg, .jpeg, gif, .bmp, .tif. For other file formats  available from the GUI in File > Export > 2D Graphics, .e.g .pdf, use Model#export.
compression = 1.0)
@note Prefer the overload with option hash instead of this variant. This
  overload is not updated with new .
@example
  filename = File.join(Sketchup.temp_dir, 'example.png')
  antialias = false
  compression = 0.9
  model = Sketchup.active_model
  view = model.active_view
  view.write_image(filename, 640, 480, antialias, compression)
@param [String] filename
  The filename for the saved image
@param [Integer] width
  Width in pixels, defaults to the current  width {#vpwidth}.
@param [Integer] height
  Height in pixels, defaults to the current  height {#vpheight}.
@param [Boolean] antialias
@param [Float] compression
  Compression factor for JPEG images, between +0.0+ and +1.0+.
    #zoom(zoom_or_ents)  ⇒ View 
  
The zoom method is used to zoom in or out by some zoom factor.
    #zoom_extents  ⇒ View 
  
The zoom_extents method is used to zoom to the extents about the entire model, as if the user has selected the zoom extents command from the menu.