Exemple #1
0
        public static IGcode Parse(string frame, IGcode current)
        {
            if (current == null)
            {
                throw new ArgumentNullException("current");
            }
            if (string.IsNullOrEmpty(frame))
            {
                throw new ArgumentException("Frame is empty", "frame");
            }
            IGcode result;
            var    paramRegex   = new Regex(@"([XxYyZzFfSsRrIiJjKk])((-?\d+)([,.]\d+)?|([,.]\d+))");
            var    gcodeRegex   = new Regex(@"([GgMm])((-?\d+)([,.]\d+)?|([,.]\d+))");
            var    paramMatches = paramRegex.Matches(frame);
            var    gcodeMatches = gcodeRegex.Matches(frame);

            if (gcodeMatches.Count == 0)
            {
                result = CloneCode(current);
            }
            else
            {
                switch (gcodeMatches.Cast <Match>().Last().Value.ToUpper())
                {
                case "G0":
                case "G00":
                    result = new G00();
                    break;

                case "G1":
                case "G01":
                    result = new G01();
                    break;

                case "G2":
                case "G02":
                    result = new G02();
                    break;

                case "G3":
                case "G03":
                    result = new G03();
                    break;

                case "G90":
                    result = new G90();
                    break;

                case "G91":
                    result = new G91();
                    break;

                default:
                    result = CloneCode(current);
                    break;
                }
            }

            return(ApplyParameters(result, current, paramMatches.Cast <Match>().Select(x => x.Value)));
        }
Exemple #2
0
        private static IGcode CloneCode(IGcode originalGcode)
        {
            var type   = originalGcode.GetType();
            var result = (IGcode)Activator.CreateInstance(type);

            foreach (var propertyInfo in type.GetProperties().Where(p => p.GetCustomAttributes(typeof(CodeParameter), true).Any(x => ((CodeParameter)x).Persistent)))
            {
                propertyInfo.SetValue(result, propertyInfo.GetValue(originalGcode));
            }
            return(result);
        }
Exemple #3
0
        public IEnumerable <IGcode> Expand(IGcode prevCommand, double defaultSpeed, double stepSize, double angle)
        {
            if (prevCommand == null)
            {
                throw new ArgumentNullException("prevCommand");
            }

            var startPoint = new Point3D(prevCommand.XDestination ?? 0, prevCommand.YDestination ?? 0, prevCommand.ZDestination ?? 0);

            var endPoint = new Point3D(XDestination ?? prevCommand.XDestination ?? 0, YDestination ?? prevCommand.YDestination ?? 0, ZDestination ?? prevCommand.ZDestination ?? 0);

            RadialInterpolation ri;

            if (RDistance.HasValue)
            {
                ri = new RadialInterpolation(startPoint, endPoint, RDistance.Value, Direction);
            }
            else
            {
                var center = new Point3D(
                    startPoint.X + IDistance ?? 0,
                    startPoint.Y + JDistance ?? 0,
                    startPoint.Z + KDistance ?? 0);

                ri = new RadialInterpolation(startPoint, center, endPoint, Direction);
            }

            var pts       = ri.GetArcPoints(stepSize, angle);
            var prevPoint = new Point3D(
                prevCommand.XDestination ?? 0,
                prevCommand.YDestination ?? 0,
                prevCommand.ZDestination ?? 0);

            foreach (var point3D in pts)
            {
                var spd = FSpeed;
                if (prevCommand is IMovementSpeed)
                {
                    spd = FSpeed ?? (prevCommand as IMovementSpeed).FSpeed ?? defaultSpeed;
                }

                yield return(new G01 {
                    FSpeed = spd, XStart = prevPoint.X, YStart = prevPoint.Y, ZStart = prevPoint.Z, XDestination = point3D.X, YDestination = point3D.Y, ZDestination = point3D.Z
                });

                prevPoint = point3D;
            }
        }
Exemple #4
0
        private static IGcode ApplyParameters(IGcode code, IGcode current, IEnumerable <string> parameters)
        {
            foreach (var paramMatch in parameters)
            {
                code.ApplyParam(paramMatch);
            }

            code.XStart = current.XDestination;
            code.YStart = current.YDestination;
            code.ZStart = current.ZDestination;

            code.XDestination = code.XDestination ?? code.XStart;
            code.YDestination = code.YDestination ?? code.YStart;
            code.ZDestination = code.ZDestination ?? code.ZStart;

            return(code);
        }