Example #1
0
        private void buttonOk_Click(object sender, EventArgs e)
        {
            string code =
            @"using System;

            namespace expNS{
            public static class expCL{
            public static double Value(double x, double y)
            {
            return {0};
            }
            }
            }".Replace("{0}", textBoxExpression.Text);

            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();

            parameters.GenerateInMemory = true;
            parameters.GenerateExecutable = false;

            CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);

            if (results.Errors.HasErrors)
            {
                StringBuilder sb = new StringBuilder();

                foreach (CompilerError error in results.Errors)
                {
                    sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
                }

                MessageBox.Show(sb.ToString());
                return;
            }

            Assembly assembly = results.CompiledAssembly;
            Type cl = assembly.GetType("expNS.expCL");
            MethodInfo Value = cl.GetMethod("Value");

            Map = new HeightMap((int)pX.Value, (int)pY.Value, (float)numericUpDownGrid.Value, (float)X.Value, (float)Y.Value);

            while (Map.NotProbed.Any())
            {
                Point p = Map.NotProbed.Dequeue();
                PointF c = Map.GetCoordinates(p);
                try
                {
                    object result = Value.Invoke(null, new object[]{(double)c.X, (double)c.Y});
                    Map[p.X, p.Y] = Convert.ToSingle(result);
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Runtime Error: " + ex.Message);
                    return;
                }
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
        private void newHeightMapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (new NewHeightMap().ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                CurrentMap = new HeightMap(Set.NewHeightMapPoints.Width, Set.NewHeightMapPoints.Height, Set.NewHeightMapGridSize, Set.NewHeightMapOffset.Width, Set.NewHeightMapOffset.Height);

                CurrentMap.OnPointAdded += HeightMapUpdated;

                toolStripButtonStart.Enabled = GRBL.Connected;

                HeightMapUpdated();
            }
        }
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                CurrentMap = new HeightMap(new BinaryReader(File.OpenRead(openFileDialogHMap.FileName)));
                CurrentMap.OnPointAdded += HeightMapUpdated;

                toolStripButtonStart.Enabled = (CurrentMap.NotProbed.Count > 0) & GRBL.Connected;

                HeightMapUpdated();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not open HeightMap\n" + ex.Message);
            }
        }
        private void TryLoadHeightMap(string path)
        {
            try
            {
                CurrentMap = new HeightMap(new BinaryReader(File.OpenRead(path)));
                CurrentMap.OnPointAdded += HeightMapUpdated;

                toolStripButtonStart.Enabled = (CurrentMap.NotProbed.Count > 0) & GRBL.Connected;

                HeightMapUpdated();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not open HeightMap\n" + ex.Message);
            }
        }
Example #5
0
        public static IEnumerable<GCodeCommand> ApplyHeightMap(this IEnumerable<GCodeCommand> commands, HeightMap map)
        {
            foreach (GCodeCommand command in commands)
            {
                if (command is OtherCode)
                {
                    yield return command;
                    continue;
                }
                else
                {
                    Movement m = (Movement)command;

                    int divisions = (int)Math.Ceiling(m.Length / map.GridSize);

                    if (m is Straight)
                    {
                        Straight s = (Straight)m;

                        if (s.Rapid)
                        {
                            Vector3 newEnd = s.End;
                            newEnd.Z += map.GetHeightAt(s.End.X, s.End.Y);

                            yield return new Straight(s.Start, newEnd, true);
                        }
                        else
                        {
                            Vector3 pos = s.Start;

                            for (int x = 1; x <= divisions; x++)
                            {
                                Vector3 end = s.Start.Interpolate(s.End, (float)x / (float)divisions);
                                end.Z += map.GetHeightAt(end.X, end.Y);
                                Straight st = new Straight(pos, end, false);
                                if (x == 1)
                                    st.FeedRate = s.FeedRate;
                                yield return st;
                                pos = end;
                            }
                        }
                    }
                    if (m is Arc)
                    {
                        Arc a = (Arc)m;

                        Vector3 pos = a.Start;

                        float stretch = a.StartAngle - a.EndAngle;

                        if (stretch <= 0)
                            stretch += 2 * (float)Math.PI;

                        if (a.Direction == ArcDirection.CCW)
                        {
                            stretch = 2 * (float)Math.PI - stretch;
                        }

                        if (stretch <= 0)
                            stretch += 2 * (float)Math.PI;

                        for (int x = 1; x <= divisions; x++)
                        {
                            Vector3 end = new Vector3(a.Radius, 0, 0);

                            if (a.Direction != ArcDirection.CW)
                                end.Roll(a.StartAngle + stretch * (float)x / (float)divisions);
                            else
                                end.Roll(a.StartAngle - stretch * (float)x / (float)divisions);

                            end += a.Center;

                            end.Z = a.Start.Z + (a.End.Z - a.Start.Z) * (float)x / (float)divisions;

                            end.Z += map.GetHeightAt(end.X, end.Y);

                            Arc arc = new Arc(pos, end, a.Center, a.Direction);

                            if (x == 1)
                                arc.FeedRate = a.FeedRate;

                            yield return arc;
                            pos = end;
                        }
                    }
                }
            }

            yield break;
        }
