How to get entities from selection?
-
I have a selection object with several entities, I am confused about how could I get all the entities from the selection object?
-
You mean into an array? The
Selection
object is anEnumerable
so you can iterate it as normal. It also have a.to_a
method so it can often be mixed with arrays.How do you want to "get" the entities? Iterate them?
-
@thomthom said:
You mean into an array? The
Selection
object is anEnumerable
so you can iterate it as normal. It also have a.to_a
method so it can often be mixed with arrays.How do you want to "get" the entities? Iterate them?
I just want to inplement the following code:
entities = selection.entities (this function is what I wanted, but not exist)
group = entities.add_group
group.add entities (this function is what I wanted, but not exist)
bounds = group.bounds
center = bounds.centerIs there any way?
-
Yes:
entities = selection.entities # group = entities.add_group # "The entities method is used to retrieve # a collection of entities in the group." group.entities.add entities bounds = group.bounds center = bounds.center
Sketchup::Group
,Sketchup::ComponentDefinition
andSketchup::Model
are similar "containers" for entities (in fact components are models in a model; and a model can be inserted as component). -
If you want to group a selection then it's safe to do it as the group is made as the selection must be in the active_entities [cross-threading group/entities will splat!]
group=model.active_entities.add_group(model.selection.to_a) center=group.bounds.center
However, if you are doing it to find the center of a selection and then exploding the temporary group you could do it different ways, e.g. ...
bb=Geom::BoundingBox.new model.selection.each{|e| bb.add(e.bounds) } center=bb.center
You can of course filter objects for the added bounds, by including tests inside the{}
, e.g. to include only groups insert an early
next unless e.is_a?(Sketchup::Group)
-
-
Try my suggestion[s] - far easier to understand and get results if you read the alternatives carefully....
I believe that Aerilius was thinking that your 'selection' was actually a 'group'.
Then the 'group'.entities would have worked... -
@tig said:
Try my suggestion[s] - far easier to understand and get results if you read the alternatives carefully....
I believe that Aerilius was thinking that your 'selection' was actually a 'group'.
Then the 'group'.entities would have worked...Thanks TIG! I have tried your suggestions. It works, but maybe there is a bug in my script, I did not get the correct coordinates.
-
@lbsswu said:
@tig said:
Try my suggestion[s] - far easier to understand and get results if you read the alternatives carefully....
I believe that Aerilius was thinking that your 'selection' was actually a 'group'.
Then the 'group'.entities would have worked...Thanks TIG! I have tried your suggestions. It works, but maybe there is a bug in my script, I did not get the correct coordinates.
Without at least a few clues we can't help you much further
If you are trying to find the center of a selection of objects then my examples will return the bounds.center
You never explained properly what it is you are trying to do...
We answer your query only to find it's not the answer to the question you ought to have asked really
Can you explain what you seek and we might help with a path to get there.
Sometimes you seem to have gone half way down the wrong road and then ask us how to get to you destination by carrying on, when really you need to go back to the start and set off in the correct direction as it'll be much easier/quicker... -
@tig said:
@lbsswu said:
@tig said:
Try my suggestion[s] - far easier to understand and get results if you read the alternatives carefully....
I believe that Aerilius was thinking that your 'selection' was actually a 'group'.
Then the 'group'.entities would have worked...Thanks TIG! I have tried your suggestions. It works, but maybe there is a bug in my script, I did not get the correct coordinates.
Without at least a few clues we can't help you much further
If you are trying to find the center of a selection of objects then my examples will return the bounds.center
You never explained properly what it is you are trying to do...
We answer your query only to find it's not the answer to the question you ought to have asked really
Can you explain what you seek and we might help with a path to get there.
Sometimes you seem to have gone half way down the wrong road and then ask us how to get to you destination by carrying on, when really you need to go back to the start and set off in the correct direction as it'll be much easier/quicker...My task is get the parts' center of a 3D car model. I find the problem which posted in the website:
http://forums.sketchucation.com/viewtopic.php?f=180&t=46520&p=416248#p416248 -
Sample test model?
Advertisement