Quickly finding groups/comps in an Entity Collection?
-
@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
-
Or to compact it as much as I can see, into a one-liner
def xis(a);a.each{|e|xis(e.explode)if e.class==(Sketchup;;Group||Sketchup;;ComponentInstance)};end
"xis" == e
X
plodeI
nstanceS
Usage:xis(entities)
PS: a shorter way to do their puts 1 to 10 is this
n=0;p n+=1while n<10
-
I take a few extra lines for readability any day.
-
@thomthom said:
I take a few extra lines for readability any day.
True, but "squish-the-code" is a fun exercise and I often learn something new.
def xis(a);a.map{|e|xis(e.explode)if e.respond_to?('move!')};end
(assuming no one has added move! to any of the built-in classes!)
Advertisement