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