From 8e88aec93fa7b5392ccefb5ee4d325ff8cf44760 Mon Sep 17 00:00:00 2001 From: omrips Date: Tue, 9 Feb 2021 18:11:02 +0200 Subject: [PATCH] Add files via upload --- build/load_stl.min.js | 4 ++-- build/parser.min.js | 2 +- build/stl_viewer.min.js | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/build/load_stl.min.js b/build/load_stl.min.js index 3f38758..2d09212 100644 --- a/build/load_stl.min.js +++ b/build/load_stl.min.js @@ -1,2 +1,2 @@ -//=========== Stl Viewer v1.11, by Omri Rips, Viewstl.com, November 2020 ; admin@viewstl.com =========== -importScripts("parser.min.js"),MSG_DATA=0,MSG_LOAD=1,MSG_ERROR=2,MSG_STL_LOADED=3,MSG_LOAD_IN_PROGRESS=4;var filename=null,local_file=null,load_from_blob_or_ab=null,x=0,y=0,z=0,model_id=-1,get_progress=!1;function isNumeric(a){return!isNaN(parseFloat(a))&&isFinite(a)}function send_error(a){postMessage({msg_type:MSG_ERROR,data:a})}function download_from_local(a){var e=new XMLHttpRequest;get_progress&&(e.onprogress=function(a){postMessage({msg_type:MSG_LOAD_IN_PROGRESS,id:model_id,loaded:a.loaded,total:a.total})}),e.onreadystatechange=function(a){4==e.readyState&&200==e.status&&after_file_load(e.response)},e.open("GET",a,!0),e.setRequestHeader("Content-type","application/x-www-form-urlencoded"),e.responseType="arraybuffer",e.send(null)}function after_file_load(a){var e;if(a){try{e=parse_3d_file(filename,a)}catch(a){e="Error parsing the file"}"string"!=typeof e?postMessage({msg_type:MSG_STL_LOADED,vertices:e.vertices,faces:e.faces,colors:e.colors}):send_error(e)}else send_error("no data")}function read_file(a){var e=new FileReader;e.onerror=function(a){var e="";switch(a.target.error.code){case a.target.error.NOT_FOUND_ERR:e="File not found";break;case a.target.error.NOT_READABLE_ERR:e="Can't read file - too large?";break;case a.target.error.ABORT_ERR:e="Read operation aborted";break;case a.target.error.SECURITY_ERR:e="File is locked";break;case a.target.error.ENCODING_ERR:e="File too large";break;default:e="Error reading file"}send_error(e)},e.onprogress=function(a){postMessage({msg_type:MSG_LOAD_IN_PROGRESS,id:model_id,loaded:a.loaded,total:a.total})},e.onload=function(a){after_file_load(a.target.result)},e.readAsArrayBuffer(a)}self.addEventListener("message",function(a){switch(a.data.msg_type){case MSG_DATA:if(!a.data.data){send_error("no data");break}if(!a.data.data.filename&&!a.data.data.local_file){send_error("no file");break}a.data.data.local_file?(filename=a.data.data.local_file.name?a.data.data.local_file.name:a.data.data.filename,local_file=a.data.data.local_file?a.data.data.local_file:null):a.data.data.filename&&(filename=a.data.data.filename),a.data.data.x&&isNumeric(a.data.data.x)&&(x=a.data.data.x),a.data.data.y&&isNumeric(a.data.data.y)&&(y=a.data.data.y),a.data.data.y&&isNumeric(a.data.data.z)&&(z=a.data.data.z),load_from_blob_or_ab=null,a.data.load_from_blob_or_ab&&(load_from_blob_or_ab=a.data.load_from_blob_or_ab),model_id=a.data.data.id?a.data.data.id:-1,get_progress=!!a.data.get_progress&&a.data.get_progress;break;case MSG_LOAD:load_from_blob_or_ab?load_from_blob_or_ab instanceof ArrayBuffer?after_file_load(load_from_blob_or_ab):read_file(load_from_blob_or_ab):local_file?read_file(local_file):filename&&download_from_local(filename);break;default:console.log("invalid msg: "+a.data.msg_type)}}); \ No newline at end of file +//=========== Stl Viewer v1.12, by Omri Rips, Viewstl.com, February 2021 ; admin@viewstl.com =========== +importScripts("parser.min.js"),MSG_DATA=0,MSG_LOAD=1,MSG_ERROR=2,MSG_STL_LOADED=3,MSG_LOAD_IN_PROGRESS=4;var filename=null,local_file=null,load_from_blob_or_ab=null,x=0,y=0,z=0,model_id=-1,get_progress=!1;function isNumeric(a){return!isNaN(parseFloat(a))&&isFinite(a)}function send_error(a){postMessage({msg_type:MSG_ERROR,data:a})}function download_from_local(a){fetch?download_from_local_fetch(a):download_from_local_xhr(a)}function download_from_local_xhr(a){var e=new XMLHttpRequest;get_progress&&(e.onprogress=function(a){postMessage({msg_type:MSG_LOAD_IN_PROGRESS,id:model_id,loaded:a.loaded,total:a.total})}),e.onreadystatechange=function(a){4==e.readyState&&200==e.status&&after_file_load(e.response)},e.open("GET",a,!0),e.responseType="arraybuffer",e.send(null)}async function download_from_local_fetch(a){const e=await fetch(a),o=e.body.getReader(),r=Number(e.headers.get("content-length")),t=new Uint8Array(r);let l=0;for(;;){const{done:a,value:e}=await o.read();if(a)break;e&&(t.set(e,l),l+=e.length,get_progress&&postMessage({msg_type:MSG_LOAD_IN_PROGRESS,id:model_id,loaded:l,total:r}))}after_file_load(t.buffer)}function after_file_load(a){var e;if(a){try{e=parse_3d_file(filename,a)}catch(a){e="Error parsing the file"}"string"!=typeof e?postMessage({msg_type:MSG_STL_LOADED,vertices:e.vertices,faces:e.faces,colors:e.colors}):send_error(e)}else send_error("no data")}function read_file(a){var e=new FileReader;e.onerror=function(a){var e="";switch(a.target.error.code){case a.target.error.NOT_FOUND_ERR:e="File not found";break;case a.target.error.NOT_READABLE_ERR:e="Can't read file - too large?";break;case a.target.error.ABORT_ERR:e="Read operation aborted";break;case a.target.error.SECURITY_ERR:e="File is locked";break;case a.target.error.ENCODING_ERR:e="File too large";break;default:e="Error reading file"}send_error(e)},e.onprogress=function(a){postMessage({msg_type:MSG_LOAD_IN_PROGRESS,id:model_id,loaded:a.loaded,total:a.total})},e.onload=function(a){after_file_load(a.target.result)},e.readAsArrayBuffer(a)}self.addEventListener("message",function(a){switch(a.data.msg_type){case MSG_DATA:if(!a.data.data){send_error("no data");break}if(!a.data.data.filename&&!a.data.data.local_file){send_error("no file");break}a.data.data.local_file?(filename=a.data.data.local_file.name?a.data.data.local_file.name:a.data.data.filename,local_file=a.data.data.local_file?a.data.data.local_file:null):a.data.data.filename&&(filename=a.data.data.filename),a.data.data.x&&isNumeric(a.data.data.x)&&(x=a.data.data.x),a.data.data.y&&isNumeric(a.data.data.y)&&(y=a.data.data.y),a.data.data.y&&isNumeric(a.data.data.z)&&(z=a.data.data.z),load_from_blob_or_ab=null,a.data.load_from_blob_or_ab&&(load_from_blob_or_ab=a.data.load_from_blob_or_ab),model_id=a.data.data.id?a.data.data.id:-1,get_progress=!!a.data.get_progress&&a.data.get_progress;break;case MSG_LOAD:load_from_blob_or_ab?load_from_blob_or_ab instanceof ArrayBuffer?after_file_load(load_from_blob_or_ab):read_file(load_from_blob_or_ab):local_file?read_file(local_file):filename&&download_from_local(filename);break;default:console.log("invalid msg: "+a.data.msg_type)}}); \ No newline at end of file diff --git a/build/parser.min.js b/build/parser.min.js index e19bbdd..eb954ed 100644 --- a/build/parser.min.js +++ b/build/parser.min.js @@ -1,2 +1,2 @@ -//=========== Stl Viewer v1.11, by Omri Rips, Viewstl.com, November 2020 ; admin@viewstl.com =========== +//=========== Stl Viewer v1.12, by Omri Rips, Viewstl.com, February 2021 ; admin@viewstl.com =========== function parse_3d_file(e,r){switch(e.split(".").pop().split("?")[0].toLowerCase()){case"stl":return parse_stl_bin(r);case"obj":return parse_obj(r);case"vf":return parse_vf(arrayBufferToString(r));default:return parse_stl_bin(r)}}function arrayBufferToString(e,r,t){if("undefined"!=typeof TextDecoder)return new TextDecoder("utf-8").decode(e);for(var a=new Uint8Array(e),n=a.length,o="",l=0;ln&&(c=n-l),o+=String.fromCharCode.apply(null,a.subarray(l,l+c))}return o}function parse_stl_ascii(e){try{var r=arrayBufferToString(e),t=[],a=[],n={};r=(r=(r=(r=(r=(r=(r=(r=(r=(r=(r=(r=r.replace(/\r/,"\n")).replace(/^solid[^\n]*/,"")).replace(/\n/g," ")).replace(/facet normal /g,"")).replace(/outer loop/g,"")).replace(/vertex /g,"")).replace(/endloop/g,"")).replace(/endfacet/g,"")).replace(/endsolid[^\n]*/,"")).replace(/facet/g,"")).replace(/\s+/g," ")).replace(/^\s+/,"");for(var o,l=0,c=r.split(" "),s=[],i=c.length/12-1,f=0;f-1&&(def_red_color=d.getUint8(u+6,!0)/31,def_green_color=d.getUint8(u+7,!0)/31,def_blue_color=d.getUint8(u+8,!0)/31);var g=80;try{var h=d.getUint32(g,!0)}catch(e){return"Can't parse file"}var _=84+50*h;if(e.byteLength!=_)return parse_stl_ascii(e);try{for(g+=4;h--;)g+=12,null==(r=f[[t=d.getFloat32(g,!0),a=d.getFloat32(g+4,!0),n=d.getFloat32(g+8,!0)]])&&(r=s.length,s.push(new Array(t,a,n)),f[[t,a,n]]=r),o=r,g+=12,null==(r=f[[t=d.getFloat32(g,!0),a=d.getFloat32(g+4,!0),n=d.getFloat32(g+8,!0)]])&&(r=s.length,s.push(new Array(t,a,n)),f[[t,a,n]]=r),l=r,g+=12,null==(r=f[[t=d.getFloat32(g,!0),a=d.getFloat32(g+4,!0),n=d.getFloat32(g+8,!0)]])&&(r=s.length,s.push(new Array(t,a,n)),f[[t,a,n]]=r),c=r,u>-1?(g+=12,face_color=d.getUint16(g,!0),32768==face_color||65535==face_color?(color_red=def_red_color,color_green=def_green_color,color_blue=def_blue_color):(p=!1,color_red=(31&face_color)/31,color_green=((992&face_color)>>5)/31,color_blue=((31744&face_color)>>10)/31),i.push(new Array(o,l,c,color_red,color_green,color_blue)),g+=2):(i.push(new Array(o,l,c)),g+=14);return f=null,{vertices:s,faces:i,colors:u>-1&&!p}}catch(e){return"Can't parse file"}}function parse_vf(e){var r=JSON.parse(e),t=[],a=[];try{var n=r.vertices.length;for(i=0;i=0?e-1:e+s.length}function n(e,r,t,n){u.push(new Array(a(e),a(r),a(t)))}function o(e,r,t){void 0===e[3]?(n(e[0],e[1],e[2]),void 0!==r&&r.length>0&&(r[0],r[1],r[2])):(void 0!==t&&t.length>0?(n(e[0],e[1],e[3],(t[0],t[1],t[3])),n(e[1],e[2],e[3],(t[1],t[2],t[3]))):(n(e[0],e[1],e[3]),n(e[1],e[2],e[3])),void 0!==r&&r.length>0&&(r[0],r[1],r[3],r[1],r[2],r[3]))}/^o /gm.test(r);for(var l,c,s=[],i=[],f=[],u=[],d=/v( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,p=/vn( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,g=/vt( +[\d|\.|\+|\-|e]+)( +[\d|\.|\+|\-|e]+)/,h=/f( +-?\d+)( +-?\d+)( +-?\d+)( +-?\d+)?/,_=/f( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+))?/,v=/f( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))( +(-?\d+)\/(-?\d+)\/(-?\d+))?/,y=/f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))?/,w=r.split("\n"),b=0;b=0?o.cameraz=o.zoom:o.cameraz=o.get_opt("cameraz",o.cameraz),o.camera_state=o.get_opt("camera_state",o.camera_state),o.allow_drag_and_drop&&o.set_drag_and_drop(!0)},o.is_ie=!!window.MSStream,this.MSG2WORKER_DATA=0,this.MSG2WORKER_LOAD=1,this.MSG2WORKER_ERROR=2,this.MSGFROMWORKER_STL_LOADED=3,this.MSGFROMWORKER_LOAD_IN_PROGRESS=4,this.load_model=function(e){return o.max_model_id=Math.max(o.max_model_id,e.id),e.filename||e.local_file?o.load_from_stl_file(e,!1):e.mesh?o.add_from_existing_mesh(e):void o.models_count--},this.add_from_existing_mesh=function(e){o.set_model_custom_props(e),e.mesh.model_id=e.id,o.set_geo_minmax(e),o.recalc_dims(e),e.color="#"+e.mesh.material.color.getHexString(),o.scene.add(e.mesh),o.model_loaded(e.id),o.check_loading_status(e,0,0),e.mesh.geometry.boundingBox||e.mesh.geometry.computeBoundingBox(),o.model_loaded_callback&&o.model_loaded_callback(e.id)},this.load_from_stl_file=function(e){var t=new Worker(("string"==typeof o.load_three_files?o.load_three_files:"")+"load_stl.min.js");t.onmessage=function(a){switch(a.data.msg_type){case o.MSGFROMWORKER_STL_LOADED:e.colors=a.data.colors;var i=o.vf_to_geo(a.data.vertices,a.data.faces,!!a.data.colors&&a.data.colors);if(i){var n=new THREE.MeshLambertMaterial({color:9474192,wireframe:!1,vertexColors:e.color?THREE.NoColors:THREE.FaceColors});o.is_ie||(n.side=THREE.DoubleSide),e.display||(e.display="flat"),o.set_material_display(e.display,n,i),e.mesh=new THREE.Mesh(i,n),o.set_model_custom_props(e),o.set_geo_minmax(e),e.mesh.model_id=e.id,o.recalc_dims(e),o.scene.add(e.mesh),o.model_loaded(e.id),o.model_loaded_callback&&o.model_loaded_callback(e.id)}else console.log("Error VF data ");t.terminate(),t=void 0,o.pre_loaded_ab_files&&e.filename&&o.pre_loaded_ab_files[e.filename]&&delete o.pre_loaded_ab_files[e.filename];break;case o.MSGFROMWORKER_LOAD_IN_PROGRESS:o.check_loading_status(e,a.data.loaded,a.data.total);break;case o.MSG2WORKER_ERROR:o.models_count--,o.model_error("ERROR: "+a.data.data,o.load_error_callback),o.pre_loaded_ab_files&&e.filename&&o.pre_loaded_ab_files[e.filename]&&delete o.pre_loaded_ab_files[e.filename]}},e.bytes_loaded=0,e.bytes_total=0;var a=null;o.pre_loaded_ab_files&&e.filename&&o.pre_loaded_ab_files[e.filename]&&(a=o.pre_loaded_ab_files[e.filename]),t.postMessage({msg_type:o.MSG2WORKER_DATA,data:e,load_from_blob_or_ab:a,get_progress:null!=o.loading_progress_callback}),t.postMessage({msg_type:o.MSG2WORKER_LOAD})},this.model_loaded=function(e){o.loaded_models_arr[e]=1,Object.keys(o.loaded_models_arr).length>=o.models_count&&(o.camera_state?o.camera_state=null:o.set_zoom(),o.set_light(),o.set_grid(!!o.grid),o.load_session++,o.all_loaded_callback&&o.all_loaded_callback())},this.set_grid=function(e,t,a){if(o.grid&&o.scene.remove(o.grid),o.grid=null,e){if(t||(t=2.5*Math.max(Math.abs(o.maxx),Math.abs(o.minx))),t<=0){var i=isNaN(window.innerHeight)?window.clientHeight:window.innerHeight,n=isNaN(window.innerWidth)?window.clientWidth:window.innerWidth;t=.8*Math.min(i,n)}a||(a=10),o.grid=new THREE.GridHelper(t,a),o.scene.add(o.grid)}},this.remove_model=function(e){if(void 0===o.models_ref[e])return o.model_error("remove_model - id not found: "+e);var t=o.models[o.models_ref[e]];t&&(delete o.models[o.models_ref[e]],delete o.models_ref[e],delete o.loaded_models_arr[e],o.max_model_id=-1,Object.keys(o.models_ref).forEach(function(e){o.max_model_id=Math.max(o.models[o.models_ref[e]].id,o.max_model_id)}),o.models_count=Object.keys(o.models).length,o.scene.remove(t.mesh))},this.zoom_done=!1,this.set_zoom=function(e,t){if(e&&(o.zoom=e),!o.zoom_done||t){o.zoom_done=!0;var a=Math.max(Math.abs(o.maxx-o.minx),Math.abs(o.maxy-o.miny),Math.abs(o.maxz-o.minz));o.camera.position.set(o.camera.position.x,o.camera.position.y,o.zoom>=0?o.zoom:2*a)}},this.get_camera_state=function(){if(!o.camera)return null;var e=new THREE.Vector3,t=new THREE.Vector3,a=new THREE.Vector3(0,0,0);return e.copy(o.camera.position),t.copy(o.camera.up),o.controls&&a.copy(o.controls.target),{position:e,up:t,target:a}},this.set_camera_state=function(e){if(!o.camera)return null;if(!e)return o.model_error("set_camera_state - no state vector");if(void 0!==e.position){if(void 0===e.position.x)return o.model_error("set_camera_state - invalid position x");if(void 0===e.position.y)return o.model_error("set_camera_state - invalid position y");if(void 0===e.position.z)return o.model_error("set_camera_state - invalid position z");o.camera.position.set(e.position.x,e.position.y,e.position.z)}if(void 0!==e.up){if(void 0===e.up.x)return o.model_error("set_camera_state invalid up x");if(void 0===e.up.y)return o.model_error("set_camera_state invalid up y");if(void 0===e.up.z)return o.model_error("set_camera_state invalid up z");o.camera.up.set(e.up.x,e.up.y,e.up.z)}if(o.controls&&void 0!==e.target){if(void 0===e.target.x)return o.model_error("set_camera_state - invalid target x");if(void 0===e.target.y)return o.model_error("set_camera_state - invalid target y");if(void 0===e.target.z)return o.model_error("set_camera_state - invalid target z");o.controls.target.set(e.target.x,e.target.y,e.target.z)}},this.set_center_models=function(e){o.center_models=e},this.set_light=function(){o.directionalLight.position.x=2*o.maxy,o.directionalLight.position.y=2*o.miny,o.directionalLight.position.z=2*o.maxz,o.pointLight.position.x=(o.miny+o.maxy)/2,o.pointLight.position.y=(o.miny+o.maxy)/2,o.pointLight.position.z=2*o.maxz},this.stop_auto_zoom=function(){o.zoom=o.camera.position.z,o.zoom_done=!0},this.set_camera=function(e,t,a){t&&(o.zoom=t),o.camera.position.set(o.is_empty(e)?o.camera.position.x:e,o.is_empty(t)?o.camera.position.y:t,o.is_empty(a)?o.camera.position.z:a)},this.set_auto_zoom=function(){o.set_zoom(-1)},this.check_loading_status=function(e,t,a){e&&(o.load_status[e.id]={loaded:t,total:a,load_session:o.load_session}),o.loading_progress_callback&&Object.keys(o.load_status).length==o.models_count&&o.loading_progress_callback(o.load_status,o.load_session)},this.set_edges=function(e,t){if(void 0===o.models_ref[e])return o.model_error("set_edges - id not found: "+e);var a=o.models[o.models_ref[e]];a&&o.set_or_update_geo_edges(a,t)},this.set_or_update_geo_edges=function(e,t,a){if(t&&!a||(e.edges&&o.scene.remove(e.edges),e.edges=null,t)){var i=!1;if(a=a||!1,!e.edges||a){var n=e.mesh.geometry;e.edges=new THREE.LineSegments(new THREE.EdgesGeometry(n),o.edges_material),i=!0}(e.x||e.y||e.z)&&e.edges.position.set(e.x?e.x:0,e.y?e.y:0,e.z?e.z:0),e.edges.rotation.setFromRotationMatrix(e.mesh.matrix),i&&o.scene.add(e.edges)}},this.set_model_custom_props=function(e){e.units=e.units?e.units:"mm",o.set_model_units(e.id,e.units,!0),e.x=e.x?e.x:0,e.y=e.y?e.y:0,e.z=e.z?e.z:0,e.mesh.position.set(e.x,e.y,e.z),e.color?o.update_mesh_color(e.mesh,e.color,!1):e.colors&&o.update_mesh_color(e.mesh,"#FFFFFF",!0),e.rotationx=e.rotationx?e.rotationx:0,e.rotationy=e.rotationy?e.rotationy:0,e.rotationz=e.rotationz?e.rotationz:0,(e.rotationx||e.rotationy||e.rotationz)&&o.set_rotation(e.id,e.rotationx,e.rotationy,e.rotationz);var t=void 0!==e.scale?e.scale:1,a=void 0!==e.scalex?e.scalex:t,i=void 0!==e.scaley?e.scaley:t,n=void 0!==e.scalez?e.scalez:t;e.scalex=a,e.scaley=i,e.scalez=n,1==a&&1==i&&1==n||o.scale_geo(e,a,i,n),e.view_edges&&o.set_or_update_geo_edges(e,!0),void 0!==e.opacity&&this.set_material_opacity(e.mesh.material,e.opacity),e.animation&&(o.animation[e.id]=1)},this.set_scale=function(e,t,a,i){if(void 0===o.models_ref[e])return o.model_error("set_scale - id not found: "+e);var n=o.models[o.models_ref[e]];if(n&&n.mesh&&n.mesh.geometry){var s=Math.max(n.scalex,.01),r=Math.max(n.scaley,.01),l=Math.max(n.scalez,.01);t&&(n.scalex=Math.max(t,.01)),n.scaley=Math.max(a||t,.01),n.scalez=Math.max(i||t,.01),o.scale_geo(n,n.scalex/s,n.scaley/r,n.scalez/l),n.edges&&o.set_or_update_geo_edges(n,!0,!0)}},this.scale_geo=function(e,t,o,a){e.mesh.geometry.scale(t,o,a)},this.recalc_dims=function(e){var t=e.mesh.geometry;o.maxx=o.maxx?Math.max(o.maxx,t.maxx+e.x):t.maxx+e.x,o.maxy=o.maxy?Math.max(o.maxy,t.maxy+e.y):t.maxy+e.y,o.maxz=o.maxz?Math.max(o.maxz,t.maxz+e.z):t.maxz+e.z,o.minx=o.maxx?Math.min(o.minx,t.minx+e.x):t.minx+e.x,o.miny=o.maxy?Math.min(o.miny,t.miny+e.y):t.miny+e.y,o.minz=o.maxz?Math.min(o.minz,t.minz+e.z):t.minz+e.z},this.update_mesh_color=function(e,t,o){null!=e&&("transparent"!=t?(e.traverse(function(e){e.visible=!0}),e.material.vertexColors=o?THREE.FaceColors:THREE.NoColors,o&&!t&&(t="#FFFFFF"),t&&e.material.color.set(parseInt(t.substr(1),16)),e.material.needsUpdate=!0):e.traverse(function(e){e.visible=!1}))},this.set_color=function(e,t){if(void 0===o.models_ref[e])return o.model_error("set_color - id not found: "+e);var a=o.models[o.models_ref[e]];a&&a.mesh&&(t.length<6||("#"!=t.charAt(0)&&(t="#"+t),a.color=t,o.update_mesh_color(a.mesh,t,!t&&a.colors)))},this.error_in_model=function(e){if(!e.id&&0!=e.id&&-1!=e.id)return o.model_error("missing id");if(!Number.isInteger(e.id))return o.model_error("invalid id");if(e.id<-1)return o.model_error("id must be positive");if(!e.filename&&!e.mesh&&!e.local_file){if(!e.name)return o.model_error("missing filename or mesh");e.filename=e.name}return o.models_ref[e.id]?o.model_error("such model ID already exists: "+e.id):null},this.model_error=function(e,t){return console.log(e),o.status=-1,o.error=e,t&&t(e),e},this.set_bg_color=function(e){"transparent"==e?this.renderer.setClearColor(0,0):this.renderer.setClearColor(e,1),o.bg_color=e},this.set_display=function(e,t){if(void 0===o.models_ref[e])return o.model_error("set_display - id not found: "+e);var a=o.models[o.models_ref[e]];a&&(o.set_material_display(t,a.mesh.material,a.mesh.geometry),a.display=t,a.mesh&&(a.mesh.normalsNeedUpdate=!0))},this.set_opacity=function(e,t){if(void 0===o.models_ref[e])return o.model_error("set_display - id not found: "+e);var a=o.models[o.models_ref[e]];a&&(a.opacity=t,this.set_material_opacity(a.mesh.material,t))},this.set_material_opacity=function(e,t){e&&(t<1?(e.opacity=t,e.transparent=!0):(e.opacity=1,e.transparent=!1))},this.onmousedown=function(e){e.stopPropagation(),e.preventDefault();var t=e.which;switch(e.type){case"touchstart":t=20;var a=e.touches[0]||e.changedTouches[0];o.mouse.x=(a.pageX-o.parent_element.offsetLeft)/o.parent_element.clientWidth*2-1,o.mouse.y=-(a.pageY-o.parent_element.offsetTop)/o.parent_element.clientHeight*2+1;break;case"dblclick":t=11;default:o.mouse.x=(e.clientX-o.parent_element.offsetLeft)/o.parent_element.clientWidth*2-1,o.mouse.y=-(e.clientY-o.parent_element.offsetTop)/o.parent_element.clientHeight*2+1}o.raycaster.setFromCamera(o.mouse,o.camera);var i=o.raycaster.intersectObjects(o.scene.children);if(i.length>0){if(void 0===i[0].object.model_id)return;o.onmousedown_callback&&o.onmousedown_callback(i[0].object.model_id,e,i[0].distance,t)}else o.send_no_model_click_event&&o.onmousedown_callback(null,e,0,t)},this.is_empty=function(e){return!e&&0!==e},this.set_model_units=function(e,t,a){if(void 0===o.models_ref[e])return o.model_error("set_model_units - id not found: "+e);var i=o.models[o.models_ref[e]];if(i&&i.mesh){var n=1;switch(t){case"mm":a&&"inch"==i.units&&(n=1/25.4),i.units="mm";break;case"inch":a&&"mm"==i.units&&(n=25.4),i.units="inch";break;default:return o.model_error("set_model_units - invalid unit "+t)}1!=n&&o.set_scale(i.id,i.scalex*n,i.scaley*n,i.scalez*n)}},this.set_position=function(e,t,a,i){if(void 0===o.models_ref[e])return o.model_error("set_position - id not found: "+e);var n=o.models[o.models_ref[e]];n&&n.mesh&&(n.x=o.is_empty(t)?n.x:t,n.y=o.is_empty(a)?n.y:a,n.z=o.is_empty(i)?n.z:i,n.mesh.position.set(n.x,n.y,n.z),n.edges&&o.set_or_update_geo_edges(n,!0,!0))},this.set_material_display=function(e,t,o){switch(e.toLowerCase()){case"wireframe":t.wireframe=!0;break;case"smooth":t.wireframe=!1,t.flatShading=!1,o&&(o.mergeVertices(),o.computeVertexNormals());break;case"flat":t.wireframe=!1,t.flatShading=!0,o&&o.computeFlatVertexNormals()}},this.set_rotation=function(e,t,a,i,n){if(void 0===o.models_ref[e])return o.model_error("rotate - id not found: "+e);var s=o.models[o.models_ref[e]];if(s){var r=n?1:0;void 0!==t&&(s.rotationx=t+s.mesh.rotation.x*r,s.mesh.rotation.x=s.rotationx),void 0!==a&&(s.rotationy=a+s.mesh.rotation.y*r,s.mesh.rotation.y=s.rotationy),void 0!==i&&(s.rotationz=i+s.mesh.rotation.z*r,s.mesh.rotation.z=s.rotationz),s.mesh.updateMatrixWorld(),s.edges&&o.set_or_update_geo_edges(s,!0)}},this.rotate=function(e,t,a,i){o.set_rotation(e,t,a,i,!0)},this.basename=function(e){return e.split(/[\\\/]/).pop()},this.get_model_filename=function(e,t,a,i){var n=null;return e.orig_url&&!i?n=decodeURIComponent(e.orig_url):e.orig_filename?n=e.orig_filename:e.temp_filename?n=e.temp_filename:e.local_file?e.local_file.name&&(n=e.local_file.name):e.filename&&(e.filename instanceof File&&(n=File.name),n=e.filename),!n&&t?"model_"+e.id+".stl":n?(a&&(n=o.basename(n)),n):null},this.add_model=function(e,t){if(Array.isArray(e))return o.add_models(e);if(!o.ready)return o.models_to_add.push(e),o.model_error("THREE JS files are not ready");var a=o.get_model_filename(e);if(a)switch(a.split(".").pop()){case"vsj":return o.load_vsj(e.local_file?e.local_file:a);case"vsb":return o.load_vsb(e.local_file?e.local_file:a)}void 0===e.id&&(e.id=-1);var i=o.error_in_model(e);if(i)return i;-1==e.id&&(e.id=++o.max_model_id),o.models.push(e);var n=o.models.indexOf(e);return t||void 0===o.models_ref[e.id]&&o.models_count++,o.models_ref[e.id]=n,o.load_model(e),o.status},this.add_models=function(e){if(!Array.isArray(e))return o.add_model(e);o.status=0;var t=Object.keys(e);return t.forEach(function(a){var i=o.get_model_filename(e[t[a]]);if(i)switch(i.split(".").pop()){case"vsj":case"vsb":break;default:void 0===o.models_ref[e[a].id]&&o.models_count++}else void 0===o.models_ref[e[a].id]&&o.models_count++}),t.forEach(function(t){o.add_model(e[t],!0)}),o.status},this.calc_volume_and_area=function(e,t){var o,a,i,n,s,r,l,d,_,c,m,u,f,h,p=e.faces.length,g=0,v=0;for(c=0;c=0&&(i=i.substring(0,n)),i.length<1&&(i="1"),window.navigator.msSaveOrOpenBlob?window.navigator.msSaveBlob(t,i+".vsj"):(a.download=i+".vsj",a.click(),URL.revokeObjectURL(a.href))},this.load_vsj=function(e){if(!e)return o.pre_loaded_vsj?(stl_viewer.init_by_json(o.pre_loaded_vsj),o.pre_loaded_vsj=null,!0):o.model_error("load_vsj - invalid filename"+e,o.load_error_callback);if(e instanceof File)return o.read_bin_file(e,o.init_by_json,null,!0);var t=new XMLHttpRequest;t.onreadystatechange=function(e){4==t.readyState&&200==t.status&&o.init_by_json(t.response.trim())},t.open("GET",e,!0),t.send(null)},this.padend=function(e,t,o){return t>>=0,o=String(void 0!==o?o:" "),e.length>t?String(e):((t-=e.length)>o.length&&(o+=o.repeat(t/o.length)),String(e)+o.slice(0,t))},this.get_normal=function(e,t,o){var a=t.x-e.x,i=t.y-e.y,n=t.z-e.z,s=o.x-e.x,r=o.y-e.y,l=o.z-e.z,d={x:0,y:0,z:0};d.x=i*l-n*r,d.y=n*s-a*l,d.z=a*r-i*s;var _=Math.sqrt(d.x*d.x+d.y*d.y+d.z*d.z);return 0!=_&&(d.x/=_,d.y/=_,d.z/=_),d},this.get_stl_bin=function(e){if(void 0===o.models_ref[e])return o.model_error("get_stl_bin - id not found: "+e);var t=o.models[o.models_ref[e]];if(t&&t.mesh){var a=t.mesh.geometry;if(a){for(var i=new ArrayBuffer(84+50*a.faces.length),n=new DataView(i),s=(new TextEncoder,o.padend("Binary"+(t.colors?" colored":"")+" STL by viewstl.com",80," ")),r=0;r<80;r++)n.setUint8(r,s.charCodeAt(r),!0);n.setUint32(80,a.faces.length,!0);var l=84;return Object.keys(a.faces).forEach(function(e){var i=a.faces[e],s=a.vertices[i.a],r=a.vertices[i.b],d=a.vertices[i.c],_=o.get_normal(s,r,d);n.setFloat32(l,_.x,!0),l+=4,n.setFloat32(l,_.y,!0),l+=4,n.setFloat32(l,_.z,!0),l+=4,n.setFloat32(l,s.x,!0),l+=4,n.setFloat32(l,s.y,!0),l+=4,n.setFloat32(l,s.z,!0),l+=4,n.setFloat32(l,r.x,!0),l+=4,n.setFloat32(l,r.y,!0),l+=4,n.setFloat32(l,r.z,!0),l+=4,n.setFloat32(l,d.x,!0),l+=4,n.setFloat32(l,d.y,!0),l+=4,n.setFloat32(l,d.z,!0),l+=4,t.colors?n.setUint16(l,Math.ceil(31*i.color.r)|Math.ceil(31*i.color.g)<<5|Math.ceil(31*i.color.b)<<10,!0):n.setUint16(l,0,!0),l+=2}),i}}},this.basename=function(e){return e.substr(e.lastIndexOf("/")+1)},this.json_without_nulls=function(e){return JSON.stringify(e).split(",null").join("")},this.download_vsb=function(e){var t=null;try{t=new JSZip}catch(e){return console.log("download_vsb - JSZip is missing ",e.message),!1}var a=o.get_vsb();t.file("json_data.vsj",o.json_without_nulls(a.vsj)),Object.keys(a.files).forEach(function(e){var i=o.get_model_filename(a.vsj.models[o.models_ref[a.files[e].id]],!0,!0);t.file(i,a.files[e].bin)}),t.generateAsync({type:"blob"}).then(function(t){var o=new Blob([t],{type:"application/zip"}),a=document.createElement("a");a.href=window.URL.createObjectURL(o);var i=e||"1",n=i.toLowerCase().indexOf(".vsb");n>=0&&(i=i.substring(0,n)),i.length<1&&(i="1"),window.navigator.msSaveOrOpenBlob?window.navigator.msSaveBlob(o,i+".vsb"):(a.download=i+".vsb",a.click(),URL.revokeObjectURL(a.href))})},this.load_vsb=function(e){if(o.pre_loaded_ab_files=[],o.pre_loaded_vsj=null,e instanceof File)return o.read_bin_file(e,o.load_vsb_from_blob);JSZipUtils.getBinaryContent(decodeURIComponent(e),function(e,t){if(e)return o.model_error("load_vsb "+e,o.load_error_callback);o.load_vsb_from_blob(t)})},this.read_bin_file=function(e,t,o,a){var i=new FileReader;i.onerror=function(e){return console.log("reading file error",e),null},i.onload=function(e){return t(e.target.result)},o&&(i.onprogress=function(e){o({loaded:e.loaded,total:e.total,load_session:-1},-1)}),a?i.readAsText(e):i.readAsArrayBuffer(e)},this.load_vsb_from_blob=function(e){var t=null;try{t=new JSZip}catch(e){return console.log("load vsb - JSZip is missing ",e.message),!1}t.loadAsync(e).then(function(){o.zip_load_count=Object.keys(t.files).length,t.forEach(function(e,a){"json_data.vsj"==a.name?t.files[a.name].async("string").then(function(e){o.pre_loaded_vsj=e,o.zip_load_count--,0==o.zip_load_count&&o.load_vsj(null)}):t.files[a.name].async("blob").then(function(e){o.pre_loaded_ab_files[a.name]=e,o.zip_load_count--,0==o.zip_load_count&&o.load_vsj(null)})})})},this.load_bin_to_ab_file=function(e,t){o.pre_loaded_ab_files||(o.pre_loaded_ab_files=[]),o.pre_loaded_ab_files[e]=t,o.add_model({id:-1,filename:e})},this.download_model=function(e,t){if(void 0===o.models_ref[e])return o.model_error("download_model - id not found: "+e);var a=o.models[o.models_ref[e]];if(a&&a.mesh){var i=new Blob([o.get_stl_bin(e)],{type:"application/sla"}),n=document.createElement("a");n.href=window.URL.createObjectURL(i);var s=o.get_model_filename(a,!0,!0,!0),r=s.toLowerCase().indexOf(".stl");r>=0&&(s=s.substring(0,r)),s.length<1&&(s="1"),window.navigator.msSaveOrOpenBlob?window.navigator.msSaveBlob(i,s+".stl"):(n.download=s+".stl",n.click(),URL.revokeObjectURL(n.href))}},this.get_model_mesh=function(e){if(void 0===o.models_ref[e])return o.model_error("get_model_mesh - id not found: "+e);var t=o.models[o.models_ref[e]];if(t&&t.mesh){var a=t.mesh.clone();return a.geometry=t.mesh.geometry.clone(),a.material=t.mesh.material.clone(),a}},this.set_auto_rotate=function(e){o.controls.autoRotate=e},this.set_mouse_zoom=function(e){o.controls.noZoom=!e},this.WORLD_X_VECTOR=null,this.WORLD_Y_VECTOR=null,this.WORLD_Z_VECTOR=null,this.maxx=null,this.maxy=null,this.maxz=null,this.minx=null,this.miny=null,this.minz=null,this.edges_material=null,this.raycaster=null,this.mouse=null,this.scene=null,this.is_webgl=null,this.renderer=null,this.camera=null,this.ambientLight=null,this.directionalLight=null,this.pointLight=null,this.controls=null,this.do_resize=function(){var e=o.parent_element.getBoundingClientRect(),t=e.width,a=e.height;o.camera.aspect=t/a,o.camera.updateProjectionMatrix(),o.renderer.setSize(t-5,a-5)},this.animation=new Array,this.animate=function(){Object.keys(o.animation).forEach(function(e){void 0!==o.models_ref[e]&&o.do_model_animation(o.models[o.models_ref[e]])}),requestAnimationFrame(o.animate),o.renderer.render(o.scene,o.camera),o.controls&&o.controls.update()},this.do_model_animation=function(e){if(e.animation){var t=Date.now();if(e.animation.start_time||(e.animation.start_time=t),e.animation.delta){var a=(t-e.animation.start_time)/e.animation.delta.msec,i=e.animation.last_time?(t-e.animation.last_time)/e.animation.delta.msec:a;if(o.animation_next_delta(e,e.animation.delta,i),a>=1){if(!e.animation.delta.loop)return void o.remove_model_animation(e,!0);e.animation.delta.start_time=null}}if(e.animation.exact){i=(t-(e.animation.last_time?e.animation.last_time:e.animation.start_time))/e.animation.exact.msec;if(o.animation_next_exact(e,e.animation.exact,i),t>=e.animation.start_time+e.animation.exact.msec)return void o.remove_model_animation(e,!1,!0)}e.animation.last_time=t}},this.animation_next_delta=function(e,t,a){var i=!1,n=!1,s=!1;Object.keys(t).forEach(function(r){switch(r){case"x":case"y":case"z":i||(i=!0,o.set_position(e.id,e.x+(void 0!==t.x?t.x*a:0),e.y+(void 0!==t.y?t.y*a:0),e.z+(void 0!==t.z?t.z*a:0)));break;case"rotationx":case"rotationy":case"rotationz":n||(n=!0,o.rotate(e.id,void 0!==t.rotationx?t.rotationx*a:0,void 0!==t.rotationy?t.rotationy*a:0,void 0!==t.rotationz?t.rotationz*a:0));break;case"scale":case"scalex":case"scaley":case"scalez":s||(s=!0,t.scalex=t.scalex?t.scalex:t.scale?t.scale:null,t.scaley=t.scaley?t.scaley:t.scale?t.scale:null,t.scalez=t.scalez?t.scalez:t.scale?t.scale:null,o.set_scale(e.id,e.scalex+(void 0!==t.scalex?t.scalex*a:0),e.scaley+(void 0!==t.scaley?t.scaley*a:0),e.scalez+(void 0!==t.scalez?t.scalez*a:0)))}})},this.animation_next_exact=function(e,t,a){var i=!1,n=!1,s=!1;Object.keys(t).forEach(function(r){switch(r){case"x":case"y":case"z":i||(i=!0,void 0===t.xtotal&&(t.xtotal=t.x-e.x),void 0===t.ytotal&&(t.ytotal=t.y-e.y),void 0===t.ztotal&&(t.ztotal=t.z-e.z),o.set_position(e.id,e.x+(void 0!==t.x?t.xtotal*a:0),e.y+(void 0!==t.y?t.ytotal*a:0),e.z+(void 0!==t.z?t.ztotal*a:0)));break;case"rotationx":case"rotationy":case"rotationz":if(!n){n=!0;var l=e.mesh.getWorldRotation();void 0===t.rotxtotal&&(t.rotxtotal=t.rotationx-l.x),void 0===t.rotytotal&&(t.rotytotal=t.rotationy-l.y),void 0===t.rotztotal&&(t.rotztotal=t.rotationz-l.z),o.rotate(e.id,void 0!==t.rotationx?t.rotxtotal*a:0,void 0!==t.rotationy?t.rotytotal*a:0,void 0!==t.rotationz?t.rotztotal*a:0)}break;case"scale":case"scalex":case"scaley":case"scalez":s||(s=!0,t.scalex=t.scalex?t.scalex:t.scale?t.scale:null,t.scaley=t.scaley?t.scaley:t.scale?t.scale:null,t.scalez=t.scalez?t.scalez:t.scale?t.scale:null,void 0===t.scalextotal&&(t.scalextotal=t.scalex-e.scalex),void 0===t.scaleytotal&&(t.scaleytotal=t.scaley-e.scaley),void 0===t.scaleztotal&&(t.scaleztotal=t.scalez-e.scalez),o.set_scale(e.id,e.scalex+(void 0!==t.scalex?t.scalextotal*a:0),e.scaley+(void 0!==t.scaley?t.scaleytotal*a:0),e.scalez+(void 0!==t.scalez?t.scaleztotal*a:0)))}})},this.remove_model_animation=function(e,t,a){t&&(e.animation.delta=null),a&&(e.animation.exact=null),e.animation.delta||e.animation.exact||(e.animation=null,delete o.animation[e.id])},this.animate_model=function(e,t){if(void 0===o.models_ref[e])return o.model_error("animate-model - id not found: "+e);var a=o.models[o.models_ref[e]];if(a){if(!t)return o.remove_model_animation(a,!0,!0);a.animation=JSON.parse(JSON.stringify(t)),a.animation.delta&&(a.animation.delta.msec||(a.animation.delta.msec=300)),a.animation.exact&&(a.animation.exact.msec||(a.animation.exact.msec=300)),o.animation[e]=1}},this.init_done=!1,this.init=function(){if(!o.init_done){switch(o.WORLD_X_VECTOR=new THREE.Vector3(1,0,0),o.WORLD_Y_VECTOR=new THREE.Vector3(0,1,0),o.WORLD_Z_VECTOR=new THREE.Vector3(0,0,1),o.edges_material=new THREE.LineBasicMaterial({color:0}),o.raycaster=new THREE.Raycaster,o.mouse=new THREE.Vector2,o.scene=new THREE.Scene,o.is_webgl=webgl_Detector.webgl,o.renderer=o.is_webgl?new THREE.WebGLRenderer({preserveDrawingBuffer:!0,alpha:!0}):new THREE.CanvasRenderer({alpha:!0}),o.camera=new THREE.PerspectiveCamera(45,1,.1,1e4),o.parent_element.appendChild(o.renderer.domElement),o.scene.add(o.camera),o.ambientLight=new THREE.AmbientLight(2105376),o.camera.add(o.ambientLight),o.directionalLight=new THREE.DirectionalLight(16777215,.75),o.directionalLight.position.x=1,o.directionalLight.position.y=1,o.directionalLight.position.z=2,o.directionalLight.position.normalize(),o.camera.add(o.directionalLight),o.pointLight=new THREE.PointLight(16777215,.3),o.pointLight.position.x=0,o.pointLight.position.y=-25,o.pointLight.position.z=10,o.camera.add(o.pointLight),o.controls_type){case 1:o.controls=new THREE.TrackballControls(o.camera,o.renderer.domElement);break;default:o.controls=new THREE.OrbitControls(o.camera,o.renderer.domElement),o.controls.autoRotate=o.auto_rotate}o.set_on_model_mousedown(o.onmousedown_callback)}o.set_bg_color(o.bg_color),!1===o.mouse_zoom&&o.set_mouse_zoom(o.mouse_zoom),o.camera_state?o.set_camera_state(o.camera_state):o.camera.position.set(o.camerax,o.cameray,o.cameraz),o.do_resize(),o.models_to_add&&o.add_models(o.models_to_add),o.set_auto_resize(o.auto_resize),o.animate(),o.init_done=!0},this.set_auto_resize=function(e){o.do_resize&&(window.removeEventListener("resize",o.do_resize),e&&window.addEventListener("resize",o.do_resize))},this.vf_to_geo=function(e,t,a){if(!e)return null;if(!t)return null;var n=[],s=[],r=e.length;for(i=0;is&&(s=o[d].x),o[d].y>r&&(r=o[d].y),o[d].z>l&&(l=o[d].z);t.minx=a+e.x,t.miny=i+e.y,t.minz=n+e.z,t.maxx=s+e.x,t.maxy=r+e.y,t.maxz=l+e.z}},this.handleDragOver=function(e){e.stopPropagation(),e.preventDefault(),e.dataTransfer.dropEffect="copy"},this.handleFileDrop=function(e){e.stopPropagation(),e.preventDefault(),e.dataTransfer.files.length>0?o.load_local_files(e.dataTransfer.files):"string"==typeof e.dataTransfer.getData("Text")&&(o.add_model({id:-1,filename:e.dataTransfer.getData("Text")}),o.on_model_drop&&o.on_model_drop(e.dataTransfer.getData("Text")))},this.load_local_files=function(e){if(e.length>0){for(var t=new Array,a=e.length,i=0;i0?e.substring(0,t+1):""}}(); \ No newline at end of file +//=========== Stl Viewer v1.12, by Omri Rips, Viewstl.com, February 2021 ; admin@viewstl.com =========== +var stl_viewer_script_path="";function StlViewer(e,t){e||console.log("error: no parent element");var o=this;this.error=null,this.options=t,this.parent_element=e,this.get_opt=function(e,t){return o.options?!1!==o.options[e]&&(o.options[e]?o.options[e]:t):t},this.canvas_width="100%",this.canvas_height="100%",this.bg_color="transparent",this.models_to_add=[],this.models=new Array,this.models_count=0,this.models_ref=new Array,this.allow_drag_and_drop=o.get_opt("allow_drag_and_drop",!0),this.model_loaded_callback=null,this.all_loaded_callback=null,this.load_error_callback=null,this.loading_progress_callback=null,this.max_model_id=0,this.load_status=new Array,this.load_session=0,this.loaded_models_arr=new Array,this.status=0,this.onmousedown_callback=null,this.zoom=-1,this.camerax=0,this.cameray=0,this.cameraz=0,this.camera_state=null,this.auto_rotate=!1,this.mouse_zoom=!0,this.load_three_files=o.get_opt("load_three_files",stl_viewer_script_path),this.ready="undefined"!=typeof THREE,this.ready_callback=null,this.auto_resize=!0,this.on_model_drop=null,this.center_models=!0,this.controls_type=0,this.zoom=-1,this.pre_loaded_ab_files=null,this.pre_loaded_vsj=null,this.zip_load_count=-1,this.send_no_model_click_event=!1,this.grid=null,this.killsign=!1,this.set_on_model_mousedown=function(e){o.onmousedown_callback=e,o.onmousedown_callback&&(o.parent_element.addEventListener("mousedown",o.onmousedown),o.parent_element.addEventListener("dblclick",o.onmousedown),o.parent_element.addEventListener("touchstart",o.onmousedown))},this.set_drag_and_drop=function(e){e?(o.parent_element.addEventListener("dragover",o.handleDragOver),o.parent_element.addEventListener("drop",o.handleFileDrop)):(o.parent_element.removeEventListener("dragover",o.handleDragOver),o.parent_element.removeEventListener("drop",o.handleFileDrop))},this.set_options=function(){o.canvas_width=o.get_opt("width",o.canvas_width),o.canvas_height=o.get_opt("height",o.canvas_height),o.bg_color=o.get_opt("bg_color",o.bg_color),o.models_to_add=o.get_opt("models",o.models_to_add),o.model_loaded_callback=o.get_opt("model_loaded_callback",o.model_loaded_callback),o.all_loaded_callback=o.get_opt("all_loaded_callback",o.all_loaded_callback),o.load_error_callback=o.get_opt("load_error_callback",o.load_error_callback),o.loading_progress_callback=o.get_opt("loading_progress_callback",o.loading_progress_callback),o.onmousedown_callback=o.get_opt("on_model_mousedown",o.onmousedown_callback),o.onmousedown_callback||(o.onmousedown_callback=o.get_opt("on_model_mouseclick",null)),o.send_no_model_click_event=o.get_opt("send_no_model_click_event",o.send_no_model_click_event),o.zoom=o.get_opt("zoom",o.zoom),o.camerax=o.get_opt("camerax",o.camerax),o.cameray=o.get_opt("cameray",o.cameray),o.auto_rotate=o.get_opt("auto_rotate",o.auto_rotate),o.mouse_zoom=o.get_opt("mouse_zoom",o.mouse_zoom),o.ready_callback=o.get_opt("ready_callback",null),o.auto_resize=o.get_opt("auto_resize",o.auto_resize),o.on_model_drop=o.get_opt("on_model_drop",o.on_model_drop),o.center_models=o.get_opt("center_models",o.center_models),o.controls_type=o.get_opt("controls",o.controls_type),o.grid=o.get_opt("grid",!!o.grid),o.zoom>=0?o.cameraz=o.zoom:o.cameraz=o.get_opt("cameraz",o.cameraz),o.camera_state=o.get_opt("camera_state",o.camera_state),o.allow_drag_and_drop&&o.set_drag_and_drop(!0)},o.is_ie=!!window.MSStream,this.MSG2WORKER_DATA=0,this.MSG2WORKER_LOAD=1,this.MSG2WORKER_ERROR=2,this.MSGFROMWORKER_STL_LOADED=3,this.MSGFROMWORKER_LOAD_IN_PROGRESS=4,this.load_model=function(e){return o.max_model_id=Math.max(o.max_model_id,e.id),e.filename||e.local_file?o.load_from_stl_file(e,!1):e.mesh?o.add_from_existing_mesh(e):void o.models_count--},this.add_from_existing_mesh=function(e){o.set_model_custom_props(e),e.mesh.model_id=e.id,o.set_geo_minmax(e),o.recalc_dims(e),e.color="#"+e.mesh.material.color.getHexString(),o.scene.add(e.mesh),o.model_loaded(e.id),o.check_loading_status(e,0,0),e.mesh.geometry.boundingBox||e.mesh.geometry.computeBoundingBox(),o.model_loaded_callback&&o.model_loaded_callback(e.id)},this.load_from_stl_file=function(e){var t=new Worker(("string"==typeof o.load_three_files?o.load_three_files:"")+"load_stl.min.js");t.onmessage=function(a){switch(a.data.msg_type){case o.MSGFROMWORKER_STL_LOADED:e.colors=a.data.colors;var i=o.vf_to_geo(a.data.vertices,a.data.faces,!!a.data.colors&&a.data.colors);if(i){var n=new THREE.MeshLambertMaterial({color:9474192,wireframe:!1,vertexColors:e.color?THREE.NoColors:THREE.FaceColors});o.is_ie||(n.side=THREE.DoubleSide),e.display||(e.display="flat"),o.set_material_display(e.display,n,i),e.mesh=new THREE.Mesh(i,n),o.set_model_custom_props(e),o.set_geo_minmax(e),e.mesh.model_id=e.id,o.recalc_dims(e),o.scene.add(e.mesh),o.model_loaded(e.id),o.model_loaded_callback&&o.model_loaded_callback(e.id)}else console.log("Error VF data ");t.terminate(),t=void 0,o.pre_loaded_ab_files&&e.filename&&o.pre_loaded_ab_files[e.filename]&&delete o.pre_loaded_ab_files[e.filename];break;case o.MSGFROMWORKER_LOAD_IN_PROGRESS:o.check_loading_status(e,a.data.loaded,a.data.total);break;case o.MSG2WORKER_ERROR:o.models_count--,o.model_error("ERROR: "+a.data.data,o.load_error_callback),o.pre_loaded_ab_files&&e.filename&&o.pre_loaded_ab_files[e.filename]&&delete o.pre_loaded_ab_files[e.filename]}},e.bytes_loaded=0,e.bytes_total=0;var a=null;o.pre_loaded_ab_files&&e.filename&&o.pre_loaded_ab_files[e.filename]&&(a=o.pre_loaded_ab_files[e.filename]),t.postMessage({msg_type:o.MSG2WORKER_DATA,data:e,load_from_blob_or_ab:a,get_progress:null!=o.loading_progress_callback}),t.postMessage({msg_type:o.MSG2WORKER_LOAD})},this.model_loaded=function(e){o.loaded_models_arr[e]=1,Object.keys(o.loaded_models_arr).length>=o.models_count&&(o.camera_state?o.camera_state=null:o.set_zoom(),o.set_light(),o.set_grid(!!o.grid),o.load_session++,o.all_loaded_callback&&o.all_loaded_callback())},this.set_grid=function(e,t,a){if(o.grid&&o.scene.remove(o.grid),o.grid=null,e){if(t||(t=2.5*Math.max(Math.abs(o.maxx),Math.abs(o.minx))),t<=0){var i=isNaN(window.innerHeight)?window.clientHeight:window.innerHeight,n=isNaN(window.innerWidth)?window.clientWidth:window.innerWidth;t=.8*Math.min(i,n)}a||(a=10),o.grid=new THREE.GridHelper(t,a),o.scene.add(o.grid)}},this.remove_model=function(e){if(void 0===o.models_ref[e])return o.model_error("remove_model - id not found: "+e);var t=o.models[o.models_ref[e]];t&&(o.set_or_update_geo_edges(t,!1),delete o.models[o.models_ref[e]],delete o.models_ref[e],delete o.loaded_models_arr[e],o.max_model_id=-1,Object.keys(o.models_ref).forEach(function(e){o.max_model_id=Math.max(o.models[o.models_ref[e]].id,o.max_model_id)}),o.models_count=Object.keys(o.models).length,o.scene.remove(t.mesh))},this.zoom_done=!1,this.set_zoom=function(e,t){if(e&&(o.zoom=e),o.zoom_done&&!t)return;o.zoom_done=!0;var a=o.zoom;o.zoom<0&&(a=o.calc_z_for_auto_zoom()),o.camera.position.set(o.camera.position.x,o.camera.position.y,a);const i=o.minz,n=i<0?-i+a:a-i;o.camera.far=3*n,o.camera.updateProjectionMatrix()},this.calc_z_for_auto_zoom=function(e){e=e||1.01;const t=new THREE.Box3(new THREE.Vector3(o.minx,o.miny,o.minz),new THREE.Vector3(o.maxx,o.maxy,o.maxz));var a=new THREE.Vector3;t.getSize(a);const i=o.camera.fov*(Math.PI/180),n=2*Math.atan(Math.tan(i/2)*o.camera.aspect);let l=a.z/2+Math.abs(a.x/2/Math.tan(n/2)),s=a.z/2+Math.abs(a.y/2/Math.tan(i/2)),r=Math.max(l,s);return r*=e},this.get_camera_state=function(){if(!o.camera)return null;var e=new THREE.Vector3,t=new THREE.Vector3,a=new THREE.Vector3(0,0,0);return e.copy(o.camera.position),t.copy(o.camera.up),o.controls&&a.copy(o.controls.target),{position:e,up:t,target:a}},this.set_camera_state=function(e){if(!o.camera)return null;if(!e)return o.model_error("set_camera_state - no state vector");if(void 0!==e.position){if(void 0===e.position.x)return o.model_error("set_camera_state - invalid position x");if(void 0===e.position.y)return o.model_error("set_camera_state - invalid position y");if(void 0===e.position.z)return o.model_error("set_camera_state - invalid position z");o.camera.position.set(e.position.x,e.position.y,e.position.z)}if(void 0!==e.up){if(void 0===e.up.x)return o.model_error("set_camera_state invalid up x");if(void 0===e.up.y)return o.model_error("set_camera_state invalid up y");if(void 0===e.up.z)return o.model_error("set_camera_state invalid up z");o.camera.up.set(e.up.x,e.up.y,e.up.z)}if(o.controls&&void 0!==e.target){if(void 0===e.target.x)return o.model_error("set_camera_state - invalid target x");if(void 0===e.target.y)return o.model_error("set_camera_state - invalid target y");if(void 0===e.target.z)return o.model_error("set_camera_state - invalid target z");o.controls.target.set(e.target.x,e.target.y,e.target.z)}},this.set_center_models=function(e){o.center_models=e},this.set_light=function(){o.directionalLight.position.x=2*o.maxy,o.directionalLight.position.y=2*o.miny,o.directionalLight.position.z=2*o.maxz,o.pointLight.position.x=(o.miny+o.maxy)/2,o.pointLight.position.y=(o.miny+o.maxy)/2,o.pointLight.position.z=2*o.maxz},this.stop_auto_zoom=function(){o.zoom=o.camera.position.z,o.zoom_done=!0},this.set_camera=function(e,t,a){t&&(o.zoom=t),o.camera.position.set(o.is_empty(e)?o.camera.position.x:e,o.is_empty(t)?o.camera.position.y:t,o.is_empty(a)?o.camera.position.z:a)},this.set_auto_zoom=function(){o.set_zoom(-1)},this.check_loading_status=function(e,t,a){e&&(o.load_status[e.id]={loaded:t,total:a,load_session:o.load_session}),o.loading_progress_callback&&Object.keys(o.load_status).length==o.models_count&&o.loading_progress_callback(o.load_status,o.load_session)},this.set_edges=function(e,t){if(void 0===o.models_ref[e])return o.model_error("set_edges - id not found: "+e);var a=o.models[o.models_ref[e]];a&&o.set_or_update_geo_edges(a,t)},this.set_or_update_geo_edges=function(e,t,a){if(t&&!a||(e.edges&&o.scene.remove(e.edges),e.edges=null,t)){var i=!1;if(a=a||!1,!e.edges||a){var n=e.mesh.geometry;e.edges=new THREE.LineSegments(new THREE.EdgesGeometry(n),o.edges_material),i=!0}(e.x||e.y||e.z)&&e.edges.position.set(e.x?e.x:0,e.y?e.y:0,e.z?e.z:0),e.edges.rotation.setFromRotationMatrix(e.mesh.matrix),i&&o.scene.add(e.edges)}},this.set_model_custom_props=function(e){e.units=e.units?e.units:"mm",o.set_model_units(e.id,e.units,!0),e.x=e.x?e.x:0,e.y=e.y?e.y:0,e.z=e.z?e.z:0,e.mesh.position.set(e.x,e.y,e.z),e.color?o.update_mesh_color(e.mesh,e.color,!1):e.colors&&o.update_mesh_color(e.mesh,"#FFFFFF",!0),e.rotationx=e.rotationx?e.rotationx:0,e.rotationy=e.rotationy?e.rotationy:0,e.rotationz=e.rotationz?e.rotationz:0,(e.rotationx||e.rotationy||e.rotationz)&&o.set_rotation(e.id,e.rotationx,e.rotationy,e.rotationz);var t=void 0!==e.scale?e.scale:1,a=void 0!==e.scalex?e.scalex:t,i=void 0!==e.scaley?e.scaley:t,n=void 0!==e.scalez?e.scalez:t;e.scalex=a,e.scaley=i,e.scalez=n,1==a&&1==i&&1==n||o.scale_geo(e,a,i,n),e.view_edges&&o.set_or_update_geo_edges(e,!0),void 0!==e.opacity&&this.set_material_opacity(e.mesh.material,e.opacity),e.animation&&(o.animation[e.id]=1)},this.set_scale=function(e,t,a,i){if(void 0===o.models_ref[e])return o.model_error("set_scale - id not found: "+e);var n=o.models[o.models_ref[e]];if(n&&n.mesh&&n.mesh.geometry){var l=Math.max(n.scalex,.01),s=Math.max(n.scaley,.01),r=Math.max(n.scalez,.01);t&&(n.scalex=Math.max(t,.01)),n.scaley=Math.max(a||t,.01),n.scalez=Math.max(i||t,.01),o.scale_geo(n,n.scalex/l,n.scaley/s,n.scalez/r),n.edges&&o.set_or_update_geo_edges(n,!0,!0)}},this.scale_geo=function(e,t,o,a){e.mesh.geometry.scale(t,o,a)},this.recalc_dims=function(e){var t=e.mesh.geometry;o.maxx=o.maxx?Math.max(o.maxx,t.maxx+e.x):t.maxx+e.x,o.maxy=o.maxy?Math.max(o.maxy,t.maxy+e.y):t.maxy+e.y,o.maxz=o.maxz?Math.max(o.maxz,t.maxz+e.z):t.maxz+e.z,o.minx=o.maxx?Math.min(o.minx,t.minx+e.x):t.minx+e.x,o.miny=o.maxy?Math.min(o.miny,t.miny+e.y):t.miny+e.y,o.minz=o.maxz?Math.min(o.minz,t.minz+e.z):t.minz+e.z},this.update_mesh_color=function(e,t,o){null!=e&&("transparent"!=t?(e.traverse(function(e){e.visible=!0}),e.material.vertexColors=o?THREE.FaceColors:THREE.NoColors,o&&!t&&(t="#FFFFFF"),t&&e.material.color.set(parseInt(t.substr(1),16)),e.material.needsUpdate=!0):e.traverse(function(e){e.visible=!1}))},this.set_color=function(e,t){if(void 0===o.models_ref[e])return o.model_error("set_color - id not found: "+e);var a=o.models[o.models_ref[e]];a&&a.mesh&&(t.length<6||("#"!=t.charAt(0)&&(t="#"+t),a.color=t,o.update_mesh_color(a.mesh,t,!t&&a.colors)))},this.error_in_model=function(e){if(!e.id&&0!=e.id&&-1!=e.id)return o.model_error("missing id");if(!Number.isInteger(e.id))return o.model_error("invalid id");if(e.id<-1)return o.model_error("id must be positive");if(!e.filename&&!e.mesh&&!e.local_file){if(!e.name)return o.model_error("missing filename or mesh");e.filename=e.name}return o.models_ref[e.id]?o.model_error("such model ID already exists: "+e.id):null},this.model_error=function(e,t){return console.log(e),o.status=-1,o.error=e,t&&t(e),e},this.set_bg_color=function(e){"transparent"==e?this.renderer.setClearColor(0,0):this.renderer.setClearColor(e,1),o.bg_color=e},this.set_display=function(e,t){if(void 0===o.models_ref[e])return o.model_error("set_display - id not found: "+e);var a=o.models[o.models_ref[e]];a&&(o.set_material_display(t,a.mesh.material,a.mesh.geometry),a.display=t,a.mesh&&(a.mesh.normalsNeedUpdate=!0))},this.set_opacity=function(e,t){if(void 0===o.models_ref[e])return o.model_error("set_display - id not found: "+e);var a=o.models[o.models_ref[e]];a&&(a.opacity=t,this.set_material_opacity(a.mesh.material,t))},this.set_material_opacity=function(e,t){e&&(t<1?(e.opacity=t,e.transparent=!0):(e.opacity=1,e.transparent=!1))},this.onmousedown=function(e){e.stopPropagation(),e.preventDefault();var t=e.which;switch(e.type){case"touchstart":t=20;var a=e.touches[0]||e.changedTouches[0];o.mouse.x=(a.pageX-o.parent_element.offsetLeft)/o.parent_element.clientWidth*2-1,o.mouse.y=-(a.pageY-o.parent_element.offsetTop)/o.parent_element.clientHeight*2+1;break;case"dblclick":t=11;default:o.mouse.x=(e.clientX-o.parent_element.offsetLeft)/o.parent_element.clientWidth*2-1,o.mouse.y=-(e.clientY-o.parent_element.offsetTop)/o.parent_element.clientHeight*2+1}o.raycaster.setFromCamera(o.mouse,o.camera);var i=o.raycaster.intersectObjects(o.scene.children);if(i.length>0){if(void 0===i[0].object.model_id)return;o.onmousedown_callback&&o.onmousedown_callback(i[0].object.model_id,e,i[0].distance,t)}else o.send_no_model_click_event&&o.onmousedown_callback(null,e,0,t)},this.is_empty=function(e){return!e&&0!==e},this.set_model_units=function(e,t,a){if(void 0===o.models_ref[e])return o.model_error("set_model_units - id not found: "+e);var i=o.models[o.models_ref[e]];if(i&&i.mesh){var n=1;switch(t){case"mm":a&&"inch"==i.units&&(n=1/25.4),i.units="mm";break;case"inch":a&&"mm"==i.units&&(n=25.4),i.units="inch";break;default:return o.model_error("set_model_units - invalid unit "+t)}1!=n&&o.set_scale(i.id,i.scalex*n,i.scaley*n,i.scalez*n)}},this.set_position=function(e,t,a,i){if(void 0===o.models_ref[e])return o.model_error("set_position - id not found: "+e);var n=o.models[o.models_ref[e]];n&&n.mesh&&(n.x=o.is_empty(t)?n.x:t,n.y=o.is_empty(a)?n.y:a,n.z=o.is_empty(i)?n.z:i,n.mesh.position.set(n.x,n.y,n.z),n.edges&&o.set_or_update_geo_edges(n,!0,!0))},this.set_material_display=function(e,t,o){switch(e.toLowerCase()){case"wireframe":t.wireframe=!0;break;case"smooth":t.wireframe=!1,t.flatShading=!1,o&&(o.mergeVertices(),o.computeVertexNormals());break;case"flat":t.wireframe=!1,t.flatShading=!0,o&&o.computeFlatVertexNormals()}},this.set_rotation=function(e,t,a,i,n){if(void 0===o.models_ref[e])return o.model_error("rotate - id not found: "+e);var l=o.models[o.models_ref[e]];if(l){var s=n?1:0;void 0!==t&&(l.rotationx=t+l.mesh.rotation.x*s,l.mesh.rotation.x=l.rotationx),void 0!==a&&(l.rotationy=a+l.mesh.rotation.y*s,l.mesh.rotation.y=l.rotationy),void 0!==i&&(l.rotationz=i+l.mesh.rotation.z*s,l.mesh.rotation.z=l.rotationz),l.mesh.updateMatrixWorld(),l.edges&&o.set_or_update_geo_edges(l,!0)}},this.rotate=function(e,t,a,i){o.set_rotation(e,t,a,i,!0)},this.basename=function(e){return e.split(/[\\\/]/).pop()},this.get_model_filename=function(e,t,a,i){var n=null;return e.orig_url&&!i?n=decodeURIComponent(e.orig_url):e.orig_filename?n=e.orig_filename:e.temp_filename?n=e.temp_filename:e.local_file?e.local_file.name&&(n=e.local_file.name):e.filename&&(e.filename instanceof File&&(n=File.name),n=e.filename),!n&&t?"model_"+e.id+".stl":n?(a&&(n=o.basename(n)),n):null},this.add_model=function(e,t){if(Array.isArray(e))return o.add_models(e);if(!o.ready)return o.models_to_add.push(e),o.model_error("THREE JS files are not ready");var a=o.get_model_filename(e);if(a)switch(a.split(".").pop()){case"vsj":return o.load_vsj(e.local_file?e.local_file:a);case"vsb":return o.load_vsb(e.local_file?e.local_file:a)}void 0===e.id&&(e.id=-1);var i=o.error_in_model(e);if(i)return i;-1==e.id&&(e.id=++o.max_model_id),o.models.push(e);var n=o.models.indexOf(e);return t||void 0===o.models_ref[e.id]&&o.models_count++,o.models_ref[e.id]=n,o.load_model(e),o.status},this.add_models=function(e){if(!Array.isArray(e))return o.add_model(e);o.status=0;var t=Object.keys(e);return t.forEach(function(a){var i=o.get_model_filename(e[t[a]]);if(i)switch(i.split(".").pop()){case"vsj":case"vsb":break;default:void 0===o.models_ref[e[a].id]&&o.models_count++}else void 0===o.models_ref[e[a].id]&&o.models_count++}),t.forEach(function(t){o.add_model(e[t],!0)}),o.status},this.calc_volume_and_area=function(e,t){var o,a,i,n,l,s,r,d,_,c,m,u,f,h,p=e.faces.length,g=0,v=0;for(c=0;c=0&&(i=i.substring(0,n)),i.length<1&&(i="1"),window.navigator.msSaveOrOpenBlob?window.navigator.msSaveBlob(t,i+".vsj"):(a.download=i+".vsj",a.click(),URL.revokeObjectURL(a.href))},this.load_vsj=function(e){if(!e)return o.pre_loaded_vsj?(stl_viewer.init_by_json(o.pre_loaded_vsj),o.pre_loaded_vsj=null,!0):o.model_error("load_vsj - invalid filename"+e,o.load_error_callback);if(e instanceof File)return o.read_bin_file(e,o.init_by_json,null,!0);var t=new XMLHttpRequest;t.onreadystatechange=function(e){4==t.readyState&&200==t.status&&o.init_by_json(t.response.trim())},t.open("GET",e,!0),t.send(null)},this.padend=function(e,t,o){return t>>=0,o=String(void 0!==o?o:" "),e.length>t?String(e):((t-=e.length)>o.length&&(o+=o.repeat(t/o.length)),String(e)+o.slice(0,t))},this.get_normal=function(e,t,o){var a=t.x-e.x,i=t.y-e.y,n=t.z-e.z,l=o.x-e.x,s=o.y-e.y,r=o.z-e.z,d={x:0,y:0,z:0};d.x=i*r-n*s,d.y=n*l-a*r,d.z=a*s-i*l;var _=Math.sqrt(d.x*d.x+d.y*d.y+d.z*d.z);return 0!=_&&(d.x/=_,d.y/=_,d.z/=_),d},this.get_stl_bin=function(e){if(void 0===o.models_ref[e])return o.model_error("get_stl_bin - id not found: "+e);var t=o.models[o.models_ref[e]];if(t&&t.mesh){var a=t.mesh.geometry;if(a){for(var i=new ArrayBuffer(84+50*a.faces.length),n=new DataView(i),l=(new TextEncoder,o.padend("Binary"+(t.colors?" colored":"")+" STL by viewstl.com",80," ")),s=0;s<80;s++)n.setUint8(s,l.charCodeAt(s),!0);n.setUint32(80,a.faces.length,!0);var r=84;return Object.keys(a.faces).forEach(function(e){var i=a.faces[e],l=a.vertices[i.a],s=a.vertices[i.b],d=a.vertices[i.c],_=o.get_normal(l,s,d);n.setFloat32(r,_.x,!0),r+=4,n.setFloat32(r,_.y,!0),r+=4,n.setFloat32(r,_.z,!0),r+=4,n.setFloat32(r,l.x,!0),r+=4,n.setFloat32(r,l.y,!0),r+=4,n.setFloat32(r,l.z,!0),r+=4,n.setFloat32(r,s.x,!0),r+=4,n.setFloat32(r,s.y,!0),r+=4,n.setFloat32(r,s.z,!0),r+=4,n.setFloat32(r,d.x,!0),r+=4,n.setFloat32(r,d.y,!0),r+=4,n.setFloat32(r,d.z,!0),r+=4,t.colors?n.setUint16(r,Math.ceil(31*i.color.r)|Math.ceil(31*i.color.g)<<5|Math.ceil(31*i.color.b)<<10,!0):n.setUint16(r,0,!0),r+=2}),i}}},this.basename=function(e){return e.substr(e.lastIndexOf("/")+1)},this.json_without_nulls=function(e){return JSON.stringify(e).split(",null").join("").split("null,").join("")},this.get_vsb_blob=function(){var e=null;try{e=new JSZip}catch(e){return console.log("download_vsb - JSZip is missing ",e.message),!1}var t=o.get_vsb();return e.file("json_data.vsj",o.json_without_nulls(t.vsj)),Object.keys(t.files).forEach(function(a){var i=o.get_model_filename(t.vsj.models[o.models_ref[t.files[a].id]],!0,!0);e.file(i,t.files[a].bin)}),e.generateAsync({type:"blob"})},this.download_vsb=function(e){o.get_vsb_blob(e).then(function(t){var o=new Blob([t],{type:"application/zip"}),a=document.createElement("a");a.href=window.URL.createObjectURL(o);var i=e||"1",n=i.toLowerCase().indexOf(".vsb");n>=0&&(i=i.substring(0,n)),i.length<1&&(i="1"),window.navigator.msSaveOrOpenBlob?window.navigator.msSaveBlob(o,i+".vsb"):(a.download=i+".vsb",a.click(),URL.revokeObjectURL(a.href))})},this.load_vsb=function(e){if(o.pre_loaded_ab_files=[],o.pre_loaded_vsj=null,e instanceof File)return o.read_bin_file(e,o.load_vsb_from_blob);JSZipUtils.getBinaryContent(decodeURIComponent(e),function(e,t){if(e)return o.model_error("load_vsb "+e,o.load_error_callback);o.load_vsb_from_blob(t)})},this.read_bin_file=function(e,t,o,a){var i=new FileReader;i.onerror=function(e){return console.log("reading file error",e),null},i.onload=function(e){return t(e.target.result)},o&&(i.onprogress=function(e){o({loaded:e.loaded,total:e.total,load_session:-1},-1)}),a?i.readAsText(e):i.readAsArrayBuffer(e)},this.load_vsb_from_blob=function(e){var t=null;try{t=new JSZip}catch(e){return console.log("load vsb - JSZip is missing ",e.message),!1}t.loadAsync(e).then(function(){o.zip_load_count=Object.keys(t.files).length,t.forEach(function(e,a){"json_data.vsj"==a.name?t.files[a.name].async("string").then(function(e){o.pre_loaded_vsj=e,o.zip_load_count--,0==o.zip_load_count&&o.load_vsj(null)}):t.files[a.name].async("blob").then(function(e){o.pre_loaded_ab_files[a.name]=e,o.zip_load_count--,0==o.zip_load_count&&o.load_vsj(null)})})})},this.load_bin_to_ab_file=function(e,t){o.pre_loaded_ab_files||(o.pre_loaded_ab_files=[]),o.pre_loaded_ab_files[e]=t,o.add_model({id:-1,filename:e})},this.download_model=function(e,t){if(void 0===o.models_ref[e])return o.model_error("download_model - id not found: "+e);var a=o.models[o.models_ref[e]];if(a&&a.mesh){var i=new Blob([o.get_stl_bin(e)],{type:"application/sla"}),n=document.createElement("a");n.href=window.URL.createObjectURL(i);var l=o.get_model_filename(a,!0,!0,!0),s=l.toLowerCase().indexOf(".stl");s>=0&&(l=l.substring(0,s)),l.length<1&&(l="1"),window.navigator.msSaveOrOpenBlob?window.navigator.msSaveBlob(i,l+".stl"):(n.download=l+".stl",n.click(),URL.revokeObjectURL(n.href))}},this.get_model_mesh=function(e){if(void 0===o.models_ref[e])return o.model_error("get_model_mesh - id not found: "+e);var t=o.models[o.models_ref[e]];if(t&&t.mesh){var a=t.mesh.clone();return a.geometry=t.mesh.geometry.clone(),a.material=t.mesh.material.clone(),a}},this.set_auto_rotate=function(e){o.controls.autoRotate=e},this.set_mouse_zoom=function(e){o.controls.noZoom=!e},this.WORLD_X_VECTOR=null,this.WORLD_Y_VECTOR=null,this.WORLD_Z_VECTOR=null,this.maxx=null,this.maxy=null,this.maxz=null,this.minx=null,this.miny=null,this.minz=null,this.edges_material=null,this.raycaster=null,this.mouse=null,this.scene=null,this.is_webgl=null,this.renderer=null,this.camera=null,this.ambientLight=null,this.directionalLight=null,this.pointLight=null,this.controls=null,this.do_resize=function(){if(o.parent_element){var e=o.parent_element.getBoundingClientRect(),t=e.width,a=e.height;o.camera.aspect=t/a,o.camera.updateProjectionMatrix(),o.renderer.setSize(t,a)}},this.animation=new Array,this.animate=function(){o.killsign||(Object.keys(o.animation).forEach(function(e){void 0!==o.models_ref[e]&&o.do_model_animation(o.models[o.models_ref[e]])}),requestAnimationFrame(o.animate),o.renderer&&o.renderer.render(o.scene,o.camera),o.controls&&o.controls.update())},this.do_model_animation=function(e){if(e.animation){var t=Date.now();if(e.animation.start_time||(e.animation.start_time=t),e.animation.delta){var a=(t-e.animation.start_time)/e.animation.delta.msec,i=e.animation.last_time?(t-e.animation.last_time)/e.animation.delta.msec:a;if(o.animation_next_delta(e,e.animation.delta,i),a>=1){if(!e.animation.delta.loop)return void o.remove_model_animation(e,!0);e.animation.delta.start_time=null}}if(e.animation.exact){i=(t-(e.animation.last_time?e.animation.last_time:e.animation.start_time))/e.animation.exact.msec;if(o.animation_next_exact(e,e.animation.exact,i),t>=e.animation.start_time+e.animation.exact.msec)return void o.remove_model_animation(e,!1,!0)}e.animation.last_time=t}},this.animation_next_delta=function(e,t,a){var i=!1,n=!1,l=!1;Object.keys(t).forEach(function(s){switch(s){case"x":case"y":case"z":i||(i=!0,o.set_position(e.id,e.x+(void 0!==t.x?t.x*a:0),e.y+(void 0!==t.y?t.y*a:0),e.z+(void 0!==t.z?t.z*a:0)));break;case"rotationx":case"rotationy":case"rotationz":n||(n=!0,o.rotate(e.id,void 0!==t.rotationx?t.rotationx*a:0,void 0!==t.rotationy?t.rotationy*a:0,void 0!==t.rotationz?t.rotationz*a:0));break;case"scale":case"scalex":case"scaley":case"scalez":l||(l=!0,t.scalex=t.scalex?t.scalex:t.scale?t.scale:null,t.scaley=t.scaley?t.scaley:t.scale?t.scale:null,t.scalez=t.scalez?t.scalez:t.scale?t.scale:null,o.set_scale(e.id,e.scalex+(void 0!==t.scalex?t.scalex*a:0),e.scaley+(void 0!==t.scaley?t.scaley*a:0),e.scalez+(void 0!==t.scalez?t.scalez*a:0)))}})},this.animation_next_exact=function(e,t,a){var i=!1,n=!1,l=!1;Object.keys(t).forEach(function(s){switch(s){case"x":case"y":case"z":i||(i=!0,void 0===t.xtotal&&(t.xtotal=t.x-e.x),void 0===t.ytotal&&(t.ytotal=t.y-e.y),void 0===t.ztotal&&(t.ztotal=t.z-e.z),o.set_position(e.id,e.x+(void 0!==t.x?t.xtotal*a:0),e.y+(void 0!==t.y?t.ytotal*a:0),e.z+(void 0!==t.z?t.ztotal*a:0)));break;case"rotationx":case"rotationy":case"rotationz":if(!n){n=!0;var r=e.mesh.getWorldRotation();void 0===t.rotxtotal&&(t.rotxtotal=t.rotationx-r.x),void 0===t.rotytotal&&(t.rotytotal=t.rotationy-r.y),void 0===t.rotztotal&&(t.rotztotal=t.rotationz-r.z),o.rotate(e.id,void 0!==t.rotationx?t.rotxtotal*a:0,void 0!==t.rotationy?t.rotytotal*a:0,void 0!==t.rotationz?t.rotztotal*a:0)}break;case"scale":case"scalex":case"scaley":case"scalez":l||(l=!0,t.scalex=t.scalex?t.scalex:t.scale?t.scale:null,t.scaley=t.scaley?t.scaley:t.scale?t.scale:null,t.scalez=t.scalez?t.scalez:t.scale?t.scale:null,void 0===t.scalextotal&&(t.scalextotal=t.scalex-e.scalex),void 0===t.scaleytotal&&(t.scaleytotal=t.scaley-e.scaley),void 0===t.scaleztotal&&(t.scaleztotal=t.scalez-e.scalez),o.set_scale(e.id,e.scalex+(void 0!==t.scalex?t.scalextotal*a:0),e.scaley+(void 0!==t.scaley?t.scaleytotal*a:0),e.scalez+(void 0!==t.scalez?t.scaleztotal*a:0)))}})},this.remove_model_animation=function(e,t,a){t&&(e.animation.delta=null),a&&(e.animation.exact=null),e.animation.delta||e.animation.exact||(e.animation=null,delete o.animation[e.id])},this.animate_model=function(e,t){if(void 0===o.models_ref[e])return o.model_error("animate-model - id not found: "+e);var a=o.models[o.models_ref[e]];if(a){if(!t)return o.remove_model_animation(a,!0,!0);a.animation=JSON.parse(JSON.stringify(t)),a.animation.delta&&(a.animation.delta.msec||(a.animation.delta.msec=300)),a.animation.exact&&(a.animation.exact.msec||(a.animation.exact.msec=300)),o.animation[e]=1}},this.init_done=!1,this.init=function(){if(!o.init_done){switch(o.WORLD_X_VECTOR=new THREE.Vector3(1,0,0),o.WORLD_Y_VECTOR=new THREE.Vector3(0,1,0),o.WORLD_Z_VECTOR=new THREE.Vector3(0,0,1),o.edges_material=new THREE.LineBasicMaterial({color:0}),o.raycaster=new THREE.Raycaster,o.mouse=new THREE.Vector2,o.scene=new THREE.Scene,o.is_webgl=webgl_Detector.webgl,o.renderer=o.is_webgl?new THREE.WebGLRenderer({preserveDrawingBuffer:!0,alpha:!0}):new THREE.CanvasRenderer({alpha:!0}),o.camera=new THREE.PerspectiveCamera(45,1,.1,1e4),o.parent_element.appendChild(o.renderer.domElement),o.scene.add(o.camera),o.ambientLight=new THREE.AmbientLight(2105376),o.camera.add(o.ambientLight),o.directionalLight=new THREE.DirectionalLight(16777215,.75),o.directionalLight.position.x=1,o.directionalLight.position.y=1,o.directionalLight.position.z=2,o.directionalLight.position.normalize(),o.camera.add(o.directionalLight),o.pointLight=new THREE.PointLight(16777215,.3),o.pointLight.position.x=0,o.pointLight.position.y=-25,o.pointLight.position.z=10,o.camera.add(o.pointLight),o.controls_type){case 1:o.controls=new THREE.TrackballControls(o.camera,o.renderer.domElement);break;default:o.controls=new THREE.OrbitControls(o.camera,o.renderer.domElement),o.controls.autoRotate=o.auto_rotate}o.set_on_model_mousedown(o.onmousedown_callback)}o.set_bg_color(o.bg_color),!1===o.mouse_zoom&&o.set_mouse_zoom(o.mouse_zoom),o.camera_state?o.set_camera_state(o.camera_state):o.camera.position.set(o.camerax,o.cameray,o.cameraz),o.do_resize(),o.models_to_add&&o.add_models(o.models_to_add),o.set_auto_resize(o.auto_resize),o.animate(),o.init_done=!0},this.set_auto_resize=function(e){o.do_resize&&(window.removeEventListener("resize",o.do_resize),e&&window.addEventListener("resize",o.do_resize))},this.vf_to_geo=function(e,t,a){if(!e)return null;if(!t)return null;var n=[],l=[],s=e.length;for(i=0;il&&(l=o[d].x),o[d].y>s&&(s=o[d].y),o[d].z>r&&(r=o[d].z);t.minx=a+e.x,t.miny=i+e.y,t.minz=n+e.z,t.maxx=l+e.x,t.maxy=s+e.y,t.maxz=r+e.z}},this.handleDragOver=function(e){e.stopPropagation(),e.preventDefault(),e.dataTransfer.dropEffect="copy"},this.handleFileDrop=function(e){e.stopPropagation(),e.preventDefault(),e.dataTransfer.files.length>0?o.load_local_files(e.dataTransfer.files):"string"==typeof e.dataTransfer.getData("Text")&&(o.add_model({id:-1,filename:e.dataTransfer.getData("Text")}),o.on_model_drop&&o.on_model_drop(e.dataTransfer.getData("Text")))},this.load_local_files=function(e){if(e.length>0){for(var t=new Array,a=e.length,i=0;i0?e.substring(0,t+1):""}}(); \ No newline at end of file