Quickly finding groups/comps in an Entity Collection?
-
No entities.groups, no - but I asked the same question when I saw the SDK API which does have this: http://forums.sketchucation.com/viewtopic.php?f=180&t=27729
Are you getting the nested groups/comps of the collection?
Alternative to iterate the collection for groups/comps, iterate the
model.definitions
and check the definitions for instances who's parents match the entities collection you are processing.
Normally that should mean less iterations unless you have a model with an insane amount of comps/groups. -
Will that work on groups? Are they listed in the model definitions?
-
Yes. In fact, Image definitions are also listed there. ComponentInstance, Groups and Images are all related.
When you iterate model.definitions, check each definition with
definition.image?
anddefinition.group?
. This is something that should be noted in the docs really. Because if you're not aware of that and process all definitions as components you may be unexpected results. You even get access to the entities of Image elements. -
Excellent, I'll look into this method for exploding internal groups and comps. I think this will be faster than parsing all the entities like I am currently doing. Thanks Thom!
Chris
-
I suspect that the
.explode
operation will be consuming most of the time. -
Write a def that iterates back on itself - something like this [untried]...
def miner(ents) ents.to_a.each{|e| if e.class==Sketchup;;Group miner(e.entities) e.explode elsif e.class==Sketchup;;ComponentInstance igroup=e.parent.entities.add_group(e) e.explode miner(igroup.entities) igroup.explode end#if }#end each end#def ### usage... miner(my_entities)
-
Looks good TIG. That code is significantly more compact than my code I am running. I need to spend some time and do some time tests on these various methods. On certain models I had felt that the process of finding internal groups and exploding them was the main thing slowing this script (shape bender) down. But now in my tests, I have not found a model that seems to freeze up on searching for explodeables. So now I'm sure it was such a HUGE problem as I thought it was. I did go throuh and clean out all my old
typename == "Group"
type comparisons for faster.class == Sketchup::Group
. That alone made the script take 25% less time (from 10 seconds down to 7.5 for example). So that was a good change.Thanks Thom and TIG for the ideas. I'll respond here once I get around to doing some more time tests on this internal exploding business.
Chris
-
@chris fullmer said:
I did go throuh and clean out all my old
typename == "Group"
type comparisons for faster.class == Sketchup::Group
.Yes - String comparisons are slooow!
-
@tig said:
> elsif e.class==Sketchup;;ComponentInstance > igroup=e.parent.entities.add_group(e) > e.explode > miner(igroup.entities) > igroup.explode > end#if >
Why are you grouping the component and exploding both of them?
-
I grouped the instance so that I could then explode it keeping its entities in the group so I can find them easily - I can then look at those for further 'mining'... Once I have processed all of its entities back to raw geometry I can explode that 'igroup' group - Chris wants everything inside the 'entities set' exploded - we can't explode the definition's entities as it would affect other instances too so I explode the instance but keep its newly created entities inside that group for easier 'use'...
-
If I understand what you want to do, you are making it harder than it is.
- Search the tree and build a list of things to explode.
- Then go through the list and explode them.
def traverse_entities(entities, list) entities.each do |entity| if entity.class == Sketchup;;Group traverse_entities(entity.entities, list) list.push(entity) elsif enitity.class == Sketchup;;ComponentInstance traverse_entities(entity.definition.entities, list) list.push(entity) end end end def explode_selected list = [] traverse_entities(Sketchup.active_model.selection, list) list.each { |entity| entity.explode } end
I think TIG's would work like this if you don't need a list or general purpose traversal; which is essentially the same thing but more specialized.
def exploder(ents) ents.to_a.each{|e| if e.class==Sketchup;;Group exploder(e.entities) e.explode elsif e.class==Sketchup;;ComponentInstance exploder(e.definition.entities) e.explode end } end ### usage... exploder(my_entities)
The tendency is to perform the explode before the recursion. Go ahead and do the recursion first, and defer the explode until after the recursion returns.
The result is that the most deeply nested objects get exploded first, and then works up the hierarchy. The last object exploded is then the first object found.
-
@jim said:
If I understand what you want to do, you are making it harder than it is.
The result is that the most deeply nested objects get exploded first, and then works up the hierarchy. The last object exploded is then the first object found.wouldn't you want to explode top > down instead of bottom > up? that way, you don't explode definitions of instances in other Entities.
or was Chris thinking of exploding the whole model?
-
My version was to explode only groups and instances within a set of entities [which I understood to be the aim] and any groups/instances found within them - definition contents shouldn't change?]. To avoid confusing arrays of things to explode I think it's best to work from the lowest level up and explode these first, isn't it ?
-
@tig said:
My version was to explode only groups and instances within a set of entities [which I understood to be the aim] and any groups/instances found within them - definition contents shouldn't change?].
Since you are exploding bottom to top, the top level definitions will change before they are exploded.
here is a method that will explode them top to bottom:
def miner(ents) ents.each{|e| if e.class==Sketchup;;Group miner(e.explode) elsif e.class==Sketchup;;ComponentInstance miner(e.explode) end#if }#end each end#def
EDIT: I just realized that's what the group is for.
-
-
Hehe, great thread. Yes, I want to explode everything inside a component. IT is possible that it might have componeonts inside it, that are used elsewhere and they should not have all their internals exploded. So I think if you work from deepest level to uppermost level, you have to do some trickery to not explode the stuff that exists in other instances outside mine.
I would think more Jim and Chris on this and explode before recursing. And ther is no double exploding of any component. That way the component is exploded and there is no chance of affecting its siblings outside in the rest of the model.
All in all, plenty of giid feedback and SIGNIFICANTLY simpler code than my silly piece I put togther
Chris
-
@cjthompson said:
Since you are exploding bottom to top, the top level definitions will change before they are exploded.
here is a method that will explode them top to bottom:
Yes, that's logical. It explodes Groups and Instances but does not decimate Definitions like I did. Thanks.
def miner(ents) ents.each do |e| if e.class==Sketchup;;Group or e.class==Sketchup;;ComponentInstance miner(e.explode) end end end
-
Is it guaranteed that groups/instances are a tree structure?
One cycle (A is a member of B; B is, directly or indirectly, a member of A) turns a recursive function into an infinite loop.
-
@martinrinehart said:
Is it guaranteed that groups/instances are a tree structure?
One cycle (A is a member of B; B is, directly or indirectly, a member of A) turns a recursive function into an infinite loop.
No - SU does not allow that.
-
this thread reminds me of this:
http://refactormycode.com/codes/2-ruby-simple-loop
with each consecutive code sample improving on the lastalthough the only improvement I could possibly add might be:
def miner(ents) for e in ents if e.class==Sketchup;;Group or e.class==Sketchup;;ComponentInstance miner(e.explode) end end end
Advertisement