# Entities used by a Component Instance

**URL:** <https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197>\
**Category:** Ruby API\
**Tags:** entities\
**Created:** [August 17, 2019, 5:20pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197 "2019-08-17T17:20:37Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 17, 2019, 5:20pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/1 "2019-08-17T17:20:37Z")

</div>

Hello everyone. How to find, by the Ruby code, the entities in a component instance?

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [August 17, 2019, 5:44pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/2 "2019-08-17T17:44:56Z")

</div>

A ComponentInstance does not have entities.  
However, it does have a definition, and a definition does have entities.  
So let’s assume you somehow get an instance, this code finds its definition’s entities:

`ents = instance.definition.entities`

We need further info to advise you in more detail…

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 17, 2019, 10:17pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/3 "2019-08-17T22:17:51Z")

</div>

Thank you for your answer TIG. I am trying to extract 3D points from an instance of a component via the syntax :  
entities = Component.model.entities

My goal is indeed to find the 3D points  
of the component in order to exploit them for another use

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 17, 2019, 11:18pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/4 "2019-08-17T23:18:21Z")

</div>

It appears that the code below is working:  
indent preformatted text by 4 spaces def recupedge ( comp )  
entities = comp.model.entities

```
tab =[]
entities.each { | entity| 
if entity.typename == "Edge" 
    puts entity.line[0]
    tab << entity.line[0]
end
}  
tab

```

end   
indent preformatted text by 4 spaces  
Except I don’t know why yet, I get aberrant points in my scoreboard!

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 17, 2019, 11:19pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/5 "2019-08-17T23:19:38Z")

</div>

```
indent preformatted text by 4 spaces

```

def recupedge ( comp )  
entities = comp.model.entities

```
tab =[]
entities.each { | entity| 
if entity.typename == "Edge" 
    puts entity.line[0]
    tab << entity.line[0]
end
}  
tab

```

end   
indent preformatted text by 4 spaces

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [August 17, 2019, 11:50pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/6 "2019-08-17T23:50:50Z")

</div>

