Add src/obj-export.js

This commit is contained in:
bjkim committed 2026-10-28 18:19:15 +09:00
1 parent 6de19266cf
commit 65a01a8889
1 file changed
+3
+3
View File
@@ -0,0 +1,3 @@
// OBJ mesh serialization and browser download.
// Export each modeled component as a closed OBJ mesh.
window.CatchpitObjExporter={download(modelData,fileName){if(!modelData)return;const lines=['# Separate sump pit parts generated from DXF'];let next=1;for(const part of modelData.parts){const ring=part.footprint,n=ring.length,offset=next;lines.push(`o ${part.name.replace(/\s+/g,'_')}`);if(part.type==='wall'){const inner=part.innerFootprint,outerBottom=offset,outerTop=offset+n,innerBottom=offset+n*2,innerTop=offset+n*3;for(const p of ring)lines.push(`v ${p.x} ${p.y} ${part.z0}`);for(const p of ring)lines.push(`v ${p.x} ${p.y} ${part.z1}`);for(const p of inner)lines.push(`v ${p.x} ${p.y} ${part.z0}`);for(const p of inner)lines.push(`v ${p.x} ${p.y} ${part.z1}`);for(let i=0;i<n;i++){const j=(i+1)%n;lines.push(`f ${outerBottom+i} ${outerBottom+j} ${outerTop+j} ${outerTop+i}`);lines.push(`f ${innerBottom+j} ${innerBottom+i} ${innerTop+i} ${innerTop+j}`)}for(const e of part.edgePairs){lines.push(`f ${outerTop+e.outerStart} ${outerTop+e.outerEnd} ${innerTop+e.innerEnd} ${innerTop+e.innerStart}`);lines.push(`f ${outerBottom+e.outerEnd} ${outerBottom+e.outerStart} ${innerBottom+e.innerStart} ${innerBottom+e.innerEnd}`)}next+=n*4}else{for(const p of ring)lines.push(`v ${p.x} ${p.y} ${part.z0}`);for(const p of ring)lines.push(`v ${p.x} ${p.y} ${part.z1}`);for(let i=0;i<n;i++){const j=(i+1)%n;lines.push(`f ${offset+i} ${offset+j} ${offset+n+j} ${offset+n+i}`)}lines.push('f '+Array.from({length:n},(_,i)=>offset+n+i).join(' '));lines.push('f '+Array.from({length:n},(_,i)=>offset+n-1-i).join(' '));next+=n*2}}const blob=new Blob([lines.join('\n')],{type:'text/plain'}),a=document.createElement('a');a.href=URL.createObjectURL(blob);a.download=fileName.replace(/\.dxf$/i,'')+'.obj';a.click();setTimeout(()=>URL.revokeObjectURL(a.href),1000)}};