sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Getting cursor position while SP simulation is running.

    Scheduled Pinned Locked Moved SketchyPhysics
    7 Posts 3 Posters 1.0k 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.
    • A Offline
      Anton_S
      last edited by

      I post this because of November92's question: http://sketchup.google.com/3dwarehouse/details?mid=c366d4f6b95601de846af18c94f69f1a&prevstart=0

      My Scirpt in previous model is old and is not working now because of different LS.
      November92 basicly wants to get the cursor position while simulation is running, so the script is here.

      Note:
      This script only works in SU8. For some reason SU7 crashes when click play.
      If anybody could make this script work in SU7 too, please post and say what is wrong in the script or what is need to be added or changed.

      Anyway, copy this script to Scripted file:

      onstart{
       #### Variables ####
       $cursor_pos=[0,0]
       
       #### Loading File ####
       if defined?($tempfile_loaded)==nil then #prevent from loading more than once
        directory=Object;;Dir.pwd.split(";/")[0]+";/"
        tempfile=Object;;File.new(directory+"SUtempfile.rb","w+")
        tempfile.puts('module MSketchyPhysics3
       class SketchyPhysicsClient
        def onMouseMove(flags,x,y,view)
         $cursor_pos=[x,y] #Here is our variable
         @mouseX=x
         @mouseY=y    
         if @CursorMagnetBody==nil then return() end
         ip=Sketchup;;InputPoint.new
         ip.pick(view,x,y)
         if ip.valid? then
          if @pickedBody!=nil then
           @simulationContext.doOnMouse(;drag,@pickedBody,x,y) 
           if getKeyState(VK_LSHIFT) then
            #project the input point on a plane described by our normal and center.
            ep=Geom.intersect_line_plane([view.camera.eye,ip.position],[@attachWorldLocation,view.camera.zaxis])
            @attachWorldLocation=ep
            pos=ep
           else
            ep=Geom.intersect_line_plane([view.camera.eye,ip.position], [@attachWorldLocation,Z_AXIS])
            pos=ep
            @attachWorldLocation=ep
           end
           @magnetLocation=pos
           if @CursorMagnet!=nil then NewtonServer.magnetMove(@CursorMagnet,pos.to_a.pack("f*")) end
          end
         end
        end #OnMouseMove
       end #class SketchyPhysicsClient
      end')
        tempfile.close
        load(directory+"SUtempfile.rb")
        Object;;File.delete(directory+"SUtempfile.rb")
        $tempfile_loaded=true
       end
       
       #### Functions ####
       def getCursor(state=0)
        view=Sketchup.active_model.active_view #Shortcut
        case state
         when 0, "coordinates";
          return($cursor_pos) #return's cursor coordinates
         when 1, "position";
          return(view.inputpoint($cursor_pos[0],$cursor_pos[1]).position.to_a) #returns 3d position
         when 2, "best_picked";
          return(view.pick_helper($cursor_pos[0],$cursor_pos[1]).best_picked) #returns entity
        end
       end
      }
      

      Now by calling function getCursor(state) you can get the cursor position and some other.
      Example:

      ontick{
       Sketchup.active_model.entities[1].text=getCursor(1).inspect
      }
      

      Other:
      Writing my scripts I always began using notepad++.
      If anybody need it go here, it's free: http://notepad-plus-plus.org/release/5.9.2

      1 Reply Last reply Reply Quote 0
      • M Offline
        MrDailyBlah
        last edited by

        i thought u can just use MSketchyPhysics3::getCursor(), and put an array number thingy after it?

        
        curpos=MSketchyPhysics3;;getCursor()
        mousex=curpos[0] #get x coordinate
        mousey=curpos[1] #get y coordinate
        
        
        1 Reply Last reply Reply Quote 0
        • A Offline
          Anton_S
          last edited by

          @mrdailyblah said:

          i thought u can just use MSketchyPhysics3::getCursor(), and put an array number thingy after it?

          
          > curpos=MSketchyPhysics3;;getCursor()
          > mousex=curpos[0] #get x coordinate
          > mousey=curpos[1] #get y coordinate
          > 
          

          MSketchyPhysics3::getCursor() returns cursor position relative to the whole screen, but the other one in sketchup tool, return cursor position relative to view. In most sketchup plugins, we need cursor position relative to view, not relative to screen.

          1 Reply Last reply Reply Quote 0
          • M Offline
            MrDailyBlah
            last edited by

            ah yeah...
            but can't u use a formula to work it out? i mean use things like view.center() or view.corner(), and use the coordinates from MSketchyPhysics3::getCursor() relative to them? just a thought... πŸ’­

            
            curpos=MSketchyPhysics3;;getCursor()
            mouseX=curpos[0]-view.corner(0)[0]
            mouseY=curpos[1]-view.corner(0)[1]
            mouseX=nil if curpos[0]>(view.corner(3)[0]) #constrains mouseX coordinate to right size of view
            mouseY=nil if curpos[1]>(view.corner(3)[1])#constrains mouseY coordinate to bottom size of view
            

            would that work?.. ❓ πŸ€“

            1 Reply Last reply Reply Quote 0
            • A Offline
              Anton_S
              last edited by

              No, you can't. view.center, and view.corner[n] also returns cursor position relative to view. If I would write view.corner(0) (the upper left corner), it would always return [0,0], so the function is kind of not useful...

              Well, though, there is probably another way πŸ’­ Using win32-api to locate sketchup view and get its position. It will only work on windows and for mac ox x, I would have to use its library. And yeah I, still don't have good knowledge of win32-api functions, so it'ill most likely take a while before there would actually be a good script. Plus rightnow I only peaked at mac os x library, so yeah, need time to benefit the script...

              1 Reply Last reply Reply Quote 0
              • M Offline
                mptak
                last edited by

                Can anyone tell me how to get a list of all of the methods attached to
                MSketchyPhysic3::
                like
                MSketchyPhysics3::getCursor()

                Thanks(:))

                1 Reply Last reply Reply Quote 0
                • A Offline
                  Anton_S
                  last edited by

                  To get all module methods:
                  MSketchyPhysics3.methods MSketchyPhysics3.private_methods MSketchyPhysics3.instance_methods MSketchyPhysics3.private_instance_methods

                  You can examine the sketchy physics files to see those methods and here is the source to module: http://www.ruby-doc.org/core-1.9.3/Module.html

                  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