# VBForums CodeBank > CodeBank - Visual Basic .NET >  Font to G-Code CNC VB2005E

## Steinie

Somthing I did during Thanksgiving weekend.
 This is a Font to G-Code converter Class that will generate G-Code for a CNC machine. :wave: 
  Allows you to Scale, Offset and select the Origin of X/Y.
  The result G-Code can be Copied to Clipboard or Saved to a txt file.

Example how to read graphic Point Data and Types to a Text Box. :Smilie: 



```
' Font to G-Code by: Jim Steinbrecher 24 Nov 2011
Imports System.Drawing.Drawing2D
Imports System
Imports System.Windows.Forms
Imports System.Drawing.Text
Imports System.Drawing


Public Class Form1
    Dim graphics_path1 As New Drawing2D.GraphicsPath
    Dim ZType, Org As Byte
    Dim x, y, z, XOffsetSave, YOffsetSave, XSave, YSave, ZSave, ZSafe, ZDepth, Speed, Feed, DFeed, CFeed As Single ' x y z
    Dim XOffSet, YOffSet, Xsize, Xscale, Ysize, Yscale, FontSize As Single ' X & Y Min Max Offset Size Scale
    Dim FontName, GText As String
    Dim FontStyle, i As Integer
    Dim PlotFlag As Boolean = False


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PlotFlag = False
        GText = "G00"
        ZSafe = 0.15
        ZDepth = -0.1
        Speed = 5000
        CFeed = 18.0
        DFeed = 9.0
        FontName = "Times New Roman"
        FontStyle = 1
        FontSize = 125
        TB_font.Text = FontName & ", Type: " & FontStyle
        Org = 0
    End Sub

    Public Sub GetPointData(ByVal e As PaintEventArgs, ByVal PathPoints() As PointF, ByVal pathtypes() As Byte, ByVal xOfset As Integer)
        'Build the G Code in RichTextBox1
        GText = "G00"
        ProgressBar.Maximum = PathPoints.Length - 1
        For i = 0 To PathPoints.Length - 1
            If PathPoints(i).IsEmpty = False Then
                x = PathPoints(i).X : x = (x / 10) + XOffSet : x = x * Xscale : x = Math.Round(x, 4) 'calc x
                y = PathPoints(i).Y : y = (y / 10) + YOffSet : y = y * Yscale : y = Math.Round(y, 4) : y = 0 - y ' calc y
                If TB_Xneg.Text = "*" Then TB_Xneg.Text = x
                If TB_Xpos.Text = "*" Then TB_Xpos.Text = x
                If TB_Yneg.Text = "*" Then TB_Yneg.Text = y
                If TB_Ypos.Text = "*" Then TB_Ypos.Text = y ' calc min & max x & y
                If x < TB_Xneg.Text Then TB_Xneg.Text = x
                If x > TB_Xpos.Text Then TB_Xpos.Text = x
                If y < TB_Yneg.Text Then TB_Yneg.Text = y
                If y > TB_Ypos.Text Then TB_Ypos.Text = y

                ZType = pathtypes(i) 'Bit 6: 1=Last line in chr. - Byte: 0=First line, 1=Stright line, 3=Curve 
                If ZType = 0 Then XSave = x : YSave = y 'Type 0 is first line in chr., Save for closure
                If ZType < 128 Then z = ZDepth 'Bit 8 off is pen down
                If ZType > 127 Then z = ZSafe 'Bit 8 on is pen up


                If Feed = CFeed Then RichTextBox1.AppendText(GText & " X" & x & " Y" & y & vbCrLf) ' 1st G00 & All G01's Except last one in Chr.
                If Feed = DFeed Then Feed = CFeed : RichTextBox1.AppendText(GText & " X" & x & " Y" & y & " F" & CFeed & vbCrLf)
                GText = "G01" : If z > 0 Then GText = "G00" 'Z: <0 = G01, >0 = G00
                If ZType > 127 Then RichTextBox1.AppendText("G01" & " X" & XSave & " Y" & YSave & vbCrLf) 'Last G01 line, move to start.
                If ZSave <> z And z > 0 Then RichTextBox1.AppendText("G00" & " Z" & z & vbCrLf) 'Z Change, <0 = G01, >0 = G00
                If ZSave <> z And z < 0 Then Feed = DFeed : RichTextBox1.AppendText("G01" & " Z" & z & " F" & DFeed & vbCrLf) 'Z Change, <0 = G01, >0 = G00
                ZSave = z
            End If
            ProgressBar.Value = i
        Next i
        ProgressBar.Value = 0
    End Sub

    Private Sub ButCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_CopyToClipboard.Click
        If My.Computer.Keyboard.CtrlKeyDown Then GoTo SaveFile

        Me.RichTextBox1.Focus()
        Me.RichTextBox1.SelectAll()
        Me.RichTextBox1.Copy() 'Copy to Clipboard
        Exit Sub
SaveFile:
        ' Create a SaveFileDialog to request a path and file name to save to.
        Dim saveFile1 As New SaveFileDialog()

        ' Initialize the SaveFileDialog to specify the .txt extension for the file.
        saveFile1.DefaultExt = "*.txt"
        saveFile1.Filter = "*.txt|*.txt"

        ' Determine if the user selected a file name from the saveFileDialog.
        If (saveFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
            And (saveFile1.FileName.Length) > 0 Then

            ' Save the contents of the RichTextBox into the file.
            RichTextBox1.SaveFile(saveFile1.FileName, _
                RichTextBoxStreamType.PlainText)
        End If
        saveFile1.Dispose()
    End Sub


    Private Sub But_GenPlot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_GenPlot.Click
        If TB_Input.Text = "" Then Exit Sub 'Plot setup
        TB_Xsize.Text = "" : TB_Ysize.Text = ""
        GText = "G00"
        ZSafe = Val(TB_SafeZ.Text)
        ZDepth = Val(TB_DepthZ.Text)
        Speed = Val(TB_speed.Text)
        CFeed = Val(TB_CutFeed.Text)
        DFeed = Val(TB_DropFeed.Text)
        Xscale = Val(TB_Xscale.Text) : Yscale = Val(TB_Yscale.Text)
        XOffSet = Val(TB_XOffset.Text) : YOffSet = Val(TB_YOffset.Text)
        TB_Xneg.Text = "*" : TB_Xpos.Text = "*"
        TB_Yneg.Text = "*" : TB_Ypos.Text = "*"
        RichTextBox1.Clear()
        RichTextBox1.AppendText("(Font to G-Code By: Jim Steinbrecher)" & vbCrLf)
        RichTextBox1.AppendText("(Text: " & TB_Input.Text & ")" & vbCrLf)
        RichTextBox1.AppendText("(Font: " & TB_font.Text & ")" & vbCrLf)
        If Org = 0 Then RichTextBox1.AppendText("(X/Y Origin:)" & vbCrLf)
        If Org = 1 Then RichTextBox1.AppendText("(X/Y Origin: Lower Left)" & vbCrLf)
        If Org = 2 Then RichTextBox1.AppendText("(X/Y Origin: Central)" & vbCrLf)
        RichTextBox1.AppendText("(" & Date.Now & ")" & vbCrLf)
        RichTextBox1.AppendText(GText & " Z" & ZSafe & vbCrLf)
        RichTextBox1.AppendText("F" & CFeed & vbCrLf)
        RichTextBox1.AppendText("M3 S" & Speed & vbCrLf)
        PlotFlag = True
        Me.Refresh()
    End Sub
    Private Sub But_GenPlot_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        If TB_Input.Text = "" Then Exit Sub 'Plot if have text
        If PlotFlag = False Then Exit Sub 'Only plot 1 time

        Dim graphics_path1 As New Drawing2D.GraphicsPath  ' Create a GraphicsPath.
        graphics_path1.AddString(TB_Input.Text, _
        New FontFamily(FontName), FontStyle, FontSize, _
        New Point(8, 2), StringFormat.GenericTypographic) ' Screen Start location
        PlotFlag = False
        'e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias 'This will smooth out the drawing
        graphics_path1.CloseAllFigures()
        'e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
        e.Graphics.DrawPath(New Pen(Color.Black, 0), graphics_path1) ' Draw the path.
        'e.Graphics..FillPath(Brushes.Black, graphics_path1)

        GetPointData(e, graphics_path1.PathPoints, graphics_path1.PathTypes, 0) 'x offset <------- Build the G Code in RichTextBox1
        RichTextBox1.AppendText("M5" & vbCrLf)
        RichTextBox1.AppendText("M30" & vbCrLf)
        If Org = 0 Then
            RichTextBox1.Text = Replace(RichTextBox1.Text, "(X/Y Origin:)", "(X/Y Origin: Top Left and " & TB_Xneg.Text & ", " & TB_Yneg.Text & ")") 'ROld, RNew)
        End If
        TB_Xsize.Text = Val(TB_Xpos.Text) - Val(TB_Xneg.Text) ' update stats
        TB_Ysize.Text = Val(TB_Ypos.Text) - Val(TB_Yneg.Text)
        graphics_path1.Dispose()
    End Sub

    Private Sub TBfont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB_font.Click
        If FontDialog1.ShowDialog() = DialogResult.OK Then ' change font stats
            FontName = FontDialog1.Font.Name.ToString
            FontStyle = 0
            If FontDialog1.Font.Bold Then FontStyle += 1
            If FontDialog1.Font.Italic Then FontStyle += 2
            If FontDialog1.Font.Underline Then FontStyle += 4
            If FontDialog1.Font.Strikeout Then FontStyle += 8
            FontSize = 125
            TB_font.Text = FontName & ", Type: " & FontStyle
            But_GenPlot.PerformClick()
        End If
        'ButPlot.PerformClick()
    End Sub

    Private Sub TBInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB_Input.TextChanged
        PlotFlag = False
    End Sub
    Private Sub ButOrgLowerLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_OrgLowerLeft.Click
        XOffsetSave = Val(TB_XOffset.Text) : YOffsetSave = Val(TB_YOffset.Text)
        x = TB_XOffset.Text : x = 0 - x : TB_XOffset.Text = Str(x)
        Org = 1
        But_GenPlot.PerformClick()
        Xscale = Val(TB_Xscale.Text)
        x = Val(TB_Xneg.Text) / Xscale
        x = 0 - x
        TB_XOffset.Text = Str(x)
        Yscale = Val(TB_Yscale.Text)
        y = Val(TB_Yneg.Text) / Yscale
        TB_YOffset.Text = Str(y)
        But_GenPlot.PerformClick()
        TB_XOffset.Text = Str(XOffsetSave) : TB_YOffset.Text = Str(YOffsetSave)
        Org = 0
    End Sub

    Private Sub ButOrgCenter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles But_OrgCentral.Click
        XOffsetSave = Val(TB_XOffset.Text) : YOffsetSave = Val(TB_YOffset.Text)
        x = TB_XOffset.Text : x = 0 - x : TB_XOffset.Text = Str(x)
        Org = 2
        But_GenPlot.PerformClick()
        Xscale = Val(TB_Xscale.Text)
        Xsize = Val(TB_Xsize.Text) / 2
        x = Val(TB_Xneg.Text)
        x = Xsize + x
        x = 0 - x
        x = x / Xscale
        TB_XOffset.Text = Str(x)

        Yscale = Val(TB_Yscale.Text)
        Ysize = Val(TB_Ysize.Text) / 2
        y = Val(TB_Yneg.Text)
        y = Ysize + y
        y = y / Yscale
        TB_YOffset.Text = Str(y)
        But_GenPlot.PerformClick()
        TB_XOffset.Text = Str(XOffsetSave) : TB_YOffset.Text = Str(YOffsetSave)
        Org = 0
    End Sub
End Class
```

