예제 #1
0
        public void Calculate()
        {
            model.ClearErrors();

            double speed = model.CssSpeed;
            bool   css   = model.IsCssEnabled;

            if (model.FeedRate > model.config.ZMaxFeedRate)
            {
                model.SetError(nameof(model.FeedRate), "Feed rate > max allowed.");
                return;
            }

            if (model.FeedRate == 0.0d)
            {
                model.SetError(nameof(model.FeedRate), "Feed rate is required.");
                return;
            }

            if (speed == 0.0d)
            {
                model.SetError(nameof(model.RPM), "Spindle RPM is required.");
                return;
            }

            if (css)
            {
                speed = Math.Round(speed / (Math.PI * model.XStart * model.UnitFactor) * (model.IsMetric ? 1000.0d : 12.0d * 25.4d), 0);
                if (model.config.CSSMaxRPM > 0.0d)
                {
                    speed = Math.Min(speed, model.config.CSSMaxRPM);
                }
            }

            if (speed > model.config.RpmMax && model.config.CSSMaxRPM == 0.0d)
            {
                model.SetError(nameof(model.RPM), "Spindle RPM > max allowed.");
                return;
            }

            if (speed < model.config.RpmMin)
            {
                model.SetError(nameof(model.RPM), "Spindle RPM < min allowed.");
                return;
            }

            double passdepth      = model.Passdepth;
            double passdepth_last = model.PassdepthLastPass;

            if (passdepth_last > passdepth)
            {
                model.SetError(nameof(model.Passdepth), "Last pass cut depth must be smaller than cut depth.");
                model.SetError(nameof(model.PassdepthLastPass), "Last pass cut depth must be smaller than cut depth.");
                return;
            }

            double zstart     = model.ZStart;
            double zlength    = model.ZLength;
            double ztarget    = (zstart + zlength * model.config.ZDirection);
            double xclearance = model.XClearance;
            double xtarget    = model.XTarget;
            double diameter   = model.XStart;

            if (Math.Abs(diameter - xtarget) == 0.0d) // nothing to do...
            {
                return;
            }

            if (model.config.xmode == LatheMode.Radius)
            {
                xtarget  /= 2.0d;
                diameter /= 2.0d;
            }
            else
            {
                passdepth      *= 2.0d;
                passdepth_last *= 2.0d;
                xclearance     *= 2.0d;
            }

            double angle     = 0.0d;
            double xdistance = xtarget - diameter;
            bool   boring    = xdistance > 0.0d;
            double xstart    = xtarget;

            PassCalc cut = new PassCalc(xdistance, passdepth, passdepth_last, model.Precision);

            if (model.IsTaperEnabled)
            {
                angle = Math.Tan(Math.PI * model.Taper / 180.0d);
            }

            //  error.Clear();

            if (cut.Passes < 1)
            {
                model.SetError("Diameter", "Starting diameter must be larger than target.");
                return;
            }

            if (model.IsSpringPassesEnabled)
            {
                cut.Springpasses = (int)model.SpringPasses;
            }

            if (boring)
            {
                xclearance = -xclearance;
            }

            uint pass = 1;

            model.gCode.Clear();
            model.gCode.Add(string.Format("G18 G{0} G{1}", model.config.xmode == LatheMode.Radius ? "8" : "7", model.IsMetric ? "21" : "20"));
            model.gCode.Add(string.Format("M3S{0} G4P1", speed.ToString()));
            model.gCode.Add(string.Format("G0 X{0}", model.FormatValue(diameter + xclearance)));
            model.gCode.Add(string.Format("G0 Z{0}", model.FormatValue(zstart + model.config.ZClearance / model.UnitFactor)));
            model.gCode.Add(css ? string.Format(model.config.CSSMaxRPM > 0.0d ? "G96S{0}D{1}" : "G96S{0}",
                                                model.CssSpeed, model.config.CSSMaxRPM) : "G97");

            do
            {
                xtarget = cut.GetPassTarget(pass, diameter, !boring);
                double feedrate = cut.IsLastPass ? model.FeedRateLastPass : model.FeedRate;

                model.gCode.Add(string.Format("(Pass: {0}, DOC: {1} {2})", pass, model.FormatValue(xtarget), model.FormatValue(cut.DOC)));

                // diameter = Math.Max(diameter - passdepth, xtarget);
                // TODO: G0 to prev target to keep spindle speed constant?
                //     if (css)
                //         code[i++] = string.Format("G0 X{0}", model.FormatValue(doc_prev));
                model.gCode.Add(string.Format("G1 X{0} F{1}", model.FormatValue(xtarget), model.FormatValue(feedrate)));
                if (angle != 0.0d)
                {
                    ztarget = cut.Distance / angle * model.config.ZDirection;
                    model.gCode.Add(string.Format("G1 X{0} Z{1}", model.FormatValue(diameter), model.FormatValue(zstart + ztarget)));
                }
                else
                {
                    model.gCode.Add(string.Format("G1 Z{0} F{1}", model.FormatValue(ztarget), model.FormatValue(feedrate)));
                }
                model.gCode.Add(string.Format("G0 X{0}", model.FormatValue(xtarget + xclearance)));
                model.gCode.Add(string.Format("G0 Z{0}", model.FormatValue(zstart + model.config.ZClearance / model.UnitFactor)));
            } while (++pass <= cut.Passes);

            GCode.File.AddBlock("Wizard: Turning", Core.Action.New);
            GCode.File.AddBlock(string.Format("({0}, Start: {1}, Target: {2}, Length: {3})",
                                              boring ? "Boring" : "Turning",
                                              model.FormatValue(diameter), model.FormatValue(xtarget), model.FormatValue(zlength)), Core.Action.Add);
            GCode.File.AddBlock(string.Format("(Passdepth: {0}, Feedrate: {1}, {2}: {3})",
                                              model.FormatValue(passdepth), model.FormatValue(model.FeedRate),
                                              (css ? "CSS" : "RPM"), model.FormatValue((double)model.CssSpeed)), Core.Action.Add);

            foreach (string s in model.gCode)
            {
                GCode.File.AddBlock(s, Core.Action.Add);
            }

            GCode.File.AddBlock("M30", Core.Action.End);
        }
