|
SketchUp Ruby API Reference |
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
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 definition method is used to retrieve the component definition for this component instance.
componentdefinition = instance.definition
componentdefinition - a ComponentDefinition object if successful
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
The definition= method is used to set the component definition for this component.
componentdefinition = componentinstance.definition = componentdefinition
componentdefinition - a ComponentDefinition object to set
componentdefinition - the ComponentDefinition object that was set if successful, false if unsuccessful
This method causes the instance to use a different definition, but it will use the same transformation to position it in the Model.
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
The explode method is used to explode the component instance into separate entities.
entities = componentinstance.explode
entities - an Entities object 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 "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
The glued_to method is used to retrieve the entity that this instance is glued to.
entity = componentinstance.glued_to
entity - the Entity object that the instance is glued to (if successful)
Returns nil if it is not glued to anything.
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
The glued_to= method glues this instance to a face.
face = componentinstance.glued_to = face
face - the Face object where the component is glued if successful
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.
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
The locked? method is used to determine if a component instance is locked.
status = componentinstance.locked?
status - true if the component instance is locked. False if the instance is not locked.
The locked= method is used to lock a component instance.
status = componentinstance.locked=true|false
true | false - true if you want to lock the component; false if you want to unlock the component.
status - true if the component instance is locked. False if the instance is not locked.
The make_unique method is used to create a component definition for this instance that is not used by any other instances.
status = componentinstance.make_unique
status - true 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 "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
The move! method is the same as the transform! method, except that it does not record the move as an undo operation.
status=instance.move! transformation
status - true if successful, false if unsuccessful
This method is useful for moving entities inside of an animation or page transition.
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
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 transform! method is used to apply a transformation to a component instance.
status = componentinstance.transform! transformation
transformation - the transform object to apply to the component instance
status - true if successful, false if unsuccessful
UI.messagebox "Applying a new Transformation (in this case, a move)"
status = instance.transform! transform2
The transformation method is used to retrieve the transformation of this instance.
transformation = componentinstance.transformation
transformation - the Transformation object if successful
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
The transformation= method is used to set the transformation for this instance.
transformation = instance.transformation = transformation
transformation - the new Transformation object to set
transformation - the Transformation object that was set if successful
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