Distance between 2 uvHelp points returning wrongly 0


#1

Hello, I’m using this code to rotate the material of my face:

call rotate_texture with a face and a string.

def rotate_texture(face, direction='vertical')
  return unless face.is_a?(Sketchup::Face)
  edge = face.edges.max_by(&:length)
  return unless edge.is_a?(Sketchup::Edge)

  distance = get_distance(face, edge)
  points = get_points(edge, distance, direction)

  face.position_material(face.material, points, true)
end

def get_points(edge, distance, direction)
  points = []
  points[0] = edge.start.position
  points[1] = Geom::Point3d.new(0, 0, 0)
  points[2] = edge.end.position

  if direction == 'vertical'
    points[3] = Geom::Point3d.new(0, distance, 0)
  else
    points[3] = Geom::Point3d.new(distance, 0, 0)
  end
  points
end

def get_distance(face, edge)
  tw = Sketchup.create_texture_writer
  uvHelp = face.get_UVHelper(true, true, tw)
  t = Geom::Transformation.translation [edge.length, 0, 0]

 ############ PROBLEM HEREEE ############
  p1 = uvHelp.get_front_UVQ(edge.start.position)
  p2 = uvHelp.get_front_UVQ(edge.start.position.transform(t))
  d = p1.distance(p2)     
end

But got the error: Error: #< ArgumentError: Could not compute valid matrix from points>

The problem is, in this part of code:

p1 = uvHelp.get_front_UVQ(edge.start.position)
p2 = uvHelp.get_front_UVQ(edge.start.position.transform(t))
d = p1.distance(p2)

d is 0.
The points matrix become:
(15mm, 432mm, 0mm)
(0mm, 0mm, 0mm)
(15mm, 432mm, 700mm)
(0mm, 0mm, 0mm)

The edge selected is the blue one:

I know this is the selected Edge because I put model.selection.add edge inside get_points methods
The inner face apply the paint, but the outer face doesn’t (It’s applied now because of another tests).

This problem doesn’t occurs with the faces of the front components, the code works fine for them.

am I missing something?

Thanks.

PS: This code is an adaptation of the @Guy 's code in this post: Is it possible to rotate textures using Ruby API? thank you by the way.


#2

I made some tests and if I change the:

t = Geom::Transformation.translation [edge.length, 0, 0]

to:

t = Geom::Transformation.translation [0, edge.length, 0]

the problem is solved for side components, but start to front components.
The edges of front edge’s component selected are the horizontal ones at bottom.


#3

Found the “problem”, when the edge is vertical the translation to x axis doens’t exists.
So, just verify if the edge is horizontal or vertical and do the translation about his orientation axis.