> [@LE\_GALL](#):
>
> indent preformatted text by 4 spaces

Please use this method of posting code … [[How to] Post correctly formatted and colorized code on the forum?](https://forums.sketchup.com/t/how-to-colourize-code-on-the-forum-wiki/46189)

* * *

Where **`comp`** is the reference of a component instance, …

> [@LE\_GALL](#):
>
> ```ruby
> entities = comp.model.entities
> 
> ```

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:

```ruby
ents = Sketchup.active_model.entities

```

* * *

> [@LE\_GALL](#):
>
> ```ruby
> if entity.typename == "Edge"
> 
> ```

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:

```ruby
  if entity.is_a?(Sketchup::Edge)

```

But to quickly collect a certain class of entity from an entities collection, use the [**`.grep`**](https://ruby-doc.org//core-2.5.1/Enumerable.html#method-i-grep) method that most API collection classes inherit from the Ruby [**`Enumerable`**](https://ruby-doc.org//core-2.5.1/Enumerable.html) mixin module.

```ruby
edges = entities.grep(Sketchup::Edge)

```

* * *

To find the points of edges, you use the [**`.start`**](http://ruby.sketchup.com/Sketchup/Edge.html#start-instance_method) and [**`.end`**](http://ruby.sketchup.com/Sketchup/Edge.html#end-instance_method) instance methods.  
These method return [**`Sketchup::Vertex`**](http://ruby.sketchup.com/Sketchup/Vertex.html) objects. To get the 3D points, use the vertex’s [**`.position`**](http://ruby.sketchup.com/Sketchup/Vertex.html#position-instance_method) method.  
Example:

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

```

* * *

To collect points from a number of edges …

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

```

> [@LE\_GALL](#):
>
> Except I don’t know why yet, I get aberrant points in my scoreboard!

Now the points are local to the `instance`’s [transformation](http://ruby.sketchup.com/Geom/Transformation.html).  
They need to be converted to “global” coordinates…

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

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 18, 2019, 8:14am UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/7 "2019-08-18T08:14:31Z")

</div>

Thank you very much, DanEathbun for your valuable advice. The solution is in sight

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 18, 2019, 12:43pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/8 "2019-08-18T12:43:59Z")

</div>

Sorry for getting back to the load, but I cannot for my purposes. When I’m looking for the methods by

```ruby
Puts comp.methods.sort

```

I can’t find the grep method. How should I do? I use SketchUp Make 2017

---

<div class="post-metadata">

**Author:** ![john\_drivenupthewall](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@john\_drivenupthewall](https://forums.sketchup.com/u/john_drivenupthewall)\
**Post date:** [August 18, 2019, 1:27pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/9 "2019-08-18T13:27:34Z")

</div>

> [@LE\_GALL](#):
>
> comp.methods

comp also has no entities to grep…

```ruby
# :grep in Ruby is an Enumerable method, have a look...
Enumerable.instance_methods

# and you can check that the definition has the 'grep' method...
comp.definition.entities.respond_to?('grep') # => true

# if 'comp.definition' is valid and has entities, than you can grep it...
tab = comp.definition.entities.grep(Sketchup::Edge) # returns an array of edges...

```

john

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 18, 2019, 1:32pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/10 "2019-08-18T13:32:56Z")

</div>

Thank you john.  
Here’s what works, put it because I always have aberrant points:

```ruby
def recupedge ( instance )

  ents = instance.definition.entities
  edges = ents.grep(Sketchup::Edge)
  vertices = edges.flat_map(&:vertices).uniq
  points = vertices.map(&:position)
  xform = instance.transformation
  tab = points.map {|pt| pt.transform(xform) }
puts tab
    tab
  
 end 	

```

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 18, 2019, 1:58pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/11 "2019-08-18T13:58:02Z")

</div>

As much for me, the aberrant points already exist in my input data. flop!👎

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 18, 2019, 2:00pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/12 "2019-08-18T14:00:07Z")

</div>

Thank you again, everyone.👍

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 21, 2019, 10:59pm UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/13 "2019-08-21T22:59:10Z")

</div>

Hello again DanRathbun  
It’s me again. I’m having a hard time finding documentation on the “flat\_map” method. It doesn’t even appear anywhere in Ruby’s core 2.6.3 documentation! Could you tell me about it?

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [August 22, 2019, 12:10am UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/14 "2019-08-22T00:10:48Z")

</div>

> [@LE\_GALL](#):
>
> It doesn’t even appear anywhere in Ruby’s core 2.6.3 documentation!

If you are still using SketchUp 2017, then it uses Ruby version 2.2.4 and you should use …  
[https://ruby-doc.org/core-2.2.4/index.html](https://ruby-doc.org/core-2.2.4/index.html)  
… to look up Ruby core modules, classes and methods.

* * *

> [@LE\_GALL](#):
>
> I’m having a hard time finding documentation on the “flat\_map” method.

Okay … the SketchUp API’s **[`Sketchup::Entities`](http://ruby.sketchup.com/Sketchup/Entities.html)** class [include](https://ruby-doc.org/core-2.2.4/Module.html#method-i-include)s the **[`Enumerable`](https://ruby-doc.org/core-2.2.4/Enumerable.html)** module from the Ruby core. (Many, if not all, of the API’s collection classes will [`include`](https://ruby-doc.org/core-2.2.4/Module.html#method-i-include) the **[`Enumerable`](https://ruby-doc.org/core-2.2.4/Enumerable.html)** module. I would have to say that all of the Ruby core’s collection classes also [`include`](https://ruby-doc.org/core-2.2.4/Module.html#method-i-include) the **[`Enumerable`](https://ruby-doc.org/core-2.2.4/Enumerable.html)** module.)

To see what library modules an API class [include](https://ruby-doc.org/core-2.2.4/Module.html#method-i-include)s or what superclass functionality they inherit, look at the top of the class / module documentation page.

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/f/a/faa6efb362f30ccea7648db6b589a362d8144fa8.png)

So the **[`Enumerable`](https://ruby-doc.org/core-2.2.4/Enumerable.html)** module is how the **[`Sketchup::Entities`](http://ruby.sketchup.com/Sketchup/Entities.html)** class gets the [`#grep`](https://ruby-doc.org/core-2.2.4/Enumerable.html#method-i-grep) method (which returns an **[`Array`](https://ruby-doc.org/core-2.2.4/Array.html)**.)

If you look at the Ruby core doc for **[`Array`](https://ruby-doc.org/core-2.2.4/Array.html)** in the left navigation column, below the method list, … you’ll see that the **[`Array`](https://ruby-doc.org/core-2.2.4/Array.html)** class **also** [`include`](https://ruby-doc.org/core-2.2.4/Module.html#method-i-include)s the **[`Enumerable`](https://ruby-doc.org/core-2.2.4/Enumerable.html)** module.

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/7/4/745f8c6c6a64482c6fa1c8d88f931c362ad82083.png)

The [`#flat_map`](https://ruby-doc.org/core-2.2.4/Enumerable.html#method-i-flat_map) method comes from the **[`Enumerable`](https://ruby-doc.org/core-2.2.4/Enumerable.html)** module, and is inherited by classes that mix it in using [`Module#include`](https://ruby-doc.org/core-2.2.4/Module.html#method-i-include) …

![image](https://global.discourse-cdn.com/sketchup/original/3X/9/0/904e7747d3ff9e70ec54b8edd511ce1f0b81e231.png)

The SketchUp Ruby API documentation does **not** list include or inherited methods from the Ruby core or Ruby standard libraries (if loaded.)

---

<div class="post-metadata">

**Author:** ![LE\_GALL](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/le_gall/32/74794_2.png) [@LE\_GALL](https://forums.sketchup.com/u/LE_GALL)\
**Post date:** [August 22, 2019, 7:35am UTC](https://forums.sketchup.com/t/entities-used-by-a-component-instance/101197/15 "2019-08-22T07:35:07Z")

</div>

OK. Thank you Dan Rathbun.
