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

    [Question-difficulty 4/5] Group.transform!

    Scheduled Pinned Locked Moved Plugins
    3 Posts 2 Posters 311 Views 2 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.
    • R Offline
      rvs1977
      last edited by

      Hi all.

      This one I have messed around with, for more than a week now, and is at the point to give up 😳

      My task is to make a tool that rotate a group in the direction of the mousepointer. It has turned out to be a difficult task because, it doesn't seems to be a way of keeping track of the group direction.

      The problem using t= Geom::Transformation.rotation (point, Z_AXIS, delta_angle) and group.transform! t )is the delta_angle. Using this method it adds the angle to the previous angle.

      This is what I have tried: (runable from the RCE)

      
      class YV_tool
        def initialize
          @mod = Sketchup.active_model 
          @ent = @mod.entities    
          @state = 0
      
          inches = 1
          @mm = inches.to_mm
      
            
           @pts = []
           @pts[0] = [0,0,0]
           @pts[1] = [108/@mm,0,0]
           @pts[2] = [108/@mm,224/@mm,0]
           @pts[3] = [0,224/@mm,0]
        
        end # initialize
      
        def activate 
        end 
      
        def onMouseMove(flags, x, y, view) 
           ip = Sketchup;;InputPoint.new
           ip.pick view, x,y
           ip = ip.position 
      
           if (@state == 0) # Create Wall_group 
            @yv_group = @ent.add_group
            yv_face = @yv_group.entities.add_face @pts 
            yv_face.pushpull -10
            
            wall_edge = @yv_group.entities.add_line ([0,0,0], [1000/@mm,0,0])
            @yv_group_vector = wall_edge.line
             
            point = Geom;;Point3d.new ip 
            new_transform = Geom;;Transformation.new point  
            @yv_group.transformation = new_transform
            @state = 1
          end
          if (@state == 1) # group follow mousemove
            Sketchup.status_text = "@state = 1 ; Inputpoint; ", ip, "@yv_group_vector; ", @yv_group_vector  
            point = Geom;;Point3d.new ip 
            new_transform = Geom;;Transformation.new (point)
            @yv_group.transformation =  new_transform
          end
          if (@state == 2) # onLMouseButton place group in choosen position
            Sketchup.status_text = "@state = 2 ; Inputpoint; ", ip 
            point = Geom;;Point3d.new ip 
            new_transform = Geom;;Transformation.new (point)
            @yv_group.transformation =  new_transform
            @ip1 = ip
           
            @vec1 = Geom;;Vector3d.new X_AXIS
            @state = 3      
          end
          if (@state == 3) # Rotating and stretching
            vec2 = @ip1.vector_to (ip)
            v1 = @vec1.angle_between X_AXIS
            
            v2 = vec2.angle_between X_AXIS
            #Sketchup.status_text = "@state = 3 ; v1; ", v1, " v2; ", v2, " @vec1; ",  @vec1
              #1 calulate delta_v if mousemove is counterclockwice and if vec1 and vec2 is in quadrant 1 or 2 of the unitcircle  
              if ((v2>v1)  && ((vec2.x >= 0 && vec2.y > 0)  && (@vec1.x >= 0 && @vec1.y > 0)  || (vec2.x <=0 && vec2.y > 0) && (@vec1.x >= 0 && @vec1.y > 0 ) || (vec2.x <= 0 && vec2.y > 0) && (@vec1.x <= 0 && @vec1.y > 0)))
               delta_v = v2-v1
               #Sketchup.status_text = "@state = 3 ; ; v2>v1 ; Delta_v; ", delta_v 
              end
      
              #2 calulate delta_v if mousemove is counterclockwice and if vec1 and vec2 is in quadrant 3 or 4 of the unitcircle  
              if ((v1>v2)  && ((@vec1.x <= 0 && @vec1.y < 0)  && (vec2.x <= 0 && vec2.y < 0)  || (@vec1.x <=0 && @vec1.y < 0) && (vec2.x >= 0 && vec2.y < 0 ) || (@vec1.x >= 0 && @vec1.y < 0) && (vec2.x >= 0 && vec2.y < 0)))
               delta_v = v1-v2
               #Sketchup.status_text = "@state = 3 ; ; v1>v2 ; Delta_v; ", delta_v 
              end
            
              #3 calulate delta_v if mousemove is clockwice and if vec1 and vec2 is in quadrant 1 or 2 of the unitcircle
              if ((v1>v2)  && ((@vec1.x <= 0 && @vec1.y > 0)  && (vec2.x <= 0 && vec2.y > 0)  || (@vec1.x <=0 && @vec1.y > 0) && (vec2.x >= 0 && vec2.y > 0 ) || (@vec1.x >= 0 && @vec1.y > 0) && (vec2.x >= 0 && vec2.y > 0)))
               delta_v = v2-v1
               #Sketchup.status_text = "@state = 3 ; ; v1>v2 ; Delta_v; ", delta_v 
              end
      
              #4 calulate delta_v if mousemove is clockwice and if vec1 and vec2 is in quadrant 3 or 4 of the unitcircle 
              if ((v2>v1)  && ((vec2.x <= 0 && vec2.y < 0)  && (@vec1.x <= 0 && @vec1.y < 0)  || (vec2.x <=0 && vec2.y < 0) && (@vec1.x >= 0 && @vec1.y < 0 ) || (vec2.x >= 0 && vec2.y < 0) && (@vec1.x >= 0 && @vec1.y < 0)))
               delta_v = v1-v2
               #Sketchup.status_text = "@state = 3 ; ; v2>v1 ; Delta_v; ", delta_v 
              end
      
      
            @vec1 = vec2
              
            t_yv = Geom;;Transformation.rotation (@ip1, Z_AXIS, delta_v)
            @yv_group.transform! t_yv
              
           t_old = @yv_group.transformation
           Sketchup.status_text = "@state = 3 ; Transformation ", t_old
      
          end
        end # onMouseMove
        
        def onLButtonDown (flags, x, y, view)
          if (@state == 3)
            @state = 4
          end
      
          if (@state == 1)
            @state = 2
          end
        end # onLButtonDown
      
        def reset
          @state = 0
        end
      end # class YV_tool
       
           yvtool = YV_tool.new 
           Sketchup.active_model.select_tool yvtool
      
      
      

      As you can see, the group turn around, but not directly at the mousepointer.

      I hope someone can come up with some ideas. πŸ˜‰ Until then I will keep fighting...

      Thanks in advance
      -RVS


      Get a Ruby

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

        Rasmus

        Find my FreeRotate.rb script - readily available on this ' Plugins' forum.
        It has a method of rotating the selection [which is temporarily grouped] so that it follows the cursor as it moves - it does this by constantly transforming the group and swapping the current value for the last one. In it you must pick two points to set the initial axis... but you could use your pt0=group.entities.parent.insertion_point insertion point [transformed by the tr=group.transformation as pt0.transform!(tr)] and then another logical point - perhaps pt1=pt0.offset(Z_AXIS,1) - or perhaps pt1=pt0.offset((pt0.vector_to(eye)),1) [where eye=model.active_view.camera.eye] - but this all depends on what point you want to rotate around and what initial 'vector' you want to use to kick-off with...
        πŸ€“

        TIG

        1 Reply Last reply Reply Quote 0
        • R Offline
          rvs1977
          last edited by

          well thanks TIG! πŸ˜„

          I will study the script and hopefully get it πŸŽ‰


          Get a Ruby

          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