Class: Sketchup::Entity

Inherits:
Object
  • Object
show all

Overview

This is the base class for all SketchUp entities. Entities are basically anything that can be contained in a model, including Drawingelements such as Edges, SectionPlanes, Groups, etc. and entities that relate to those Drawingelements, such as Loops, Layers, etc.

Keep in mind that the methods below are available on all subclasses. For example, an Edge's parent class is Drawingelement, and a Drawingelement's parent class is Entity. Therefore an Edge has all of the methods defined in Drawingelement and Entity.

The Object.is_a? method is the common way of determining what sort of Entity you're dealing with.

Examples:

# Count how many faces are in the current selection.
selection = Sketchup.active_model.selection
face_count = 0

# Look at all of the entities in the selection.
selection.each { |entity|
  if entity.is_a? Sketchup::Face
    face_count = face_count + 1
  end
}

UI.messagebox("There are " + face_count.to_s + " faces selected.")

Version:

  • SketchUp 6.0

Instance Method Summary # collapse

Instance Method Details

#add_observer(observer) ⇒ Object

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

Examples:

entity = Sketchup.active_model.entities[0]
if entity.valid?
  status = entity.add_observer observer
end

Returns true if successful, false if unsuccessful.

Parameters:

  • observer

    An observer.

Returns:

  • true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#attribute_dictionariesObject

The attribute_dictionaries method is used to retrieve the AttributeDictionaries collection attached to the entity.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
status = entity1.set_attribute "testdictionary", "test", 115
attrdicts = entity1.attribute_dictionaries

Returns:

  • attributedictionaries - the AttributeDictionaries object associated with the entity, or nil if there are no attribute_dictionary objects associated with the model. Care must be taken if nil is returned, for example: invoking attribute_dictionaries.length will throw a NoMethodError exception, not return 0.

Version:

  • SketchUp 6.0

#attribute_dictionary(name, create) ⇒ Object

The attribute_dictionary method is used to retrieve an attribute dictionary with a given name that is attached to an Entity.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
status = entity1.set_attribute "testdictionary", "test", 115
attrdict = entity1.attribute_dictionary "testdictionary"

Returns attributedictionary - an AttributeDictionary object if successful, or nil if there is no attribute dictionary

Parameters:

  • name

    The name of the attribute dictionary.

  • create (optional)

    boolean, if set to true then the attribute dictionary will be created if it does not exist.

Returns:

  • attributedictionary - an AttributeDictionary object if successful, or nil if there is no attribute dictionary

Version:

  • SketchUp 6.0

#delete_attribute(dictionary_name) ⇒ Object #delete_attribute(dictionary_name, key) ⇒ Object

The delete_attribute method is used to delete an attribute from an entity.

If only the dictionary_name is given, then it deletes the entire AttributeDictionary. Otherwise, delete_attribute deletes the attribute with the given key from the given dictionary.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
status = entity1.set_attribute "testdictionary", "test", 115
status = entity1.delete_attribute "testdictionary"

Overloads:

  • #delete_attribute(dictionary_name) ⇒ Object

    Returns nil

    Parameters:

    • dictionary_name

      The name of an attribute dictionary.

    Returns:

    • nil

  • #delete_attribute(dictionary_name, key) ⇒ Object

    Returns nil

    Parameters:

    • dictionary_name

      The name of an attribute dictionary.

    • key

      An attribute key.

    Returns:

    • nil

Version:

  • SketchUp 6.0

#deleted?Object

The deleted? method is used to determine if your entity is still valid (not deleted by another script, for example.)

Examples:

depth = 100
width = 100
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
entity1 = entities[1]
status = entity1.deleted?

Returns:

  • status - true if deleted, false if not deleted

  • (Boolean)

Version:

  • SketchUp 6.0

#entityIDObject

The entityID method is used to retrieve a unique ID assigned to an entity.

The entityID is not persistent between sessions.

Examples:

depth = 100
width = 100
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
entity1 = entities[1]
id = entity1.entityID

Returns:

  • id - the id for the Entity object

