SketchUp  Ruby API Reference 

Class Index

Method Index

Developers Guide

Examples

ComponentInstance class

The ComponentInstance class is used to represent component instances of a component definition or components that have been dragged from the Component Browser and placed (thus, instanced) within the Model. Therefore, the ComponentInstance class contains a reference to a corresponding ComponentDefinition object and a Transformation object (which contains the location of the component in the Drawing Window).

Parent:Drawingelement

Methods: add_observer, definition, definition=, explode, glued_to, glued_to=, locked?, locked=, make_unique, move!, remove_observer, transform!, transformation, transformation=

Example Code: componentinstancetests.rb

Instance Methods


add_observer

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

Syntax

status = object.add_observer observer

Arguments

observer - an observer

Return Value

true if successful, false if unsuccessful.

Example

 

 


definition

The definition method is used to retrieve the component definition for this component instance.

Syntax

componentdefinition = instance.definition

Return Value

componentdefinition - a ComponentDefinition object if successful

Example

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

definition = instance.definition

 

 


definition=

The definition= method is used to set the component definition for this component.

Syntax

componentdefinition = componentinstance.definition = componentdefinition         

Arguments

componentdefinition - a ComponentDefinition object to set

Return Value

componentdefinition - the ComponentDefinition object that was set if successful, false if unsuccessful

Comments

This method causes the instance to use a different definition, but it will use the same transformation to position it in the Model.

Example

point = Geom::Point3d.new 10,20,30
transform = Geom::Transformation.new point
model = Sketchup.active_model
entities = model.active_entities
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

instance = entities.add_instance componentdefinition1, transform

UI.messagebox "Added Bed"

definition = instance.definition=componentdefinition2

 

 


explode

The explode method is used to explode the component instance into separate entities.

Syntax

entities = componentinstance.explode

Return Value

entities - an Entities object if successful, false if unsuccessful

Example

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 "TableRound36.skp" ,"Components/Furniture/"
definitions = model.definitions
componentdefinition = definitions.load path
instance = entities.add_instance componentdefinition, transform # Status is an array of all of the entities, verticies, edges uses, and everything

# that makes up the component

status = instance.explode

 

 


glued_to

The glued_to method is used to retrieve the entity that this instance is glued to.

Syntax

entity = componentinstance.glued_to

Return Value

entity - the Entity object that the instance is glued to (if successful)

Comments

Returns nil if it is not glued to anything.

Example

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 "TableRound36.skp" ,"Components/Furniture/"
definitions = model.definitions
componentdefinition = definitions.load path
instance = entities.add_instance componentdefinition, transform
status = instance.glued_to

 

 


glued_to=

The glued_to= method glues this instance to a face.

Syntax

face = componentinstance.glued_to = face

Return Value

face - the Face object where the component is glued if successful

Comments

This method will raise an exception if the instance cannot be glued to the given face. Instances cannot be glued if the definition of the instance doesn't support gluing or if the alignment is wrong.

Example

depth = 100
width = 100
path = Sketchup.find_support_file "Door3-0x6-8RH.skp" ,"Components/Doors/"
model = Sketchup.active_model
entities = model.active_entities
pts = []
pts[0] = [0, 0, 0]
pts[1] = [width, 0, 0]
pts[2] = [width, depth, 0]
pts[3] = [0, depth, 0]
# Add the face to the entities in the model
face = entities.add_face pts
point = Geom::Point3d.new 10,10,0
transform = Geom::Transformation.new point
definitions = model.definitions
componentdefinition = definitions.load path
instance = entities.add_instance componentdefinition, transform
begin
status = instance.glued_to=face
rescue
UI.messagebox $!.message
end
if (status)
UI.messagebox status
else
UI.messagebox "Failure"
end

 

 


locked?

The locked? method is used to determine if a component instance is locked.

Syntax

status = componentinstance.locked?

Return Value

status - true if the component instance is locked. False if the instance is not locked.

 

 



locked=

The locked= method is used to lock a component instance.

Syntax

status = componentinstance.locked=true|false

Arguments 

true | false - true if you want to lock the component; false if you want to unlock the component.

Return Value

status - true if the component instance is locked. False if the instance is not locked.

 

 


make_unique

The make_unique method is used to create a component definition for this instance that is not used by any other instances.

Syntax

status = componentinstance.make_unique

Return Value

status - true if successful, false if unsuccessful

Example

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 "TableRound36.skp" ,"Components/Furniture/"

definitions = model.definitions

componentdefinition = definitions.load path

instance = entities.add_instance componentdefinition, transform

# Returns unique component instance

status = instance.make_unique

 

 


move!

The move! method is the same as the transform! method, except that it does not record the move as an undo operation.

Syntax

status=instance.move! transformation

Return Value

status - true if successful, false if unsuccessful

Comments

This method is useful for moving entities inside of an animation or page transition.

Example

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 "TableRound36.skp" ,"Components/Furniture/"
definitions = model.definitions
componentdefinition = definitions.load path
instance = entities.add_instance componentdefinition, transform
point2 = Geom::Point3d.new 30, 40, 50
transform2 = Geom::Transformation.new point2 UI.messagebox "Applying a new Transformation (in this case, a move)"
status = instance.move! transform2

 

 



remove_observer

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

Syntax

status = object.remove_observer observer

Arguments

observer - an observer

Return Value

true if successful, false if unsuccessful.

Example

 

 


transform!

The transform! method is used to apply a transformation to a component instance.

Syntax

status = componentinstance.transform! transformation 

Arguments

transformation - the transform object to apply to the component instance

Return Value

status - true if successful, false if unsuccessful

Example

UI.messagebox "Applying a new Transformation (in this case, a move)"
status = instance.transform! transform2

 

 


transformation

The transformation method is used to retrieve the transformation of this instance.

Syntax

transformation = componentinstance.transformation

Return Value

transformation - the Transformation object if successful

Example

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 "TableRound36.skp" ,"Components/Furniture/"

definitions = model.definitions

componentdefinition = definitions.load path

instance = entities.add_instance componentdefinition, transform

transformation = instance.transformation

 

 


transformation=

The transformation= method is used to set the transformation for this instance.

Syntax

transformation = instance.transformation = transformation

Arguments

transformation - the new Transformation object to set

Return Value

transformation - the Transformation object that was set if successful

Example

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 "TableRound36.skp" ,"Components/Furniture/"
definitions = model.definitions
componentdefinition = definitions.load path
instance = entities.add_instance componentdefinition, transform
point2 = Geom::Point3d.new 30, 40, 50
transform2 = Geom::Transformation.new point2 UI.messagebox "Applying a new Transformation (in this case, a move)"
status = instance.transformation=transform2 if (status)

UI.messagebox status

else

UI.messagebox "Failure"

end