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.
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.
Arguments:
Returns:
path=Sketchup.find_support_file "Bed.skp", "Components/Components Sampler/" model = Sketchup.active_model definitions = model.definitions componentdefinition = definitions.load path component = definitions[0]
The add method is used to add a new component definition to the definition list with the given name.
Arguments:
Returns:
model = Sketchup.active_model definitions = model.definitions componentdefinition = definitions.add "BedTraditional" component = definitions[0]
The add_observer method is used to add an observer to the current object.
Arguments:
Returns:
definitions = Sketchup.active_model.definitions status = definitions.add_observer observer
The at method is used to retrieve a component definition at a specific index. This is an alias for the [] method.
Arguments:
Returns:
model = Sketchup.active_model definitions = model.definitions componentdefinition = definitions.add "BedTraditional" component = definitions.at 0
The count method is an alias for length. See also length.
Returns:
model = Sketchup.active_model definitions = model.definitions componentdefinition = definitions.add "BedTraditional" number = definitions.count
The each method is used to iterate through all of the component definitions
in the definition list.
Throws an exception if there are no component definitions.
Arguments:
Returns:
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.add "BedTraditional"
number = definitions.each {|definition| UI.messagebox definition.to_s }The length method is used to retrieve number of component definitions in the list.
Returns:
model = Sketchup.active_model definitions = model.definitions componentdefinition = definitions.add "BedTraditional" number = definitions.count if (number) UI.messagebox number.to_s else UI.messagebox "Failure" end
The load method is used to load a component from a file.
Arguments:
Returns:
path=Sketchup.find_support_file "Bed.skp", "Components/Components Sampler/" model = Sketchup.active_model definitions = model.definitions componentdefinition = definitions.load path
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:
Arguments:
Returns:
class LoadHandler
def onPercentChange(p)
Sketchup::set_status_text("LOADING: " + p.to_i.to_s + "%")
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)
# A real implementation would probably not use a global variable,
# but this demonstrates storing any error we receive.
$last_error = error_message
Sketchup::set_status_text('')
end
end
# Replace this with a real URL...
url = 'http://sketchup.google.com/model.skp'
load_handler = LoadHandler.new
Sketchup.active_model.definitions.load_from_url url, load_handler
if load_handler.error == nil
last_def_id = Sketchup.active_model.definitions.count - 1
loaded_definition = Sketchup.active_model.definitions[last_def_id]
else
UI.messagebox("Error: " + $last_error.to_s)
endThe purge_unused method is used to remove the unused component definitions.
Returns:
# Need ruby code example here.
The remove_observer method is used to remove an observer from the current object.
Arguments:
Returns:
definitions = Sketchup.active_model.definitions status = definitions.remove_observer observer
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"
Arguments:
Returns:
model = Sketchup.active_model definitions = model.definitions new_name = definitions.unique_name "My Base Name"