"""플랜지 원판 배치 확인: 손목-토치 클로즈업 캡처 + 플랜지판 존재/치수."""
import asyncio, json
from playwright.async_api import async_playwright
URL='http://localhost:8090/DOZIKWORKS_OS_v5_locked.dc.html'
OUT=r'E:\도진팩토리\3D스캔및티칭시스템\shots\ceo_feedback'
async def main():
    async with async_playwright() as p:
        br=await p.chromium.launch(channel='msedge',headless=True)
        pg=await br.new_page(viewport={'width':1400,'height':1000})
        errs=[]; pg.on('pageerror',lambda e: errs.append(str(e)))
        await pg.goto(URL)
        await pg.wait_for_function("window.__dzw && window.__dzw._torchMesh", timeout=15000)
        info=await pg.evaluate("""()=>{ const d=window.__dzw, T=d._T; const out={};
          out.hasPlate=!!d._flangePlate; out.plateName=d._flangePlate?.name;
          out.diam=d._flangeDiam||140; out.thick=d._flangeThick||20;
          // 플랜지판 밝게(빨강)로 식별 + 토치 잠시 숨김
          if(d._flangePlate){ d._flangePlate.material=new T.MeshStandardMaterial({color:0xff3020,metalness:0.3,roughness:0.5}); }
          if(d._torchNode){ d._torchNode.visible=false; }
          // 플랜지판/툴원점 좌표
          const tt=d._truthTool; tt.updateMatrixWorld(true);
          const F=new T.Vector3(); d._flangePlate.getWorldPosition(F);
          const To=new T.Vector3(); tt.getWorldPosition(To);
          out.plateWorld=[+F.x.toFixed(3),+F.y.toFixed(3),+F.z.toFixed(3)];
          out.toolOrigin=[+To.x.toFixed(3),+To.y.toFixed(3),+To.z.toFixed(3)];
          if(d.orbit){ d.orbit.tgt.copy(F); d.orbit.r=0.32; d.orbit.theta=-0.9; d.orbit.phi=1.4; d.updateCam&&d.updateCam(); }
          return out;
        }""")
        await pg.wait_for_timeout(400)
        await pg.screenshot(path=OUT+r'\FLANGEPLATE_v1.png')
        print(json.dumps(info,ensure_ascii=False)); print("errs:",errs if errs else "없음")
        await br.close()
asyncio.run(main())
