misc_exporter_gcode.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - exporter - GCode</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <meta property="og:title" content="three.js webgl - exporter - GCode">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/misc_exporter_gcode.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/misc_exporter_gcode.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - exporter - <a href="https://github.com/jgphilpott/polyslice" target="_blank" rel="noopener">GCode</a>
  16. <br>Slice 3D models to G-code for 3D printing
  17. <br><a href="https://www.youtube.com/watch?v=V2h3SiafXRc" target="_blank" rel="noopener">Watch the Demo Video</a>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.module.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three';
  29. import Polyslice from 'https://cdn.jsdelivr.net/npm/@jgphilpott/polyslice@26.4.0/dist/index.browser.esm.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. // Make THREE available globally for Polyslice
  33. window.THREE = THREE;
  34. let camera, scene, renderer, mesh;
  35. const params = {
  36. addCube: addCube,
  37. addCylinder: addCylinder,
  38. addCone: addCone,
  39. addSphere: addSphere,
  40. addTorus: addTorus,
  41. exportToGCode: exportToGCode,
  42. geometryName: 'cube',
  43. printer: 'Ender3',
  44. filament: 'GenericPLA',
  45. layerHeight: 0.2,
  46. infillDensity: 20,
  47. infillPattern: 'grid'
  48. };
  49. // Dropdown options (extend as supported by Polyslice profiles)
  50. const PRINTER_OPTIONS = [ 'Ender3', 'UltimakerS5', 'PrusaI3MK3S', 'AnycubicI3Mega', 'BambuLabP1P' ];
  51. const FILAMENT_OPTIONS = [ 'GenericPLA', 'GenericPETG', 'GenericABS' ];
  52. init();
  53. function init() {
  54. renderer = new THREE.WebGLRenderer( { antialias: true } );
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. renderer.setAnimationLoop( animate );
  58. document.body.appendChild( renderer.domElement );
  59. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  60. // Use Z-up coordinate system
  61. THREE.Object3D.DEFAULT_UP.set( 0, 0, 1 );
  62. camera.up.set( 0, 0, 1 );
  63. camera.position.set( 42, 42, 42 );
  64. scene = new THREE.Scene();
  65. scene.background = new THREE.Color( 0xa0a0a0 );
  66. const ambientLight = new THREE.AmbientLight( 0xffffff, 0.5 );
  67. scene.add( ambientLight );
  68. const directionalLight = new THREE.DirectionalLight( 0xffffff, 2.5 );
  69. directionalLight.position.set( 0, 200, 100 );
  70. scene.add( directionalLight );
  71. // Add ground plane for reference (XY plane when Z is up)
  72. const gridHelper = new THREE.GridHelper( 220, 10 );
  73. gridHelper.rotation.x = - Math.PI / 2; // rotate from XZ to XY
  74. scene.add( gridHelper );
  75. const gui = new GUI();
  76. let h = gui.addFolder( 'Printer & Filament' );
  77. h.add( params, 'printer', PRINTER_OPTIONS ).name( 'Printer' );
  78. h.add( params, 'filament', FILAMENT_OPTIONS ).name( 'Filament' );
  79. h = gui.addFolder( 'Slicer Settings' );
  80. h.add( params, 'layerHeight', 0.1, 0.4, 0.05 ).name( 'Layer Height (mm)' );
  81. h.add( params, 'infillDensity', 0, 100, 5 ).name( 'Infill Density (%)' );
  82. h.add( params, 'infillPattern', [ 'grid', 'triangles', 'hexagons' ] ).name( 'Infill Pattern' );
  83. h = gui.addFolder( 'Geometry Selection' );
  84. h.add( params, 'addCube' ).name( 'Cube' );
  85. h.add( params, 'addCylinder' ).name( 'Cylinder' );
  86. h.add( params, 'addCone' ).name( 'Cone' );
  87. h.add( params, 'addSphere' ).name( 'Sphere' );
  88. h.add( params, 'addTorus' ).name( 'Torus' );
  89. h = gui.addFolder( 'Export' );
  90. h.add( params, 'exportToGCode' ).name( 'Export G-code' );
  91. gui.open();
  92. addCube();
  93. window.addEventListener( 'resize', onWindowResize );
  94. const controls = new OrbitControls( camera, renderer.domElement );
  95. controls.target.set( 0, 0, 0 );
  96. controls.update();
  97. }
  98. function exportToGCode() {
  99. try {
  100. // Check if Polyslice is loaded
  101. if ( typeof Polyslice === 'undefined' ) {
  102. alert( 'Polyslice library failed to load from CDN.\n\nPossible solutions:\n1. Disable ad blockers or browser extensions\n2. Check your network connection\n3. Ensure cdn.jsdelivr.net is not blocked by your firewall' );
  103. return;
  104. }
  105. // Create printer and filament configurations using Polyslice
  106. const printer = new Polyslice.Printer( params.printer || 'Ender3' );
  107. const filament = new Polyslice.Filament( params.filament || 'GenericPLA' );
  108. // Create the slicer instance with user-defined settings
  109. const slicer = new Polyslice.Polyslice( {
  110. printer: printer,
  111. filament: filament,
  112. layerHeight: params.layerHeight,
  113. infillPattern: params.infillPattern,
  114. infillDensity: params.infillDensity,
  115. verbose: true
  116. } );
  117. // Slice the current mesh directly
  118. const gcode = slicer.slice( mesh );
  119. // Download the G-code file (include geometry name)
  120. const name = params.geometryName || 'model';
  121. saveString( gcode, `${ name }-geometry.gcode` );
  122. } catch ( error ) {
  123. console.error( 'Error exporting to G-code:', error );
  124. alert( 'Error exporting to G-code. Check console for details.' );
  125. alert( 'Note: Polyslice is an external library. Please ensure it is loaded correctly.' );
  126. }
  127. }
  128. function clearScene() {
  129. if ( mesh ) {
  130. mesh.geometry.dispose();
  131. mesh.material.dispose();
  132. scene.remove( mesh );
  133. }
  134. }
  135. // Ensure the mesh sits on the XY plane with min Z = 0
  136. function placeOnXYPlane( object ) {
  137. // Update world matrix so bounding box reflects transforms
  138. object.updateMatrixWorld( true );
  139. const box = new THREE.Box3().setFromObject( object );
  140. const minZ = box.min.z;
  141. if ( isFinite( minZ ) ) {
  142. // Shift object upward by -minZ so it rests on z=0
  143. object.position.z -= minZ;
  144. object.updateMatrixWorld( true );
  145. }
  146. }
  147. function addCube() {
  148. clearScene();
  149. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  150. const geometry = new THREE.BoxGeometry( 10, 10, 10 );
  151. mesh = new THREE.Mesh( geometry, material );
  152. params.geometryName = 'cube';
  153. placeOnXYPlane( mesh );
  154. scene.add( mesh );
  155. }
  156. function addCylinder() {
  157. clearScene();
  158. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  159. const geometry = new THREE.CylinderGeometry( 5, 5, 10, 42 );
  160. mesh = new THREE.Mesh( geometry, material );
  161. mesh.rotation.x = Math.PI / 2;
  162. params.geometryName = 'cylinder';
  163. placeOnXYPlane( mesh );
  164. scene.add( mesh );
  165. }
  166. function addCone() {
  167. clearScene();
  168. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  169. const geometry = new THREE.ConeGeometry( 5, 10, 42 );
  170. mesh = new THREE.Mesh( geometry, material );
  171. mesh.rotation.x = Math.PI / 2;
  172. params.geometryName = 'cone';
  173. placeOnXYPlane( mesh );
  174. scene.add( mesh );
  175. }
  176. function addSphere() {
  177. clearScene();
  178. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  179. const geometry = new THREE.SphereGeometry( 5, 42, 42 );
  180. mesh = new THREE.Mesh( geometry, material );
  181. params.geometryName = 'sphere';
  182. placeOnXYPlane( mesh );
  183. scene.add( mesh );
  184. }
  185. function addTorus() {
  186. clearScene();
  187. const material = new THREE.MeshLambertMaterial( { color: 0x00cc00 } );
  188. const geometry = new THREE.TorusGeometry( 5, 2, 24, 100 );
  189. mesh = new THREE.Mesh( geometry, material );
  190. params.geometryName = 'torus';
  191. placeOnXYPlane( mesh );
  192. scene.add( mesh );
  193. }
  194. const link = document.createElement( 'a' );
  195. link.style.display = 'none';
  196. document.body.appendChild( link );
  197. function save( blob, filename ) {
  198. link.href = URL.createObjectURL( blob );
  199. link.download = filename;
  200. link.click();
  201. }
  202. function saveString( text, filename ) {
  203. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  204. }
  205. function onWindowResize() {
  206. camera.aspect = window.innerWidth / window.innerHeight;
  207. camera.updateProjectionMatrix();
  208. renderer.setSize( window.innerWidth, window.innerHeight );
  209. }
  210. function animate() {
  211. renderer.render( scene, camera );
  212. }
  213. </script>
  214. </body>
  215. </html>
粤ICP备19079148号