예제 #2
0
        public void Calculate()
        {
            model.ClearErrors();

            double speed = model.CssSpeed;
            bool   css   = model.IsCssEnabled;

            if (model.FeedRate > model.config.ZMaxFeedRate)
            {
                model.SetError(nameof(model.FeedRate), "Feed rate > max allowed.");
                return;
            }

            if (model.FeedRate == 0.0d)
            {
                model.SetError(nameof(model.FeedRate), "Feed rate is required.");
                return;
            }

            if (speed == 0.0d)
            {
                model.SetError(nameof(model.RPM), "Spindle RPM is required.");
                return;
            }

            if (css)
            {
                speed = Math.Round(speed / (Math.PI * model.XStart * model.UnitFactor) * (model.IsMetric ? 1000.0d : 12.0d * 25.4d), 0);
                if (model.config.CSSMaxRPM > 0.0d)
                {
                    speed = Math.Min(speed, model.config.CSSMaxRPM);
                }
            }

            if (speed > model.config.RpmMax && model.config.CSSMaxRPM == 0.0d)
            {
                model.SetError(nameof(model.RPM), "Spindle RPM > max allowed.");
                return;
            }

            if (speed < model.config.RpmMin)
            {
                model.SetError(nameof(model.RPM), "Spindle RPM < min allowed.");
                return;
            }

            if (css)
            {
                speed = Math.Round(speed / (Math.PI * model.XStart * model.UnitFactor) * (model.IsMetric ? 1000.0d : 12.0d * 25.4d), 0);
                if (model.config.CSSMaxRPM > 0.0d)
                {
                    speed = Math.Min(speed, model.config.CSSMaxRPM);
                }
            }

            if (speed > model.config.RpmMax && model.config.CSSMaxRPM == 0.0d)
            {
                model.SetError(nameof(model.RPM), "Spindle RPM > max allowed.");
                return;
            }

            if (speed < model.config.RpmMin)
            {
                model.SetError(nameof(model.RPM), "Spindle RPM < min allowed.");
                return;
            }

            double passdepth      = model.Passdepth;
            double passdepth_last = model.PassdepthLastPass;

            double zstart     = model.ZStart;
            double ztarget    = (zstart * model.config.ZDirection);
            double xclearance = model.XClearance;
            double xtarget    = model.XTarget;
            double diameter   = model.XStart;

            if (Math.Abs(diameter - xtarget) == 0.0d) // nothing to do...
            {
                return;
            }

            //if (xtarget == 0.0d) // nothing to do...
            //    return;

            if (passdepth_last > passdepth)
            {
                model.SetError(nameof(model.Passdepth), "Last pass cut depth must be smaller than cut depth.");
                model.SetError(nameof(model.PassdepthLastPass), "Last pass cut depth must be smaller than cut depth.");
                return;
            }

            if (model.config.xmode == LatheMode.Radius)
            {
                xtarget  /= 2.0d;
                diameter /= 2.0d;
            }
            //else
            //{
            //    passdepth *= 2.0d;
            //    passdepth_last *= 2.0d;
            //    xclearance *= 2.0d;
            //}

            double angle  = 0.0d;
            double xstart = diameter;
            double xclear = xstart;

            PassCalc cut = new PassCalc(0d, passdepth, passdepth_last, model.Precision);


            //   error.Clear();

            if (cut.Passes < 1)
            {
                model.SetError(nameof(model.XStart), "Starting diameter must be larger than target.");
                return;
            }

            uint pass = 1;

            model.gCode.Clear();
            model.gCode.Add(string.Format("G18 G{0} G{1}", model.config.xmode == LatheMode.Radius ? "8" : "7", model.IsMetric ? "21" : "20"));
            model.gCode.Add(string.Format("M3S{0} G4P1", speed.ToString()));
            model.gCode.Add(string.Format("G0 X{0}", model.FormatValue(diameter + xclearance)));
            model.gCode.Add(string.Format("G0 Z{0}", model.FormatValue(zstart + model.config.ZClearance / model.UnitFactor)));
            model.gCode.Add(css ? string.Format(model.config.CSSMaxRPM > 0.0d ? "G96S{0}D{1}" : "G96S{0}",
                                                model.CssSpeed, model.config.CSSMaxRPM) : "G97");

            do
            {
                ztarget = cut.GetPassTarget(pass, zstart, true);
                double feedrate = cut.IsLastPass ? model.FeedRateLastPass : model.FeedRate;

                model.gCode.Add(string.Format("(Pass: {0}, DOC: {1} {2})", pass, ztarget, cut.DOC));

                model.gCode.Add(string.Format("G1 Z{0} F{1}", model.FormatValue(ztarget), model.FormatValue(feedrate)));
                //if (angle != 0.0d)
                //{
                //    ztarget = doc / angle * config.zdir;
                //    code[i++] = string.Format("G1 X{0} Z{1}", FormatValue(diameter), FormatValue(zstart + ztarget));
                //}
                //else
                model.gCode.Add(string.Format("G1 X{0}", model.FormatValue(xtarget)));
                model.gCode.Add(string.Format("G0 Z{0}", model.FormatValue(ztarget + model.config.ZClearance / model.UnitFactor)));
                model.gCode.Add(string.Format("G0 X{0}", model.FormatValue(xstart + model.config.XClearance)));
            } while (++pass <= cut.Passes);

            GCode.File.AddBlock("Wizard: Parting", Core.Action.New);
            GCode.File.AddBlock(string.Format("({0}, Start: {1}, Target: {2}, Length{3})",
                                              "Parting",
                                              model.FormatValue(zstart), model.FormatValue(ztarget), model.FormatValue(0d)), Core.Action.Add);
            GCode.File.AddBlock(string.Format("(Passdepth: {0}, Feedrate: {1}, {2}: {3})",
                                              model.FormatValue(passdepth), model.FormatValue(model.FeedRate),
                                              (css ? "CSS" : "RPM"), model.FormatValue((double)model.CssSpeed)), Core.Action.Add);

            foreach (string s in model.gCode)
            {
                GCode.File.AddBlock(s, Core.Action.Add);
            }

            GCode.File.AddBlock("M30", Core.Action.End);
        }