----------


## CodeDabbler

That's pretty cool!
Something I've wanted to do for a while, but just never makes it high enough on the priorty list.
Any chance that you could post the entire project here?

----------


## Steinie

Yes, I can do that, but not sure how or what files I need to post. I know that I can't post .exe files. So do I copy the folder, delete the .exe's then zip-it and post? Or do I just pick each file out and zip-it? If so, what files? Thanks.

----------


## CodeDabbler

> Yes, I can do that, but not sure how or what files I need to post. I know that I can't post .exe files. So do I copy the folder, delete the .exe's then zip-it and post? Or do I just pick each file out and zip-it? If so, what files? Thanks.


You remove the .exe's from your project, then *(I think)*  you zip the project folder and attach that.
Again, it's not something I do regularly.

I sure appreciate you making the effort, as it would save me some time! :Thumb:

----------


## BlindSniper

Just delete the debug and/or release folder and the obj folder and zip and upload.

----------


## moti barski

do you have more g code generators for shapes and stuff ?
btw, awesome !

----------


## Steinie

Thanks for the info. Will give it a go in next few days. :Eek Boom: 
@Moti; I did one, I call it G-Code clean-up. It allows you to do some editing of G Code like Change Origin to Lower Left, Center or anywere you want using offsets. Will Scale the code in X, Y, or X&Y. Insert or Remove line numbers also. Not done yet, will upload when I clean it up. I use it alot. :wave:

