My favorites | English | Sign in

Google SketchUp Ruby API

Entities

class

Parent: Object

Introduction

SketchUp 6.0+

The Entities class is a container class for all entities in a model (it is a collection of Entity objects).

Methods

Entities.[]SketchUp 6.0+

The [] method is used to retrieve an entity by its index in an array of entities. The index is a number between 0 and entities.count - 1. In general, it is preferable to use the "each" method to iterate though all of the entities in the collection as it will be much more efficient.

Arguments:

index
The index for a specific entity.

Returns:

entity
an Entity object if successful, nil if not found
 entities = Sketchup.active_model.entities
 entity0 = entities[0]
 if (entity0)
   UI.messagebox entity0
 else
   UI.messagebox "Failure"
 end

Entities.add_3d_textSketchUp 6.0+

The add_3d_text is used to create 3D text. It will be added as edges and faces drawn at the origin.

Arguments:

string
The text to create.
alignment
Number that defines the alignment. There are constants called TextAlignLeft, TextAlignRight, and TextAlignCenter that can be passed.
font
String font name.
is_bold
(optional) Boolean, true for bold.
is_italic
(optional) Boolean, true for italic.
letter_height
(optional) Height of the text in inches.
tolerance
(optional) Tolerance of the curve creation. Defaults to 0.0, which creates the highest possible curve quality.
z
(optional) z position in inches.
is_filled
(optional) Boolean, true for filled, which will put a face between the edges of the letters.
extrusion
(optional) Extrusion depth in inches. Defaults to 0.0.

Returns:

true if successful
 # Draw the word "test" at the origin of the model, aligned left, in
 # Arial Bold, not italics, 1" in size, best tolerance quality, filled,
 # with an extrusion size of 5".
 entities = Sketchup.active_model.entities
 success = entities.add_3d_text('test', TextAlignLeft, "Arial", 
   true, false, 1.0, 0.0, 0.5, true, 5.0)

Entities.add_arcSketchUp 6.0+

The add_arc method is used to create an arc curve segment.

Arguments:

center
A Point3d object representing the center .
xaxis
A Vector3d object representing xaxis for the arc.
normal
A Vector3d object representing normal for the arc.
radius
The radius of the arc.
start_angle
Start angle for the arc.
end_angle
End angle for the arc.
num_segments
(optional) How many segments to draw.

Returns:

arccurve
an ArcCurve object if successful
 centerpoint = Geom::Point3d.new
 # Create a circle perpendicular to the normal or Z axis
 vector = Geom::Vector3d.new 0,0,1
 vector2 = Geom::Vector3d.new 1,0,0
 vector3 = vector.normalize!
 model = Sketchup.active_model
 entities = model.active_entities
 arccurve = entities.add_arc centerpoint, vector2, vector3, 10, 15, 35
 UI.messagebox arccurve

Entities.add_circleSketchUp 6.0+

The add_circle method is used to create a circle.

Arguments:

center
A Point3d object representing the center .
normal
A Vector3d object representing normal for the arc.
radius
The radius of the arc.
numsegs
(optional) number of segments. Defaults to 24.

Returns:

circle
an Array object containing edges if successful
 centerpoint = Geom::Point3d.new
 # Create a circle perpendicular to the normal or Z axis
 vector = Geom::Vector3d.new 0,0,1
 vector2 = vector.normalize!
 model = Sketchup.active_model
 entities = model.active_entities   
 edges = entities.add_circle centerpoint, vector2, 10

Entities.add_clineSketchUp 6.0+

The add_cline method is used to create a construction line.

Arguments:

start_point
A Point3d object where the line will start.
end
If a Vector3d, then an infinite line passing through the start_point will be created in that direction. If a Point3d, then a finite line will be created between the two points.
stipple
(optional) Stipple pattern. See ConstructionLine.stipple for acceptable patterns.

Returns:

construction_line
a ConstructionLine object if successful
 model = Sketchup.active_model
 entities = model.active_entities
 point1 = Geom::Point3d.new (0,0,0)
 point2 = Geom::Point3d.new (20,20,20)
 constline = entities.add_cline point1,point2
 if (constline)
   UI.messagebox constline
 else
   UI.messagebox "Failure"
 end

Entities.add_cpointSketchUp 6.0+

The add_cpoint method is used to create a construction point.

Arguments:

point
A Point3d object.

Returns:

