Click here to Skip to main content
Click here to Skip to main content

CNC Graphical Backplotter

, 30 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Article and source code for creating a CNC graphical backplotter
Sample image

Introduction

This program will process and graphically display CNC code. This plain text code is used by machine tools to cut metal and other materials. Wikipedia has information on CNC if you are not familiar with it. A CNC Programmer would use this type of program to verify code (G-code) before sending it to a machine to cut a part.

Background

My big plan was to simply use the Upgrade Wizard on an old CNC viewer program that I had written in VB6 and then "Give it away give it away give it away now" when I was finished. It ended up being an exercise in GDI+ and a total rewrite of the parser using Regular Expressions. It's part of a larger project that I am currently rewriting in VB.NET 2005.

These were my requirements for this project.

  • Faster G-code parsing. Regular Expressions helps here.
  • Multiple viewports. 1,2 and 4. A custom User Control was key.
  • 3-D view manipulation. A nimble display list is created with a geometry transformation.
  • Tool filtering to hide and show specific tools.
  • Graphical selection or picking. Combination of a back-buffer and real-time drawing was the answer.

Using the Code

Parsing

G-code is fairly simple. It's a plain ASCII text file made up of lines or "blocks" as they are sometimes called. Each line has a number of words that begin with a letter and is always followed by a number. For example; G00 X1.0. CNC programs can have callable procedures as well. They are commonly used for a set of hole locations that will be used for a number of tools. These procedures or "sub programs" have a specific beginning and end label.

Regular expressions seemed perfect for the job of breaking the file into programs, breaking the programs into blocks and then finally breaking the blocks into individual words. The .NET implementation of Regular Expressions also has an option to be compiled. This helps us get the best performance if the match expression does not change very often.
Because it's not unusual for some CNC programs to exceed 100,000 lines of code, performance is important.

Regular Expressions helped me get the time down from "Go get a coffee" to about 10 seconds on my 97,000-line dataset.

The first match expression that matches programs looks like this. [:\$O]+([0-9]+) It identifies a program label by matching a :, a$ or the Letter O. The \ is needed to escape the $ so RegEx doesn't get confused. The string is concatenated at run time using labels that are specific to each machine tool as shown.

mRegSubs = New Regex(comment & "[" & progId & "]([0-9]+)", 
                     RegexOptions.Compiled) 

As you can see you simply add RegexOptions.Compiled as an optional parameter.
Because g-code supports human readable comments within the code a little extra work is needed.

Comments are usually between parentheses. For example. (REMOVE CLAMP) The comments can also span multiple lines. So the expression match needed to distinguish between comments and actual G-code words. Not a big deal. I used Expresso to help me nail it down.

This match is a little more complicated and looks like this.

\([^\(\)]*\)|[/\*].*\n|\n|[A-Z][-+]?[0-9]*\.?[0-9]*

The first part of the match will match a comment or a newline. The second part will match a letter followed by a number.
In code, the match string is constructed based on what the user specifies as a comment character.

Dim progId As String = Regex.Escape(mCurMachine.ProgramId) 
Dim comment As String = comments(0) & "[^" & comments(0) &_
                        comments(1) & "]*" & comments(1) & "|" 
 
mRegWords = New Regex(comment & "[" & skipChars & "].*\n|\n|" &_
                      My.Resources.datRegexNcWords, _
                      RegexOptions.Compiled Or RegexOptions.IgnoreCase) 

Once the match strings are set we parse the file.

For Each m As Match In Me.mRegSubs.Matches(sFileContents)
    ...
Next

Each program match is then processed for word matching.

For Each p In mNcProgs
    mTotalBites = p.Contents.Length
    ProcessSubWords(p)
Next

Matches are either a newline, a comment or a word.

Private Sub ProcessSubWords(ByVal p As clsProg)
    For Each ncWord As Match In Me.mRegWords.Matches(p.Contents)
        'Each word
        If ncWord.Value = vbLf Then 'Is this a newline
            CreateGcodeBlock()
        ElseIf MatchIsComment(ncWord) Then
            'Comment
            mTotalLines += ncWord.Value.Split(CChar(vbLf)).Length - 1
        ElseIf mCurMachine.BlockSkip.Contains(ncWord.Value.Chars(0)) Then
            'Blockskip.
            mTotalLines += 1
        Else
            'Word
             EvaluateWord()
        End If
    Next
End Sub

