Smoothing edges when creating faces in a loop

Testing for when to soften (and not) becomes easier if you use a Range for columns and rows.

Here is an example of using Range#first and Range#last comparisons as the argument to Edge#soft=

  • ASIDE: I suppose since you know the dimensions of the grid, (ie: the values of first and last,) you can do the same test without ranges.

  • EDITED: to use a, b, c and d for the point references. Now also smooths non-boundary edges.

def calc_points(col, row)
  [
    Geom::Point3d.new(col, row, 0)
    Geom::Point3d.new(col + 1, row, 0)
    Geom::Point3d.new(col + 1, row + 1, 0)
    Geom::Point3d.new(col, row + 1, 0)
  ]
end ### calc_points()

def build_grid( row_num = 20, col_num = 20 )

  model = Sketchup.active_model
  model.start_operation('Create Grid', true)

  ments = model.entities
  group = ments.add_group
  entities = group.entities
  entities.add_cpoint(ORIGIN)

  # Define ranges for rows and columns:
  rows = 0..row_num-1
  cols = 0..col_num-1

  # Create triangulated grid with soft+smooth internal edges.
  for row in rows
    for col in cols
      #  d +--5--+ c
      #    | \   |
      #    3 2,6 4
      #    |   \ |
      #  a +--1--+ b

      a, b, c, d = calc_points(col, row)

      edge1 = entities.add_line(a, b)
      edge2 = entities.add_line(b, d)
      edge3 = entities.add_line(d, a)

      edge4 = entities.add_line(b, c)
      edge5 = entities.add_line(c, d)
      edge6 = entities.add_line(d, b)

      # Soften & smooth the bottom edge if not in first row:
      edge1.soft= edge1.smooth= row != rows.first
      # Always soften & smooth the dialognal edge:
      edge2.soft= edge2.smooth= true
      # Soften & smooth the left edge if not in first column:
      edge3.soft= edge3.smooth= col != cols.first

      # Soften & smooth the right edge if not in last column:
      edge4.soft= edge4.smooth= col != cols.last
      # Soften & smooth the top edge if not in last row:
      edge5.soft= edge5.smooth= row != rows.last
      # Always soften & smooth the dialognal edge:
      edge6.soft= edge6.smooth= true

      face1 = entities.add_face(edge1, edge2, edge3)
      face2 = entities.add_face(edge4, edge5, edge6)

    end # each col
  end # each row

  model.commit_operation
end ### build_grid()
1 Like