Entities used by a Component Instance

Please use this method of posting code … [How to] Post correctly formatted and colorized code on the forum?


Where comp is the reference of a component instance, …

Does not return a reference to a component instance’s definition’s entities.
It rather returns a reference to the “root level” entities collection of the model, which can always be gotten via:

ents = Sketchup.active_model.entities

Do not use the .typename method for class typing as Ruby string comparison is very slow.

Use instead a class identity comparison which is very fast. Example:

  if entity.is_a?(Sketchup::Edge)

But to quickly collect a certain class of entity from an entities collection, use the .grep method that most API collection classes inherit from the Ruby Enumerable mixin module.

edges = entities.grep(Sketchup::Edge)

To find the points of edges, you use the .start and .end instance methods.
These method return Sketchup::Vertex objects. To get the 3D points, use the vertex’s .position method.
Example:

pt1 = edge1.start.position
pt2 = edge1.end.position

To collect points from a number of edges …

ents = instance.definition.entities
edges = ents.grep(Sketchup::Edge)
vertices = edges.flat_map(&:vertices).uniq
points = vertices.map(&:position)

Now the points are local to the instance’s transformation.
They need to be converted to “global” coordinates…

xform = instance.transformation
global_points = points.map {|pt| pt.transform(xform) }