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.
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.
Code:
' 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
Last edited: