On Mouse Move method is not running



require 'sketchup.rb'

module Examples
  module ControlComponents

      def onMouseMove flags, x, y, view
            UI.messagebox("Mouse Moved")
      end

  end # module ControlComponents
end # module Examples

Why this code is not run?

Class: Sketchup::Tool

…
To create a new tool in Ruby, you must define a new class that implements the methods for the events that you want to respond to.
…

module Examples
  class ControlComponents

    def onMouseMove flags, x, y, view
      puts "Mouse Moved"
    end

  end # class ControlComponents
end # module Examples
2 Likes

BTW. I would not use UI.messagebox() even for testing in this method because of the every little movement of mouse you will get a modal message box…
I suggest to print it to the console like in the example of the #onMouseMove method.

2 Likes

Thank you very much @dezmo

1 Like

But how can I show the result of puts statement in case after installing my extension and trying it? @dezmo

This a basic thing that during development the Ruby Console is always open,… :innocent:
Open Ruby Console:
openconsole

2 Likes

Really I don’t know if I run the extension it will print in console, but I know this tool already.
Thank you very much @dezmo

Here after I run my extension, I need to determine the component to make an effect on it, but the extension is stopped.
ezgif.com-gif-maker (1)

What do you mean by “effect”? On which component? What does it mean “extension is stopped.” ?

Unfortunately I could not follow the animation, it is unreadable small for me…

I mean in ''effect": I read transformation array for all components that I determined it to change this information to keep it at some distance.
I mean in ''extension is stopped": when i click on component to move it , the extension has stopped

Without seeing your code and/or a proper screenshots/copy about the messages in Ruby Console, I can not tell so much more…

1 Like

Thank you @dezmo

module Examples
  module ControlComponent
    class ControlComponents
      def onMouseMove flags, x, y, view
        model = Sketchup.active_model
        puts "Model  #{model}"
        entities = model.selection.entries
        puts "Entities  #{entities}"
        componentNO = entities.grep(Sketchup::ComponentInstance).size
        instances = entities.grep(Sketchup::ComponentInstance)
        puts "componentNO  #{componentNO}"
        instances.each do |ins|

          transformationArray = ins.transformation
          t = transformationArray.to_a
          #str4x4.each do |coordinate|
          if componentNO > 0
            puts "X = #{t[12]} , Y = #{t[13]}"
            if(t[12] > (598/2.54))
              t[12] = (598/2.54)
            end
            if(t[13] > (398/2.54))
              t[13] = (398/2.54) 
            end
            if(t[12] < 0)
              t[12] = 0
            end
            if(t[13] < 0)
              t[13] = 0
            end
            ins.transformation = t
          end
        end
      end
    end
  end
end

This is my code, and I try to put the components in a special land.

I need to determine and move the component with the extension is running.

I see now.

Your “extension” (better to call it as a code snippet) above is a custom Tool.
You have defined a tool class, then - most probably (this is not apparent from your code) - you selected that tool by creating an instance of it and passing it to Model#select_tool. method.( or maybe you used the Tools#push_tool method )

In your code above there is no #onLButtonDown method exist, therefore I assume, it means that you have chosen another Tool, namely the native Select tool image.

This, of course, causes you to quit your Tool, as only one Tool can be active at a time. (actually there is a possibility to suspend and resume the tool, like e.g. the Orbit tool does)

In addition, to be able to move the selected component, you had to select the third (also built in) Move Tool image.


:thinking:.
Maybe It is possible to use Sketchup::ToolsObserver to react to tool events and other observers to determine the instance movements during using the native tools and “react accordingly”, but for sure only one Tool can be active a t a time.

Most probably the best if you are writing your own method into your own Tool to select and move component.


I really strongly recommend to study (and again study) the documentation of
Sketchup::Tool (There is a link there at the beginning of doc with other example)
the GitHub examples I mentioned above.
the entire SketchUp Ruby API documentation


One more thing:
The #onMouseMove method is called by SketchUp whenever the mouse is moved.
Try to make this method as efficient as possible because this method is called often. Writing "everything " into this method is bad practice.

3 Likes

Thank you very much @dezmo
I need to do that, finally :sweat_smile:

You are the best
ezgif.com-gif-maker (2)