Locking to an Angle (not an Axis)

I’ve been looking at my “Draw Wall Tool” and I really like how the tool is now able to lock onto the X and Y axis. It would be convenient if I could also somehow have it lock to the 45 deg. line or the 135 deg. line.

I’m going to play around with it tonight but I thought I might put the question out there in case someone has done this before and prevent me from wasting a lot of time on reinventing the wheel.

(As you will see in my code I’m at beginning of learning Ruby…I don’t really understand everything in it yet, and sorry if it looks a little unclear/garbage) …
…but I’ve got some idea:
In my PomPi tool (PomPi 2016 (Pompous Piping) | SketchUcation) I have been implemented a lock on perpendicular plane. (You can check the Dezmo_pompi2016_line_auto.rb) I’m using the down arrow key to switch the lock on and off.

The quickly created code below I’m assuming the @ip1 is the first point of your wall and @ip2 the second, you can project the @ip2 to line of axes (or angled line) and use that “@lock_point” instead of the inputpoint, if you have a locked status. The locked status will be periodically changing to the next angle or to "off " by pressing the down key.

Something like this:
(I did not tested this code, and it is only a kind of idea… may need to take into consideration some transformation, syntax correction etc. )

def onMouseMove(flags, x, y, view)
  @ip2.pick view,x,y,@ip1
  if @lock_count==0
    @lock_point = @ip2.position #no lock
   else 
    vec=Geom::Vector3d.new(1,0,0) if @lock_cuont==1 #x axis
    vec=Geom::Vector3d.new(1,1,0) if @lock_cuont==2 #45
    vec=Geom::Vector3d.new[0,1,0) if @lock_cuont==3 #y axis
    vec=Geom::Vector3d.new(-1,1,0) if @lock_cuont==4 #135
    point=@ip1.position.offset(vec)
    line=[@ip1.position,point]
    @lock_point = @ip2.position.project_to_line(line)
  end
end

def onKeyDown(key, repeat, flags, view)
  if( key == VK_DOWN  )
    @lock_count +=1 #0: no lock; 1:0degrees ; 2:45; 3:90; 4: 135
    @lock_cuont=0 if @lock_cuont>4 
  end	
end

def draw(view)
  view.draw_line(@ip1.position,@lock_point)
  view.draw_text(view.screen_coords(@ip2.position), "locked X")  if  @lock_cuont==1
  view.draw_text(view.screen_coords(@ip2.position), "locked 45deg")  if  @lock_cuont==2
  .
 .
end

I hope it will work… (if nothing else it can be used as a bad example :slight_smile: )

1 Like

You can temporarily change the angle snapping in the Units hash (model.options).

Or temporarily change the drawing axis by transforming them with a rotational transform. (v16+)

1 Like

I’m trying out elements of this code and I’ve almost got it working but for some reason it requires two clicks of the alt key. The first click locks up the cursor entirely. Other than that it is working as expected.

Good to hear…
Because of the alt key “basically” locking the interface, try other key! (?)

1 Like

I just thought of that literally a few seconds before I read your post.

1 Like

BTW. I have been tried recently the method, what @DanRathbun offering:

Sketchup.active_model.options["UnitsOptions"]["AngleSnapEnabled"]=true
Sketchup.active_model.options["UnitsOptions"]["AngleSnapEnabled"]=false
Sketchup.active_model.options["UnitsOptions"]["SnapAngle"]=44 #eg.

… but unfortunately it does not influence anything… the cursor is not “jumping” . Did I missed something? I think it is only works in the native rotate tool.
(The “LengthSnapEnabled” works better.)

1 Like

A little more involved than I originally thought but I think I’ve finally got it to work.

Good to hear! :slight_smile:
Good look with you tool!

Um… yea I guess so.

When writing a custom tool, the coder would have to write it in a way that honors the user’s snap angle setting.

Until now I did not find that “way”… but I will not give up! :grinning:

I just accidentally discovered some undocumented feature. If you return true in onKeyDown it will prevent the original behaviour of alt key. Something like this:

def onKeyDown(key, repeat, flags, view)
  #other
  #
  if( key == ALT_MODIFIER_KEY)
    #something
    #something else
    return true # prevent ALT from focusing the menu
  end
  #any other
  #
end

It may help in the future… :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.