Class: Sketchup::Snap

Inherits:
Drawingelement show all

Overview

A Snap is a custom grip used by SketchUp's Move tool. Snaps can be added at strategic places such as connectors to help assembling objects.

#direction is the direction a snap is “pointing”. This can be thought of as the normal direction of the snap. It can also be thought of as the direction you move an object when plugging it into another object, e.g. inserting a power coord.

#up controls the rotation around the Snap's axis.

When two objects are snapped together, the Snaps have opposite #direction vectors but matching #up vectors.

Examples:

# Copy all snaps from source group/component to the corresponding
# location in a target group/component, as if the objects have
# have been snapped together.
#
# @param [Sketchup::Group, Sketchup::ComponentInstance] source
# @param [Sketchup::Group, Sketchup::ComponentInstance] target
def copy_snap(source, target)
  # Transformation for going from the coordinate system of the source to
  # that of the target.
  transformation =
    target.transformation.inverse * source.transformation

  source.definition.entities.grep(Sketchup::Snap) do |source_snap|
    # Transform position and orientation between local coordinate systems.
    position = source_snap.position.transform(transformation)
    # Direction vector is reversed between two connected snaps;
    # the snaps point "into" each other.
    direction = source_snap.direction.transform(transformation).reverse
    # Up vector is aligned between two connected snaps.
    up = source_snap.up.transform(transformation)

    target.entities.add_snap(position, direction, up)
  end
end

Version:

  • SketchUp 2025.0

Instance Method Summary # collapse

Methods inherited from Drawingelement

#bounds, #casts_shadows=, #casts_shadows?, #erase!, #hidden=, #hidden?, #layer, #layer=, #material, #material=, #receives_shadows=, #receives_shadows?, #visible=, #visible?

Methods inherited from Entity

#add_observer, #attribute_dictionaries, #attribute_dictionary, #delete_attribute, #deleted?, #entityID, #get_attribute, #inspect, #model, #parent, #persistent_id, #remove_observer, #set_attribute, #to_s, #typename, #valid?

Instance Method Details

#directionGeom::Vector3d

The #direction method is used to get the direction this Snap is “pointing”.

When two Snaps are snapped into each other, they have the opposite #direction.

Examples:

snap = Sketchup.active_model.entities.add_snap(ORIGIN)
direction = snap.direction

Returns:

Version:

  • SketchUp 2025.0

#positionGeom::Point3d

The #position method is used to get the position of this Snap.

Examples:

snap = Sketchup.active_model.entities.add_snap(ORIGIN)
position = snap.position

Returns:

Version:

  • SketchUp 2025.0

#set(position) ⇒ Sketchup::Snap #set(position, direction) ⇒ Sketchup::Snap #set(position, direction, up) ⇒ Sketchup::Snap

The #set method is used to move and/or reorient a Snap.

Examples:

snap = Sketchup.active_model.entities.add_snap(ORIGIN)
snap.set(Geom::Point3d.new(1.m, 0, 0))

Overloads:

Returns:

Raises:

  • ArgumentError if direction and up are parallel.

Version:

  • SketchUp 2025.0

#upGeom::Vector3d

The #up method is used to get a vector representing the rotation of this Snap along its axis.

When two Snaps are snapped into each other, they have the same aligned #up direction.

Examples:

snap = Sketchup.active_model.entities.add_snap(ORIGIN)
up = snap.up

Returns:

Version:

  • SketchUp 2025.0