# encoding: UTF-8
begin
  puts "###DZW-BUILD-H80-20260920"
  m = Sketchup.active_model
  bk = 'E:/도진팩토리/3D스캔및티칭시스템/_자동화/autosave/BACKUP_before_H80.skp'
  m.save_copy(bk)
  puts "BACKUP=#{bk}"

  xb = 4539.1   # 절곡선 = 간살 시작점
  h  = 80.0     # 절곡 높이
  th = 12.0     # 판두께(수직)
  # [앞끝X, 뒤끝X, Y시작, 폭, 뒤평판 바닥Z, 새이름, 기존이름]
  spec = [
    [3848.5, 5449.3, 7472.2, 90.0, 20.4, 'RJ102-0604_LH_H80_20260920', 'RJ102-0604_LH_20260918'],
    [3833.8, 5448.4, 8722.9, 90.0, 23.8, 'RJ102-0602_RH_H80_20260920', 'RJ102-0602_RH_20260918']
  ]
  # 검산
  okc = true
  spec.each do |xt, xr, y0, w, zf, nn, on|
    run = xb - xt
    ang = Math.atan(h / run) * 180.0 / Math::PI
    zt = zf + h
    okc = false if (zt - zf - h).abs > 1.0e-6 || run <= 0
    puts "검산 #{nn}: 절곡선X=#{xb} 앞끝X=#{xt} 런=#{run.round(1)} 절곡높이=#{(zt - zf).round(3)} 경사각=#{ang.round(4)}deg 뒤평판Z=#{zf}~#{zf + th}"
  end
  if !okc
    puts "STOP: 검산 실패"
  else
    olds = {}
    m.active_entities.each do |e|
      next unless e.is_a?(Sketchup::Group) || e.is_a?(Sketchup::ComponentInstance)
      nm = (e.respond_to?(:name) ? e.name.to_s : '')
      olds[nm] = e
    end
    m.start_operation('H80 절곡판 신규 생성', true)
    made = []
    spec.each do |xt, xr, y0, w, zf, nn, on|
      zt = zf + h
      pts = [
        Geom::Point3d.new(xt.mm, y0.mm, zt.mm),
        Geom::Point3d.new(xb.mm, y0.mm, zf.mm),
        Geom::Point3d.new(xr.mm, y0.mm, zf.mm),
        Geom::Point3d.new(xr.mm, y0.mm, (zf + th).mm),
        Geom::Point3d.new(xb.mm, y0.mm, (zf + th).mm),
        Geom::Point3d.new(xt.mm, y0.mm, (zt + th).mm)
      ]
      g = m.active_entities.add_group
      fc = g.entities.add_face(pts)
      fc.pushpull(w.mm)
      if g.bounds.min.y.to_mm < y0 - 1.0
        g.transform!(Geom::Transformation.new(Geom::Vector3d.new(0, w.mm, 0)))
      end
      g.name = nn
      made << [nn, g, xt, zf, zt]
      o = olds[on]
      if o
        o.hidden = true
        puts "기존 '#{on}' hidden=#{o.hidden?}"
      else
        puts "경고: 기존 '#{on}' 미발견"
      end
    end
    m.commit_operation
    made.each do |nn, g, xt, zf, zt|
      b = g.bounds
      pts = []
      g.entities.grep(Sketchup::Edge).each { |ed| ed.vertices.each { |v| pts << v.position.transform(g.transformation) } }
      xs = pts.map { |p| p.x.to_mm }
      ztip = pts.select { |p| p.x.to_mm <= xs.min + 0.5 }.map { |p| p.z.to_mm }.min
      zcr  = pts.select { |p| (p.x.to_mm - xb).abs < 0.5 }.map { |p| p.z.to_mm }.min
      ang = Math.atan((ztip - zcr) / (xb - xs.min)) * 180.0 / Math::PI
      puts "== #{nn} 면수=#{g.entities.grep(Sketchup::Face).length} 솔리드?=#{g.manifold? rescue 'n/a'}"
      puts "   절곡선X=#{xb} 앞끝X=#{xs.min.round(1)} 절곡높이(실측)=#{(ztip - zcr).round(2)}mm 경사각(실측)=#{ang.round(4)}deg"
      puts "   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}"
    end
    ok = m.save
    puts "SAVE=#{ok.inspect} modified?=#{m.modified?.inspect}"
    puts "DONE"
  end
rescue Exception => e
  puts "EXC #{e.class}: #{e.message}"
  puts e.backtrace.first(5).join(" | ")
end
nil
