Class: Sketchup::Styles

Inherits:
Entity
  • Object
show all
Includes:
Enumerable

Overview

The Styles class contains methods for manipulating a collection of styles in a model. Typically, you will access this via the active_model:

There are two objects of this class that play important roles: the #selected_style and the #active_style.

The latter is a temporary copy made from the #selected_style that allows the user to edit the style without committing to save anything. To save the changes, one should use #update_selected_style.

Examples:

styles = Sketchup.active_model.styles
puts "There are #{styles.size} styles in your model."

Version:

  • SketchUp 6.0

Instance Method Summary # collapse

Methods inherited from Entity

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

Instance Method Details

#[](name) ⇒ Sketchup::Style? #[](index) ⇒ Sketchup::Style?

The #[] method is used to retrieves a style by either name or index.

Examples:

styles = Sketchup.active_model.styles
style1 = styles[0]
style2 = styles["MyStyle"]

Overloads:

Version:

  • SketchUp 6.0

#active_styleSketchup::Style

The #active_style method is used to retrieve the active style.

While #selected_style is the style being selected in the Style Browser, the ##active_style is a different object also including any unsaved style changes. These changes are silently dropped once a new style is selected. To save these changes to the selected style, call ##update_selected_style.

Examples:

styles = Sketchup.active_model.styles
style = styles.active_style

Returns:

Version:

  • SketchUp 6.0

#active_style_changedBoolean

The #active_style_changed method tells you if the active style has been edited by the user since it was last saved.

Examples:

styles = Sketchup.active_model.styles
style_changed = styles.active_style_changed

Returns:

  • (Boolean)

Version:

  • SketchUp 6.0

#add_style(filename, select) ⇒ Boolean #add_style(filename, select = false) ⇒ Sketchup::Style?

The #add_style method is used to create and load a style from the given file.

Examples:

For SketchUp 2025.0 and older

filename = File.expand_path('./MyStyle.style')
styles = Sketchup.active_model.styles
status = styles.add_style(filename, true)

For SketchUp 2026.0 and newer

filename = File.expand_path('./MyStyle.style')
styles = Sketchup.active_model.styles
style = styles.add_style(filename, true)

Overloads:

  • #add_style(filename, select) ⇒ Boolean
    Note:

    Signature for versions prior to SketchUp 2026.0.

    Parameters:

    • filename (String)

      The file path to the style file.

    • select (Boolean)

      true if you want to set the style to be the active style.

    Returns:

    • (Boolean)

    Version:

    • SketchUp 6.0

  • #add_style(filename, select = false) ⇒ Sketchup::Style?

    Parameters:

    • filename (String)

      The file path to the style file.

    • select (Boolean) (defaults to: false)

      true if you want to set the style to be the active style.

    Version:

    • SketchUp 2026.0

Returns:

  • (Sketchup::Style, nil)

    The newly created style or nil if the the style could not be added.

#countInteger

Note:

Since SketchUp 2014 the count method is inherited from Ruby's Enumerable mix-in module. Prior to that the #count method is an alias for #length.

Examples:

styles = Sketchup.active_model.styles
number = styles.count

Returns:

  • (Integer)

Version:

  • SketchUp 6.0

#each {|style| ... } ⇒ nil

The #each method is used to iterate through styles.

Examples:

styles = Sketchup.active_model.styles
styles.each { |style|
  puts style.name
}

Yield Parameters:

Returns:

  • (nil)

Version:

  • SketchUp 6.0

#lengthInteger

The #length method is an alias of #size.

Examples:

styles = Sketchup.active_model.styles
number = styles.length

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 2014

#parentSketchup::Model

The #parent method is used to return the model for the styles.

Examples:

styles = Sketchup.active_model.styles
model = styles.parent

Returns:

Version:

  • SketchUp 6.0

#purge_unusednil

The #purge_unused method is used to remove unused styles from the model.

Examples:

styles = Sketchup.active_model.styles
styles.purge_unused

Returns:

  • (nil)

Version:

  • SketchUp 6.0

#remove_style(style) ⇒ nil

The #remove_style method is used to remove a Sketchup::Style from the Sketchup::Styles.

Examples:

filename = File.expand_path('./MyStyle.style')
styles = Sketchup.active_model.styles
status = styles.add_style(filename, true)
styles.remove(styles.first)

Parameters:

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    If the style is not found in the Styles collection.

  • (ArgumentError)

    If the styles contains only one style.

Version:

  • SketchUp 2026.0

#selected_styleSketchup::Style

The #selected_style method is used to retrieve the style currently selected in the Styles Browser.

Examples:

styles = Sketchup.active_model.styles
style = styles.selected_style

Returns:

Version:

  • SketchUp 6.0

#selected_style=(style) ⇒ nil

The #selected_style= method is used to set the currently selected style.

Examples:

styles = Sketchup.active_model.styles
filename = File.expand_path('./MyStyle.style')
styles.add_style(filename, true)
styles.selected_style = styles['[MyStyle]']

Parameters:

Returns:

  • (nil)

Raises:

Version:

  • SketchUp 6.0

Known Bugs:

#sizeInteger

The #size method is used to retrieve the number of styles in the collection.

Examples:

styles = Sketchup.active_model.styles
number = styles.size

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0

#update_selected_stylenil

The #update_selected_style method commits the current style settings to the style selected in the Style Browser.

Examples:

styles = Sketchup.active_model.styles
styles.update_selected_style

Returns:

  • (nil)

See Also:

Version:

  • SketchUp 6.0