Then we evaluate each word appropriately based on the letter or label value.

    Private Sub EvaluateWord()
        Select Case mCurAddress.Label
            Case "X"c
                mXpos = FormatAxis(mCurAddress.StringValue, 
                                   mCurMachine.Precision)
            Case "Y"c
                mYpos = FormatAxis(mCurAddress.StringValue, 
                                   mCurMachine.Precision)
            Case "Z"c
            ...

When a newline is matched we add a record to a collection. This is similar to the way the CNC machine will process the g-code. This collection will be shared amongst all the instances of the viewer control and will be used to create viewer-specific display lists.

'Results of the parsing stored here 
Public Shared MotionBlocks As New List(Of clsMotionRecord) 
'Each viewer control will have it's own version of this list 
Private mDisplayLists As New List(Of clsDisplayList) 

Processing and Rendering

GDI+ is quite rich but as the namespace System.Drawing.Drawing2D suggests its 2D only. It will draw just about anything and allow you to transform it using a 3 x 3 matrix. The approach I used was to create a master list (MotionBlocks) that is shared among all the viewports. Then create a slimmer display list (clsDisplayList)that has had the coordinates rotated in 3-D space.

This transformation is done any time the view orientation is changed. The 3-D rotation code is not fancy but it gets the job done. Look for Private Sub DrawEachElmt() in MG_BasicViewer.vb for details.

The way that machine tools actually move when in rapid positioning mode is also considered. if the machine is at X0,Y0,Z0 and rapids to X1.0 Y2.0 Z3.0 it does not actually move in one linear path. It will move in a dog-leg type motion by completing the shortest axis distance first.

If you download and play with the program you will also notice that it displays a coordinate system indicator at X,Y,Z zero, and that it never changes size. This is done by using an exclusive matrix for the coordinate system that maintains a constant scale factor.

Arcs and circles are another thing that GDI+ will draw just fine but if you need to rotate the arc in 3D space you're out of luck. All the arcs and circles in this program are drawn using line segments that resemble arcs. See the PolyCircle sub for details. You would think this would be wicked slow but it works fine and actually has a couple of advantages.

One of them is that a helix is easy to do if you draw it as line segments. CNC machines often cut a circle while moving in the Z axis to form a helix. Milling screw threads is an example of this.

The other advantage is that you have control over the quality of the arc by drawing more or less line legments.

If you have a large number of arcs that are small on the screen you can draw them with a small number of segments to save rendering time.

The fact that everything is drawn as a line segment also helps when we need to find the extents of the geometry at a particular view orientation. We just rip through all the end points in the display list and set the max and min values.

For Each l As clsDisplayList In mDisplayLists
    For Each p As PointF In l.Points
        mExtentX(0) = Math.Min(mExtentX(0), p.X)
        mExtentX(1) = Math.Max(mExtentX(1), p.X)
        mExtentY(0) = Math.Min(mExtentY(0), p.Y)
        mExtentY(1) = Math.Max(mExtentY(1), p.Y)
    Next
Next

Hit Testing

Selection was another important feature I wanted. Basically, if the mouse is near enough to a line that we have previously drawn, it should be identified by drawing it in a thicker pen. The following is a fragment from the custom rectangle class that can determine if a line passes through it. I was pleasantly surprised with the performance of the code.

    Public Function Contains(ByVal x As Single, ByVal y As Single) _
                                                              As Boolean
        Return x > Left And x < Right And y > Bottom And y < Top
    End Function

    Public Function IntersectsLine(ByVal x1 As Single, ByVal y1 As Single,_
                       ByVal x2 As Single, ByVal y2 As Single) As Boolean
        'Trivial test inside
        If Me.Contains(x1, y1) Or Me.Contains(x2, y2) Then
            Return True
        End If
        'Trivial test outside
        If x1 < Me.Left And x2 < Me.Left Then
            Return False
        ElseIf x1 > Me.Right And x2 > Me.Right Then
            Return False
        ElseIf y1 < Me.Bottom And y2 < Me.Bottom Then
            Return False
        ElseIf y1 > Me.Top And y2 > Me.Top Then
            Return False
        End If

        'Trivial test vertical or horizontal
        If x1 = x2 Then
            Return True
        End If
        If y1 = y2 Then
            Return True
        End If

        Dim slope As Single = (y2 - y1) / (x2 - x1)
        Dim Yintercept As Single = y1 - (slope * x1)
        Dim iptX As Single
        Dim iptY As Single

        'Left edge
        iptX = Me.Left
        iptY = (slope * iptX) + Yintercept
        If iptY > Me.Bottom And iptY < Me.Top Then
            Return True
        End If

        'Right edge
        iptX = Me.Right
        If iptY > Me.Bottom And iptY < Me.Top Then
            Return True
        End If

        'Top edge
        iptY = Me.Top
        iptX = ((iptY - Yintercept) / slope)
        If iptX > Me.Left And iptX < Me.Right Then
            Return True
        End If

        'Bottom edge
        iptY = Me.Bottom
        iptX = ((iptY - Yintercept) / slope)
        If iptX > Me.Left And iptX < Me.Right Then
            Return True
        End If
        Return False
    End Function