----------


## moti barski

I think vb.net has the power to run a full scall cad and cam program, if you comment the codes and give me some walkthroughs for g codes I will try to help.

this is what I have so far :

MDI, gcodes to do stuff:
S speed of spindle(drill), example S 1800
M03- clockwise spin
M04- ccw spin
M05- spindle off
M07- mist coolant on
M08- coolant on
M09 coolant off
T15- ready tool 15
m06-tool change
M60 auto pallet change

----------


## Steinie

See: Font To Gcode.zip 
in first post. :Smilie:

----------


## CodeDabbler

Thanks!
You should remove the deployment .EXE too.(I think?)

----------


## moti barski

no .txt walkthrough for g codes ?

----------


## Prabhat7471

Can you change some code so that instead of Z retract code (like Z-1) it writes M03 (Spindle ON) and instead of Z0 it writes M05 (Spindle OFF).

----------


## tucoali

Hello, how could I change Steinie's code so that the letters are broken into small 0.05mm segments? I need to machine small segments and not long straight

----------


## boops boops

> Hello, how could I change Steinie's code so that the letters are broken into small 0.05mm segments? I need to machine small segments and not long straight


Hi tucoali, I think that what you need is the *GraphicsPath.Flatten* method. What this does is approximate each curve in the graphics path to a succession of straight line segments. 

