Exemplo n.º 1
0
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            string ip4address = (string)value;

            System.Net.IPAddress ip4;
            if (!System.Net.IPAddress.TryParse(ip4address, out ip4))
            {
                return(new ValidationResult(false, (string)LibStrings.FindResource("ValNotIPV4")));
            }

            return(ValidationResult.ValidResult);
        }
Exemplo n.º 2
0
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            double v = (double)value;

            if (!double.IsNaN(Min) && !double.IsNaN(Max) && (v < Min || v > Max))
            {
                return(new ValidationResult(false, string.Format(LibStrings.FindResource("ValAllowedRange"), Min, Max)));
            }

            if (!double.IsNaN(Min) && v < Min)
            {
                return(new ValidationResult(false, string.Format(LibStrings.FindResource("ValAllowedMin"), Min)));
            }

            if (!double.IsNaN(Max) && v > Max)
            {
                return(new ValidationResult(false, string.Format(LibStrings.FindResource("ValAllowedMax"), Max)));
            }

            return(ValidationResult.ValidResult);
        }
Exemplo n.º 3
0
        public int SetupAndOpen(string appname, GrblViewModel model, System.Windows.Threading.Dispatcher dispatcher)
        {
            int    status     = 0;
            bool   selectPort = false;
            int    jogMode    = -1;
            string port       = string.Empty;

            CNC.Core.Resources.Path = AppDomain.CurrentDomain.BaseDirectory;

            string[] args = Environment.GetCommandLineArgs();

            int p = 0;

            while (p < args.GetLength(0))
            {
                switch (args[p++].ToLowerInvariant())
                {
                case "-inifile":
                    CNC.Core.Resources.IniName = GetArg(args, p++);
                    break;

                case "-debugfile":
                    CNC.Core.Resources.DebugFile = GetArg(args, p++);
                    break;

                case "-configmapping":
                    CNC.Core.Resources.ConfigName = GetArg(args, p++);
                    break;

                case "-locale":
                case "-language":     // deprecated
                    CNC.Core.Resources.Locale = GetArg(args, p++);
                    break;

                case "-port":
                    port = GetArg(args, p++);
                    break;

                case "-selectport":
                    selectPort = true;
                    break;

                case "-islegacy":
                    CNC.Core.Resources.IsLegacyController = true;
                    break;

                case "-jogmode":
                    if (int.TryParse(GetArg(args, p++), out jogMode))
                    {
                        jogMode = Math.Min(Math.Max(jogMode, 0), (int)JogConfig.JogMode.KeypadAndUI);
                    }
                    break;

                default:
                    if (!args[p - 1].EndsWith(".exe") && File.Exists(args[p - 1]))
                    {
                        FileName = args[p - 1];
                    }
                    break;
                }
            }

            if (!Load(CNC.Core.Resources.IniFile))
            {
                if (MessageBox.Show(LibStrings.FindResource("CreateConfig"), appname, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    if (!Save(CNC.Core.Resources.IniFile))
                    {
                        MessageBox.Show(LibStrings.FindResource("CreateConfigFail"), appname);
                        status = 1;
                    }
                }
                else
                {
                    return(1);
                }
            }

            if (jogMode != -1)
            {
                Base.Jog.Mode = (JogConfig.JogMode)jogMode;
            }

            if (!string.IsNullOrEmpty(port))
            {
                selectPort = false;
            }

            if (!selectPort)
            {
                if (!string.IsNullOrEmpty(port))
                {
                    setPort(port);
                }
#if USEWEBSOCKET
                if (Base.PortParams.ToLower().StartsWith("ws://"))
                {
                    new WebsocketStream(Base.PortParams, dispatcher);
                }
                else
#endif
                if (char.IsDigit(Base.PortParams[0])) // We have an IP address
                {
                    new TelnetStream(Base.PortParams, dispatcher);
                }
                else
#if USEELTIMA
                { new EltimaStream(Config.PortParams, Config.ResetDelay, dispatcher); }
#else
                { new SerialStream(Base.PortParams, Base.ResetDelay, dispatcher); }
#endif
            }

            if ((Comms.com == null || !Comms.com.IsOpen) && string.IsNullOrEmpty(port))
            {
                PortDialog portsel = new PortDialog();

                port = portsel.ShowDialog(Base.PortParams);
                if (string.IsNullOrEmpty(port))
                {
                    status = 2;
                }

                else
                {
                    setPort(port);
#if USEWEBSOCKET
                    if (port.ToLower().StartsWith("ws://"))
                    {
                        new WebsocketStream(Base.PortParams, dispatcher);
                    }
                    else
#endif
                    if (char.IsDigit(port[0])) // We have an IP address
                    {
                        new TelnetStream(Base.PortParams, dispatcher);
                    }
                    else
#if USEELTIMA
                    { new EltimaStream(Config.PortParams, Config.ResetDelay, dispatcher); }
#else
                    { new SerialStream(Base.PortParams, Base.ResetDelay, dispatcher); }
#endif
                    Save(CNC.Core.Resources.IniFile);
                }
            }

            if (Comms.com != null && Comms.com.IsOpen)
            {
                Comms.com.DataReceived += model.DataReceived;

                CancellationToken cancellationToken = new CancellationToken();

                // Wait 400ms to see if a MPG is polling Grbl...

                new Thread(() =>
                {
                    MPGactive = WaitFor.SingleEvent <string>(
                        cancellationToken,
                        null,
                        a => model.OnRealtimeStatusProcessed += a,
                        a => model.OnRealtimeStatusProcessed -= a,
                        500);
                }).Start();

                while (MPGactive == null)
                {
                    EventUtils.DoEvents();
                }

                // ...if so show dialog for wait for it to stop polling and relinquish control.
                if (MPGactive == true)
                {
                    MPGPending await = new MPGPending(model);
                    await.ShowDialog();
                    if (await.Cancelled)
                    {
                        Comms.com.Close(); //!!
                        status = 2;
                    }
                }

                model.IsReady = true;
            }
            else if (status != 2)
            {
                MessageBox.Show(string.Format(LibStrings.FindResource("ConnectFailed"), Base.PortParams), appname, MessageBoxButton.OK, MessageBoxImage.Error);
                status = 2;
            }

            return(status);
        }
Exemplo n.º 4
0
        public RestartResult Restart()
        {
            Message       = model.Message;
            model.Message = string.Format(LibStrings.FindResource("MsgWaiting"), AppConfig.Settings.Base.PortParams);

            string response = GrblInfo.Startup(model);

            if (response.StartsWith("<"))
            {
                if (model.GrblState.State != GrblStates.Unknown)
                {
                    switch (model.GrblState.State)
                    {
                    case GrblStates.Alarm:

                        model.Poller.SetState(AppConfig.Settings.Base.PollInterval);

                        switch (model.GrblState.Substate)
                        {
                        case 1:         // Hard limits
                            if (!GrblInfo.IsLoaded)
                            {
                                if (model.LimitTriggered)
                                {
                                    MessageBox.Show(string.Format(LibStrings.FindResource("MsgNoCommAlarm"), model.GrblState.Substate.ToString()), "ioSender");
                                    if (AttemptReset())
                                    {
                                        model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                    }
                                    else
                                    {
                                        MessageBox.Show(LibStrings.FindResource("MsgResetFailed"), "ioSender");
                                        return(RestartResult.Close);
                                    }
                                }
                                else if (AttemptReset())
                                {
                                    model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                }
                            }
                            else
                            {
                                response = string.Empty;
                            }
                            break;

                        case 2:         // Soft limits
                            if (!GrblInfo.IsLoaded)
                            {
                                MessageBox.Show(string.Format(LibStrings.FindResource("MsgNoCommAlarm"), model.GrblState.Substate.ToString()), "ioSender");
                                if (AttemptReset())
                                {
                                    model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                                }
                                else
                                {
                                    MessageBox.Show(LibStrings.FindResource("MsgResetFailed"), "ioSender");
                                    return(RestartResult.Close);
                                }
                            }
                            else
                            {
                                response = string.Empty;
                            }
                            break;

                        case 10:         // EStop
                            if (GrblInfo.IsGrblHAL && model.Signals.Value.HasFlag(Signals.EStop))
                            {
                                MessageBox.Show(LibStrings.FindResource("MsgEStop"), "ioSender", MessageBoxButton.OK, MessageBoxImage.Warning);
                                while (!AttemptReset() && model.GrblState.State == GrblStates.Alarm)
                                {
                                    if (MessageBox.Show(LibStrings.FindResource("MsgEStopExit"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        return(RestartResult.Close);
                                    }
                                }
                                ;
                            }
                            else
                            {
                                AttemptReset();
                            }
                            if (!GrblInfo.IsLoaded)
                            {
                                model.ExecuteCommand(GrblConstants.CMD_UNLOCK);
                            }
                            break;

                        case 11:         // Homing required
                            if (GrblInfo.IsLoaded)
                            {
                                response = string.Empty;
                            }
                            else
                            {
                                Message = LibStrings.FindResource("MsgHome");
                            }
                            break;
                        }
                        break;

                    case GrblStates.Tool:
                        Comms.com.WriteByte(GrblConstants.CMD_STOP);
                        break;

                    case GrblStates.Door:
                        if (!GrblInfo.IsLoaded)
                        {
                            if (MessageBox.Show(LibStrings.FindResource("MsgDoorOpen"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                            {
                                return(RestartResult.Close);
                            }
                            else
                            {
                                bool exit = false;
                                do
                                {
                                    Comms.com.PurgeQueue();

                                    bool?res = null;
                                    CancellationToken cancellationToken = new CancellationToken();

                                    new Thread(() =>
                                    {
                                        res = WaitFor.SingleEvent <string>(
                                            cancellationToken,
                                            s => TrapReset(s),
                                            a => model.OnGrblReset += a,
                                            a => model.OnGrblReset -= a,
                                            200, () => Comms.com.WriteByte(GrblConstants.CMD_STATUS_REPORT));
                                    }).Start();

                                    while (res == null)
                                    {
                                        EventUtils.DoEvents();
                                    }

                                    if (!(exit = !model.Signals.Value.HasFlag(Signals.SafetyDoor)))
                                    {
                                        if (MessageBox.Show(LibStrings.FindResource("MsgDoorExit"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                        {
                                            exit = true;
                                            return(RestartResult.Close);
                                        }
                                    }
                                } while (!exit);
                            }
                        }
                        else
                        {
                            MessageBox.Show(LibStrings.FindResource("MsgDoorPersist"), "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                            response = string.Empty;
                        }
                        break;

                    case GrblStates.Hold:
                    case GrblStates.Sleep:
                        if (MessageBox.Show(string.Format(LibStrings.FindResource("MsgNoComm"), model.GrblState.State.ToString()),
                                            "ioSender", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                        {
                            return(RestartResult.Close);
                        }
                        else if (!AttemptReset())
                        {
                            MessageBox.Show(LibStrings.FindResource("MsgResetExit"), "ioSender");
                            return(RestartResult.Close);
                        }
                        break;

                    case GrblStates.Idle:
                        if (response.Contains("|SD:Pending"))
                        {
                            AttemptReset();
                        }
                        break;
                    }
                }
            }
            else
            {
                MessageBox.Show(response == string.Empty
                                    ? "No respone received from controller, exiting."
                                    : string.Format("Unexpected response received from controller: \"{0}\", exiting.", response),
                                "ioSender", MessageBoxButton.OK, MessageBoxImage.Stop);
                return(RestartResult.Exit);
            }

            return(response == string.Empty ? RestartResult.NoResponse : RestartResult.Ok);
        }
Exemplo n.º 5
0
        public void ApplyRotation(double angle, Vector3 offset, bool compress = false)
        {
            uint              G53lnr       = 0;
            int               precision    = GCode.File.Decimals;
            GCPlane           plane        = new GCPlane(GrblParserState.Plane == Plane.XY ? Commands.G17 : Commands.G18, 0);
            DistanceMode      distanceMode = GrblParserState.DistanceMode;
            Vector3           pos          = new Vector3(Grbl.GrblViewModel.Position.X, Grbl.GrblViewModel.Position.Y, Grbl.GrblViewModel.Position.Z);
            List <GCodeToken> toolPath     = new List <GCodeToken>();

            toolPath.Add(new GCComment(Commands.Comment, 0, string.Format("{0} degree rotation applied", Math.Round(angle * 180d / Math.PI, 1).ToInvariantString())));

            foreach (var token in GCode.File.Tokens)
            {
                switch (token.Command)
                {
                case Commands.G0:
                case Commands.G1:
                {
                    var motion = token as GCLinearMotion;

                    if (motion.AxisFlags != AxisFlags.None && G53lnr != token.LineNumber)
                    {
                        var target = ToAbsolute(pos, motion.Values);

                        if (distanceMode == DistanceMode.Incremental)
                        {
                            if (!motion.AxisFlags.HasFlag(AxisFlags.X))
                            {
                                target = new Vector3(0d, target.Y, 0d);
                            }

                            if (!motion.AxisFlags.HasFlag(AxisFlags.Y))
                            {
                                target = new Vector3(target.X, 0d, 0d);
                            }

                            target = target.RotateZ(0d, 0d, angle).Round(precision);
                        }
                        else
                        {
                            target = target.RotateZ(offset.X, offset.Y, angle).Round(precision);
                        }

                        if (target.X != pos.X)
                        {
                            motion.AxisFlags |= AxisFlags.X;
                        }

                        if (target.Y != pos.Y)
                        {
                            motion.AxisFlags |= AxisFlags.Y;
                        }

                        if (distanceMode == DistanceMode.Incremental)
                        {
                            pos += target;
                        }
                        else
                        {
                            pos = target;
                        }

                        toolPath.Add(new GCLinearMotion(motion.Command, motion.LineNumber, target.Array, motion.AxisFlags));
                    }
                    else
                    {
                        G53lnr = 0;
                        toolPath.Add(new GCLinearMotion(motion.Command, motion.LineNumber, motion.Values, motion.AxisFlags));
                    }
                }
                break;

                case Commands.G2:
                case Commands.G3:
                {
                    if (plane.Plane != Plane.XY)
                    {
                        throw new Exception(LibStrings.FindResource("HasG17G18Arcs"));
                    }

                    if ((token as GCArc).IsRadiusMode)         // for now...
                    {
                        throw new Exception(LibStrings.FindResource("HasRadiusArcs"));
                    }

                    var arc = token as GCArc;

                    Vector3 target    = ToAbsolute(pos, arc.Values).RotateZ(offset.X, offset.Y, angle).Round(precision);
                    Vector3 targetijk = arc.IsRadiusMode ? new Vector3(double.NaN, double.NaN, double.NaN) : new Vector3(arc.IJKvalues).RotateZ(0d, 0d, angle).Round(precision);

                    if (pos.X != target.X)
                    {
                        arc.AxisFlags |= AxisFlags.X;
                    }

                    if (pos.Y != target.Y)
                    {
                        arc.AxisFlags |= AxisFlags.Y;
                    }

                    pos = target;

                    toolPath.Add(new GCArc(arc.Command, arc.LineNumber, pos.Array, arc.AxisFlags, targetijk.Array, arc.IjkFlags, arc.R, arc.IJKMode));
                }
                break;

                case Commands.G5:
                {
                    var spline = token as GCSpline;
                    pos = new Vector3(spline.X, spline.Y, 0d).RotateZ(offset.X, offset.Y, angle).Round(precision);
                    var ij = new Vector3(spline.I, spline.J, 0d).RotateZ(offset.X, offset.Y, angle).Round(precision);
                    var pq = new Vector3(spline.P, spline.Q, 0d).RotateZ(offset.X, offset.Y, angle).Round(precision);

                    toolPath.Add(new GCSpline(spline.Command, spline.LineNumber, pos.Array, spline.AxisFlags, new double[] { ij.X, ij.Y, pq.X, pq.Y }));
                }
                break;

                case Commands.G17:
                case Commands.G18:
                case Commands.G19:
                    plane = token as GCPlane;
                    toolPath.Add(token);
                    break;

                case Commands.G53:
                    G53lnr = token.LineNumber;     // No rotation for next linear move
                    toolPath.Add(token);
                    break;

                case Commands.G73:
                case Commands.G81:
                case Commands.G82:
                case Commands.G83:
                case Commands.G85:
                case Commands.G86:
                case Commands.G89:
                {
                    var drill  = token as GCCannedDrill;
                    var target = ToAbsolute(pos, drill.Values).RotateZ(offset.X, offset.Y, angle);

                    if (pos.X != target.X)
                    {
                        drill.AxisFlags |= AxisFlags.X;
                    }

                    if (pos.Y != target.Y)
                    {
                        drill.AxisFlags |= AxisFlags.Y;
                    }

                    if (distanceMode == DistanceMode.Incremental)
                    {
                        pos = new Vector3(pos.X + target.X * drill.L, pos.Y + target.Y * drill.L, pos.Z + target.Z);
                    }
                    else
                    {
                        pos = target;
                    }

                    toolPath.Add(new GCCannedDrill(drill.Command, drill.LineNumber, target.Round(precision).Array, drill.AxisFlags, drill.R, drill.L, drill.P, drill.Q));
                }
                break;

                case Commands.G90:
                case Commands.G91:
                    distanceMode = (token as GCDistanceMode).DistanceMode;
                    toolPath.Add(token);
                    break;

                default:
                    toolPath.Add(token);
                    break;
                }
            }

            List <string> gc = GCodeParser.TokensToGCode(toolPath, compress);

            //            GCodeParser.Save(@"C:\Users\terjeio\Desktop\Probing\file.nc", gc);

            GCode.File.AddBlock(string.Format("{0} degree rotation applied: {1}", Math.Round(angle * 180d / Math.PI, 1).ToInvariantString(), Grbl.GrblViewModel.FileName), Core.Action.New);

            foreach (string block in gc)
            {
                GCode.File.AddBlock(block, Core.Action.Add);
            }

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