|
SketchUp Ruby API Reference |
The ComponentDefinition class is used to define the contents for a component (the entities contained within a component). The ComponentDefinition can be thought of as the class used to represent components in the SketchUp Component Browser.
Parent:Drawingelement
Methods: <=>, ==, add_observer, behavior, count_instances, description, description=, entities, group?, guid, hidden?, instances, image?, insertion_point, internal? , name, name=, path, remove_observer, save_thumbnail
Example Code: cdtests.rb
The <=> method is used to compare two ComponentDefinition objects for sorting. The comparison is done based on the component name.
status = componentdefinition1 <=> componentdefinition2
componet1 - the first component in the comparison
component2 - the second component in the comparison
status - a -1 if component1 is less then component2. A 1 if component1 greater than component2
path1=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
path2=Sketchup.find_support_file "TableRound36.skp", "Components/Furniture/"
model = Sketchup.active_model definitions = model.definitions
componentdefinition1 = definitions.load path1
componentdefinition2 = definitions.load path2 # Yields a -1
status = componentdefinition1 <=> componentdefinition2
The == method is used to test if two ComponentDefinition objects are the same (based on their address in memory).
status = componentdefinition1 == componentdefinition2
componetdefinition1 - the first component definition in the comparison
componentdefinition2 - the second component definition in the comparison
status - true if the ComponentDefinition objects are the same object. False if the objects are not the same.
path1=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
path2=Sketchup.find_support_file "TableRound36.skp", "Components/Furniture/" model = Sketchup.active_model
definitions = model.definitions
componentdefinition1 = definitions.load path1
componentdefinition2 = definitions.load path2 # Yields a false
status = componentdefinition1 == componentdefinition2
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 behavior method is used to retrieve the Behavior object associated with a component definition.
behavior = componentdefinition.behavior
behavior - a Behavior object if successful
path=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.load path
behavior = componentdefinition.behavior if (behavior)
UI.messagebox behavior
else
UI.messagebox "Failure"
end
The count_instances method is used to count the number of component instances in a model using this component definition.
count = componentdefinition.count_instances
count - the number of component instances of this component definition (if successful)
path=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.load path
number = componentdefinition.count_instances
if (number)
UI.messagebox number
else
UI.messagebox "Failure"
end
The description method is used to retrieve the description of the component definition.
description = componentdefinition.description
description - the description of the component definition if successful
Component definitions do not have a description, by default.
# Set the description
componentdefinition.description="This is a traditional bed"
description = componentdefinition.description
The description= method is used to set the description for the component definition.
status = componentdefinition.description = description
status - the description if successful, false if unsuccessful
componentdefinition.description="This is a traditional bed"
description = componentdefinition.description
The entities method retrieves a collection of all the entities in the component definition
entities=componentdefinition.entities
entities - an Entities object if successful
entities = componentdefinition.entities if (entities)
length = entities.length
UI.messagebox "Number of Entities in Component: #{length}"
else
UI.messagebox "Failure"
end
The group? method is used to determine if this component definition is used to hold the elements of a group.
status = componentdefinition.group?
status - true if the definition is used to hold a group, false if the definition does not hold a group.
path=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.load path
status = componentdefinition.group? if (status)
UI.messagebox status
else
UI.messagebox "No Groups in the Component"
end
The guid method is used to retrieve the unique identifier of this component definition.
guid = componentdefinition.guid
guid - a string guid if successful
guid = componentdefinition.guid
The hidden method is used to determine if this component definition should be hidden on the component browser.
status = componentdefinition.hidden?
status - true if the definition should be hidden, false if the definition should not be hidden
status = componentdefinition.hidden?
The instances method is used to determine the number of component instances that use this component definition
instances = componentdefinition.instances
instances - the number of instances based on this component definition (if successful)
The image method is used to determine if this component definition is used to define an image.
status = componentdefinition.image?
status - true if the component definition defines an image, false if the component definition does not define an image.
status = componentdefinition.image?
if (status)
UI.messagebox "Component definition defines an image"
else
UI.messagebox status
UI.messagebox "Component definition does not define an image"
end
The insertion_point method is used to retrieve the Point3d object where the component was inserted.
point = componentdefinition.insertion_point
point - the Point3d where the component was inserted if successful. False if unsuccessful.
point = Geom::Point3d.new 10,20,30
transform = Geom::Transformation.new point
model = Sketchup.active_model
entities = model.active_entities
path=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
definitions = model.definitions\
componentdefinition = definitions.load path
instance = entities.add_instance componentdefinition, transform\
point = componentdefinition.insertion_point
The internal? method is used to determine if the component definition is internal to the Component Browser
status = componentdefinition.internal?
status - true if the component definition is internal to SketchUp's Component Browser. False if the component definition is not internal to SketchUp.
point = Geom::Point3d.new 10,20,30
transform = Geom::Transformation.new point
model = Sketchup.active_model
entities = model.active_entities
path=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
definitions = model.definitions
componentdefinition = definitions.load path
status = componentdefinition.internal?
The name method retrieves the name of the component definition.
name = componentdefinition.name
name - the component definition's 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
The name= method is used to set the name of the component definition.
name - componentdefinition.name = name
name - the name assigned to the component definition 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="Bed"
The path method is used to retrieve the path where the component was loaded.
path = componentdefinition.path
path - a valid path if successful, false if unsuccessful
Returns nil if it is an internal component.
path=Sketchup.find_support_file "BedTraditional.skp" ,"Components/Furniture/"
model = Sketchup.active_model
definitions = model.definitions
componentdefinition = definitions.load path
path = componentdefinition.path
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 save_thumbnail method is used to save a thumbnail image to a file.
status = componentdefinition.save_thumbnail "filename"
"filename" - the name of the file, with extension, to save the thumbnail as
status - true if successful, false if unsuccessful
Returns nil if it is an internal component.
path = Sketchup.find_support_file "fan.skp" ,"Components/Architecture_Sampler/"model = Sketchup.active_modeldefinitions = model.definitionscomponentdefinition = definitions.load pathstatus = componentdefinition.save_thumbnail "testthumbnail.jpg"UI.messagebox status