How to tell what group is the current context


#1

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.


#2

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.)

#3

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.)


#4

I’m not kidding. Lol.


#5

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.


#6

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


#7

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.