Trying to draw a point cloud with ruby

My 1st suggestion would be a method like …

def import_point_cloud
  path = UI.openpanel("Open Text File", "c:/", "Text Files|*.txt||")
  return unless path
  text = File.readlines(path)
  points = text.map {|line|
    next nil unless line.start_with?(/\A[0-9.+-]/)
    coords = line.strip.split(',').map(&:to_f)
    Geom::Point3d.new(coords)
  }
  points.compact! # remove nil members
  model = Sketchup.active_model
  entities = model.active_entities
  t = Time.now.to_i
  model.start_operation("Point Cloud",true)
    grp = entities.add_group
    gents = grp.entities
    points.each { |pt| gents.add_cpoint(pt) }
  model.commit_operation
  puts "Elapsed time: #{Time.now.to_i - t} secs"
end

But as @dezmo says a very large file will cause SketchUp to go into “not Responding” mode.

The task of iterating the text array could be broken up into smaller chunks. (More on this later.)

This is bad because in your code, the file is open whilst you are iterating it and adding geometry.
Ruby does not have a chance to close the file handle. I would suggest rebooting the computer whenever you have to force quit an application.

1 Like