# encoding: UTF-8
begin
  puts "###DZW-OILCOVER-BUILD2-20260920"
  m = Sketchup.active_model
  bk = 'E:/도진팩토리/3D스캔및티칭시스템/_자동화/autosave/BACKUP_before_oilcover2.skp'
  m.save_copy(bk)
  puts "BACKUP=#{bk}"
  x0 = 4478.4
  y0 = 7562.2
  y1 = 8722.9
  th = 12.0
  seat = nil
  ['RJ102-0604_LH_20260918', 'RJ102-0602_RH_20260918'].each do |want|
    m.entities.each do |e|
      next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
      next unless (e.respond_to?(:name) ? e.name.to_s : '') == want
      pts = []
      (e.is_a?(Sketchup::Group) ? e.entities : e.definition.entities).grep(Sketchup::Edge).each { |ed| ed.vertices.each { |v| pts << v.position.transform(e.transformation) } }
      next if pts.empty?
      xs = pts.map { |p| p.x.to_mm }
      rear = pts.select { |p| p.x.to_mm >= xs.max - 1.0 }
      zz = rear.map { |p| p.z.to_mm }.select { |z| z < 100.0 }.max
      seat = zz if zz && (seat.nil? || zz > seat)
    end
  end
  seat = 44.4 if seat.nil?
  puts "검산: 착좌면 Z=#{seat.round(2)} / A X=[#{x0},#{x0 + 420}] B X=[#{x0 + 420},#{x0 + 840}] / Y=[#{y0},#{y1}] 폭=#{(y1 - y0).round(1)} / 두께=#{th}"
  made = []
  m.start_operation('오일탱크커버 A/B 생성', true)
  2.times do |k|
    xa = x0 + 420.0 * k
    xb = xa + 420.0
    nm = k == 0 ? '오일탱크커버_A_420_20260920' : '오일탱크커버_B_420_20260920'
    g = m.entities.add_group
    z = seat
    fc = g.entities.add_face([
      Geom::Point3d.new(xa.mm, y0.mm, z.mm),
      Geom::Point3d.new(xb.mm, y0.mm, z.mm),
      Geom::Point3d.new(xb.mm, y1.mm, z.mm),
      Geom::Point3d.new(xa.mm, y1.mm, z.mm)
    ])
    [y0 + 30.0, y1 - 30.0].each do |yy|
      4.times do |i|
        cx = xa + 40.0 + 113.3 * i
        g.entities.add_circle(Geom::Point3d.new(cx.mm, yy.mm, z.mm), Geom::Vector3d.new(0, 0, 1), 9.0.mm, 16)
      end
    end
    small = g.entities.grep(Sketchup::Face).select { |f| (f.area * 0.00155) < 500.0 }
    g.entities.erase_entities(small) unless small.empty?
    big = g.entities.grep(Sketchup::Face).max_by { |f| f.area }
    big.pushpull(th.mm) if big
    g.name = nm
    holes = 0
    g.entities.grep(Sketchup::Face).each { |f| f.loops.each { |lp| holes += 1 if !lp.outer? && lp.vertices.length >= 8 } }
    made << [nm, g, holes]
  end
  m.commit_operation
  made.each do |nm, g, holes|
    b = g.bounds
    puts "== #{nm} bbox min=#{b.min.to_a.map { |v| v.to_mm.round(1) }.inspect} max=#{b.max.to_a.map { |v| v.to_mm.round(1) }.inspect} 면수=#{g.entities.grep(Sketchup::Face).length} 홀(내부루프)=#{holes}"
  end
  vw = m.active_view
  cam0 = vw.camera
  begin
    c = Geom::Point3d.new((x0 + 420.0).mm, ((y0 + y1) / 2.0).mm, seat.mm)
    [['bottom', Geom::Point3d.new(c.x.to_f, c.y.to_f, c.z - 2200.mm), Geom::Vector3d.new(1, 0, 0)],
     ['iso', Geom::Point3d.new(c.x - 2200.mm, c.y - 2600.mm, c.z - 1400.mm), Geom::Vector3d.new(0, 0, 1)]].each do |tag, eye, upv|
      vw.camera = Sketchup::Camera.new(eye, c, upv)
      f = "E:/도진팩토리/3D스캔및티칭시스템/_자동화/inbox/_oilcov2_#{tag}.png"
      vw.write_image(f, 1600, 1000, true)
      puts "캡처 #{tag}=#{f} 존재=#{File.exist?(f)}"
    end
  rescue Exception => e3
    puts "캡처 실패 #{e3.message}"
  ensure
    vw.camera = cam0
  end
  ok = m.save
  puts "SAVE=#{ok.inspect} modified?=#{m.modified?.inspect}"
  puts "DONE"
rescue Exception => e
  puts "EXC #{e.class}: #{e.message}"
  puts e.backtrace.first(6).join(" | ")
end
nil
