Script to apply offset

Post colorized code correctly in the forum and please edit your previous post.


I created a quick code (based on other one I also posted in a forum).
You will get a context menu “Face Around Hole”, after copy paste it to the Ruby Console.
To be able to use it you must install TIG Smart offset first!
Dezmo_FAH

module Dezmo
module FaceAroundHole

  @@loaded = false unless defined?(@@loaded)
  extend self
  
  def tig_smart_offset
    begin
      require 'TIG-Smart_offset.rb'
    rescue LoadError
      url = "https://sketchucation.com/pluginstore?pln=TIG_Smart_offset"
      UI.messagebox( "Please install TIG: Smart Offset v3.0\n\n" + url)
      return
    end
    true
  end

  def get_size
    return unless tig_smart_offset

    @offset = 50.mm unless @offset
    prompts = [
      "Offset"
    ]
    defaults = [
      @offset
    ]
    title = "Face Around Hole"
    inputbox(prompts, defaults, title)
  end
  
  def msg
    UI.messagebox("Face Around Hole :\n\nPlease Right click on a Group Contains one Face with one hole !")
    nil
  end
  
  def create_structure
    model = Sketchup.active_model
    ents = model.active_entities
    sel = model.selection
    return msg unless sel.single_object?
    return msg unless sel.first.is_a?(Sketchup::Group)
    group = sel.first
    group.make_unique
    entities = group.entities
    faces = entities.grep(Sketchup::Face)
    return msg unless faces.first
    return msg if faces.size > 1
    face = faces.first
    loops = face.loops
    return msg if loops.size != 2
    o_loop = face.outer_loop
    i_loop = face.loops.find { |loop| !loop.outer? }
    edges_to_erase = o_loop.edges
    vertices = i_loop.vertices
    input = get_size
    return unless input
    @offset = input.first
    norm = face.normal
    model.start_operation('Face Around Hole', true)
    entities.erase_entities(edges_to_erase)
    temp_face = entities.add_face(vertices)
    temp_face.reverse! unless temp_face.normal.samedirection?(norm)
    face_new = TIG::Smart_offset.new(temp_face, @offset)
    entities.erase_entities(temp_face)
    model.commit_operation
  end
  
  unless @@loaded
    cmd = UI::Command.new("Face Around Hole"){
      create_structure()
    }
    UI.add_context_menu_handler{|menu|
      menu.add_item(cmd) if Sketchup.active_model.selection.grep(Sketchup::Group).first
    }
    @@loaded = true
  end

end
end