Exemplo n.º 1
0
        private static GearProfiles CreateGearPlot(Cutouts cutoutCalculator, double size)
        {
            // Create the output plot file of the gear

            List <IEnumerable <PointF> > gearPoints = new()
            {
                cutoutCalculator.Gear.GenerateCompleteGearPath()
            };

            GearGenerator.GenerateCutoutPlot(cutoutCalculator, gearPoints);

            // Now convert to image bytes to return from Web API

            using Image img       = Plot.PlotGraphs(gearPoints, 2048, 2048, Color.Black);
            using MemoryStream ms = new();
            img.Save(ms, ImageFormat.Jpeg);
            ms.Seek(0L, SeekOrigin.Begin);
            byte[] bytes = ms.GetBuffer();

            return(new GearProfiles
            {
                Description = cutoutCalculator.Gear.Information + cutoutCalculator.Information,
                ShortName = cutoutCalculator.Gear.ShortName,
                JpegBase64 = Convert.ToBase64String(bytes),
                SvgData = GearGenerator.GenerateSVG(cutoutCalculator, (float)size)
            });
        }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            // First chop the spindle description off the end of the arg list

            string[] spindleArgs = null;
            int      idx         = Array.FindIndex(args, a => string.Compare(a, "-s", true) == 0);

            if (idx >= 0)
            {
                spindleArgs = new string[args.Length - idx];
                Array.Copy(args, idx, spindleArgs, 0, spindleArgs.Length);
                string[] newArgs = new string[idx];
                Array.Copy(args, 0, newArgs, 0, idx);
                args = newArgs;
            }

            if (args.Length > 0)
            {
                if (args[0] == "-h")
                {
                    Usage(null);
                    return;
                }
                if (args[0] == "-m")
                {
                    if (args.Length != 5 ||
                        !int.TryParse(args[1], out int numerator) || !int.TryParse(args[2], out int denominator) ||
                        !int.TryParse(args[3], out int minTeeth) | !int.TryParse(args[4], out int maxTeeth))
                    {
                        Usage("-m option needs 4 arguments");
                        return;
                    }
                    Console.WriteLine(GearParameters.MatchedPairs(numerator, denominator, minTeeth, maxTeeth));
                    return;
                }
                if (args[0] == "-C")
                {
                    if (args.Length != 2)
                    {
                        Usage("-C option needs just a filename as an argument");
                        return;
                    }
                    int[] pressureAngles = new int[] { 145, 200, 250 };
                    int[] teeth          = new int[] { 8, 10, 12, 14, 16, 18, 24, 30, 36, 48, 72, 144 };
                    using StreamWriter sw = new StreamWriter(args[1]);
                    sw.Write(GenerateGearTables(pressureAngles, teeth, 100, 0));
                    return;
                }
                if (args[0] == "-c")
                {
                    if (args.Length != 6)
                    {
                        Usage("-c option needs 5 arguments");
                        return;
                    }
                    string[] values = args[2].Split(',', StringSplitOptions.RemoveEmptyEntries);

                    List <int> angles = new List <int>();
                    foreach (string s in values)
                    {
                        if (int.TryParse(s, out int result))
                        {
                            angles.Add(result);
                        }
                        else
                        {
                            Usage("Pressure angles should be a comma-separated list of integers, measured in tenths of a degree");
                            return;
                        }
                    }

                    values = args[3].Split(',', StringSplitOptions.RemoveEmptyEntries);
                    List <int> toothList = new List <int>();
                    foreach (string s in values)
                    {
                        if (int.TryParse(s, out int result))
                        {
                            toothList.Add(result);
                        }
                        else
                        {
                            Usage("Tooth counts should be a comma-separated list of integers");
                            return;
                        }
                    }

                    if (!int.TryParse(args[4], out int module))
                    {
                        Usage("Module should be an integer measured in 100ths of a mm");
                        return;
                    }

                    if (!int.TryParse(args[5], out int cutterDiameter))
                    {
                        Usage("Cutter diameter should be an integer measured in 100ths of a mm");
                        return;
                    }

                    using StreamWriter sw = new StreamWriter(args[1]);
                    sw.Write(GenerateGearTables(angles, toothList, module, cutterDiameter));
                    return;
                }
                if (args[0].ToLower() == "-p")
                {
                    if (args.Length != 8 ||
                        !int.TryParse(args[1], out int teeth) ||
                        !int.TryParse(args[2], out int shift) ||
                        !int.TryParse(args[3], out int maxErr) ||
                        !int.TryParse(args[4], out int pressureAngle) ||
                        !int.TryParse(args[5], out int module) ||
                        !int.TryParse(args[6], out int backlash) ||
                        !int.TryParse(args[7], out int cutterDiameter))
                    {
                        Usage("-p and -P options need seven arguments, plus an optional following -s argument list");
                        return;
                    }
                    GearParameters gear = new GearParameters(
                        teeth,
                        module / 100.0,
                        Math.PI * pressureAngle / 1800.0,
                        shift / 1000.0,
                        maxErr / 100.0,
                        backlash / (double)module,
                        cutterDiameter / 100.0);

                    Cutouts cutoutCalculator = CreateCutouts(gear, spindleArgs);

                    // Generate the SVG version of the gear path

                    GearGenerator.GenerateSVGFile(cutoutCalculator, (float)gear.AddendumCircleDiameter,
                                                  $"t{gear.ToothCount}p{shift}a{pressureAngle}m{module}b{backlash}c{cutterDiameter}");

                    // Create the output plot file of the gear

                    int    limit = gear.ToothCount;
                    double angle = Math.PI;
                    if (args[0] == "-P")
                    {
                        limit = 1;
                        angle = gear.ToothAngle / 2;
                    }

                    List <IEnumerable <PointF> > gearPoints = new List <IEnumerable <PointF> >
                    {
                        Involutes.CirclePoints(-angle, angle, Involutes.AngleStep, gear.PitchCircleDiameter / 2),
                        Involutes.CirclePoints(-angle, angle, Involutes.AngleStep, gear.BaseCircleDiameter / 2)
                    };

                    //IEnumerable<PointF> addendCircle = Involutes.CirclePoints(-Math.PI / gear.ToothCount, Math.PI / gear.ToothCount, GearParameters.AngleStep, gear.AddendumCircleDiameter / 2);
                    //IEnumerable<PointF> dedendCircle = Involutes.CirclePoints(-Math.PI / gear.ToothCount, Math.PI / gear.ToothCount, GearParameters.AngleStep, gear.DedendumCircleDiameter / 2);

                    for (int i = 0; i < limit; i++)
                    {
                        gearPoints.AddRange(gear.GeneratePointsForOnePitch(i));
                    }

                    if (args[0] == "-p")
                    {
                        GearGenerator.GenerateCutoutPlot(cutoutCalculator, gearPoints);
                    }

                    // Report what was created

                    Console.WriteLine(gear.Information);
                    Console.WriteLine(cutoutCalculator.Information);

                    using Image img = Plot.PlotGraphs(gearPoints, 2048, 2048);
                    img.Save($"t{gear.ToothCount}p{shift}a{pressureAngle}m{module}b{backlash}c{cutterDiameter}.png", ImageFormat.Png);
                }
                if (args[0].ToLower() == "-e")
                {
                    if (args.Length != 8 ||
                        !int.TryParse(args[1], out int teeth) ||
                        !int.TryParse(args[2], out int maxErr) ||
                        !int.TryParse(args[3], out int undercutAngle) ||
                        !int.TryParse(args[4], out int module) ||
                        !int.TryParse(args[5], out int toothFaceLength) ||
                        !int.TryParse(args[6], out int tipPitch) ||
                        !int.TryParse(args[7], out int cutDiameter))
                    {
                        Usage("-e and -E options require 7 arguments, plus an optional -s argument list");
                        return;
                    }
                    EscapeGearParameters gear = new EscapeGearParameters(
                        teeth,
                        module / 100.0,
                        Math.PI * undercutAngle / 1800.0,
                        toothFaceLength / 100.0,
                        tipPitch / 100.0,
                        cutDiameter / 100.0,
                        maxErr / 100.0
                        );

                    Cutouts cutoutCalculator = CreateCutouts(gear, spindleArgs);

                    // Generate the SVG version of the gear path

                    GearGenerator.GenerateSVGFile(cutoutCalculator, (float)gear.PitchCircleDiameter,
                                                  $"e{gear.ToothCount}u{undercutAngle}m{module}f{toothFaceLength}p{tipPitch}d{cutDiameter}");

                    // Create the output plot file of the gear

                    List <IEnumerable <PointF> > gearPoints = new List <IEnumerable <PointF> >();
                    int    limit = gear.ToothCount;
                    double angle = 2 * Math.PI;
                    if (args[0] == "-E")
                    {
                        limit = 1;
                        angle = gear.ToothAngle;

                        gearPoints.Add(Involutes
                                       .CirclePoints(0, angle, Involutes.AngleStep, gear.PitchCircleDiameter / 2));
                        gearPoints.Add(Involutes
                                       .CirclePoints(0, angle, Involutes.AngleStep, gear.InnerDiameter / 2));
                    }

                    for (int i = 0; i < limit; i++)
                    {
                        gearPoints.Add(gear.ToothProfile(i));
                    }

                    if (args[0] == "-e")
                    {
                        GearGenerator.GenerateCutoutPlot(cutoutCalculator, gearPoints);
                    }

                    // Report what was created

                    Console.WriteLine(gear.Information);
                    Console.WriteLine(cutoutCalculator.Information);

                    using Image img = Plot.PlotGraphs(gearPoints, 2048, 2048);
                    img.Save($"e{gear.ToothCount}u{undercutAngle}m{module}f{toothFaceLength}p{tipPitch}d{cutDiameter}.png", ImageFormat.Png);
                }
                if (args[0].ToLower() == "-r")
                {
                    if (args.Length != 6 ||
                        !int.TryParse(args[1], out int teeth) ||
                        !int.TryParse(args[2], out int maxErr) ||
                        !int.TryParse(args[3], out int module) ||
                        !int.TryParse(args[4], out int innerDiameter) ||
                        !int.TryParse(args[5], out int cutterrDiameter))
                    {
                        Usage("-r option requires 5 arguments, plus an optional -s argument list");
                        return;
                    }
                    Ratchet gear = new Ratchet(
                        teeth,
                        module / 100.0,
                        maxErr / 100.0,
                        innerDiameter / 100.0,
                        cutterrDiameter / 100.0
                        );

                    Cutouts cutoutCalculator = CreateCutouts(gear, spindleArgs);

                    // Generate the SVG version of the gear path

                    GearGenerator.GenerateSVGFile(cutoutCalculator, (float)gear.PitchCircleDiameter,
                                                  $"t{gear.ToothCount}m{module}i{innerDiameter}");

                    // Create the output plot file of the gear

                    List <IEnumerable <PointF> > gearPoints = new List <IEnumerable <PointF> >();
                    for (int i = 0; i < gear.ToothCount; i++)
                    {
                        gearPoints.Add(gear.ToothProfile(i));
                    }

                    GearGenerator.GenerateCutoutPlot(cutoutCalculator, gearPoints);

                    // Report what was created

                    Console.WriteLine(gear.Information);
                    Console.WriteLine(cutoutCalculator.Information);

                    using Image img = Plot.PlotGraphs(gearPoints, 2048, 2048);
                    img.Save($"t{teeth}m{module}e{maxErr}i{innerDiameter}.png", ImageFormat.Png);
                }
            }
            else
            {
                Usage("Missing or unrecognised arguments");
            }
        }