Monday, August 5, 2013

Keerbot Drawing Program

My friend Gilad is building a wall drawing robot.

A very cool one.

You can see some of his work here: http://www.keerbot.com/

Gilad asked me to write a little utility that will allow him to better debug the robot. This little tool allows you to draw on the PC, and then save the resulting drawing to a text file with G-Codes the robot can understand. It's a C# Visual Studio 2010 express project, but you can easily adopt it to any .Net environment.

I think it's a good tutorial for anyone who wants to create a simple Windows drawing program.

Hope you like that, and go check the Keerbot site and stay tuned for more cool stuff from Gilad.

So, here is the code, and you can get the whole VisualStudio project here: https://dl.dropboxusercontent.com/u/3647610/KeerbotProject.zip

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

// Keerbot Drawing Creator Program
//
// Written by: Israel Roth
//
// This code is provided with no warranties, but its free to use for any purpose.
//
// (c) 2013. Israel Roth
//

namespace Keerbot1
{
    public partial class Form1 : Form
    {
        private bool isMouseDown = false;
        private List points;
        private List> shapes;

        // scalePoint translate a point from the screen coordinates to the Keerbot coordinate space

        private Point scalePoint(Point inPoint)
        {
            int xx = (inPoint.X - 175) * 2;
            int yy = (250 - inPoint.Y) * 2;
            return new Point(xx, yy);
        }

        public Form1()
        {
            InitializeComponent();
            points = new List(100);
            shapes = new List>(10);
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
            if (points.Count > 2)
            {
                shapes.Add(points);
            }
            points = new List(100);
        }
                                                                                                                                                                            
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            Point scaled = scalePoint(e.Location);
            toolStripStatusLabel1.Text = scaled.X + "," + scaled.Y;
            if (isMouseDown)
            {
                points.Add(new Point(e.X, e.Y));
                panel1.Invalidate();
            }

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (shapes.Count > 0)
            {
                foreach (List s in shapes)
                {
                    DrawPoints(s, e);
                }
            }
            DrawPoints(points, e);
       }

        private void DrawPoints(List points, PaintEventArgs e)
        {
            if (points.Count > 2)
            {
                Pen pen = new Pen(Color.Black, 1);
                Point[] pArray = points.ToArray();
                e.Graphics.DrawLines(pen, pArray);
            }
         }

        private void button1_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string name = saveFileDialog1.FileName;
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(name))
            {
                foreach (List s in shapes)
                {
                    bool isFirst = true;
                    foreach (Point q in s)
                    {
                        Point p = scalePoint(q);
                        if (isFirst)
                        {
                            file.WriteLine(startLineText.Text + " X" + p.X + " Y" + p.Y + " Z" + zUpText.Text);
                            isFirst = false;
                        }
                        else
                        {
                            file.WriteLine(startLineText.Text + " X" + p.X + " Y" + p.Y + " Z" +
                                                                                    zDownText.Text);
                        }
                    }
                }
            }

        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            shapes.Clear();
            points.Clear();
            panel1.Invalidate();
        }

    }
}


1 comment:

  1. Thanks man ! really appreciated !

    KeerBot.com

    ReplyDelete