Sunday, December 3, 2023
Home3D ModelingBizarre conduct of Sketchup::InputPoint#choose() methodology - Ruby API

Bizarre conduct of Sketchup::InputPoint#choose() methodology – Ruby API


FYI, the native Transfer device does this.


One factor your device doesn’t but do is about it’s cursor. While you do not do that the person’s system cursor is used and is probably not acceptable.

  def onSetCursor()
    UI.set_cursor(633)  # 633 is Choose cursor
  finish

With current Ruby variations warnings will likely be output if an object has not outlined @occasion variables earlier than accessing them. In older variations of Ruby nil could be returned when accessing uninitialized @variables. However that is not the case.

You might be accessing @edge and @vertex earlier than they’re initialized.

It’s best to initialize them in activate() … or in initialize() as nicely.
It’s at all times good to do that for “greatest practices” and readability.

  def activate
    @ip = Sketchup::InputPoint.new
    @ip1 = Sketchup::InputPoint.new
    @mode = 0
    @edge = nil
    @vertex = nil
  finish

It ought to really be …

view.mannequin.active_entities.transform_entities(tr, @vertex)

This would possibly be simplified to:

    @ip1.choose(view, x, y) # Right here is the one place to vary @ip1
    @vertex = @edge.vertices.discover  vertex.place == @ip1.place 

That is drawing the purple sq. on the ORIGIN when @ip1 has not but picked some extent.
This could draw solely in device @mode > 0 or @ip1,legitimate?

  def draw(view)
    @ip.draw(view)
    view.draw_points(@ip1.place, 10, 1, "purple") if @ip1.legitimate?
  finish

The identical for places the @ip1 place to the console in onMouseMove callback in order not to flood the console with ORIGIN coordinates because the mouse is moved earlier than any level is picked.

Additionally the @ip tooltip must be displayed …

  def onMouseMove(flags, x, y, view)
    @ip.choose(view, x, y)
    view.tooltip= @ip.tooltip

    if @vertex
      pt = @vertex.place
      vector = pt.vector_to(@ip.place)
      tr = Geom::Transformation.translation(vector)
      view.mannequin.active_entities.transform_entities(tr, @vertex)
    finish

    places "@ip1: #{@ip1.place}" if @ip1.legitimate?
    view.invalidate
  finish

You must also have a deactivate callback to clear the view when the device is completed.

  def deactivate(view)
    view.invalidate
  finish

Concerning an undo operation, the onLButtonDown callback must be like …

  def onLButtonDown(flags, x, y, view)
    if @mode == 0
      ph = view.pick_helper
      ph.do_pick(x, y)
      @edge = ph.picked_edge
      @mode = 1 if @edge
      return
    elsif @mode == 1
      @ip1.choose(view, x, y) # Right here is the one place to vary @ip1
      @vertex = @edge.vertices.discover 
        vertex.place == @ip1.place
      
      if @vertex
        @mode = 2
        view.mannequin.start_operation('Transfer Vertex')
      finish
    elsif @mode == 2
      # Cease transferring the vertex and reset the device:
      view.mannequin.commit_operation
      deactivate(view)
      activate()
    finish
  finish

… and an onCancel callback so the person can reset the device through ESC key …

  def onCancel(purpose, view)
    # Restore the vertex and reset the device:
    view.mannequin.abort_operation if @mode == 2
    deactivate(view)
    activate()
  finish

Right here is the entire modifications of the Software class.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments