Пример #1
0
        /// <summary>
        /// Entry point method.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            Console.WriteLine("FourBar linkage");
            TemporaryFileManager.DeleteAllTemporaryFiles();
            Program program = new Program();

            program.defaultResponder = new HttpAnimationResponder("animation");
            program.RegisterResponder(new HttpFaviconResponder());
            program.Start();
        }
        /// <summary>
        /// Generates the octave code.
        /// </summary>
        /// <returns>The octave code.</returns>
        /// <param name="fbl">four-bar linkage to be used.</param>
        /// <param name="divisor">Number of steps in which the left angle-span is to be divided. Each angle is used to compute a configuration.</param>
        private static string GenerateOctaveCode(FourBarLinkage fbl, int divisor)
        {
            string result = TemporaryFileManager.GetTemporaryFilename();

            using (TextWriter tw = File.CreateText(result)) {
                tw.WriteLine(DeclareVariable("a", fbl.a));
                tw.WriteLine(DeclareVariable("b", fbl.b));
                tw.WriteLine(DeclareVariable("c", fbl.c));
                tw.WriteLine(DeclareVariable("d", fbl.d));
                tw.Write(executable);
                tw.Write(string.Format("r = compute({0});", divisor));

                foreach (var varName in new string[] { "alpha_left", "alpha_right", "beta_left", "beta_right" })
                {
                    tw.Write(string.Format("disp({0});", varName));
                }
                tw.WriteLine("disp(r);");
                tw.WriteLine("puts(\"----\\n\");");
                tw.WriteLine(string.Format("disp(compute_trajectory({0},{1}, r));", fbl.PtX, fbl.PtY));
            }
            return(result);
        }
        /// <summary>
        /// Compute configurations of the passed four bar linkage.
        /// The steps the method performs are the following.
        /// 1. Generate the octave code using the parameters defining the linkage.
        /// 2. Call octave and get the results back from the stdout stream.
        /// 3. Decode the results of the octave code storing the computed values into the object describing the instance of four-bar linkage to be simulated.
        /// </summary>
        /// <param name="device">Four-bar linkage whose configuration is to be computed.</param>
        public bool Compute(FourBarLinkage device)
        {
            System.Diagnostics.Process octave = new System.Diagnostics.Process();
            octave.StartInfo.UseShellExecute = false;
            octave.StartInfo.CreateNoWindow  = false;
            octave.StartInfo.FileName        = "octave";
            string tempFilename = GenerateOctaveCode(device, 300);

            octave.StartInfo.Arguments              = "-qf " + tempFilename;
            octave.StartInfo.RedirectStandardInput  = true;
            octave.StartInfo.RedirectStandardError  = true;
            octave.StartInfo.RedirectStandardOutput = true;
            // Launch the octave process
            octave.Start();
            string output = octave.StandardOutput.ReadToEnd();
            string error  = octave.StandardError.ReadToEnd();

            if (error.Length > 0)
            {
                // If there is something in the stderr file it means that there is some error.
                // The stdout content as well as the octave source code (with the line numbers) are shown
                string[] code = File.ReadAllLines(tempFilename);
                for (int i = 0; i < code.Length; i++)
                {
                    Console.WriteLine("{0,4} {1}", i + 1, code [i]);
                }
                Console.WriteLine("Error: " + error);
            }

            int  rowCounter  = 0;
            bool bSaveConfig = true;

            //Console.WriteLine ("output " + output);
            using (TextWriter tw = File.CreateText("./output.txt")) {
                tw.Write(output);
            }


            // The results are sent back trough the stdout stream
            // The sequence is as follows
            // 1. The extrema angles
            // 2. The configuration (list of alpha, beta angles)
            // 3. The trajectory (list of P (x,y) coordinates)
            foreach (var line in output.Split('\n'))
            {
                if (rowCounter++ < 4)
                {
                    double val = double.Parse(line);
                    // Console.WriteLine (string.Format ("{0} - {1}", rowCounter, val));
                    switch (rowCounter)
                    {
                    case 1:
                        device.alpha_left = val;
                        break;

                    case 2:
                        device.alpha_right = val;
                        break;

                    case 3:
                        device.beta_left = val;
                        break;

                    case 4:
                        device.beta_right = val;
                        break;
                    }
                }
                else
                {
                    if (line.StartsWith("----"))
                    {
                        bSaveConfig = false;
                    }
                    var ss = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    if (ss.Length < 2)
                    {
                        continue;
                    }
                    if (bSaveConfig)
                    {
                        double alpha = double.Parse(ss [0]);
                        double beta  = double.Parse(ss [1]);
                        device.AddConfiguration(alpha, beta);
                    }
                    else
                    {
                        double x = double.Parse(ss [0]);
                        double y = double.Parse(ss [1]);
                        device.AddPoint(x, y);
                    }
                }
            }
            TemporaryFileManager.DeleteTemporaryFile(tempFilename);
            return(true);
        }