sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    [Code] User::ToolbarSet collection

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 1 Posters 303 Views 1 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.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by Dan Rathbun

      [ Code ] User::ToolbarSet collection

      An example of a universal toolbar set collection.
      (I keep these scripts in a "User" sub-dir of the "Plugins" dir.)

      #  User_ToolbarSet.rb
      #
      #  VERSION ; 2.0.1
      #
      #  An example by; Dan Rathbun
      #
      #  Maintain a Hash of custom Ruby Toolbar object references,
      #  and access them by using their name as the hash key;
      #  Example;
      #  User;;ToolbarSet["Shadow Strings Fix"].show()
      #
      #  You can add new references into the Hash when you create toolbar;
      #  Ex;
      #  tb = UI;;Toolbar.new("Nifty Tools")
      #  User;;ToolbarSet[tb.name]= tb
      #
      #  OR;
      #  caption = "Nifty Tools"
      #  User;;ToolbarSet[caption]= UI;;Toolbar.new(caption)
      #
      #  You can call refresh() anytime to be sure the hash is up to date;
      #  Ex;
      #  User;;ToolbarSet.refresh()
      #
      
      module User
      
        ### Hash to hold references to Ruby Toolbar objects;
        #
        ToolbarSet = Hash.new {|this,key|
          # block of code that gets called if the [key] method
          # fails to find a member with the given key argument;
          this.refresh()
          if this.has_key?(key)
            this[key] # return the new toolbar reference
          else
            msg = "Unknown Key '#{key.to_s}' in User;;ToolbarSet hash!"
            puts('ERROR; '<<msg) if $VERBOSE
            raise(IndexError,msg,caller)
          end
        }
      
        ### Extend the ToolbarSet hash with a refresh method;
        #
        def ToolbarSet.refresh()
          added = 0
          count = ObjectSpace.each_object(UI;;Toolbar){|tb|
            next if ToolbarSet.has_key?(tb.name) && ToolbarSet[tb.name].is_a?(UI;;Toolbar)
            # Otherwise, add it's reference to the hash;
            ToolbarSet[tb.name]= tb
            added += 1
          }
          if $VERBOSE # normally false (can be nil for silent mode)
            if added > 0
              plural = added>1 ? "s" ; ""
              puts("#{Module.nesting[0].name};;ToolbarSet#refresh(); #{added} new toolbar#{plural} added.")
            else
              puts("#{Module.nesting[0].name};;ToolbarSet#refresh(); No new toolbars.")
            end
            puts("  total UI;;Toolbar objects; #{count}")
          end # if $VERBOSE
        end
      
        ### initially load the ToolbarSet hash by calling refresh now;
        #
        ToolbarSet.refresh()
      
      
      end # module User
      

      VERSION : 2.0.0 - updated as explained in the 2nd post below.
      VERSION : 2.0.1 - fixed refresh() method next statement, to avoid vicious loop
      πŸ’­

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Perhaps we can modify the User::ToolbarSet hash's [] method's error Proc, so that it refreshes the hash automatically.

        Instead of line 28:
        ToolbarSet = {}
        which creates a hash that returns nil if the [] method does not find a member with the given key ...

        We replace line 28 with a call using the block form of the Hash::new() class constructor.
        NOW.. whenever the [] method does not find a member with the given key, the block is run:

        ToolbarSet = Hash.new {|this,key|
          this.refresh()
          if this.has_key?(key)
            this[key] # return the new ref
          else
            msg = "Unknown Key '#{key.to_s}' in User;;ToolbarSet hash!"
            puts('ERROR; '<<msg) if $VERBOSE
            raise(IndexError,msg,caller)
          end
        }
        

        πŸ’­

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          UPDATED code in 1st post to v2.0.0, per 2nd post above.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            UPDATED code in 1st post to v2.0.1, in refresh() method's next statement to avoid a vicious loop. (We use Hash#has_key?(key) rather than Hash#[key]!=nil, because our v2.0.0 update makes the [] method call the refresh method, so we must be very careful from within the refresh method that any [] method call does not cause a unwanted or unneeded, call to refresh.)

            I'm not here much anymore.

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

            Advertisement