Rendering

Once the hit-test code was written things looked pretty good. On a small CNC program that is. It's always good to test your program on large datasets periodically. And this test failed badly. If the entire display list is enumerated every time the mouse is moved things slow down. All we need to do is just draw a few lines in a thicker pen.

The solution was to first draw all the graphics to a buffer, and then in the Paint event of the control simply draw that buffer, which is very fast. Then draw any selected geometry in real-time on top of the buffer.

When the mouse is in selection mode, the graphics never change position so there is no need to re-create the entire display list. So as the mouse moves we draw only the items in the display list that are in the hit zone after we draw the buffer.

DrawSelectionOverlay is simply called from the MouseMove event when in selection mode. The BufferedGraphics class was used to implement my custom back buffer.

 Private Sub SetBufferContext()
    If mGfxBuff IsNot Nothing Then
        mGfxBuff.Dispose()
        mGfxBuff = Nothing
    End If
    ' Retrieves the BufferedGraphicsContext for the current application 
    ' domain.
    mContext = BufferedGraphicsManager.Current
    ' Sets the maximum size for the primary graphics buffer
    mContext.MaximumBuffer = New Size(Me.Width + 1, Me.Height + 1)
    ' Allocates a graphics buffer the size of this control
    mGfxBuff = mContext.Allocate(CreateGraphics(), 
                                 New Rectangle(0, 0, Me.Width, Me.Height))
    mGfx = mGfxBuff.Graphics
End Sub        
        
 Private Sub DrawSelectionOverlay()
    'Draw the buffer
    mGfxBuff.Render()
    
    'Draw the selection overlay.
    mCurPen.Width = ((1 / mDpiX) / mScaleToReal) * 2
    With Graphics.FromHwnd(Me.Handle)
        .PageUnit = GraphicsUnit.Inch
        .ResetTransform()
        .MultiplyTransform(mMtxDraw)
        For Each p As clsDisplayList In mSelectionHitLists
            mCurPen.Color = p.Color
            If p.Rapid Then
                mCurPen.DashStyle = Drawing2D.DashStyle.Dash
            Else
                mCurPen.DashStyle = Drawing2D.DashStyle.Solid
            End If
            .DrawLines(mCurPen, p.Points)
        Next
    End With
End Sub

Points of Interest

The code that does the 3D transformations is old and certainly OpenGL and DirectX are better solutions. Subsequently, I didn't spend any time optimizing it. The whole project was done in "Git-r-done" mode because a more advanced viewer is currently being written using OpenGL and C++.

The selection performance was a big surprise. When there are so many lines on the screen that you can barely see the background color and it's fast, I'm happy. I thought this level of performance was only possible using OpenGL.

Another thing I ran into that I have seen before is a condition where a line does not render completely. A gap can be seen. This will happen when the end points of the line are way off the screen and the line is at a very slight angle. I ended up testing for this condition and making corrections for it. If anyone knows why this happens and how to prevent it please let me know.

You will find some other tricks in the code to boost performance. For example, a test is done to determine if the line is completely off the screen. If it is then it is not drawn into the buffer. It takes way less time to do the test than to draw into the buffer. So the closer you zoom into a large dataset the faster the view manipulation gets.

Also the System.Drawing.Drawing2D.GraphicsPath class looked like it was made for this type of project but it proved to be slow on large datasets. As you can see in the above fragment, the DrawLines method which accepts an array of points was my choice.

You won't find too much error handling in his demo. I stripped it out to keep it simple. I also removed the entire machine configuration form.

History

This is my second article on The Code Project and I hope it's of some value to others.
And of course I welcome suggestions and criticisms.

  • 31st January, 2007: Initial post
  • 28th November, 2008: Source code updated to include C#

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jason Titcomb
Web Developer
United States United States
Currently working as a CAD(Computer aided design) trainer and macro developer for a CAD company. Background also includes 8 years of CNC(computer numerical control) programming and machine tool automation.

