• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

How to make toolbar in method(def..end) has parameters

Scheduled Pinned Locked Moved Developers' Forum
9 Posts 3 Posters 1.6k Views 3 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.
  • C Offline
    CEIT81729
    last edited by 10 Oct 2015, 03:37

    Hello everyone:
    I want to know how to make toolbar in method(def..end) has parameters
    e.g.

    def slab(valueq)
    ....
    ....
    ....
    path = File.dirname(FILE) + "/image/"
    menu = UI.menu("Plugins").add_submenu("xx")
    toolbar = UI::Toolbar.new("xx")
    command = UI::Command.new("ss") {slab}
    command.small_icon = command.large_icon = path + "/slab.png"
    command.tooltip = command.status_bar_text = "s"
    menu.add_item command
    toolbar.add_item command
    toolbar.show

    Thank you for your answer!

    1 Reply Last reply Reply Quote 0
    • M Offline
      MSP_Greg
      last edited by 10 Oct 2015, 14:12

      Using instance variables

      def slab(var1, ..., varN)
      ...
      end
      
      command = UI;;Command.new("ss") { |@var1, ..., @varN| slab(@var1, ..., @varN) }
      

      Greg

      1 Reply Last reply Reply Quote 0
      • C Offline
        CEIT81729
        last edited by 11 Oct 2015, 00:50

        Sorry...Greg
        I try to use instance variables like this:

        def slab(valueq)
        ...
        end
        command = UI::Command.new("ss") { |@valueq| slab(@valueq) }

        but ruby editor shows the situation like this":

        cannot be an instance variable
        command = UI::Command.new("ss") { |@valueq| slab(@valueq) }

        and...if I change the code like this:

        def slab(valueq)
        ...
        end
        command = UI::Command.new("ss") { |valueq| slab(valueq) }
        command.small_icon = command.large_icon = path + "/slab.png"
        command.tooltip = command.status_bar_text = "s"
        menu.add_item command
        toolbar.add_item command
        toolbar.show

        ruby editor shows the situation like this":
        Error: #<TypeError: no implicit conversion of NameError into String>

        It seems to be...undefined local variable or method ‘menu’ and ‘toolbar’ for command...


        Situation1


        Situation2

        1 Reply Last reply Reply Quote 0
        • M Offline
          MSP_Greg
          last edited by 11 Oct 2015, 03:09

          Try the following (class variable just in case...)

          @@valueq = 1
          
          def slab(valueq)
            puts "valueq = #{valueq}"
          end
          
          # below is for testing
          command = UI;;Command.new("ss") { slab(@@valueq) ; @@valueq += 1 }
          # normal line is below, as before
          # command = UI;;Command.new("ss") { slab(@@valueq) }
          
          command.small_icon = command.large_icon = path + "/slab.png"
          command.tooltip = command.status_bar_text = "s"
          menu.add_item command
          toolbar.add_item command
          toolbar.show
          

          As you click the icon, you should see valueq increment each time.

          Note, inside of braces/blocks, the || are only used when the calling code has parameters to pass. Command.new doesn't do that. Methods like .each (and others) do...

          Greg

          1 Reply Last reply Reply Quote 0
          • C Offline
            CEIT81729
            last edited by 11 Oct 2015, 17:54

            Greg, I'm sorry because I tried it or not successful...
            then, I think in this way, use "class...end"

            the code like this:

            class SphereTool
            def slab(valueq)
            ...
            end
            end

            sphere_cmd = UI::Command.new("Sphere") {
            Sketchup.active_model.select_tool SphereTool.new
            }

            sphere_cmd.small_icon = "sphere_small.gif"
            sphere_cmd.large_icon = "sphere_large.gif"
            sphere_cmd.tooltip = "Create a sphere"

            sphere_toolbar = UI::Toolbar.new "Sphere"
            sphere_toolbar.add_item sphere_cmd
            sphere_toolbar.show

            but it shows...

            "Nil result (no result returned or run failed)"
            Error: #<NoMethodError: undefined method slab' for #<AS_RubyEditor::RubyEditor:0x00000007e1cad8>> (eval):76:in block (2 levels) in initialize'
            SketchUp:1:in `call'


            class...end

            1 Reply Last reply Reply Quote 0
            • TIGT Online
              TIG Moderator
              last edited by 11 Oct 2015, 18:28

              ...
              def self.slab(valueq=nil) ### what is 'valueq' ?
              ...
              
              OR PERHAPS
              
              def initialize(valueq=nil) ### run by 'class.new()'
                slab(valueq)
                ### OR move all of the slab code into here ??
              end
              

              AND remember to pass a ' valueq' to the tool, otherwise you'll get an error if it's nil ?

              TIG

              1 Reply Last reply Reply Quote 0
              • C Offline
                CEIT81729
                last edited by 12 Oct 2015, 03:11

                To TIG:
                "valueq" is the argument I entered through Webdialog
                by WebDialog (JavaScript pass value) to the ruby
                I try to packaged this slab code with Class or Method(def...end)
                and click the button to be able to run the code
                but it seems problems in pass value and convert argument

                1 Reply Last reply Reply Quote 0
                • TIGT Online
                  TIG Moderator
                  last edited by 12 Oct 2015, 11:19

                  @ceit81729 said:

                  To TIG:
                  "valueq" is the argument I entered through Webdialog
                  by WebDialog (JavaScript pass value) to the ruby
                  I try to packaged this slab code with Class or Method(def...end)
                  and click the button to be able to run the code
                  but it seems problems in pass value and convert argument
                  OK.
                  So do NOT include it as an argument for any methods.
                  Put the tool into the menu thus.
                  sphere_cmd = UI::Command.new("Sphere") { Sketchup.active_model.select_tool(SphereTool.new()) }
                  This will ' new' will initialize the tool.
                  Normally you include so variable defaulting etc in the def initialize() method.
                  As you have a tool the 'activate' [or 'reactivate'] methods will set up some variables and kick off the processes: perhaps opening a dialog to take in settings and then changing a @status variable's value so the mouse tools click in to place something etc...
                  If you are using a webdialog it needs to setup callback, which returns some value[s] to the Ruby-side.
                  Pass these into the callback {} block.
                  Values will be strings so you need to convert them to floats or lengths as appropriate.
                  Set these values to be an instance-variable like @valueq which can be read by all methods within the same class.
                  If you want to remember it across uses of the class then use @@valeq - you need to set up as a class-variable @@valueq=1.23 [or any logical default value!] made inside the class, but outside of any methods, so that it's created for future use as the class is initially defined.
                  This way next time the class is re-run the value is remembered from the previous time.
                  Then, after the @@/@valueq is set up by the callback, run the method slab() or self.slab() [depending on how you have coded it].
                  Within the 'slab' code use @/@@valueq and it'll be taken as the value the callback set - even though that was within a separate method.

                  There are several kinds of 'variables' - here let's assume you use the name 'valueq'

                  A plain-variable - valueq - which is used only in that def method - it's always lower-case and starts [a-z] and can only contain letters, numbers and an _

                  An instance-variable - @valueq - which is used/seen/set through a Module's methods and its value will persist across uses, or when used in a Class, but for one use - these variables can also be accessed from outside the Module etc if set up properly with code like attr_reader :valueq etc.

                  A class-variable - @@valueq - like an @var but its value persists across uses of a Class.

                  There is also a 'Constant' which you must initially set up within the Module/Class, but outside any methods - Valueq or more traditionally VALUEQ [and it always starts with an uppercase A-Z]. You should never change this later - although you can it will produce a disconcerting Ruby Console 'warning'. Note how Ruby/SketchUp use Constants for unchanging references like [ruby:16pk0se5]ORIGIN[/ruby:16pk0se5] and [ruby:16pk0se5]Z_AXIS[/ruby:16pk0se5]. It behaves like a read-only @@var...

                  And finally there's the 'global' variable - [ruby:16pk0se5]$valueq[/ruby:16pk0se5] - which is accessible to/by ALL Methods/Modules/Classes - even those you have not written - BUT you should NEVER use these - it's sure to one day clash with somebody else's poorly set up code !
                  Other variable types do everything you'll need...
                  I include them for information only.
                  Note how Ruby/SketchUp does use some Globals like [ruby:16pk0se5]$:[/ruby:16pk0se5] or [ruby:16pk0se5]$LOAD_PATH[/ruby:16pk0se5] etc to store globally accessible info, but you do NOT need to do so...

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • C Offline
                    CEIT81729
                    last edited by 21 Oct 2015, 07:32

                    Thanks for everybody help and advice 👍
                    The problem has been resolved 😄

                    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