Example #6
0
        Image GetPreview(HeightMap map, Size ImgSize)
        {
            //Determine Size of single Check (without two pixel border
            //according to http://www.wolframalpha.com/input/?i=solve+s%3Dg*%28x+-+1%29+%2Bb+*+x+for+g
            PixelsPerCheck = Math.Min(
                (ImgSize.Width - border * map.SizeX) / (map.SizeX - 1),
                (ImgSize.Height - border * map.SizeY) / (map.SizeY - 1));

            ActPreviewSize = new Size(PixelsPerCheck * (map.SizeX - 1) + map.SizeX * border, PixelsPerCheck * (map.SizeY - 1) + map.SizeY * border);

            Bitmap b = new Bitmap(ActPreviewSize.Width, ActPreviewSize.Height);

            Graphics gfx = Graphics.FromImage(b);

            for (int x = 0; x < map.SizeX - 1; x++)
            {
                for (int y = 0; y < map.SizeY - 1; y++)
                {
                    if (!(map.HasValue[x, y] && map.HasValue[x + 1, y] && map.HasValue[x, y + 1] && map.HasValue[x + 1, y + 1]))
                        continue;

                    int offsetX = x * (border + PixelsPerCheck) + border;
                    int offsetY = b.Size.Height - 2 - ((y + 1) * (border + PixelsPerCheck)) + border;

                    GraphicsPath rectpath = new GraphicsPath();

                    rectpath.AddLine(offsetX + 0,				offsetY + PixelsPerCheck,	offsetX + PixelsPerCheck,	offsetY + PixelsPerCheck);
                    rectpath.AddLine(offsetX + PixelsPerCheck,	offsetY + PixelsPerCheck,	offsetX + PixelsPerCheck,	offsetY + 0);
                    rectpath.AddLine(offsetX + PixelsPerCheck,	offsetY + 0,				offsetX + 0,				offsetY + 0);

                    PathGradientBrush brush = new PathGradientBrush(rectpath);
                    brush.SurroundColors = new Color[]
                    {
                        Helpers.ColorFromHeight(map[x, y], map.MinZ, map.MaxZ),
                        Helpers.ColorFromHeight(map[x + 1, y], map.MinZ, map.MaxZ),
                        Helpers.ColorFromHeight(map[x + 1, y + 1], map.MinZ, map.MaxZ),
                        Helpers.ColorFromHeight(map[x, y + 1], map.MinZ, map.MaxZ)
                    };

                    PointF center = map.GetCoordinates(new Point(x, y));
                    center.X += map.GridSize / 2;
                    center.Y += map.GridSize / 2;
                    brush.CenterColor = Helpers.ColorFromHeight(map.GetHeightAt(center), map.MinZ, map.MaxZ);

                    gfx.FillRectangle(brush,
                        offsetX,
                        offsetY,
                        PixelsPerCheck,
                        PixelsPerCheck
                        );

                    rectpath.Dispose();
                    brush.Dispose();
                }
            }

            Pen crossPen = new Pen(Color.Black, 2);

            for (int x = 0; x < map.SizeX; x++)
            {
                for (int y = 0; y < map.SizeY; y++)
                {
                    if (!map.HasValue[x, y])
                        continue;

                    gfx.DrawLine(crossPen,
                        x * (PixelsPerCheck + border) + .5f,
                        ActPreviewSize.Height - (y * (PixelsPerCheck + border)) + 4,
                        x * (PixelsPerCheck + border) + .5f,
                        ActPreviewSize.Height - (y * (PixelsPerCheck + border)) - 6
                        );

                    gfx.DrawLine(crossPen,
                        x * (PixelsPerCheck + border) - 4,
                        ActPreviewSize.Height - y * (PixelsPerCheck + border) - 1.5f,
                        x * (PixelsPerCheck + border) + 6,
                        ActPreviewSize.Height - y * (PixelsPerCheck + border) - 1.5f
                        );
                }
            }

            gfx.Dispose();

            return b;
        }
