|
SketchUp Ruby API Reference |
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.
Parent:Entity
Methods:[], add, add_observer, at, count, each, length, load, purge_unused, remove_observer, unique_name
Example Code:
definitionlisttests.rb
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 for the component definition, or a string that is the name of the component definition.
componentdefinition = definitionlist[index] componentdefinition = definitionlist[guid] componentdefinition = definitionlist["componentdefinitionname"]
index - the index for a specific component definition
guid - the unique ID for the component definition
“componentdefinitionname” – the name of an existing component definition
componentdefinition - a ComponentDefinition object if successful, nil if not found
path=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
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.
componentdefinition = definitionlist.add "componentdefinitionname"
componentdefinitionname - the new component definition to add to the definition list
componentdefinition - the ComponentDefinition object that was added (if successful)
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.
status = object.add_observer observer
observer - an observer
true if successful, false if unsuccessful.
The at method is used to retrieve a component definition at a specific index.
componentdefinition = definitionlist.at[index]
index - the index of the component definition
componentdefinition - the ComponentDefinition object at the specific index if successful, nil if unsuccessful
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.
count = definitionlist.length
count - the number of component definitions in the definition list if successful
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.
definitionlist.each {| componentdefinition | ...}
componentdefinition – a variable that will hold each ComponentDefinition object as they are found
Throws an exception if there are no component definitions.
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.add "BedTraditional"
number = definitions.each {|definition| UI.messagebox definition }
The length method is used to retrieve number of component definitions in the list.
length = definitionlist.length
length - the number of component definitions in the definition list (if successful)
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.add "BedTraditional"
number = definitions.count
if (number)
UI.messagebox number
else
UI.messagebox "Failure"
end
The load method is used to load a component from a file.
componentdefinition = definitionlist.load path
path - the path where the component definition file is located
componentdefinition - the loaded ComponentDefinition object if successful
path=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.load path
The purge_unused method is used to remove the unused component definitions.
true if successful, false if unsuccessful.
The remove_observer method is used to remove an observer from the current object.
status = object.remove_observer observer
observer - an observer
true if successful, false if unsuccessful.
The unique_name method is used to generate a unique name for a component definition.
name = definitionlist.unique_name "startingname"
"startingname" - the starting or base name which will be used to form the unique name
name - the unique name if successful
path=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.load path
name = componentdefinition.name
newname = definitions.unique_name name