What is "entities"
-
So this is the start of my code and I am curious what "entities" is. Is it an array? a variable? a method?
model = Sketchup.active_model entities = model.active_entities entity = entities[0]
I guess its an array, since I can make "entity = entities[0]" But I can't get entities to print out all its contents to the screen. In my mind, I should be able to write:
UI.messagebox entities
and have it put all of its contents out, but instead it returns "#Sketchup::Entities:0xb658ee0" So its like a single name, instead of an array of entity names.
So what is "entities" exactly?
-
Hi Chris.
entities is sort of a like an array, but a bit different. The reason that you get the object id when you put it in a messagebox is because there is no default to_s (to string) method defined for it.
For your personal testing, you could extend Entities to add a to_s method to see what would happen. If you want to do this, I can show you how.
Todd
-
Thanks Todd, I think that explains it enough for now. That helps me feel a little more stable in working with it. But I do have another questions about its array and how it holds and reorganizes itself.
So entities is an array of all entities. Then lets say I run
entity = entities[0] ents = entity.all_connected group = entities.add_group(ents)
So entity now equals the first object of the entities array and ents is an array of all objects connected to ent (including ent). Then I group those objects.
I was imagining that the entities array would be the same as when I started, but it is not. It automatically removes the objects that were sent to ents and then adds an array item at the end of the array for the newly created group. So its updating itself, whereas I thought I would have to be updating it.
I guess I just want to make sure that this is the expected behavior and that I'm understanding it correctly. Its important for how I am (slowly) coding my little plugin. Thanks,
Chris
-
Yes, it would update itself since you're making a reference to .entities, not a copy of the list.
If you want to make a copy of the list I think you can do:
copy_array = Sketchup.active_model.entities.collect
copy_array won't change when you make changes to the scene. But you might end up with references to objects that doesn't exists. So you'd have to validate your entities if you use that method.
-
Ahh cool, I'll play around with that. I don't need the entities to not update, so its not a big deal I just wanted to make sure I was seeing things right. And that makes sense that it updates since I am calling entities when I make the group.
Thanks Todd, thanks thomthom!
Chris
-
Chris, just some more thoughts on entities..
entities, in your example, is a variable which points to Sketchup.active_model.active_entities. It is not a copy of Sketchup.active_model.active_entities, but think of it as a shortcut (aka pointer, or reference) to Sketchup.active_model.active_entities.
If you want to copy the current Sketchup.active_model.active_entities, you can call .to_a also:
ents_copy = Sketchup.active_model.active_entities.to_a
Sketchup::Entities is single object, but it is a container of Sketchup::Entity objects. If you would like to know what is a Sketchup::Entity, you can see my API graph.
The Ruby module Enumerable is mixed-in to Sketchup::Entities, which means Sketchup::Enities will respond to the mothods defined in Enumerable.
Advertisement