Comments and Discussions

 
QuestionWhat is full form of "Wcs" in source code? PinmemberAruna Bhayani2-Jan-15 1:55 
QuestionMessage Removed PinmemberAruna Bhayani8-Dec-14 21:32 
QuestionTool Library Pinmemberpatch.wheatley11-Apr-13 7:27 
GeneralMy vote of 5 Pinmembermanoj kumar choubey22-Feb-12 22:42 
QuestionConversational Cnc Programming System Pinmemberc0g06-Feb-12 9:04 
AnswerRe: Conversational Cnc Programming System PinmemberJason Titcomb7-Feb-12 14:07 
GeneralRe: Conversational Cnc Programming System Pinmemberc0g08-Feb-12 2:13 
GeneralHelp on the code Pinmemberbetselot hailu12-Jan-12 22:07 
GeneralMy vote of 5 PinmemberJon Gunnar Brekke7-Dec-11 12:54 
Questionneed references and links to guide me in my project !! Pinmembersessem133-Oct-11 10:41 
QuestionC# Code Pinmembererkankocakaya16-Jul-11 8:16 
AnswerRe: C# Code PinmemberDaniel Dumitru20-Sep-11 0:24 
GeneralC# code Pinmembersteve234911-Feb-11 3:15 
GeneralMy vote of 5 PinmemberInas Paulo Souz24-Oct-10 7:47 
GeneralC# code Pinmemberilpulcinocalimero14-Oct-10 6:54 
Generallatest c# code PinmemberMischel6-Oct-10 3:35 
GeneralRe: latest c# code PinmemberDaniel Dumitru9-Dec-10 0:08 
GeneralG code in C# PinmemberOneManDo19-Aug-10 3:35 
GeneralV1.0.0.18 Pinmembernoam shapira5-Mar-10 6:49 
GeneralV1.0.0.17 Pinmemberoverraider29-Sep-09 8:31 
GeneralRe: V1.0.0.17 PinmemberDaDoo7017-Feb-10 6:20 
GeneralI need your code in C# too!! Pinmembernemo_mse24-May-09 2:55 
GeneralOld version Pinmemberterodactilo2-Apr-09 5:07 
GeneralRe: Old version Pinmemberron72228-Jun-09 19:12 
QuestionIs possible new C# code to me? Pinmembercncserwisghnb23-Jan-09 10:11 
GeneralG-code generator Pinmemberberes275-Dec-08 9:43 
NewsSource code is now available in C# PinmemberJason Titcomb1-Dec-08 1:58 
GeneralRe: Source code is now available in C# PinmemberMischel6-Oct-10 2:18 
GeneralVery Nice PinmemberPaul Conrad30-Nov-08 18:58 
Questionstupid error PinmemberDavis Gentry19-Nov-08 11:45 
AnswerRe: stupid error PinmemberJason Titcomb22-Nov-08 4:13 
GeneralRe: stupid error PinmemberDavis Gentry25-Nov-08 14:42 
GeneralGreat program in VB Pinmemberjamki26-Sep-08 5:58 
QuestionAny chances of having the new source code? PinmemberJoe Sonderegger26-Oct-07 5:56 
AnswerRe:Your translated C# source code? PinmemberIkey_Boy19-May-08 5:03 
GeneralRe:Your translated C# source code? PinmemberJoe Sonderegger25-May-08 20:33 
GeneralRe:Your translated C# source code? Pinmemberlemac14-Jun-08 14:36 
GeneralRe:Your translated C# source code? PinmemberJoe Sonderegger19-Jun-08 2:53 
GeneralRe:Your translated C# source code? Pinmemberlemac19-Jun-08 5:30 
GeneralRe:Your translated C# source code? PinmemberRekha Jayaram7-Aug-08 3:00 
GeneralRe:Your translated C# source code? PinmemberIkey_Boy5-Aug-08 4:52 
GeneralRe:Your translated C# source code? Pinmemberlemac7-Aug-08 0:15 
AnswerRe: Any chances of having the new source code? PinmemberRekha Jayaram5-Aug-08 9:19 
AnswerRe: Any chances of having the new source code? PinmemberDavis Gentry19-Nov-08 11:23 
QuestionHow to Load G-Code File from RTB? Pinmembercannalane29-Apr-07 0:23 
AnswerRe: How to Load G-Code File from RTB? PinmemberJason Titcomb29-Apr-07 3:32 
GeneralGreat Program Pinmembercnc_maker15-Mar-07 5:23 
GeneralRe: Great Program PinmemberJason Titcomb1-Apr-07 11:58 
GeneralOpenGL PinmemberPaul A. Howes10-Feb-07 4:32 
GeneralRe: OpenGL PinmemberMember 18687365-Jul-11 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150326.1 | Last Updated 30 Nov 2008
Article Copyright 2007 by Jason Titcomb
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid