public void OnGcodeExist(GcodeCreateBean gcodeBean)
 {
     Debug.Log("@@ OnGcodeExist" + "\n");
 }
Esempio n. 2
0
    private void sliceToCreateGcode(string stlFilePath, string configJsonFilePath, string gcodeFilePath)
    {
        Debug.Log("sliceToCreateGcode..." + "\n");

        _infoStruct.isSlicing = true;
        _infoStruct.progress  = 0.0f;
        _infoStruct.isCurGcodeCreateBeanAvailable = false;

        curGcodeBean = new GcodeCreateBean(stlFilePath, configJsonFilePath, gcodeFilePath);

        //2.new process to call shell: use CuraEngine to slice and create g-code
        System.Diagnostics.Process process = new System.Diagnostics.Process();

        string shellPath      = null;
        string CuraEnginePath = null;

        //Attention: mac and win, Arguments and FileName are different
        //In shell/bat : CuraEnginePath="$1"  configPath="$2" gcodePath="$3" stlPath="$4"
        switch (Global.GetInstance().GetOSPlatform())
        {
        case Global.OS_Platform_Enum.Mac:

            shellPath      = PathManager.shellPath_createGcode_mac();
            CuraEnginePath = PathManager.enginePath_mac();

            process.StartInfo.FileName  = "/bin/sh";
            process.StartInfo.Arguments =
                "\"" +
                shellPath +
                "\"" +
                " " +
                "\"" +
                CuraEnginePath +
                "\"" +
                " " +
                "\"" +
                configJsonFilePath +
                "\"" +
                " " +
                "\"" +
                gcodeFilePath +
                "\"" +
                " " +
                "\"" +
                stlFilePath +
                "\"";

            break;

        case Global.OS_Platform_Enum.Win_64:
            shellPath      = PathManager.shellPath_createGcode_win();
            CuraEnginePath = PathManager.enginePath_win64();

            process.StartInfo.FileName  = shellPath;
            process.StartInfo.Arguments =
                "\"" +
                CuraEnginePath +
                "\"" +
                " " +
                "\"" +
                configJsonFilePath +
                "\"" +
                " " +
                "\"" +
                gcodeFilePath +
                "\"" +
                " " +
                "\"" +
                stlFilePath +
                "\"";

            break;

        case Global.OS_Platform_Enum.Win_32:
            shellPath      = PathManager.shellPath_createGcode_win();
            CuraEnginePath = PathManager.enginePath_win32();

            process.StartInfo.FileName  = shellPath;
            process.StartInfo.Arguments =
                "\"" +
                CuraEnginePath +
                "\"" +
                " " +
                "\"" +
                configJsonFilePath +
                "\"" +
                " " +
                "\"" +
                gcodeFilePath +
                "\"" +
                " " +
                "\"" +
                stlFilePath +
                "\"";

            break;
        }

        Debug.Log("process FileName:" + process.StartInfo.FileName + "\n");
        Debug.Log("process Arguments :" + process.StartInfo.Arguments.Replace(" ", "\n") + "\n");

        process.StartInfo.CreateNoWindow         = true;
        process.StartInfo.UseShellExecute        = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError  = true;

        process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) => {
            if (!System.String.IsNullOrEmpty(e.Data))
            {
                string outputStr = e.Data.Trim();
//				Debug.Log (outputStr + "\n");
                if (outputStr.StartsWith("Progress:inset+skin:") || outputStr.StartsWith("Progress:export:"))
                {
                    if (outputStr.EndsWith("%"))
                    {
                        _infoStruct.progress = float.Parse(outputStr.Substring(outputStr.Length - 9, 8));   //not include %
                    }
                }
                else if (outputStr.StartsWith(";Filament used:"))
                {
                    //Filament is either in mm3 or mm (depending on your g-code flavour. Reprap uses mm, ultiCode uses mm3)
                    curGcodeBean.filamentLength = float.Parse(outputStr.Split(':') [1].Replace("m", "").Trim());
                    curGcodeBean.filamentWeight = Mathf.PI * (1.75f / 2) * (1.75f / 2) * curGcodeBean.filamentLength * 1.24f;
                }
                else if (outputStr.StartsWith("Print time:"))
                {
                    //add empirical parameter : 1.07
                    curGcodeBean.printTime = (int)(int.Parse(outputStr.Split(':') [1].Trim()) * 1.07f);
                }
            }
        });

        process.EnableRaisingEvents = true;

        //3.slice finish
        process.Exited += (sender, e) => {
            Debug.Log("Slice process exited" + "\n");

            _infoStruct.progress  = 1.0F;
            _infoStruct.isSlicing = false;
            _infoStruct.isCurGcodeCreateBeanAvailable = true;

            foreach (Listener listener in _listenerList)
            {
                listener.OnGcodeCreated(curGcodeBean);
            }
        };

        process.Start();
        process.BeginOutputReadLine();
    }
    /*************** GcodeCreateManager.Listener: callback ***************/
    //on sub thread
    public void OnGcodeCreated(GcodeCreateBean gcodeBean)
    {
        Debug.Log("@@ OnGcodeCreated" + "\n");

        GcodeRenderManager.GetInstance().StartParseGcodeFile(gcodeBean.gcodePath);
    }