[Code] (Iterating) Finding Definitions
-
[ Code ] (Iterating) Finding Definitions
# # An example prepared by Dan Rathbun. (2012) # # Version ; 0.3.0 # module Author ### <-- replace with your top level namespace module Util ### <-- change sub-module name to suit class << self # proxy class public def find_defintions(ents_array) defns = [] ents_array = ents_array.to_a unless ents_array.empty? for e in ents_array if e.is_a?(Sketchup;;ComponentInstance) defns << e.definition elsif e.is_a?(Sketchup;;Group) defns << e.entities.parent end end # for end # defns.uniq! return defns # end def find_nested_defintions(ents_array) defns_all = [] # the array to return ents_array = ents_array.to_a return defns_all if ents_array.empty? defns_chk = find_defintions(ents_array) until defns_chk.empty? defn = defns_chk.pop defns_all.include?(defn) ? next ; defns_all << defn defns_new = find_defintions(defn.entities.to_a) next if defns_new.empty? for d in defns_new unless defns_all.include?(d) || defns_chk.include?(d) defns_chk << d end end # for end # until # return defns_all # end def find_defintions_from_selection( all = false, sel = Sketchup.active_model.selection ) defns_all = [] # the array to return sel = sel.to_a return defns_all if sel.empty? if all find_nested_defintions(sel) else find_defintions(sel) end # return defns_all # end end # proxy class end # submodule end # toplevel module
Untested as of: 2012-08-25 : Use at own risk. No warranty.
-
REVISED: (v0.2.0)
-
condensed
find_nested_defintions()
a bit. -
replaced all
.each
iterators withfor
...in
loops for speed.
-
-
Shouldn't it be
return defns.uniq
which would always return a unique array of defns; whereasreturn defns.uniq**!**
returns a unique array of defns only IF there were duplicates in the original, BUT 'nil
' if there were no duplicates in the original... -
Yes it should, and now is. Updated the code.
(I was
uniq!
'ing in the calling methods, and at the last moment replaced several in those methods, by adding the.uniq!
to thereturn
statement. I did think about which uniq to use, for a split-second, and didn't want to create a new array object, so I added!
to the end. That's what happens when ya' rush.)Thanx for the check TIG!
Advertisement