construction_point
a ConstructionPoint object if successful
 model = Sketchup.active_model
 entities = model.active_entities
 point1 = Geom::Point3d.new (100,200,300)
 constpoint = entities.add_cpoint point1
 if (constpoint)
   UI.messagebox constpoint
 else
   UI.messagebox "Failure"
 end

Entities.add_curveSketchUp 6.0+

The add_curve method is used to create a curve from a collection of edges.

The arguments are either Points or an Array of Points. At least 2 points are required.

Arguments:

pts_or_array
You can pass either a series of Point3d objects or a single array containing Point3d objects.

Returns:

curve
a Curve object if successful
 entities = Sketchup.active_model.entities
 curve = entities.add_curve [0,0,0], [0,10,0], [1,20,0]

Entities.add_edgesSketchUp 6.0+

The add_edges method is used to add a set of connected edges to the entities array.

Arguments:

pts_or_array
You can pass either a series of Point3d objects or a single array containing Point3d objects.

Returns:

edges
an array of Edge objects if successful
 model = Sketchup.active_model
 entities = model.active_entities
 point1 = Geom::Point3d.new (0,0,0)
 point2 = Geom::Point3d.new (20,20,20)
 edges = entities.add_edges point1, point2

Entities.add_faceSketchUp 6.0+

The add_face method is used to create a face. You can call this method a number of ways:

  • entities.add_face(edge1, edge2, edge3, ...)
  • entities.add_face(edgearray)
  • entities.add_face(pt1, pt2, pt3, ...)
  • entities.add_face([pt1, pt2, pt3,...])
  • entities.add_face(curve)


For the last form that takes a Curve, the curve must be closed - like a circle.

Arguments:

some_entities
Either a series of Edge or Point3d objects, an array of Point3d objects, or a closed Curve.

Returns:

face
a Face object if successful
 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
 if (face)
   UI.messagebox face
 else
   UI.messagebox "Failure"
 end

Entities.add_faces_from_meshSketchUp 6.0+

The add_faces_from_mesh method is used to add Face objects to the collection of entities from a PolygonMesh.

The 2nd through 4th parameters are optional. The smooth_flags parameter can contain any of the following values if passed.

  • 0 = no soften/smooth,
  • 4 = soften edges (aka hide them),
  • 8 = smooth edges,
  • 12 = soften and smooth edges.
  • Arguments:

    polygonmesh
    A Polygon mesh object.
    smooth_flags
    (optional) An integer containing flags for soften and smooth settings. Defaults to 0. See above.
    f_material
    (optional) Material object to paint front faces with.
    b_material
    (optional) Material object to paint back faces with.

    Returns:

    faces
    an array of Face objects if successful
     # Grab a mesh from a face.
     pts = []
     pts.push[0,0,0]
     pts.push[0,9,0]
     pts.push[9,9,0]
     pts.push[9,0,0]
     my_face = Sketchup.active_model.entities.add_face pts
     my_mesh = my_face.mesh
    
     # Create a new group that we will populate with the mesh.
     group = Sketchup.active_model.entities.add_group
     f_material = Sketchup.active_model.materials[0]
     f_material.color = "green"
     group.add_faces_from_mesh my_mesh, 0, f_material

Entities.add_groupSketchUp 6.0+

The add_group method is used to create an empty group or a group with entities.

NOTE: calling add_group with entities in its parameters has been known to crash SketchUp. It is preferable to create an empty group and then add things to its Entities collection.

Arguments:

(optional)
an entities collection to add to the group.

Returns:

group
the new group
 model = Sketchup.active_model
 entities = model.active_entities
 group = entities.add_group
 if (group)
   UI.messagebox group
 else
   UI.messagebox "Failure"
 end

Entities.add_imageSketchUp 6.0+

The add_image method is used to add an image to the collection of entities.

The width and height are measured in model units (i.e. inches). If the height is not given, then it is computed from the width to preserve the aspect ratio of the image.

Arguments:

filename
A filename for the image file.
point
A Point3d object representing the insertion point of the image.
width
Width for the image.
height
(optional) height for the image if you want to control width and height independently.

Returns:

image
an Image object if successful.
 model = Sketchup.active_model
 entities = model.active_entities
 point = Geom::Point3d.new 10,20,30
 image = entities.add_image "Shapes.jpg", point, 300
 if (image)
   UI.messagebox image
 else
   UI.messagebox "Failure"
 end

Entities.add_instanceSketchUp 6.0+

