Removing rectangle diagonal


#1

Originally, I was looking for a way of removing the diagonal that is essentially created when moving face vertices to a given plane. I have realised that erasing the line works without destroying the face in which it lies. However, in arriving at this solution I initially tried adding a line between a point on the diagonal and a vertex which did not lie on the diagonal and then moving the internal node to the vertex dragging the attached lines with it. This appeared to work fine until I repeated the process on the same object and I therefor decided to take a closer look at what was going on. My conclusion is that the vertex/face association of the final vertex to which the node is moving is incorrect and thus causes problems when further actions are attempted on the entity.
Below is a test file that shows the problem.

require 'sketchup.rb'
class TTest1
  
  def initialize(*args)
   depth = 1000.mm
   width = 500.mm
   model = Sketchup.active_model
   ents = model.entities
   pts = []
   pts[0] = [0, 0, 0]
   pts[1] = [width, 0, 0]
   pts[2] = [width, depth, 0]
   pts[3] = [0, depth, 0]
   face = ents.add_face pts
   line = ents.add_line [pts[0], pts[2]]
   eraseCommonEdge(ents, line, pts[1])
   model.commit_operation
  end

  def commonEdgeNode(edge1, edge2)
    if (edge1.start == edge2.start) || (edge1.start == edge2.end)
      return edge1.start
    elsif (edge1.end == edge2.start) || (edge1.end == edge2.end)
      return edge1.end
    else
      return nil
    end
  end

  def eraseCommonEdge(ents, line, cornerNotOnLine)
    ReportEdgeFaces("1.line",line)
    new_edge = line.split(0.5)
    ReportEdgeFaces("1.new_edge",new_edge)
    vertex = commonEdgeNode(new_edge, line)
    ReportVertexFaces("1.vertex", vertex)
    if vertex != nil
      added_line = ents.add_line(vertex.position, cornerNotOnLine)
      ReportEdgeFaces("added_line",added_line)
      ReportEdgeFaces("2.line",line)
      ReportEdgeFaces("2.new_edge",new_edge)
      p = vertex.position
      move = p.vector_to(cornerNotOnLine)
      transm = Geom::Transformation.translation(move)
      ents.transform_entities(transm, vertex)      # move internal common node to cornerNotOnLine
      ReportVertexFaces("2.vertex", vertex)
    end 
  end  

  def ReportEdgeFaces(strg, edge)
    faces = []
    edge.faces.each{|face| faces << face.entityID}
    print(strg+".faces= ", faces, "\n")
    i = 0
    edge.vertices.each{|vertex| ReportVertexFaces("  vertex["+i.to_s+"]", vertex, true); i +=1}
    puts
  end

  def ReportVertexFaces(strg, vertex, suppressCR=false)
    ReportVertex(strg, vertex, nil, true, true)
    faces = []
    vertex.faces.each{|face| faces << face.entityID}
    print(" :faces= ", faces, "\n")
    puts if !suppressCR
  end

  def ReportVertex strgIn, p, trans=nil, metricOutput=true, suppressCR=false
    if suppressCR
      cR = "" 
      eq = ""
    else
      cR = "\n"
      eq = "="
    end
    if p == nil
      print @Trace.lead, strgIn, "= nil", cR
    else 
      # for some reason Vertex output does not reflect metric environment (but Point3d does)
      if trans == nil
        trans = Geom::Transformation.new  # identity transform
      end
      pt = p.position.transform!(trans)
      if metricOutput
        print(strgIn, eq, "(",pt.x.to_mm.round(2),"mm, ", pt.y.to_mm.round(2),"mm, ",pt.z.to_mm.round(2),"mm)", cR)
      else
        print(strgIn, eq, "(",pt.x.round(2),", ", pt.y.round(2),", ",pt.z.round(2),")", cR)
      end
    end
  end

end
#-----------------------------------------------------------------------------

if( not file_loaded?("test1.rb") )
    UI.menu("Draw").add_item("Test1"){TTest1.new}
    file_loaded("test1.rb")
end

Although the face ID will probably be different for another session the problem can be appreciated form the follwing ruby console output

1.line.faces= [6389, 6399]
vertex[0](0.0mm, 0.0mm, 0.0mm) :faces= [6389, 6399]
vertex[1](500.0mm, 1000.0mm, 0.0mm) :faces= [6389, 6399]

1.new_edge.faces= [6389, 6399]
vertex[0](250.0mm, 500.0mm, 0.0mm) :faces= [6389, 6399]
vertex[1](500.0mm, 1000.0mm, 0.0mm) :faces= [6389, 6399]

1.vertex(250.0mm, 500.0mm, 0.0mm) :faces= [6389, 6399]

added_line.faces= [6389, 6408]
vertex[0](250.0mm, 500.0mm, 0.0mm) :faces= [6389, 6399, 6408]
vertex[1](500.0mm, 0.0mm, 0.0mm) :faces= [6389, 6408]

2.line.faces= [6389, 6399]
vertex[0](0.0mm, 0.0mm, 0.0mm) :faces= [6389, 6399]
vertex[1](250.0mm, 500.0mm, 0.0mm) :faces= [6389, 6399, 6408]

2.new_edge.faces= [6408, 6399]
vertex[0](250.0mm, 500.0mm, 0.0mm) :faces= [6389, 6399, 6408]
vertex[1](500.0mm, 1000.0mm, 0.0mm) :faces= [6408, 6399]

2.vertex(500.0mm, 0.0mm, 0.0mm) :faces= [6389, 6410, 6408]

You will see that the final vertex thinks that it is at the junction of 3 faces but, in fact there is only 1. I believe that the face ID that remains should be 6399. 6389 and 6408 should have been deleted and I don’t have any idea from where 6410 has come.

I think that this is worth flagging up.