Ejemplo n.º 1
0
        static public void generateAutomataImage(Automata <String> automata, string output_file = "", string fileformat = "svg")
        {
            string executable_file = "";
            string file_opener     = "";
            bool   no_open         = false;

            if (output_file != "")
            {
                no_open = true;
            }
            bool windows = false;
            int  p       = (int)Environment.OSVersion.Platform;

            if ((p == 4) || (p == 6) || (p == 128))               //UNIX
            {
                if (output_file == "")
                {
                    if (InputOutput.IsRunningOnMac())
                    {
                        output_file = Path.GetTempPath() + "generatedAutomata." + fileformat;
                    }
                    else
                    {
                        output_file = "/tmp/tmp." + fileformat;
                    }
                }
                executable_file = "dot";
                if (InputOutput.IsRunningOnMac())
                {
                    file_opener = "open";
                }
                else
                {
                    file_opener = "xdg-open";
                }
            }
            else
            {
                windows = true;
                if (output_file == "")
                {
                    output_file = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    output_file = output_file + "\\img." + fileformat;
                }
                executable_file = "C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe";
            }

            string dotinfo = automata.printGraphviz();

            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.CreateNoWindow        = false;
                startInfo.UseShellExecute       = false;
                startInfo.FileName              = executable_file;
                startInfo.RedirectStandardInput = true;
                startInfo.WindowStyle           = ProcessWindowStyle.Hidden;
                startInfo.Arguments             = "  -T" + fileformat + " -o " + output_file;

                Process exeProcess = new Process();
                exeProcess.StartInfo = startInfo;
                exeProcess.Start();
                //exeProcess.WaitForInputIdle();
                StreamWriter writer = exeProcess.StandardInput;
                writer.Write(dotinfo);
                writer.Flush();
                writer.WriteLine();
                if (!windows)
                {
                    exeProcess.WaitForInputIdle();
                }
                writer.Close();
                exeProcess.WaitForExit();
                if (!no_open)
                {
                    if (!windows)
                    {
                        Process openProcess = new Process();
                        openProcess.StartInfo.FileName  = file_opener;
                        openProcess.StartInfo.Arguments = output_file;
                        openProcess.Start();
                    }
                    else
                    {
                        Process.Start(output_file);
                    }
                }
            }
            catch
            {
                Console.Write("Random exception");
            }
        }