The add_instance method adds a component instance to the collection of entities.

Arguments:

definition
A ComponentDefinition object.
transform
A Transformation object.

Returns:

componentinstance
a ComponentInstance 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 "Bed.skp",
   "Components/Components Sampler/"
 definitions = model.definitions
 componentdefinition = definitions.load path
 instance = entities.add_instance componentdefinition, transform
 if (instance)
   UI.messagebox instance
 else
   UI.messagebox "Failure"
 end

Entities.add_lineSketchUp 6.0+

The add_line method is used to add an edge to the collection of entities. This is not to be confused with the concept of a "line" from a geometric sense, which is an invisible object represented by an Array of a point and a vector. (See the Array class for more information on geometric lines in SketchUp.)

This method is the same as add_edges method, but returns a single edge.

Arguments:

point1
Point3d object representing the edge's starting point.
point2
Point3d object representing the edge's ending point.

Returns:

edge
a Edge object if successful
 model = Sketchup.active_model
 entities = model.active_entities
 point1 = Geom::Point3d.new (0,0,0)
 point2 = Geom::Point3d.new (20,20,20)
 line = entities.add_line point1,point2
 if (line)
   UI.messagebox line
 else
   UI.messagebox "Failure"
 end

Entities.add_ngonSketchUp 6.0+

The add_ngon method is used to create a multi-sided polygon.

Arguments:

center
A Point3d object representing the center of the polygon.
normal
A Vector3d object.
radius
A radius.
numsides
The number of sides for the polygon.

Returns:

edges
an array of Edges that make up the polygon if successful
 entities = Sketchup.active_model.entities
 center = Geom::Point3d.new
 normal = Geom::Vector3d.new(0,0,1)
 radius = 100
 numsides = 6
 edges = entities.add_ngon center, normal, radius, numsides

Entities.add_observerSketchUp 6.0+

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

Arguments:

observer
An observer.

Returns:

true if successful, false if unsuccessful.
 entities = Sketchup.active_model.entities
 status = entities.add_observer observer

Entities.add_textSketchUp 6.0+

The add_text method adds a note or label text entity to the entities

Arguments:

text
The text string to add.
point
A Point3d object representing the insertion point.
vector
(optional) vector representing an arrow leader.

Returns:

text
a Text object if successful
 coordinates = [10, 10, 10]
 model = Sketchup.active_model        
 entities = model.entities
 point = Geom::Point3d.new coordinates
 text = entities.add_text "This is a Test", point

Entities.atSketchUp 6.0+

The at method is an alias for []. See [].

Returns:

nil
 entities = Sketchup.active_model.entities
 entity0 = entities.at(0)
 if (entity0)
   UI.messagebox entity0
 else
   UI.messagebox "Failure"
 end

Entities.clear!SketchUp 6.0+

The clear! method is used to remove all entities from the collection of entities.

Returns:

status
true if successful, false if unsuccessful
 coordinates = [10, 10, 10]
 model = Sketchup.active_model        
 entities = model.entities
 point = Geom::Point3d.new coordinates
 text = entities.add_text "This is a Test", point
 UI.messagebox "Clearing All"
 status = entities.clear!

Entities.countSketchUp 6.0+

The count method is an alias for the length method. See length.

Returns:

nil
 coordinates = [10, 10, 10]
 model = Sketchup.active_model        
 entities = model.entities
 point = Geom::Point3d.new coordinates
 text = entities.add_text "This is a Test", point
 length = entities.count

Entities.eachSketchUp 6.0+

The each method is used to iterate through the entities in the collection of entities.

Arguments:

entity
A variable that will hold each Entity object as they are found if successful.

Returns:

nil
 coordinates = [10, 10, 10]
 model = Sketchup.active_model        
 entities = model.entities
 point = Geom::Point3d.new coordinates
 text = entities.add_text "This is a Test", point
 entities.each { | entity| UI.messagebox entity }

Entities.erase_entitiesSketchUp 6.0+

The erase_entities method is used to erase one or more entities from the model.

Arguments:

entities
An entity or array of entities.

Returns:

nil
 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 entity in the
 # entities objects is an edge, so erase it.
 UI.messagebox entities
 entities.erase_entities entities[1]
 UI.messagebox entities

Entities.fill_from_meshSketchUp 6.0+

