Move vertex sometimes add_line fails silently

I am on sketchup make 2017. I am trying to move a vertex and reconnect all the edges and recreate the face. Usually it works. But occasionally i get nil as the return value for
new_edge = entities.add_line(point1, point2)

I cannot figure out why. The points are both valid they aren’t too close together. What causes add_line to fail and what can I do to force the creation of the line if it does? I tried add edge as well with the same result.

def self.move_vertex(old_position, new_position)
      entities = Sketchup.active_model.active_entities
      old_edges = []
      new_edge_pairs = []
      creation_errors = 0

      entities.find_all { |e| e.is_a?(Sketchup::Edge) }.each do |edge|
        next if edge.deleted?
        if Vector3.from_geom_point3d(edge.vertices[0].position) == old_position || Vector3.from_geom_point3d(edge.vertices[1].position) == old_position
          point1 = Geom::Point3d.new(edge.vertices.find { |v| Vector3.from_geom_point3d(v.position) != old_position }.position)
          point2 = new_position.to_geom_point3d
          new_edge_pairs << [point1, point2, edge.soft?, edge.smooth?]
          old_edges << edge
        end
      end

      old_edges.each do |old_edge|
        next if old_edge.deleted?
        old_edge.erase!
      end
      new_edges = []
      failed_edges = []
      new_edge_pairs.each do |point1, point2, soft, smooth|
        new_edge = entities.add_line(point1, point2)
        if new_edge
          new_edge.soft = soft
          new_edge.smooth = smooth
          new_edges << new_edge
        else
          creation_errors += 1
          failed_edges << [point1, point2]
        end
      end

      new_edges.each do |edge|
        next if edge == nil || edge.deleted?
        edge.find_faces
      end
      return creation_errors
    end

I don’t know why your points are failing, but I do know that you need not erase and recreate edges or vertex objects. They can be adjusted by applying a transformation.

See these:

You can transform any Entity subclass object if it is a member of an Entities collection, using either of the above methods. (Ie, just because there is an example in the first showing moving vertices doesn’t mean you are limited to that method. You can use the 2nd one if you’d rather create a Geom::Transformation object. Also, be aware that many API methods that want a transform, will accept an 3 element numeric array as a default transitional vector.)

1 Like

Checking your code the “points” (old_position, new_position) are doesn’t seems to be a Point3d objects rather a vectors, because there is a class methods you are using (and we do not know about what is it)…

…and a modified base class ( Vector3d) !? (and we do not know about how is it modified) :

So it is possible that he point1 , point2 you created, are in a same position… therefor the edge creation method will return nil.

__

You can check if the object is nil or not using
edge.nil? method, but here can be like:
next if !edge || edge.deleted?

__

more efficient to use:
entities.grep(Sketchup::Edge) do |edge|

1 Like