Version:

  • SketchUp 6.0

#get_attribute(dict_name, key, default_value = nil) ⇒ Object

The get_attribute method is used to retrieve the value of an attribute in the entity's attribute dictionary.

If the third parameter, default_value, is not passed and there is no attribute that matches the given name, it returns nil.

If default_value is provided and there is no matching attribute it returns the given value. It does not create an attribute with that name though.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
status = entity1.set_attribute "testdictionary", "test", 115
value = entity1.get_attribute "testdictoinary", "test"

Returns value - the retrieved value

Parameters:

  • dict_name

    The name of an attribute dictionary.

  • key

    An attribute key.

  • default_value (optional) (defaults to: nil)

    A default value to return if no attribute is found.

Returns:

  • value - the retrieved value

Version:

  • SketchUp 6.0

#inspectObject

The to_s method is used to retrieve the string representation of the entity.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
st = entity1.to_s

Returns:

  • string - the string representation of the entity if successful

Version:

  • SketchUp 6.0

#modelObject

The model method is used to retrieve the model for the entity.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
m = entity1.model

Returns:

  • model - the model that contains the Entity object

Version:

  • SketchUp 6.0

#parentObject

The parent method is used to retrieve the parent of the entity.

The parent will be a ComponentDefinition, a Group, or a Model, whatever the entity is contained within.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
parent = entity1.parent

Returns:

  • entity - a Entity object representing the parent of this entity

Version:

  • SketchUp 6.0

#persistent_idInteger

The #persistent_id method is used to retrieve a unique persistent id assigned to an entity.

The persistent id persistent between sessions.

Examples:

model = Sketchup.active_model
entities = model.active_entities
pts = [
  Geom::Point3d.new(0, 0, 0)
  Geom::Point3d.new(9, 0, 0)
  Geom::Point3d.new(9, 9, 0)
  Geom::Point3d.new(0, 9, 0)
]

# Add the face to the entities in the model
group = entities.add_group
face = group.entities.add_face(pts)
pid = face.persistent_id
# Exploding the group will preserve the pid.
pid == face.persistent_id # Should return true

Returns:

Version:

  • SketchUp 2017

#remove_observer(observer) ⇒ Object

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

Examples:

entity = Sketchup.active_model.entities[0]
if entity.valid?
  status = entity.remove_observer observer
end

Returns true if successful, false if unsuccessful.

Parameters:

  • observer

    An observer.

Returns:

  • true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0

#set_attribute(dict_name, key, value) ⇒ Object

The set attribute is used to set the value of an attribute in an attribute dictionary with the given name.

This method will create a new AttributeDictionary if none exists.

Note, a bug prior to SketchUp 2015 would corrupt the model if the key is an empty string. This also includes values that will evaluate to empty strings, such as nil.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
status = entity1.set_attribute "testdictionary", "test", 115

Returns value - the newly set value if successful

Parameters:

  • dict_name

    The name of an attribute dictionary.

  • key

    An attribute key.

  • value

    The value for the attribute.

Returns:

  • value - the newly set value if successful

Version:

  • SketchUp 6.0

#to_sObject

The to_s method is used to retrieve the string representation of the entity.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
st = entity1.to_s

Returns:

  • string - the string representation of the entity if successful

Version:

  • SketchUp 6.0

#typenameObject

The typename method retrieves the type of the entity, which will be a string such as “Face”, “Edge”, or “Group”.

Examples:

depth = 100
width = 100
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

# I just happen to know that the second and third entities in the
# entities objects are edges.
entity1 = entities[1]
type = entity1.typename

Returns:

  • type - the type of the entity

Version:

  • SketchUp 6.0

#valid?Object

The valid? method is used to determine if your entity is still valid (not deleted by another script, for example.)

This method is functionally identical to the deleted? method.

Examples:

depth = 100
width = 100
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
entity1 = entities[1]
status = entity1.valid?

Returns:

  • status - true if deleted, false if not deleted

  • (Boolean)

Version:

  • SketchUp 6.0