Class: Sketchup::DefinitionList

Inherits:
Entity
  • Object
show all
Includes:
Enumerable

Overview

A DefinitionList object holds a list of all of the ComponentDefinition objects in a model. This class contains methods for adding and retrieving definitions from the list.

Version:

  • SketchUp 6.0

Instance Method Summary # collapse

Methods inherited from Entity

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

Instance Method Details

#[](index) ⇒ Sketchup::ComponentDefinition? #[](name) ⇒ Sketchup::ComponentDefinition? #[](guid) ⇒ Sketchup::ComponentDefinition?

The [] method is used to retrieve a component definition from the list. You can give an integer index in the range 0 to length, a string which represents the GUID (a unique internal identifier), or a string that is the name of the definition.

Examples:

path=Sketchup.find_support_file "Bed.skp",
  "Components/Components Sampler/"
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.load path
component = definitions[0]

Overloads:

Version:

  • SketchUp 6.0

#add(def_name) ⇒ Sketchup::ComponentDefinition

The add method is used to add a new component definition to the definition list with the given name.

Examples:

model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.add "BedTraditional"
component = definitions[0]

Parameters:

  • def_name (String)

    The new component definition to add to the definition list.

Returns:

Version:

  • SketchUp 6.0

#add_observer(observer) ⇒ Boolean

The add_observer method is used to add an observer to the current object.

Examples:

definitions = Sketchup.active_model.definitions
status = definitions.add_observer observer

Parameters:

  • observer (Object)

    An observer.

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#[](index) ⇒ Sketchup::ComponentDefinition? #[](name) ⇒ Sketchup::ComponentDefinition? #[](guid) ⇒ Sketchup::ComponentDefinition?

The [] method is used to retrieve a component definition from the list. You can give an integer index in the range 0 to length, a string which represents the GUID (a unique internal identifier), or a string that is the name of the definition.

Examples:

path=Sketchup.find_support_file "Bed.skp",
  "Components/Components Sampler/"
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.load path
component = definitions[0]

Overloads:

Version:

  • SketchUp 6.0

#countInteger

Note:

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

Examples:

model = Sketchup.active_model
definitions = model.definitions
number = definitions.count

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0

#each {|Sketchup::ComponentDefinition| ... } ⇒ nil

Note:

Don't remove content from this collection while iterating over it with #each. This would change the size of the collection and cause elements to be skipped as the indices change. Instead copy the current collection to an array using to_a and then use each on the array, when removing content.

The #each method is used to iterate through all of the component definitions in the definition list.

Examples:

model = Sketchup.active_model
definitions = model.definitions
definitions.add("BedTraditional")
number = definitions.each { |definition| puts definition.name }

Yields:

Returns:

  • (nil)

Version:

  • SketchUp 6.0

#lengthInteger

The #length method is used to retrieve number of component definitions in the list.

Examples:

model = Sketchup.active_model
definitions = model.definitions
number = definitions.length

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0

#load(path) ⇒ Sketchup::ComponentDefinition #load(path, allow_newer: true) ⇒ Sketchup::ComponentDefinition

The #load method is used to load a component from a file.

Examples:

path = Sketchup.find_support_file("Bed.skp",
  "Components/Components Sampler/")
model = Sketchup.active_model
definitions = model.definitions
definition = definitions.load(path)

Overloads:

  • #load(path) ⇒ Sketchup::ComponentDefinition

    Parameters:

    • path (String)

      The path where the component definition file is located.

  • #load(path, allow_newer: true) ⇒ Sketchup::ComponentDefinition

    Starting with SketchUp 2021.0 SketchUp attempts to load newer SketchUp models. If a newer model is loaded some information might have been skipped and extensions should be careful to not save over the file they loaded from as information might be lost.

    Parameters:

    • path (String)

      The path where the component definition file is located.

    • allow_newer (Boolean) (defaults to: true)

      Indicate that it is ok to load a model with of a newer version.

    Version:

    • SketchUp 2021.0

Returns:

Raises:

  • IOError If the file is not a valid SketchUp model.

  • RuntimeError If the file is the same as the model being loaded into.

  • IOError If the file is an empty component.

  • RuntimeError If the file contains only screen text.

  • RuntimeError If the file is of a newer file version that the executing SketchUp version cannot open.

Version:

  • SketchUp 6.0

Known Bugs:

  • In SketchUp versions prior to SketchUp 2019 the application would crash if you tried to open a newer model instead of raising the expected RuntimeError.

#load_from_url(url) ⇒ Sketchup::ComponentDefinition #load_from_url(url, load_handler) ⇒ Sketchup::ComponentDefinition

The #load_from_url method loads a component from a location specified by string url. This method throws an exception if an url string is not given, or an error occurs during retrieval from url and a load_handler was not given. Optional second parameter load_handler can be used to pass in a ruby object that responds to the following methods:

- cancelled?(a_boolean)
- onPercentChange(a_float)
- onSuccess()
- onFailure(message_string)

Examples:

class LoadHandler
  attr_accessor :error

  def onPercentChange(percent)
    Sketchup::set_status_text("loading: #{percent.round}%")
  end

  def cancelled?
    # You could, for example, show a messagebox after X seconds asking if the
    # user wants to cancel the download. If this method returns true, then
    # the download cancels.
    return false
  end

  def onSuccess
    Sketchup::set_status_text('')
  end

  def onFailure(error_message)
    self.error = error_message
    Sketchup::set_status_text('')
  end
end

# Replace this with a real URL...
url = 'http://www.sketchup.com/model.skp'
model = Sketchup.active_model
load_handler = LoadHandler.new
definition = model.definitions.load_from_url(url, load_handler)

if definition.nil?
  puts "Error: #{load_handler.error}"
end

Overloads:

Returns:

Version:

  • SketchUp 7.0

#purge_unusedSketchup::DefinitionList

The purge_unused method is used to remove the unused component definitions.

Examples:

definitions = Sketchup.active_model.definitions
definitions.purge_unused

Returns:

Version:

  • SketchUp 6.0

#remove(definition) ⇒ Boolean

The #remove method is used to remove a component definition from the definition list with the given component definition. This will remove all instances of the definition.

Examples:

model = Sketchup.active_model
definitions = model.definitions
definition = definitions[0]
definitions.remove(definition)

Parameters:

Returns:

  • (Boolean)

Version:

  • SketchUp 2018

#remove_observer(observer) ⇒ Boolean

The remove_observer method is used to remove an observer from the current object.

Examples:

definitions = Sketchup.active_model.definitions
status = definitions.remove_observer observer

Parameters:

  • observer (Object)

    An observer.

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#sizeInteger

The #size method is an alias for #length.

Examples:

model = Sketchup.active_model
definitions = model.definitions
number = definitions.size

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 2014

#unique_name(base_name) ⇒ String

The unique_name is used to generate a unique name for a definition based on a base_name string. For example, a base_name of “Joe” might return “Joe #2”

Examples:

model = Sketchup.active_model
definitions = model.definitions
new_name = definitions.unique_name "My Base Name"

Parameters:

Returns:

  • (String)

    the unique name.

Version:

  • SketchUp 6.0