The fill_from_mesh method is used to add faces and edges to the collection of entities from a PolygonMesh. It requires that the entities collection to be filled is empty. It has higher performance than add_faces_from_mesh, but does less error checking as it builds the geometry.

The 2nd through 5th parameters are optional. The smooth_flags parameter can contain any of the following values if passed.

  • 0 = no soften/smooth,
  • 4 = soften edges (aka hide them),
  • 8 = smooth edges,
  • 12 = soften and smooth edges.


The 4rd and 5th parameters will accept a Material object or a string name of a material currently in the model. They will not accept any string that could be interpreted as a color or material.

Arguments:

polygonmesh
A Polygon mesh object.
weld_vertices
(optional) Boolean. True to automatically weld matching vertices. Defaults to false.
smooth_flags
(optional) An integer containing flags for soften and smooth settings. Defaults to 0. See above.
f_material
(optional) Material object to paint front faces with.
b_material
(optional) Material object to paint back faces with.

Returns:

status
true if successful
 # Grab a mesh from a face.
 pts = []
 pts.push[0,0,0]
 pts.push[0,9,0]
 pts.push[9,9,0]
 pts.push[9,0,0]
 my_face = Sketchup.active_model.entities.add_face pts
 my_mesh = my_face.mesh

 # Create a new group that we will populate with the mesh.
 group = Sketchup.active_model.entities.add_group
 f_material = Sketchup.active_model.materials[0]
 f_material.color = "red"
 group.fill_from_mesh my_mesh, true, 12, f_material

Entities.intersect_withSketchUp 6.0+

The intersect_with method is used to intersect an entities, component instance, or group object with a entities object.

Arguments:

recurse
true if you want this entities object to be recursed (intersection lines will be put inside of groups and components within this entities object).
transform1
The transformation for this entities object.
entities1
The entities where you want the intersection lines to appear.
transform2
The transformation for entities1.
hidden
true if you want hidden geometry in this entities object to be used in the intersection.
entities2
An entities object, or an array of entity.

Returns:

nil
 entities.intersect_with recurse, transformation1, entities1,
   transformation2, hidden, entities2

Entities.lengthSketchUp 6.0+

The length method is used to retrieve the number of entities in the collection of entities.

Returns:

length
the number of entities in the collection of entities if successful
 coordinates = [10, 10, 10]
 model = Sketchup.active_model        
 entities = model.entities
 point = Geom::Point3d.new coordinates
 text = entities.add_text "This is a Test", point
 length = entities.length

Entities.modelSketchUp 6.0+

The model method is used to retrieve the model that contains the collection of entities.

Returns:

model
the model that contains the collection of entities if successful.
 coordinates = [10, 10, 10]
 model = Sketchup.active_model        
 entities = model.entities
 point = Geom::Point3d.new coordinates
 text = entities.add_text "This is a Test", point
 model = entities.model

Entities.parentSketchUp 6.0+

The parent method is used to retrieve the parent or object that contains the collection of entities. A parent can be either a Model or ComponentDefinition object.

Returns:

parent
the object that contains the collection of entities if successful
 coordinates = [10, 10, 10]
 model = Sketchup.active_model        
 entities = model.entities
 point = Geom::Point3d.new coordinates
 text = entities.add_text "This is a Test", point
 parent = entities.parent

Entities.remove_observerSketchUp 6.0+

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

Arguments:

observer
An observer.

Returns:

true if successful, false if unsuccessful.
 entities = Sketchup.active_model.entities
 status = entities.remove_observer observer

Entities.transform_by_vectorsSketchUp 6.0+

The transform_by_vectors method is used to apply several vectors to several sub-entities all at once.

Arguments:

sub_entities
An array of entities to transform.
vectors
An array of vectors to apply.

Returns:

nil
 # Need better Ruby example.
 entities = entities.transform_by_vectors sub_entities, vector_array

Entities.transform_entitiesSketchUp 6.0+

The transform_entities method is used to apply a transform to several sub-entities all at once.

Arguments:

transform
The Transformation to apply.
entities
An array or series of entities to transform.

Returns:

status
results of the transform.
 entities = Sketchup.active_model.entities
 entities.add_line([0,0,0],[100,100,100])
 entities.add_line([0,0,0],[200,-10,-10])

 entities_to_transform = []
 entities_to_transform.push(entities[0])
 entities_to_transform.push(entities[1])

 transform = Geom::Transformation.new([100,0,0])
 entities.transform_entities(transform, ent1, ent2, ent3)