2019-09-03 18:36:09 +00:00
var object ;
2020-04-20 20:58:03 +00:00
var simIdx , draw , line , timefactor = 1 ,
object , simRunning = false ,
simSingleLine = - 1 ;
2018-08-28 15:09:20 +00:00
var loader = new THREE . ObjectLoader ( ) ;
2019-06-17 19:22:36 +00:00
2021-01-05 21:23:40 +00:00
function convertParsedDataToObject ( jsonData ) {
try {
parsedData = JSON . parse ( jsonData )
} catch ( e ) {
console . log ( e , jsonData ) ; // error in the above string (in this case, yes)!
return ;
}
2019-10-16 13:57:56 +00:00
var geometry = new THREE . BufferGeometry ( ) ;
2021-01-05 21:23:40 +00:00
2019-10-16 13:57:56 +00:00
var material = new THREE . LineBasicMaterial ( {
2021-01-05 21:23:40 +00:00
vertexColors : THREE . VertexColors ,
transparent : true ,
opacity : 0.8 ,
2019-10-16 13:57:56 +00:00
} ) ;
var positions = [ ] ;
var colors = [ ] ;
2021-01-05 21:23:40 +00:00
for ( i = 0 ; i < parsedData . linePoints . length ; i ++ ) {
2021-01-07 20:05:25 +00:00
2021-01-05 21:23:40 +00:00
var x = parsedData . linePoints [ i ] . x ;
var y = parsedData . linePoints [ i ] . y ;
var z = parsedData . linePoints [ i ] . z ;
positions . push ( x , y , z ) ;
if ( parsedData . linePoints [ i ] . g == 0 ) {
2022-11-10 21:06:34 +00:00
colors . push ( Theme . lines [ 0 ] . R ) ;
colors . push ( Theme . lines [ 0 ] . G ) ;
colors . push ( Theme . lines [ 0 ] . B ) ;
2021-01-05 21:23:40 +00:00
} else if ( parsedData . linePoints [ i ] . g == 1 ) {
2022-11-10 21:06:34 +00:00
colors . push ( Theme . lines [ 1 ] . R ) ;
colors . push ( Theme . lines [ 1 ] . G ) ;
colors . push ( Theme . lines [ 1 ] . B ) ;
2021-01-05 21:23:40 +00:00
} else if ( parsedData . linePoints [ i ] . g == 2 ) {
2022-11-10 21:06:34 +00:00
colors . push ( Theme . lines [ 2 ] . R ) ;
colors . push ( Theme . lines [ 2 ] . G ) ;
colors . push ( Theme . lines [ 2 ] . B ) ;
2021-01-05 21:23:40 +00:00
} else {
2022-11-10 21:06:34 +00:00
colors . push ( Theme . lines [ 3 ] . R ) ;
colors . push ( Theme . lines [ 3 ] . G ) ;
colors . push ( Theme . lines [ 3 ] . B ) ;
2019-05-10 18:36:51 +00:00
}
2021-01-05 21:23:40 +00:00
2019-10-16 13:57:56 +00:00
}
geometry . addAttribute ( 'position' , new THREE . Float32BufferAttribute ( positions , 3 ) ) ;
geometry . addAttribute ( 'color' , new THREE . Float32BufferAttribute ( colors , 3 ) ) ;
geometry . computeBoundingSphere ( ) ;
var line = new THREE . Line ( geometry , material ) ;
line . geometry . computeBoundingBox ( ) ;
var box = line . geometry . boundingBox . clone ( ) ;
2021-01-05 21:23:40 +00:00
// line.userData.lines = parsedData.lines
line . userData . linePoints = parsedData . linePoints ;
line . userData . bbbox2 = box ;
line . userData . inch = parsedData . inch ;
line . userData . totalTime = parsedData . totalTime ;
2019-10-16 13:57:56 +00:00
line . name = 'gcodeobject'
return line ;
}
function parseGcodeInWebWorker ( gcode ) {
2019-10-18 18:55:41 +00:00
if ( webgl ) {
if ( ! disable3Dgcodepreview ) {
simstop ( )
scene . remove ( object )
object = false ;
// var worker = new Worker('lib/3dview/workers/gcodeparser.js');
2021-01-07 20:05:25 +00:00
var worker = new Worker ( 'lib/3dview/workers/verylitegcodeviewer.js' ) ;
2019-10-18 18:55:41 +00:00
worker . addEventListener ( 'message' , function ( e ) {
2020-04-17 21:07:39 +00:00
// console.log('webworker message', e)
2021-01-05 21:23:40 +00:00
if ( e . data . progress != undefined ) {
$ ( '#3dviewlabel' ) . html ( ' 3D View (rendering, please wait... ' + e . data . progress + '% )' )
} else {
if ( scene . getObjectByName ( 'gcodeobject' ) ) {
scene . remove ( scene . getObjectByName ( 'gcodeobject' ) )
object = false ;
2019-10-16 13:57:56 +00:00
}
2021-01-05 21:23:40 +00:00
object = convertParsedDataToObject ( e . data ) ;
2021-03-30 19:42:35 +00:00
//console.log(object)
2021-01-05 21:23:40 +00:00
if ( object && object . userData . linePoints . length > 1 ) {
worker . terminate ( ) ;
scene . add ( object ) ;
if ( object . userData . inch ) {
// console.log(scaling)
object . scale . x = 25.4
object . scale . y = 25.4
object . scale . z = 25.4
2019-10-16 13:57:56 +00:00
}
2021-03-22 19:38:53 +00:00
if ( localStorage . getItem ( 'unitsMode' ) ) {
if ( localStorage . getItem ( 'unitsMode' ) == "in" ) {
if ( object . userData . inch ) {
redrawGrid ( object . userData . bbbox2 . min . x , object . userData . bbbox2 . max . x , object . userData . bbbox2 . min . y , object . userData . bbbox2 . max . y , true ) ;
} else {
redrawGrid ( object . userData . bbbox2 . min . x / 25.4 , object . userData . bbbox2 . max . x / 25.4 , object . userData . bbbox2 . min . y / 25.4 , object . userData . bbbox2 . max . y / 25.4 , true ) ;
}
} else {
if ( object . userData . inch ) {
redrawGrid ( object . userData . bbbox2 . min . x * 25.4 , object . userData . bbbox2 . max . x * 25.4 , object . userData . bbbox2 . min . y * 25.4 , object . userData . bbbox2 . max . y * 25.4 , false ) ;
} else {
redrawGrid ( object . userData . bbbox2 . min . x , object . userData . bbbox2 . max . x , object . userData . bbbox2 . min . y , object . userData . bbbox2 . max . y , false ) ;
}
}
}
2019-10-18 18:55:41 +00:00
// animate();
2021-01-05 21:23:40 +00:00
setTimeout ( function ( ) {
if ( webgl ) {
$ ( '#gcodeviewertab' ) . click ( ) ;
2019-10-18 18:55:41 +00:00
}
2021-01-05 21:23:40 +00:00
clearSceneFlag = true ;
resetView ( ) ;
// animate();
var timeremain = object . userData . totalTime ;
if ( ! isNaN ( timeremain ) ) {
2021-03-30 19:42:35 +00:00
//console.log(timeConvert(timeremain));
2021-01-05 21:23:40 +00:00
// output formattedTime to UI here
2021-03-30 19:42:35 +00:00
$ ( '#timeRemaining' ) . html ( timeConvert ( timeremain ) + " / " + timeConvert ( timeremain ) ) ;
printLog ( "<span class='fg-red'>[ GCODE Parser ]</span><span class='fg-darkGreen'> GCODE Preview Rendered Succesfully: Total lines: <b>" + object . userData . linePoints . length + "</b> / Estimated GCODE Run Time: <b>" + timeConvert ( timeremain ) + "</b>" )
2019-10-18 18:55:41 +00:00
}
2021-01-05 21:23:40 +00:00
} , 200 ) ;
$ ( '#3dviewicon' ) . removeClass ( 'fa-pulse' ) ;
$ ( '#3dviewlabel' ) . html ( ' 3D View' )
} else {
// Didn't get an Object
$ ( '#3dviewicon' ) . removeClass ( 'fa-pulse' ) ;
$ ( '#3dviewlabel' ) . html ( ' 3D View' )
}
2019-10-18 18:55:41 +00:00
}
2021-01-05 21:23:40 +00:00
2019-10-18 18:55:41 +00:00
} , false ) ;
worker . postMessage ( {
'data' : gcode
} ) ;
2020-06-22 21:04:23 +00:00
2019-10-18 18:55:41 +00:00
$ ( '#3dviewicon' ) . addClass ( 'fa-pulse' ) ;
$ ( '#3dviewlabel' ) . html ( ' 3D View (rendering, please wait...)' )
2020-06-22 21:04:23 +00:00
2019-10-18 18:55:41 +00:00
// populateToolChanges(gcode)
}
2019-10-16 13:57:56 +00:00
}
2018-08-28 15:09:20 +00:00
} ;
function simSpeed ( ) {
timefactor = timefactor * 10 ;
if ( timefactor > 1024 ) timefactor = 0.1 ;
$ ( '#simspeedval' ) . text ( timefactor ) ;
}
2020-04-20 20:58:03 +00:00
function runSimFrom ( startindex , singleLineOnly ) {
if ( singleLineOnly ) {
simSingleLine = startindex ;
}
2018-09-06 21:08:06 +00:00
$ ( '#gcodeviewertab' ) . click ( )
2020-04-20 20:58:03 +00:00
if ( startindex ) {
for ( i = 0 ; i < object . userData . lines . length ; i ++ )
if ( object . userData . lines [ i ] . args . indx == startindex ) {
simIdx = i + 1 ;
sim ( ) ;
}
} else {
sim ( ) ;
}
2018-09-06 21:08:06 +00:00
}
2020-04-20 20:58:03 +00:00
function sim ( ) {
2018-09-06 16:10:40 +00:00
if ( typeof ( object ) == 'undefined' || ! scene . getObjectByName ( 'gcodeobject' ) ) {
2020-04-17 21:07:39 +00:00
// console.log('No Gcode in Preview yet')
2020-04-20 20:58:03 +00:00
var message = ` No Gcode in Preview yet: Please load GCODE from the Open GCODE button first before running simulation `
2018-09-06 16:10:40 +00:00
Metro . toast . create ( message , null , 10000 , 'bg-red' ) ;
simstop ( )
} else {
2019-10-18 18:55:41 +00:00
if ( ! disable3Drealtimepos ) {
$ ( "#conetext" ) . show ( ) ;
2020-04-20 20:58:03 +00:00
if ( simIdx == 0 ) {
2021-01-05 21:23:40 +00:00
var posx = object . userData . linePoints [ 0 ] . x ; //- (sizexmax/2);
var posy = object . userData . linePoints [ 0 ] . y ; //- (sizeymax/2);
2022-07-06 20:08:38 +00:00
var posz = object . userData . linePoints [ 0 ] . z ;
2020-04-20 20:58:03 +00:00
} else {
2021-01-05 21:23:40 +00:00
var posx = object . userData . linePoints [ simIdx - 1 ] . x ; //- (sizexmax/2);
var posy = object . userData . linePoints [ simIdx - 1 ] . y ; //- (sizeymax/2);
2022-07-06 20:08:38 +00:00
var posz = object . userData . linePoints [ simIdx - 1 ] . z ;
2020-04-20 20:58:03 +00:00
}
2019-10-18 18:55:41 +00:00
cone . position . x = posx ;
cone . position . y = posy ;
cone . position . z = posz ;
cone . material = new THREE . MeshPhongMaterial ( {
color : 0x28a745 ,
2022-07-06 20:08:38 +00:00
specular : 0x08701f ,
2019-10-18 18:55:41 +00:00
shininess : 100 ,
2022-07-06 20:08:38 +00:00
opacity : 0.6 ,
2019-10-18 18:55:41 +00:00
transparent : true
} )
}
2020-04-20 20:58:03 +00:00
lastLine = {
x : posx ,
y : posy ,
z : posz ,
e : 0 ,
f : 0 ,
feedrate : 10000 ,
extruding : false
} ;
$ ( '#runSimBtn' ) . hide ( )
$ ( '#stopSimBtn' ) . show ( )
clearSceneFlag = true ;
2018-09-06 16:10:40 +00:00
simRunning = true ;
// timefactor = 1;
2018-08-28 15:09:20 +00:00
$ ( '#simspeedval' ) . text ( timefactor ) ;
2018-09-06 16:10:40 +00:00
$ ( '#simstartbtn' ) . attr ( 'disabled' , true ) ;
$ ( '#simstopbtn' ) . attr ( 'disabled' , false ) ;
$ ( '#editorContextMenu' ) . hide ( ) // sometimes we launch sim(linenum) from the context menu... close it once running
2019-02-15 18:56:37 +00:00
runSim ( ) ; //kick it off
}
}
2018-08-28 15:09:20 +00:00
2019-02-15 18:56:37 +00:00
function runSim ( ) {
2019-05-30 18:25:27 +00:00
2020-04-20 20:58:03 +00:00
if ( object . userData . inch ) {
2021-01-05 21:23:40 +00:00
var posx = object . userData . linePoints [ simIdx ] . x * 25.4 ; //- (sizexmax/2);
var posy = object . userData . linePoints [ simIdx ] . y * 25.4 ; //- (sizeymax/2);
var posz = object . userData . linePoints [ simIdx ] . z * 25.4 ;
2019-02-15 18:56:37 +00:00
2020-04-20 20:58:03 +00:00
} else {
2021-01-05 21:23:40 +00:00
var posx = object . userData . linePoints [ simIdx ] . x ; //- (sizexmax/2);
var posy = object . userData . linePoints [ simIdx ] . y ; //- (sizeymax/2);
var posz = object . userData . linePoints [ simIdx ] . z ;
2019-02-15 18:56:37 +00:00
}
2021-01-07 20:05:25 +00:00
// Disabled as of 1.0.271: object.userData.linePoints[simIdx] doesn't line up with gcode line numbers anymore as comments, etc are not added to linePoints[]
//$("#conetext").html(`<span class="tally success drop-shadow">Line ` + simIdx + ": " + editor.session.getLine(simIdx) + `</span>`);
2021-01-05 21:23:40 +00:00
var simTime = object . userData . linePoints [ simIdx ] . timeMins / timefactor ;
$ ( '#gcodesent' ) . html ( "Sim Line: " + parseInt ( simIdx ) - 1 ) ;
2020-04-20 20:58:03 +00:00
2019-02-15 18:56:37 +00:00
var simTimeInSec = simTime * 60 ;
2021-01-05 21:23:40 +00:00
if ( ! disable3Drealtimepos ) {
if ( ! object . userData . linePoints [ simIdx ] . fake ) {
2019-10-18 18:55:41 +00:00
TweenMax . to ( cone . position , simTimeInSec , {
2020-04-20 20:58:03 +00:00
ease : Linear . easeNone ,
2019-10-18 18:55:41 +00:00
x : posx ,
y : posy ,
2022-07-06 20:08:38 +00:00
z : posz ,
2019-10-18 18:55:41 +00:00
onComplete : function ( ) {
if ( simRunning == false ) {
//return
simstop ( ) ;
2018-09-06 16:10:40 +00:00
} else {
2020-04-20 20:58:03 +00:00
simIdx ++ ;
2021-01-05 21:23:40 +00:00
if ( simSingleLine > 0 && simIdx > simSingleLine + 1 ) {
2020-04-20 20:58:03 +00:00
simstop ( ) ;
2021-01-05 21:23:40 +00:00
} else if ( simIdx < object . userData . linePoints . length ) {
2019-10-18 18:55:41 +00:00
runSim ( ) ;
2020-04-20 20:58:03 +00:00
} else {
simstop ( ) ;
2019-10-18 18:55:41 +00:00
}
2018-09-06 16:10:40 +00:00
}
}
2019-10-18 18:55:41 +00:00
} )
2019-02-15 18:56:37 +00:00
} else {
2021-01-05 21:23:40 +00:00
if ( simRunning == false ) {
//return
2019-02-15 18:56:37 +00:00
simstop ( ) ;
2021-01-05 21:23:40 +00:00
} else {
simIdx ++ ;
if ( simSingleLine > 0 && simIdx > simSingleLine + 1 ) {
simstop ( ) ;
} else if ( simIdx < object . userData . linePoints . length ) {
runSim ( ) ;
} else {
simstop ( ) ;
}
2019-02-15 18:56:37 +00:00
}
}
2021-01-05 21:23:40 +00:00
2018-09-06 16:10:40 +00:00
}
2019-02-15 18:56:37 +00:00
} ;
2018-09-06 16:10:40 +00:00
2020-04-20 20:58:03 +00:00
2018-09-06 16:10:40 +00:00
function simstop ( ) {
2018-09-06 21:08:06 +00:00
simIdx = 0 ;
2018-09-06 16:10:40 +00:00
simRunning = false ;
2020-04-20 20:58:03 +00:00
simSingleLine = - 1 ;
2018-09-06 16:10:40 +00:00
$ ( '#runSimBtn' ) . show ( )
$ ( '#stopSimBtn' ) . hide ( )
2018-09-06 21:08:06 +00:00
// timefactor = 1;
2018-09-06 16:10:40 +00:00
$ ( '#simspeedval' ) . text ( timefactor ) ;
editor . gotoLine ( 0 )
2020-04-20 20:58:03 +00:00
$ ( "#conetext" ) . hide ( ) ;
2018-09-06 16:10:40 +00:00
clearSceneFlag = true ;
2022-07-06 20:08:38 +00:00
cone . material = new THREE . MeshPhongMaterial ( {
color : 0x0000ff ,
specular : 0x0000ff ,
shininess : 100 ,
opacity : 0.6 ,
transparent : true
} )
2020-04-20 20:58:03 +00:00
}
function simAnimate ( ) {
if ( simRunning ) {
if ( cone ) {
// 160widthx200height offset?
if ( cone . position ) {
var conepos = toScreenPosition ( cone , camera )
var offset = $ ( "#renderArea" ) . offset ( )
var farside = $ ( "#renderArea" ) . offset ( ) . left + $ ( "#renderArea" ) . outerWidth ( )
var bottomside = $ ( "#renderArea" ) . outerHeight ( )
// console.log(conepos)
// console.log(offset)
if ( conepos . y < 25 ) {
conepos . y = 25 ;
}
if ( conepos . y > bottomside - 40 ) {
conepos . y = bottomside - 40 ;
}
if ( conepos . x < 0 ) {
conepos . x = 0 ;
}
if ( conepos . x > farside - $ ( "#conetext" ) . outerWidth ( ) ) {
conepos . x = farside - $ ( "#conetext" ) . outerWidth ( ) ;
}
$ ( "#conetext" ) . css ( 'left' , conepos . x + "px" ) . css ( 'top' , conepos . y - 20 + "px" ) ;
}
}
}
}
function toScreenPosition ( obj , camera ) {
var vector = new THREE . Vector3 ( obj . position . x , obj . position . y + 10 , obj . position . z + 30 ) ;
var widthHalf = 0.5 * renderer . getContext ( ) . canvas . width ;
var heightHalf = 0.5 * renderer . getContext ( ) . canvas . height ;
vector . project ( camera ) ;
vector . x = ( vector . x * widthHalf ) + widthHalf ;
vector . y = - ( vector . y * heightHalf ) + heightHalf ;
return {
x : vector . x ,
y : vector . y
} ;
} ;