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

    [solved!!]onMouseMove show component instance

    Scheduled Pinned Locked Moved Developers' Forum
    32 Posts 9 Posters 3.5k Views 9 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.
    • TIGT Offline
      TIG Moderator
      last edited by

      I just suggested adding the instance at the ORIGIN as it was simple - you could add it onto the current cursor position.
      Your code is missing some important parts... like how the instance is added and referred to, and the closing 'end' to the 'onLButtonDown' method...
      The .transform! method should work...
      There is also a 'translation' transformation where you find the current instance.position and the vector to the current cursor.position and slide the instance along it BUT it should work as well as the 'transform!.........

      TIG

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

        @tig said:

        I just suggested adding the instance at the ORIGIN as it was simple - you could add it onto the current cursor position.
        Your code is missing some important parts... like how the instance is added and referred to, and the closing 'end' to the 'onLButtonDown' method...
        The .transform! method should work...
        There is also a 'translation' transformation where you find the current instance.position and the vector to the current cursor.position and slide the instance along it BUT it should work as well as the 'transform!.........

        I didn't post all the code sorry I thought it would be simpler if I just put those methods in.

        The things you referred to are in the code. the tree appears at the origin, then as I move my mouse it gets transformed but in some other direction, and farther and farther away quite rapidly...

        This is what I have been using to test it with:
        (I know I might be using class variables when I could be using instance...but that shouldn't be the problem)

        
        class AddTree1
        	@@model = Sketchup.active_model
        	@@ents = @@model.entities
        
        # Initialize the input point upon start
        def activate
         dl = @@model.definitions
         file_path = Sketchup.find_support_file "KorbinTest/components/Tree_2D_Schematic_Sparse_12_.skp", "Plugins"
         @pt1 = Sketchup;;InputPoint.new
         @@comp_def_tree1 = dl.load file_path
         @insert_tree_1 = @@ents.add_instance( @@comp_def_tree1, Geom;;Transformation.new)
         @clicked = false
        end
        
        
        def onMouseMove flags, x, y, view
         if @clicked == false
          @pt1.pick view, x, y
          @newpt = Geom;;Transformation.new( @pt1.position )
          @insert_tree_1.transform!( @newpt )
         end
        end
        
        def onLButtonDown flags, x, y, view
         @pt1.pick view, x, y
         @newpt = Geom;;Transformation.new( @pt1.position )
         @insert_tree_1.transform!( @newpt )
         @clicked = true
         reset
        end
        
        def reset
         @pt1.clear
        end
        
        def onCancel flags, view
         reset
        end
        	
        end
        
        Sketchup.active_model.select_tool AddTree1.new
        
        
        1 Reply Last reply Reply Quote 0
        • K Offline
          kdasilva
          last edited by

          hmmm i have been testing away and:

          (this is just the one relevant methods)

          
          	def onLButtonDown flags, x, y, view
          		@pt1.pick view, x, y
          		newpt = Geom;;Transformation.new( @pt1.position )
          		puts newpt.origin
          		@insert_tree_1.transform!( newpt )
          		reset
          	end
          
          

          (reset is just @pt1.clear)

          So from my testing I am pretty sure the pick is working because when I just had a new tree put down for every new click it went where it was supposed to. So I started testing the above code, so i could keep better track of what was going on then with the onMouseMove (when it was in onMouseMove it spazzed off really fast is some direction). From the above code I could tell that the first click I do (my tree starts at the origin) it successfully translates to the proper position, however each subsequent click something weird goes on.

          I clicked on the red axis, the tree went to the right spot, I then click on approximately the same distance down the green axis and the tree translated to from what I could tell the intersect of going straight out from the point on the red and the green (my math fails me off the top of my, inst that the the sum of the two points?)...some sort of math went on..

          I tried adding

          
          @insert_tree_1.transform!( [0,0,0] )
          
          

          right before the translation in hopes that it would reset the tree position but I got the same result. So i am lead to believe its a problem with my @pt1? is it perhaps not getting reset? (i use .clear after each click)......

          This is as far as my investigating could get me 😞

          If anyone can shed any light for me it would be appreciated.

          *edit
          Im pretty sure now that it is to do with the my instance variable and the translation.....from what I can tell it is adding its current location before it translates itself...thus why the first translation works because its adding 0,0,0. ....could I subtract the current location from the transformation to cancel this out?? I think I'm just using the method wrong...

          Cheers,
          Korbin

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            The code should move the instance with the cursor until you click then it stops.
            After that your code needs to add a new instance and start again.
            In initialize() set @instance=nil.
            When you add an instance set it to be @instance [within activate].
            In the mouse-move method add a test at the start like 'return nil if not @instance' - so nothing is done unless @instance is set...
            BUT when you click to place it you run reset which sets @instance=nil again, so it is then fixed where you clicked, reset also runs 'activate' again...
            Repeat adding another instance etc etc...

            TIG

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

              @tig said:

              The code should move the instance with the cursor until you click then it stops.

              Thank you for continuing to help, and I did implement what you suggested however I still have my main problem which is that, although the code does move the instance with my cursor (or with my clicks in the more controlled testing) it does not move to where the cursor is.....however the transform! method is working it is not simply moving the instance to my cursor...it is moving it in some other weird direction, which I beleive I found out somewhat why in my previous post...however I don't know how to make it not do that 😞

              cheers,
              Korbin

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                @kdasilva said:

                @tig said:

                The code should move the instance with the cursor until you click then it stops.

                Thank you for continuing to help, and I did implement what you suggested however I still have my main problem which is that, although the code does move the instance with my cursor (or with my clicks in the more controlled testing) it does not move to where the cursor is.....however the transform! method is working it is not simply moving the instance to my cursor...it is moving it in some other weird direction, which I beleive I found out somewhat why in my previous post...however I don't know how to make it not do that 😞
                cheers,
                Korbin
                For example - assuming the component-instance is added and referenced as @instance

                
                def onMouseMove(flags, x, y, view)
                  return nil if not @instance ### only works when instance is set
                  @ip.pick(view, x, y)
                  if @ip != @ip1
                    view.invalidate if( @ip.display? or @ip1.display? )
                    @ip1.copy!(@ip)
                    view.tooltip=@ip1.tooltip
                    @instance.transform!(Geom;;Transformation.new(@ip1.position))
                  end
                end
                
                

                Later

                
                def onLButtonDown(flags, x, y, view)
                  return nil if not @instance
                  @ip1.pick(view, x, y)
                  if @ip1.valid?
                    @instance.transform!(Geom;;Transformation.new(@ip1.position))
                    @instance=nil ### un-sets instance
                  end
                end
                

                Then reset @instance to another new instance... πŸ€“

                TIG

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

                  @tig said:

                  @kdasilva said:

                  @tig said:

                  The code should move the instance with the cursor until you click then it stops.

                  Thank you for continuing to help, and I did implement what you suggested however I still have my main problem which is that, although the code does move the instance with my cursor (or with my clicks in the more controlled testing) it does not move to where the cursor is.....however the transform! method is working it is not simply moving the instance to my cursor...it is moving it in some other weird direction, which I beleive I found out somewhat why in my previous post...however I don't know how to make it not do that 😞
                  cheers,
                  Korbin
                  For example - assuming the component-instance is added and referenced as @instance

                  
                  > def onMouseMove(flags, x, y, view)
                  >   return nil if not @instance ### only works when instance is set
                  >   @ip.pick(view, x, y)
                  >   if @ip != @ip1
                  >     view.invalidate if( @ip.display? or @ip1.display? )
                  >     @ip1.copy!(@ip)
                  >     view.tooltip=@ip1.tooltip
                  >     @instance.transform!(Geom;;Transformation.new(@ip1.position))
                  >   end
                  > end
                  > 
                  

                  Later

                  
                  > def onLButtonDown(flags, x, y, view)
                  >   return nil if not @instance
                  >   @ip1.pick(view, x, y)
                  >   if @ip1.valid?
                  >     @instance.transform!(Geom;;Transformation.new(@ip1.position))
                  >     @instance=nil ### un-sets instance
                  >   end
                  > end
                  

                  Then reset @instance to another new instance... πŸ€“

                  wow so i was definitely missing a few things...alas put your code in directly, changed the necessary variable names, ...but same result 😞

                  When I creep my mouse onto the screen, it will move to my mouse for the very first move...but any subsequent move sends the instance off rapidly in a another direction...is this just a problem for me??

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    Do you get any errors in the Ruby Console ?

                    TIG

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

                      no totally error free BUT..i just made it work by.....(drumroll)... changing .transform!() to .move!()

                      I don't know why it worked...i just did it on a whim, but presto!

                      thanks for all the help, this was driving me crazy...so much thesis to do, but I tend to obsess on things I cant solve.

                      cheers
                      Korbin

                      1 Reply Last reply Reply Quote 0
                      • TIGT Offline
                        TIG Moderator
                        last edited by

                        @kdasilva said:

                        no totally error free BUT..i just made it work by.....(drumroll)... changing .transform!() to .move!()

                        I don't know why it worked...i just did it on a whim, but presto!

                        thanks for all the help, this was driving me crazy...so much thesis to do, but I tend to obsess on things I cant solve.

                        cheers
                        Korbin

                        I was just going to suggest .move! instead of .transform! as it's 'outside of the undo stack'... [honest!]
                        Glad it's working at last... πŸ˜„

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • Chris FullmerC Offline
                          Chris Fullmer
                          last edited by

                          (I'm not sure that there is a question in this - its more of me just talking out loud).

                          I'm trying to do the same, and I'm getting the problem that once I attache the component to the cursor, then the ip point easily grabs onto the component when the mouse moves. And sine the component is closer to the camera, than the center of the component that is attached tot he cursor, then the ip jumps to the closer position, moving the whole component closer. And repeats. So every time I start move the mouse,t he component gets closer and closer to the camera until it clips out of view, and the mouse can reach geometry in the model to reset the ip back to the model.

                          I have not thought much about this yet, but I was wondering how you guys got it to work? It just doesn't seem to do the trick for me. I was really hoping it would be easy to place a component in the model, and have the component attach to the cursor.

                          Its not the end of the world if it turns out to not work, I don't truly need to show the component.

                          Lately you've been tan, suspicious for the winter.
                          All my Plugins I've written

                          1 Reply Last reply Reply Quote 0
                          • thomthomT Offline
                            thomthom
                            last edited by

                            Do you need a custom tool? Can you not use model.place_component ?

                            Thomas Thomassen β€” SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund

                            1 Reply Last reply Reply Quote 0
                            • Chris FullmerC Offline
                              Chris Fullmer
                              last edited by

                              I think I do - I need to know what edge in what group it was placed on so I can break the edge and insert new geometry. I can do all that easy enough if I do it with my own tool. But I'm scared of relying on too many observers. I've had bad luck with them already, and I know you (thom) have had many projects ruined by them.

                              So I think I need my own tool. Its working ok though, I'm actually drawing lines to the viewport that represent the component I'm placing. I can attach that to the mouse easy enough, and it looks pretty cool.

                              Lately you've been tan, suspicious for the winter.
                              All my Plugins I've written

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

                                Hmmm... would have been nice if the method returned a ref to the newly placed instance (instead of the model object itself*****.) Then you might have been able to easily get the instances origin point and go from there.

                                Just weird that a ref to the placed instance is the obvious preferred return for a method like this. Sometimes I just do not understand how the obvious can be so often overlooked.

                                ***** The API doc is in error when it says it returns nil.

                                I'm not here much anymore.

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

                                  Ok.. you could call:
                                  lastdefn = defn.instances.last
                                  after using model.place_component( defn, false )

                                  Wait... weirdness, the method returns immediately, BEFORE the instance is actually placed. (It spits out a redraw time to $stdout after the instance is placed.) The move component tool does not exit with a false 2nd arg, but remains in the move tool, and the user could inadvertently move the instance again.
                                  It would be nice if this method took a block to be run after the instance was placed.

                                  I'm not here much anymore.

                                  1 Reply Last reply Reply Quote 0
                                  • thomthomT Offline
                                    thomthom
                                    last edited by

                                    Agree 100% with you Dan.

                                    Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                    List of my plugins and link to the CookieWare fund

                                    1 Reply Last reply Reply Quote 0
                                    • F Offline
                                      fatihbarut
                                      last edited by

                                      I think eventhough the title has "Solved" the last solution is missing

                                      Here is one of them.

                                      
                                      model = Sketchup.active_model
                                      ent = model.entities
                                      sel = model.selection
                                      
                                      defn = Sketchup.active_model.definitions
                                      
                                      def onMouseMove(flags, x, y, view)
                                        return nil if not @instance ### only works when instance is set
                                        @ip.pick(view, x, y)
                                        if @ip != @ip1
                                          view.invalidate if( @ip.display? or @ip1.display? )
                                          @ip1.copy!(@ip)
                                          view.tooltip=@ip1.tooltip
                                          @instance.transform!(Geom;;Transformation.new(@ip1.position))
                                        end
                                      end
                                      
                                      def onLButtonDown(flags, x, y, view)
                                        return nil if not @instance
                                        @ip1.pick(view, x, y)
                                        if @ip1.valid?
                                          @instance.transform!(Geom;;Transformation.new(@ip1.position))
                                          @instance=nil ### un-sets instance
                                        end
                                      end
                                      
                                      lastdefn = defn[0] # ! I had just one definition in my component definition list therefor defn[0] worked well for me
                                      model.place_component( lastdefn , false )
                                      
                                      
                                      1 Reply Last reply Reply Quote 0
                                      • J Offline
                                        Jim
                                        last edited by

                                        As has been discovered already in this thread, you probably want to use @instance.move!(..) rather than .transform!(..) so as not to disturb the Undo stack.

                                        Another idea might be to use the opengl draw commands to draw a proxy object instead of actually moving an Instance with the mouse.

                                        Hi

                                        1 Reply Last reply Reply Quote 0
                                        • Chris FullmerC Offline
                                          Chris Fullmer
                                          last edited by

                                          @jim said:

                                          Another idea might be to use the opengl draw commands to draw a proxy object instead of actually moving an Instance with the mouse.

                                          That's what I ended up doing, which was disappointing to me, but still looks pretty cool.

                                          Lately you've been tan, suspicious for the winter.
                                          All my Plugins I've written

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

                                            I'm trying out several methods for a plugin-user to define a plane. Picking 3 points works but maybe more easy would be to use a component plane thats glues to the underlying objects.

                                            Using the code from this thread I can attach a component-plane to the mouse pointer.

                                            behavior = @plane_def.behavior behavior.snapto = SnapTo_Arbitrary behavior.is2d = true

                                            Adding this code makes the component-plane glue to objects but only outside the ruby code as it seems. The glue works if the component is picked from the component browser OR if the component is added with model.place_component componentdefinition, repeat. Then I would have to add an observer to have the code pick it up again.

                                            Is there any way around this?

                                            edit: changed 'listener' to 'observer'

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

                                            Advertisement