# Trying to learn to use layers and objects in API

**URL:** <https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057>\
**Category:** Ruby API\
**Tags:** sketchup, advice, modeling\
**Created:** [July 1, 2020, 8:36pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057 "2020-07-01T20:36:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwatts](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cwatts](https://forums.sketchup.com/u/cwatts)\
**Post date:** [July 1, 2020, 8:36pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057/1 "2020-07-01T20:36:44Z")

</div>

I am trying to make both objects and layers in the API and I am having trouble. I would like for the layer to give color to the object but it seems that some of the faces don’t belong to the correct object. below I will include the code. The problem lies in the last image where the insulator layer’s face has the via’s color. Any resources for learning the API would be appreciated. I already browsed through the resources listed in the forums. I just am a visual/from code learner.

Thanks!

```auto
model = Sketchup.active_model # Open model
entities = model.active_entities
layers_array = model.layers

# add layers to the array.
layers_array.add "insulator"
layers_array.add "via"

model.active_layer= layers_array["insulator"]
entities = model.active_entities
# Add the group to the entities in the model
insulator = entities.add_group
insulator.name = "insulator"

# make some points
scale = 10.mm
top = 3.mm
pts = [[0,0,0],[scale,0,0],[scale,scale,0],[0,scale,0],[0,0,0]]

# add points to group
face = insulator.entities.add_face pts
if face.normal.z == -1
  face.reverse!
end
face.pushpull top

# now the via
model.active_layer= layers_array["via"]
entities = model.active_entities
# Add the group to the entities in the model
via = entities.add_group
via.name = "via"
# get group entities
ent2 = via.entities

scale = 3.mm
centering = 3.mm
pts = [[centering,centering,top],[scale+centering,centering,top],[scale+centering,scale+centering,top],[centering,scale+centering,top],[centering,centering,top]]

face2 = ent2.add_face pts
if face2.normal.z == 1
  face2.reverse!
end
face2.pushpull(2.mm,true)

via_copy = via.parent.entities.add_instance(via.definition, via.transformation)

insulator = via.subtract(insulator)
insulator.name = "insulator"

via = via_copy # let the copy now be grp1
via.name = "via"
zface = via.entities.find {|ent| ent.typename == "Face" && ent.normal == [0, 0, -1]}
zface.pushpull -2.mm

model.rendering_options['DisplayColorByLayer']= true # colors each layer

```

Full view nothing off.

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/f/a/fa1b042b25a97fa70d74dbc868081ddd6e2a84ab.png)  
Layer Insulator tag view off  
 ![image](https://global.discourse-cdn.com/sketchup/original/3X/1/e/1e2f486e8265f8fe6d2a4c6a8c292b05f230b54c.png)  
Layer Via tag off  
 ![image](https://global.discourse-cdn.com/sketchup/original/3X/6/9/691113fbea094a8e162dfccb10957fb63ba639f2.png)  
Group insulator tag off  
 ![image](https://global.discourse-cdn.com/sketchup/original/3X/0/2/0200fcc359eaa83158c7ed6bfdbb38481dc8ee47.png)  
Group Via tag off  
 ![image](https://global.discourse-cdn.com/sketchup/original/3X/e/4/e40f317d8c3806d395d3c8eacf6d53e31ef82e9f.png)

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [July 1, 2020, 9:10pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057/2 "2020-07-01T21:10:28Z")

</div>

I believe the source of your problems is that you are making one of your tags (still called “layers” in the API for backward compatibility) active before adding geometry to the model. This causes new edges and faces to use that tag. But in SketchUp only groups and components should use tags, edges and faces should always use untagged (aka Layer0). Instead, you should leave untagged active and explicitly assign each of your groups to use the appropriate layer, as

```ruby

insulator_tag = layers_array.add "insulator"
via_tag = layers_array.add "via"
# get rid of the line that says 
#model.active_layer= layers_array["insulator"]
# then later
insulator = entities.add_group
insulator.name = "insulator"
insulator.layer = insulator_tag
# and likewise a bit further do the same for via
# kill the line saying
#model.active_layer= layers_array["via"]
# then do
via = entities.add_group
via.name = "via"
via.layer = via_tag

```

Note: the same best practice applies when drawing by hand in SketchUp

Edit: There is more going on as well. I think your creation of copies of groups is losing their association with tags.

Edit2: yes, because editing a group or making a copy creates a new, distinct group, you also need to assign the tags to via\_copy (which replaces via) and to insulator (which gets replaced by the subtract operation).

Here’s the result of making those various changes:

![layers](https://global.discourse-cdn.com/sketchup/original/3X/b/4/b4314e19af93e71bdbbe453ff1571a315f95d379.gif)

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [July 1, 2020, 9:40pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057/3 "2020-07-01T21:40:22Z")

</div>

Some further thoughts:

Even in a sample like this, you should enclose your code in a module so that it can’t collide with anything else.

```ruby
module Cwatts
  # put your code here
end

```

When you search for a Face, use for example

```ruby
zface = via.entities.find {|ent| ent.is_a?(Sketchup::Face && ent.normal == [0, 0, -1]}

```

string comparison in Ruby is vastly slower than is\_a?. It won’t make a difference in this short example, but in a more complicated loop it can slow things down by a large amount!

---

<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:** [July 1, 2020, 9:50pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057/4 "2020-07-01T21:50:43Z")

</div>

… also beware that [`Enumerable#find`](https://ruby-doc.org/core-2.5.5/Enumerable.html#method-i-find) will return **`nil`** when nothing is found, so always check the result before using it.

```ruby
if zface
  # do something with zface
end

```

… or …

```ruby
call_some_method(zface) unless zface.nil?

```

… etc. …

---

<div class="post-metadata">

**Author:** ![cwatts](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cwatts](https://forums.sketchup.com/u/cwatts)\
**Post date:** [July 2, 2020, 4:49pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057/5 "2020-07-02T16:49:36Z")

</div>

Thanks! so should I make groups then assign them to layers at the end? Also, if you don’t mind me asking. How can I make a gif like the one you show so that I can easily post questions without having a bunch of different images?

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [July 2, 2020, 5:06pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057/6 "2020-07-02T17:06:07Z")

</div>

> [@cwatts](#):
>
> Thanks! so should I make groups then assign them to layers at the end?

It doesn’t matter whether you assign a layer to a group before or after adding edges and faces to it, just that you assign the layer to the group itself and not to those contents. Or in this case, after making the copies.

> [@cwatts](#):
>
> How can I make a gif like the one you show so that I can easily post questions without having a bunch of different images?

I use a little free utility called [LICEcap](https://www.cockos.com/licecap/).

---

<div class="post-metadata">

**Author:** ![cwatts](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cwatts](https://forums.sketchup.com/u/cwatts)\
**Post date:** [July 2, 2020, 5:07pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057/7 "2020-07-02T17:07:19Z")

</div>

Thanks! That worked great. The code you included was missing a parenthesis though. Should have been:

```auto
zface = via.entities.find {|ent| ent.is_a?(Sketchup::Face) && ent.normal == [0, 0, -1]}

```

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [July 2, 2020, 5:08pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057/8 "2020-07-02T17:08:41Z")

</div>

Oops! Correct. I just slammed that in without actually running it 😳

---

<div class="post-metadata">

**Author:** ![cwatts](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cwatts](https://forums.sketchup.com/u/cwatts)\
**Post date:** [July 2, 2020, 5:10pm UTC](https://forums.sketchup.com/t/trying-to-learn-to-use-layers-and-objects-in-api/130057/9 "2020-07-02T17:10:47Z")

</div>

That’s okay I appreciate your help!
