Grid

2023.06.06 13:11
@margin = 1800.mm
@patterns = "."
# Filter and sort Constructionlines
def cline_in_order
  all_cl = Sketchup.active_model.entities.grep(Sketchup::ConstructionLine)
  h_cl = all_cl.select { |cl| cl.direction.parallel?([0,1,0]) }
  z_cl = all_cl.select { |cl| cl.direction.parallel?([1,0,0]) }
  if h_cl.length == 0
    pointh = Geom::Point3d.new(@margin, 0, 0)
    point0 = Geom::Point3d.new(@margin, 1, 0)
    cl_h0 = Sketchup.active_model.entities.add_cline(pointh, point0)
    h_cl << cl_h0
  end
  if z_cl.length == 0
    pointz = Geom::Point3d.new(0, @margin, 0)
    point0 = Geom::Point3d.new(1, @margin, 0)
    cl_z0 = Sketchup.active_model.entities.add_cline(pointz, point0)
    z_cl << cl_z0
  end
  @sorted_h_cl = h_cl.sort_by { |cl| cl.end.x }
  @sorted_z_cl = z_cl.sort_by { |cl| cl.end.y }
end # cline_in_order
#-----------------------------------
# Trimming constructionlines 
def cline_trim
  cline_in_order
  px_min = @sorted_h_cl[0].end.x - @margin
  py_min = @sorted_z_cl[0].end.y - @margin
  px_max = @sorted_h_cl[-1].end.x + @margin
  py_max = @sorted_z_cl[-1].end.y + @margin
  point_max = Geom::Point3d.new(px_max, py_max, 0)
  @sorted_h_cl.each do |cl|
    start_h = Geom::Point3d.new(cl.end.x, py_min, 0)
    cl.end = point_max
    cl.position = start_h
    cl.stipple = @patterns
  end
  @sorted_z_cl.each do |cl|
    start_z = Geom::Point3d.new(px_min, cl.end.y, 0)
    cl.end = point_max
    cl.position = start_z
    cl.stipple = @patterns
  end
end # cline_trim
#----------------------------------
# Delete overlapping constructionlines
def delete_overlapping
  cl_to_delete = []
  all_cl = Sketchup.active_model.entities.grep(Sketchup::ConstructionLine)
  all_cl.each_with_index do |line1, index1|
    all_cl.each_with_index do |line2, index2|
      next if index1 == index2 
      next if cl_to_delete.include?(line1.entityID) 
      next if cl_to_delete.include?(line2.entityID)
      if line1.end.distance(line2.end) < 1
        cl_to_delete << line2.entityID
      end
    end
  end
  cl_to_delete.each do |id|
    overlapping = Sketchup.active_model.find_entity_by_id(id)
    Sketchup.active_model.entities.erase_entities(overlapping)
  end
end # delete_overlapping
#----------------------------------
def add_cline
  Sketchup.active_model.start_operation("Add axis line", true)
  n = @inputs[2].to_i
  for i in 1..n
    if @inputs[1] == 0
      cline_trim
      return
    elsif @inputs[1] != 0 and @inputs[0] == "right"
      cline_in_order
      cline_x = @sorted_h_cl[-1].end.x + @inputs[1].to_f.mm
      cline = Sketchup.active_model.entities.add_cline([cline_x, 0, 0], [cline_x, 1, 0])
    elsif @inputs[1] != 0 and @inputs[0] == "left"
      cline_in_order
      cline_x = @sorted_h_cl[0].end.x - @inputs[1].to_f.mm
      cline = Sketchup.active_model.entities.add_cline([cline_x, 0, 0], [cline_x, 1, 0])
    elsif @inputs[1] != 0 and @inputs[0] == "top"
      cline_in_order
      cline_y = @sorted_z_cl[-1].end.y + @inputs[1].to_f.mm
      cline = Sketchup.active_model.entities.add_cline([0, cline_y, 0], [1, cline_y, 0])
    elsif @inputs[1] != 0 and @inputs[0] == "bottom"
      cline_in_order
      cline_y = @sorted_z_cl[0].end.y - @inputs[1].to_f.mm
      cline = Sketchup.active_model.entities.add_cline([0, cline_y, 0], [1, cline_y, 0])
    end
  end
  cline_trim
  Sketchup.active_model.commit_operation
end # add_cline
#----------------------------------
def grid_input
# Set html dialog
dialog = UI::HtmlDialog.new(
  dialog_title: "Grid - AWhouse",
  scrollable: true,
  resizable: true,
  width: 300,
  height: 450,
  left: 1500,
  top: 200,
)
dialog.set_file(File.join(__dir__, 'grid.html'))
dialog.show

# Callback from html dialog
dialog.add_action_callback("submitForm") { |dialog, params|
  direction, span, count = params.split("@")
  @inputs = [direction, span, count]
  add_cline
}
end # grid_input
#----------------------------------
# Add Command
cmd_add_cline = UI::Command.new("Add axis line") {
  grid_input
}

# Toolbar Setting
toolbar = UI::Toolbar.new "AWhouse"
cmd_add_cline.tooltip = "Add axis line"
cmd_add_cline.status_bar_text = "Add axis line by direction and span "
cmd_add_cline.small_icon = cmd_add_cline.large_icon = "images/add_cline.png"
toolbar = toolbar.add_item(cmd_add_cline)
toolbar.show
# Menu Setting
UI.menu("Draw").add_separator
submenu = UI.menu("Draw").add_submenu("AWhouse")
submenu.add_item(cmd_add_cline)
© AWhouse