How to tell what group is the current context

I have ruby code that adds drawing elements upon user clicking a toolbar button.
I want them to be added to the current context: If the user is currently working inside a group, the elements should be added to that group.
How do I tell what is the current context?

Now the elements are always added outside of all groups.

1 Like

Add the elements using Model.active_entities instead of Model.entities.

The active_entities method is used to retrieve an Entities object containing all of the entities in the active model, component, or group (if you are within a group or component edit session.)
1 Like

WOW thanks.
I just have to replace:
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
with:
mod = Sketchup.active_model # Open model
ent = mod.active_entities # All entities in model
at the beginning of every method definition.
since I’m always only concerned with the current context whether I’m adding or looping through entities.

Clearly the object model terminology is in conflict with the UI terminology in many ways.
context = active entities
scene = page and so one.

The book Automatic Sketchup doesn’t even mention active_entites except in an appendix list (and misdefines it).

I nominate you to prepare a translation glossary for the object model in your spare time. (Just kidding.)

I’m not kidding. Lol.

A noble effort for sure, but it does not deal with the translation from user terminology.
There is no reference to “Scenes” for one example, in your API-Doc, only “Pages”.
Only exhaustive reading of the object model combined with leaps of insight will lead a developer to discover which UI and Help terms are synonymous with which object names.

Scenes used to be named Pages in SketchUp - and the Ruby API predated “Scenes”.

Besides the 2 examples you gave, the SketchUp object names and equivalent API classes are closely matched. Not to say other inconsistencies and oddities are non-existent.