In Steinie's code, find the paint sub (But_GenPlot_Paint) and add the Flatten statement immediately after the AddString statement. Example:


```
Dim flatValue As Single = 0.01 'example value
graphics_path1.Flatten(Nothing, flatValue)
```

The *flatValue* determines the length of the resulting line segments. You will probably have to try out different values of flatValue to get your required spacing in millimetres, because that will depend on things like the pixel resolution of your screen.

BB

----------


## tucoali

Hello boops!
I added the lines you proposed and it seems to work. I generated the gcode of number 0 with 0.5 and gave 83 lines of code, then generated with 0.001 and resulted 900 lines of code.
However, my ncviwer does not show this change. It looks very broken and I didn't find another friendly gcode viewer, but thanks for the tip, you are very smart.

If anyone knows any nc viewer, please send me the tip. Thank you all.

----------


## tucoali

Boops, something else ...
In the capital letter L in arial, the number of segments has not changed, ie when the path type is straight line, the "flatten" does not interfere and I need all types of paths to be broken into small segments equally. Did you know how to solve this?

----------


## boops boops

Sorry tucoali, I am getting a bit rusty! I forgot that flattening doesn't guarantee _equal_ segments. The vertices will always be more crowded in sharp curves, and straight-line segments won't be changed at all. I think it must be possible to divide up a GraphicsPath into equal small segments and I have been planning to solve this problem. And flattening will still be useful.

Another mistake I made was to reply to this thread! That's because the Code Bank forum isn't meant for asking questions, particularly questions that aren't addressed to the original poster (who may have disappeared years ago for all I know). 

Would you please post your question to the VB.Net forum. I will try to answer it there, and perhaps other people can contribute too.

BB

----------


## tucoali

Thanks Boops, I just need to understand how to draw a text and then get all the points that define that text. Amazingly, there is no such thing available on the internet.

----------

