<?php

// Author: Jesse Campbell
// Date: 2014-06-03
// Website: http://www.3dpartprice.com, http://www.jbcse.com
// Tested with PHP 5.5.9 on Windows 7 & XAMPP
// Version: 0.1

define("STL_UPLOAD_PATH", 'c:/xampp/htdocs/stlUploads/');
define("GCODE_OUTPUT_PATH", 'c:/xampp/htdocs/gCodeOutput/');
?>

<html>
    <head>
        <title>3D Part Price Library Demo</title>
        <style>body{padding: 20px;}.slicer{padding: 10px; margin-bottom: 10px; border: solid 1px lightgray;}pre{white-space: pre-wrap;}</style>
        <link rel="shortcut icon" href="http://www.3dpartprice.com/favicons/price.ico" type="image/x-icon">
        <link rel="icon" href="http://www.3dpartprice.com/favicons/price.ico" type="image/x-icon">
    </head>
<body>
    <h1>3D Part Price Library Demo</h1>

<?php
    $uploadSuccessful = true;
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) && empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0){       
        $displayMaxSize = ini_get('post_max_size');
 
        switch (substr($displayMaxSize,-1)){
            case 'G':
                $displayMaxSize = $displayMaxSize * 1024;
            case 'M':
                $displayMaxSize = $displayMaxSize * 1024;
            case 'K':
                $displayMaxSize = $displayMaxSize * 1024;
        }
         
        echo '<p>Posted data is too large. '.$_SERVER['CONTENT_LENGTH'].' bytes exceeds the maximum size of '.$displayMaxSize.' bytes.</p>';
        $uploadSuccessful = false;
    }
    else if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_FILES['stlFile']['size'] == 0){
        echo '<p>Error: STL file not chosen</p>';
        $uploadSuccessful = false;
    }
    
   if ($_SERVER['REQUEST_METHOD'] != 'POST'){
 ?>
    
    <form method="POST" enctype="multipart/form-data">
    
    <table style='border: solid 1px lightgray; padding: 10px;'>
        <tr><td>STL File:</td><td><input type="file" name="stlFile" accept="stl"></td></tr>
        <tr><td>Layer Height: </td><td><input name="layerHeight" value='0.5'> (min 0.1mm, max 1.2mm)</td></tr>
        <tr><td>Infill Percentage: </td><td><input name="infillPercentage" value='50'> (min 0, max 100)</td></tr>
        <tr><td>Print Speed: </td><td><input name="printSpeed" value='30'> (millimeters/sec, min 1, max 300)</td></tr>
        <tr><td>Price per hour: </td><td><input name="pricePerHour" value='2'> ($USD/hour)</td></tr>
        <tr><td>Price per gram: </td><td><input name="pricePerGram" value='0.2'> ($USD/gram)</td></tr>
        <tr><td>Material: </td><td><input type='radio' name="material" value='PLA' checked> PLA, <input type='radio' name="material" value='ABS'>ABS</td></tr>
        <tr><td>Slicer: </td><td>
            <input type="checkbox" name="slicer[]" value='Cura' checked> CuraEngine,
            <input type="checkbox" name="slicer[]" value='KISSlicer'> KISSlicer, 
            <input type="checkbox" name="slicer[]" value='slic3r'> slic3r
        </td></tr>
        <tr><td></td><td><input type='submit'></td></tr>
    </table>
</div>
</form>

<?php
   } 
   
  if ($_SERVER['REQUEST_METHOD'] == 'POST' && $uploadSuccessful == true){
      
      set_time_limit(60);
      require_once("3dPartPriceLib.php");
      
      if (file_exists(STL_UPLOAD_PATH) == FALSE)
        mkdir(STL_UPLOAD_PATH, 777);
      
      if (file_exists(GCODE_OUTPUT_PATH) == FALSE)
        mkdir(GCODE_OUTPUT_PATH, 777);
      
      move_uploaded_file($_FILES['stlFile']['tmp_name'],STL_UPLOAD_PATH.$_FILES['stlFile']['name']);
      
      $slicer = new Slicer();
      $slicer->stlFile = STL_UPLOAD_PATH.$_FILES['stlFile']['name'];
      $slicer->gCodeFile = GCODE_OUTPUT_PATH.$_FILES['stlFile']['name'].'.gcode';
      
      $slicer->layerHeight = $_POST['layerHeight'];
      $slicer->infillPercentage = $_POST['infillPercentage'];
      $slicer->printSpeed = $_POST['printSpeed'];
      $slicer->pricePerHour = $_POST['pricePerHour'];
      $slicer->pricePerGram = $_POST['pricePerGram'];
      $slicer->material = $_POST['material'];
      
      if (isset($_POST['slicer']) == false || count($_POST['slicer']) == 0)
        echo "<p>Error: Choose at least one slicer</p>";
      else
         foreach($_POST['slicer'] as $slicerName){
             echo "<div class='slicer'><h3>{$slicerName}</h3>";
             $start = microtime(true);
             $results = $slicer->slice($slicerName);
             $results['sliceTimeSeconds'] = number_format(microtime(true)-$start,2);
             echo "<pre>".print_r($results,true)."</pre></div>";
         }
  }
  
?>

<a href="/3dpartpricelib/3dpartpricelib.zip"><h4>Source Code</h4></a>

<h4>Programs used</h4>
<ul>
    <li>STL to Gcode converters (slicers)</li>
    <ul>
    <li><a href="http://kisslicer.com/">KISSlicer (KISSlicer.exe input.stl -o output.gcode)</a></li>
    <li><a href="https://github.com/Ultimaker/CuraEngine">CuraEngine (CuraEngine.exe -v -o output.gcode input.stl)</a></li>
    <li><a href="http://slic3r.org/">slic3r (slic3r.exe -o output.gcode input.stl)</a></li>
    </ul>
    <li>Gcode analyzers</li>
    <ul>
    <li><a href="http://www.dietzm.de/gcodesim/down.html?GCodeInfo.jar">GCodeInfoV98.jar (java -jar GCodeInfoV98.jar m output.gcode 2>&1)</a></li>
    </ul>
</ul>

<h4>Cross-Reference Tools</h4>
<ul>
    <li><a href="http://gcode.ws/">gCode Viewer</a></li>
    <li><a href="https://github.com/Ultimaker/CuraEngine/blob/master/src/settings.cpp">CuraEngine Settings</a></li>
    <li><a href="http://lazarsoft.info/objstl/#">STL Viewer</a></li>
    <li><a href="http://orion.math.iastate.edu/burkardt/g_src/ivcon/ivcon.html">ivcon.exe, 3D model format converter</a></li>
    <li><a href="http://www.cs.princeton.edu/~min/meshconv/">meshconv.exe, 3D model format converter</a></li>
</ul>

<hr>
Author: J. Campbell, Date: June 1, 2014, Website: <a href='http://www.jbcse.com'>http://www.jbcse.com</a>, Email: <script>document.write("jcamp"+"@"+"gmx"+"."+"com");</script>
</body>
</html>