Example #7
0
		Image GetPreview(HeightMap map, Size ImgSize)
		{
			//Determine Size of single Check (without two pixel border
			//according to http://www.wolframalpha.com/input/?i=solve+s%3Dg*%28x+-+1%29+%2Bb+*+x+for+g
			PixelsPerCheck = Math.Min(
				(ImgSize.Width - border * map.SizeX) / (map.SizeX - 1),
				(ImgSize.Height - border * map.SizeY) / (map.SizeY - 1));

			ActPreviewSize = new Size(PixelsPerCheck * (map.SizeX - 1) + map.SizeX * border, PixelsPerCheck * (map.SizeY - 1) + map.SizeY * border);

			Bitmap b = new Bitmap(ActPreviewSize.Width, ActPreviewSize.Height);
					
			Graphics gfx = Graphics.FromImage(b);

			for (int x = 0; x < map.SizeX - 1; x++)
			{
				for (int y = 0; y < map.SizeY - 1; y++)
				{
					if (!(map.HasValue[x, y] && map.HasValue[x + 1, y] && map.HasValue[x, y + 1] && map.HasValue[x + 1, y + 1]))
						continue;

					int offsetX = x * (border + PixelsPerCheck) + border;
					int offsetY = b.Size.Height - 2 - ((y + 1) * (border + PixelsPerCheck)) + border;

					GraphicsPath rectpath = new GraphicsPath();

					rectpath.AddLine(offsetX + 0,				offsetY + PixelsPerCheck,	offsetX + PixelsPerCheck,	offsetY + PixelsPerCheck);
					rectpath.AddLine(offsetX + PixelsPerCheck,	offsetY + PixelsPerCheck,	offsetX + PixelsPerCheck,	offsetY + 0);
					rectpath.AddLine(offsetX + PixelsPerCheck,	offsetY + 0,				offsetX + 0,				offsetY + 0);

					PathGradientBrush brush = new PathGradientBrush(rectpath);
					brush.SurroundColors = new Color[] 
					{
						Helpers.ColorFromHeight(map[x, y], map.MinZ, map.MaxZ),
						Helpers.ColorFromHeight(map[x + 1, y], map.MinZ, map.MaxZ),
						Helpers.ColorFromHeight(map[x + 1, y + 1], map.MinZ, map.MaxZ),
						Helpers.ColorFromHeight(map[x, y + 1], map.MinZ, map.MaxZ)
					};

					PointF center = map.GetCoordinates(new Point(x, y));
					center.X += map.GridSize / 2;
					center.Y += map.GridSize / 2;
					brush.CenterColor = Helpers.ColorFromHeight(map.GetHeightAt(center), map.MinZ, map.MaxZ);

					gfx.FillRectangle(brush,
						offsetX,
						offsetY,
						PixelsPerCheck,
						PixelsPerCheck
						);

					rectpath.Dispose();
					brush.Dispose();
				}
			}


			Pen crossPen = new Pen(Color.Black, 2);

			for (int x = 0; x < map.SizeX; x++)
			{
				for (int y = 0; y < map.SizeY; y++)
				{
					if (!map.HasValue[x, y])
						continue;

					gfx.DrawLine(crossPen,
						x * (PixelsPerCheck + border) + .5f,
						ActPreviewSize.Height - (y * (PixelsPerCheck + border)) + 4,
						x * (PixelsPerCheck + border) + .5f,
						ActPreviewSize.Height - (y * (PixelsPerCheck + border)) - 6
						);
					
					gfx.DrawLine(crossPen,
						x * (PixelsPerCheck + border) - 4,
						ActPreviewSize.Height - y * (PixelsPerCheck + border) - 1.5f,
						x * (PixelsPerCheck + border) + 6,
						ActPreviewSize.Height - y * (PixelsPerCheck + border) - 1.5f
						);
				}
			}

			gfx.Dispose();


			return b;
		}