• Login
sketchucation logo sketchucation
  • Login
ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

[Code] (Iterating) Finding Definitions

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 2 Posters 614 Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    Dan Rathbun
    last edited by Dan Rathbun 25 Aug 2012, 19:55

    [ 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.

    💭

    I'm not here much anymore.

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 25 Aug 2012, 21:04

      REVISED: (v0.2.0)

      • condensed find_nested_defintions() a bit.

      • replaced all .each iterators with for ... in loops for speed.

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 25 Aug 2012, 21:25

        Shouldn't it be return defns.uniq which would always return a unique array of defns; whereas return 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...

        TIG

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 25 Aug 2012, 22:35

          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 the return 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!

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          1 / 1
          • First post
            1/4
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement