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

    From AutoLisp to Ruby

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 5 Posters 1.9k Views 5 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
      driven
      last edited by

      expanding on Sam's example, if you want something more flexible...

      module SaM # namespace it so it doesn't clash
      class Pick_Points
        def initialize(n)
          @ipt = Sketchup;;InputPoint.new
          @@pts = []
          @count = n
          @tally = 0
        end
        def onLButtonDown(flags,x,y,view)
           
            @ipt.pick view,x,y
            @@pts << @ipt.position.to_a
            p @tally  += 1
          if @@pts.length == @count
            p 'done'
            Sketchup.active_model.select_tool(nil)
          end
        end
      end
      end
      

      and run it from your code with..

      
      n = 5 # any number
      Sketchup.active_model.select_tool SaM;;Pick_Points.new(n)
      
      

      then retrieve it's values with...

      SaM;;Pick_Points.class_variable_get(;@@pts)
      

      john

      learn from the mistakes of others, you may not live long enough to make them all yourself...

      1 Reply Last reply Reply Quote 0
      • D Offline
        Deanby7
        last edited by

        Many thanks to both of you

        1 Reply Last reply Reply Quote 0
        • D Offline
          Deanby7
          last edited by

          I think I am not understanding something as I'm getting an error....
          Error: #<NoMethodError: private method `class_variable_get' called for SaM::Pick_Points:Class>

          This is my test code.....

          require('sketchup.rb')
          def pick
          n = 2
          Sketchup.active_model.select_tool SaM::Pick_Points.new(n)
          SaM::Pick_Points.class_variable_get(:@@pts)
          puts @@pts[0]
          puts @@pts[1]

          end

          module SaM # namespace it so it doesn't clash
          class Pick_Points
          def initialize(n)
          @ipt = Sketchup::InputPoint.new
          @@pts = []
          @count = n
          @tally = 0
          end
          def onLButtonDown(flags,x,y,view)

            @ipt.pick view,x,y
            @@pts << @ipt.position.to_a
            p @tally  += 1
          if @@pts.length == @count
            p 'done'
            Sketchup.active_model.select_tool(nil)
          end
          

          end
          end
          end

          if (not file_loaded?("dbpick.rb"))
          UI.menu("Plugins").add_item("dbpick") { pick }
          end
          file_loaded("dbpick.rb")

          1 Reply Last reply Reply Quote 0
          • D Offline
            Deanby7
            last edited by

            Is the code to have the pick points snapping to geometry and inferencing involve a lot more code?

            1 Reply Last reply Reply Quote 0
            • D Offline
              driven
              last edited by

              you have a few things going wrong there and it's easier for me to add some note to one that works...

              the main one is you need to get your own copy of the class variable and ruby doesn't wait when you call the tool, so you have to...

              this one adds a timer, and adds your method into the same namespace as the class...

              
              module BbN # namespace it so it doesn't clash
                extend self
              
                # avoid common names for methods
              	def bd_pick
              	n = 5
              	# call the tool
              	Sketchup.active_model.select_tool BbN;;Pick_Points.new(n)
              	# get it's id 
              	tool_id = Sketchup.active_model.tools.active_tool_id
              	#create a timer and use the id to know if it's finnished
              	wait_for_tool = UI.start_timer(1.0, true) do
              		if Sketchup.active_model.tools.active_tool_id == tool_id
              		else 
              		  # this is the 'pop' back, we retrieve the values from the @@var
              			result = (BbN;;Pick_Points.class_variable_get(;@@pts))
              			# I prefer p over puts as it shows a usable array
              			p result[0]
              			p result[1]
              			# clean up
              			UI.stop_timer(wait_for_tool)
              		end
              	end
              	end
              
              	class Pick_Points
              		def initialize(n)
              			@ipt = Sketchup;;InputPoint.new
              			@@pts = []
              			@count = n
              			@tally = 0
              		end
              
              		def onLButtonDown(flags,x,y,view)
              			@ipt.pick view,x,y
              			@@pts << @ipt.position.to_a
              			p @tally += 1
              			if @@pts.length == @count
              				p 'done'
              				Sketchup.active_model.select_tool(nil)
              			end
              		end
              	end
              	# add menu , toolbar or shortcut key cmd
              	unless  file_loaded?("dbpick.rb")
              	  cmd = UI;;Command.new(BbN;;bd_pick)
                  UI.menu("Plugins").add_item("dbpick") {cmd}
                end
              end
              
              

              also, use code blocks in the forum, it's in the toolbar...

              john

              learn from the mistakes of others, you may not live long enough to make them all yourself...

              1 Reply Last reply Reply Quote 0
              • D Offline
                Deanby7
                last edited by

                Sorry John
                With the above code I'm getting......

                Error Loading File dbpick.rb
                Unrecognized command id

                I'm running Version 8 Sketchup.
                Dean

                1 Reply Last reply Reply Quote 0
                • G Offline
                  Garry K
                  last edited by

                  Dean - why don't you study linetool.rb
                  It comes with sketchup 8 and is in your plugins folder.

                  linetool.rb gives a pretty good example of what is possible. It is missing a few features that you might want such as setting up a tool bar.

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    driven
                    last edited by

                    @deanby7 said:

                    ...I'm running Version 8 Sketchup

                    Sketchup.active_model.tools.active_tool_id has been around since v6?

                    try it in Ruby Console after a tool change, does it fail there?

                    but, I agree with Garry on linetool.rb, I made a few tools based on it...

                    john

                    learn from the mistakes of others, you may not live long enough to make them all yourself...

                    1 Reply Last reply Reply Quote 0
                    • D Offline
                      Deanby7
                      last edited by

                      The menu item does not appear in the plugin drop down, but the script is obviously running because the ruby console is repeating an error message! I never thought picking two points on the screen could be so difficult!!

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        driven
                        last edited by

                        Sam's was the basic that you should get working first...

                        mine was an elaboration to show passing the the pts to something else...

                        have a look at the query tool in the examples folder as well...

                        john

                        learn from the mistakes of others, you may not live long enough to make them all yourself...

                        1 Reply Last reply Reply Quote 0
                        • D Offline
                          Deanby7
                          last edited by

                          I now have the two pick points working from sdmitch's code. I will study the linetool.rb script to fathom getting the cursor to snap to geometry! Something tells me this will be easy task! 😕

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

                            @deanby7 said:

                            I'm running Version 8 Sketchup.

                            ... which runs Ruby v1.8.6-p287 on PC, and v1.8.5-p0 on OSX.

                            @deanby7 said:

                            I'm getting an error....
                            Error: #<NoMethodError: private methodclass_variable_get' called for SaM::Pick_Points:Class>`

                            For some reason Ruby 1.8 had made class_variable_get() and class_variable_set() to be private methods.

                            In SketchUp v14+ using Ruby 2.0+, they were changed to be public methods.

                            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