CNC Programming with Python — Nobody Told You You Can Do It?

Having spent 7 years in CNC programming, I never realized how many options were available for programming, until recently. The one thing that was always clear was that programming was fun. Just you and the machine working together to create something no one else had ever created. As my carrier has progressed, I have had to get more and more away from CNC programming to overcome wage ceilings associated with CNC Programming. However, the message or thought always remained that I was never happier than when problem-solving with a machine.

So, when I discovered that Python Programmers earn the same as a Program Manager it was a no-brainer. I had to jump in and see what it was all about. First, I had to pick up the learner’s mind again and that is tough on the ego going from a senior position to being a novice again. But being humbled is good for the soul.

The Big Surprise

While learning Python Programming I stumbled on PyCNC, Python G code Generator and Python CNC Code Simulator. I was floored when I saw this and so the next phase of my Python Education began

PyCNC

PyCNC created by Nikolay Khabarov essentially allows you to create your own CNC controller.

For the ultimate tinkerer, developer or the real DIY PyCNC allows almost someone to take an existing CNC machine connect it to a Raspberry Pi and with a bit of wiring use Python to write G Code with Python. With PyCNC your code controls the motors using a Linux system which normally would not be feasible due to Linux not having real-time GPIO (General Purpose I/O. However, with PyCNC utilizing a DMA (Direct Memory Access) module to overcome the Linux system limitation Python and Linux are now a feasible option for CNC controller, albeit maybe not in an industrial setting.   

For the moment PyCNC is not feasible for use in high production environments as it is not fast enough for high-speed machining, but for someone who wants to tinker and create some 3D printed parts or machine some test pieces and save on costs this a great option. For more industrial CNC applications Python has some particularly useful G Code Generators.

You can find the Python documentation for PYCNC here.

You can also watch a video with PyCNC in action here.

G Code Generator

G Code generators are super exciting because they can eliminate the need for expensive CNC program generators for most applications. Normally a CNC programmer can generate his code on his own but there some applications that force them to use a code generator using a part model or 2D drawing. Now, this causes a couple of issues one is that the CNC programmers usually get hand me down computers from engineering so using and working with modelling and CAD software is a nightmare as you hope your computer will only crash once or twice as you execute the software but with some luck and a few well-intentioned prayers you can get the software to execute. When I started learning I had to provide my own laptop if I wanted to learn CNC programming.

A few of the G Code Generators I would have liked to know existed when I was coding would have been:

  • Text Engraving
  • Arc Generator
  • Bolt Circle Array
  • Rectangular- Circular Pocketing Generator

There are even drilling speeds and Feeds calculators, I might be old-fashioned, but you are better served learning how to calculate those yourself it gives you a better understanding of the variables you can use to improve or adjust the process which comes in handy often.  

Let us see one of these generators in action. Personally, the programming I would have liked to have had as a CNC programmer would have been text engraving, so we are going to test this generator out.

Suppose we want to engrave “Python” on the side of our workpiece.

https://github.com/LinuxCNC/simple-gcode-generators/blob/master/engrave/README.md

Text Engraving Generator

How to execute code

Picture of text that is output by CNC Code.

The CNC Code output

  • G21
    • M10
    • G00 X4.314115 Y0.68025
    • M09
    • G01 X4.314115 Y2.361702
    • G01 X4.442832 Y2.361702
    • G01 X4.442832 Y2.122848
    • G01 X4.514051 Y2.275318
    • G01 X4.603529 Y2.36689
    • G01 X4.711493 Y2.397339
    • G01 X4.760939 Y2.391249
    • G01 X4.807662 Y2.37298
    • G01 X4.851778 Y2.342756
    • G01 X4.89079 Y2.302609
    • G01 X4.92277 Y2.254793
    • G01 X4.947493 Y2.199759
    • G01 X4.966545 Y2.136832
    • G01 X4.981401 Y2.066912
    • G01 X4.992062 Y1.989324
    • G01 X4.996484 Y1.923464
    • G01 X4.999093 Y1.831216
    • G01 X5 Y1.712578
    • G01 X5 Y0.68025
    • G01 X4.856654 Y0.68025
    • G01 X4.856654 Y1.701977
    • G01 X4.854726 Y1.80821
    • G01 X4.849169 Y1.89482
    • G01 X4.839983 Y1.962033
    • G01 X4.825921 Y2.015713
    • G01 X4.806301 Y2.061499
    • G01 X4.780785 Y2.099617
    • G01 X4.750845 Y2.128261
    • G01 X4.71773 Y2.145403
    • G01 X4.681213 Y2.151042
    • G01 X4.623149 Y2.138185
    • G01 X4.570642 Y2.099842
    • G01 X4.523691 Y2.035561
    • G01 X4.486947 Y1.935644
    • G01 X4.464832 Y1.78949
    • G01 X4.457461 Y1.597549
    • G01 X4.457461 Y0.68025
    • G01 X4.314115 Y0.68025
    • M10
    • G00 X3.465037 Y1.520186
    • M09
    • G01 X3.472862 Y1.32351
    • G01 X3.496564 Y1.162695
    • G01 X3.53603 Y1.037967
    • G01 X3.587403 Y0.948876
    • G01 X3.646942 Y0.895647
    • G01 X3.714646 Y0.877829
    • G01 X3.781896 Y0.895647
    • G01 X3.841208 Y0.949327
    • G01 X3.892468 Y1.038644
    • G01 X3.931933 Y1.164724
    • G01 X3.955635 Y1.328472
    • G01 X3.96346 Y1.52966
    • G01 X3.955522 Y1.720021
    • G01 X3.93182 Y1.877002
    • G01 X3.892128 Y2.000602
    • G01 X3.840641 Y2.089693
    • G01 X3.781556 Y2.142922
    • G01 X3.714646 Y2.16074
    • G01 X3.646942 Y2.143147
    • G01 X3.587403 Y2.089918
    • G01 X3.53603 Y2.001278
    • G01 X3.496564 Y1.877002
    • G01 X3.472862 Y1.716638
    • G01 X3.465037 Y1.520186
    • M10
    • G00 X3.318402 Y1.520863
    • M09
    • G01 X3.332918 Y1.804601
    • G01 X3.376353 Y2.034659
    • G01 X3.448933 Y2.211037
    • G01 X3.526844 Y2.314563
    • G01 X3.615414 Y2.376588
    • G01 X3.714646 Y2.397339
    • G01 X3.823743 Y2.372077
    • G01 X3.918664 Y2.296519
    • G01 X3.999524 Y2.170664
    • G01 X4.06099 Y2.000376
    • G01 X4.097848 Y1.791745
    • G01 X4.110096 Y1.544546
    • G01 X4.104652 Y1.344034
    • G01 X4.088322 Y1.174423
    • G01 X4.061217 Y1.035261
    • G01 X4.023566 Y0.920908
    • G01 X3.976162 Y0.824825
    • G01 X3.918778 Y0.747237
    • G01 X3.854476 Y0.690174
    • G01 X3.786546 Y0.656116
    • G01 X3.714646 Y0.644613
    • G01 X3.603961 Y0.669875
    • G01 X3.508358 Y0.744756
    • G01 X3.427839 Y0.870386
    • G01 X3.367053 Y1.042929
    • G01 X3.330536 Y1.259905
    • G01 X3.318402 Y1.520863
    • M10
    • G00 X0.154687 Y1.898429
    • M09
    • G01 X0.456236 Y1.898429
    • G01 X0.546281 Y1.910608
    • G01 X0.617047 Y1.947598
    • G01 X0.668534 Y2.009172
    • G01 X0.703464 Y2.093076
    • G01 X0.724444 Y2.197053
    • G01 X0.731362 Y2.320878
    • G01 X0.727279 Y2.413352
    • G01 X0.714918 Y2.496579
    • G01 X0.694391 Y2.570333
    • G01 X0.667173 Y2.631456
    • G01 X0.634625 Y2.677242
    • G01 X0.596974 Y2.70724
    • G01 X0.563633 Y2.718518
    • G01 X0.515662 Y2.725509
    • G01 X0.453061 Y2.727765
    • G01 X0.154687 Y2.727765
    • G01 X0.154687 Y1.898429
    • M10
    • G00 X0 Y0.68025
    • M09
    • G01 X0 Y3
    • G01 X0.440359 Y3
    • G01 X0.511692 Y2.997519
    • G01 X0.570891 Y2.990076
    • G01 X0.617841 Y2.977671
    • G01 X0.672163 Y2.953086
    • G01 X0.720134 Y2.916999
    • G01 X0.762095 Y2.869634
    • G01 X0.798272 Y2.809413
    • G01 X0.829459 Y2.734982
    • G01 X0.855656 Y2.646117
    • G01 X0.875275 Y2.547327
    • G01 X0.887069 Y2.441997
    • G01 X0.891039 Y2.330351
    • G01 X0.880492 Y2.143598
    • G01 X0.848965 Y1.977145
    • G01 X0.796344 Y1.830765
    • G01 X0.716165 Y1.717089
    • G01 X0.602078 Y1.648974
    • G01 X0.453968 Y1.626194
    • G01 X0.154687 Y1.626194
    • G01 X0.154687 Y0.68025
    • G01 X0 Y0.68025
    • M10
    • G00 X1.105605 Y0.034283
    • M09
    • G01 X1.089274 Y0.297497
    • G01 X1.119327 Y0.283287
    • G01 X1.146658 Y0.274942
    • G01 X1.171381 Y0.272235
    • G01 X1.201093 Y0.275618
    • G01 X1.22661 Y0.286219
    • G01 X1.24793 Y0.303812
    • G01 X1.265849 Y0.32772
    • G01 X1.281612 Y0.357492
    • G01 X1.294881 Y0.392677
    • G01 X1.305881 Y0.435306
    • G01 X1.320511 Y0.506127
    • G01 X1.338769 Y0.605143
    • G01 X1.341945 Y0.623412
    • G01 X1.346254 Y0.646643
    • G01 X1.351584 Y0.674837
    • G01 X1.030643 Y2.361702
    • G01 X1.183742 Y2.361702
    • G01 X1.360317 Y1.390948
    • G01 X1.382431 Y1.265769
    • G01 X1.402844 Y1.136306
    • G01 X1.421783 Y1.002782
    • G01 X1.439361 Y1.12999
    • G01 X1.459094 Y1.256071
    • G01 X1.480868 Y1.381024
    • G01 X1.661072 Y2.361702
    • G01 X1.804192 Y2.361702
    • G01 X1.484611 Y0.651154
    • G01 X1.45263 Y0.486279
    • G01 X1.425753 Y0.359522
    • G01 X1.403978 Y0.271108
    • G01 X1.377214 Y0.185851
    • G01 X1.347955 Y0.117284
    • G01 X1.316201 Y0.065409
    • G01 X1.281159 Y0.029096
    • G01 X1.241807 Y0.007218
    • G01 X1.198145 Y0
    • G01 X1.169453 Y0.003834
    • G01 X1.138493 Y0.015112
    • G01 X1.105605 Y0.034283
    • M10
    • G00 X2.270294 Y0.934216
    • M09
    • G01 X2.289914 Y0.682731
    • G01 X2.251128 Y0.668972
    • G01 X2.215065 Y0.660402
    • G01 X2.18195 Y0.657695
    • G01 X2.133979 Y0.663108
    • G01 X2.093833 Y0.679573
    • G01 X2.061399 Y0.706639
    • G01 X2.035655 Y0.742726
    • G01 X2.015582 Y0.785806
    • G01 X2.001066 Y0.835877
    • G01 X1.991427 Y0.908954
    • G01 X1.98553 Y1.021051
    • G01 X1.983601 Y1.172619
    • G01 X1.983601 Y2.141343
    • G01 X1.877679 Y2.141343
    • G01 X1.877679 Y2.361702
    • G01 X1.983601 Y2.361702
    • G01 X1.983601 Y2.777611
    • G01 X2.126948 Y2.948575
    • G01 X2.126948 Y2.361702
    • G01 X2.270294 Y2.361702
    • G01 X2.270294 Y2.141343
    • G01 X2.126948 Y2.141343
    • G01 X2.126948 Y1.156379
    • G01 X2.127855 Y1.084655
    • G01 X2.13035 Y1.032328
    • G01 X2.134546 Y0.999173
    • G01 X2.140557 Y0.977521
    • G01 X2.148722 Y0.959026
    • G01 X2.159042 Y0.943689
    • G01 X2.17197 Y0.932186
    • G01 X2.188301 Y0.925419
    • G01 X2.207807 Y0.923164
    • G01 X2.225272 Y0.924291
    • G01 X2.246139 Y0.928126
    • G01 X2.270294 Y0.934216
    • M10
    • G00 X2.430198 Y0.68025
    • M09
    • G01 X2.430198 Y3
    • G01 X2.573545 Y3
    • G01 X2.573545 Y2.165251
    • G01 X2.645898 Y2.294264
    • G01 X2.729819 Y2.371401
    • G01 X2.825421 Y2.397339
    • G01 X2.88496 Y2.389219
    • G01 X2.939055 Y2.364634
    • G01 X2.987707 Y2.32381
    • G01 X3.029327 Y2.268551
    • G01 X3.062102 Y2.200662
    • G01 X3.086257 Y2.120592
    • G01 X3.102815 Y2.021126
    • G01 X3.112681 Y1.895722
    • G01 X3.11597 Y1.744155
    • G01 X3.11597 Y0.68025
    • G01 X2.972624 Y0.68025
    • G01 X2.972624 Y1.743478
    • G01 X2.96752 Y1.872716
    • G01 X2.952097 Y1.976242
    • G01 X2.926354 Y2.053831
    • G01 X2.891311 Y2.107962
    • G01 X2.847763 Y2.140215
    • G01 X2.795595 Y2.151042
    • G01 X2.754315 Y2.143824
    • G01 X2.714623 Y2.121946
    • G01 X2.676631 Y2.085407
    • G01 X2.643063 Y2.036689
    • G01 X2.616639 Y1.97737
    • G01 X2.597247 Y1.907676
    • G01 X2.584091 Y1.823547
    • G01 X2.576153 Y1.720247
    • G01 X2.573545 Y1.598
    • G01 X2.573545 Y0.68025
    • G01 X2.430198 Y0.68025
    • M10
    • G00 X0 Y0
    • M02

Bolt Circle Generator

Next, let us see a Bolt Circle Generator in action. When you learn CNC programming in school many teachers seem to spend quite a bit of time teaching how to program a bolt hole circle but what if it could be so much easier

https://github.com/LinuxCNC/simple-gcode-generators/tree/master/boltcircle

How to Execute Code

Text Output

  • (Hole # 1)
    • G00 Z0.5 F70
    • G00 X28.1908 Y10.2606 F70
    • G01 Z-1 F50
    • (Hole # 2)
    • G00 Z0.5 F70
    • G00 X-1.047 Y29.9817 F70
    • G01 Z-1 F50
    • (Hole # 3)
    • G00 Z0.5 F70
    • G00 X-28.8379 Y8.2691 F70
    • G01 Z-1 F50
    • (Hole # 4)
    • G00 Z0.5 F70
    • G00 X-16.7758 Y-24.8711 F70
    • G01 Z-1 F50
    • (Hole # 5)
    • G00 Z0.5 F70
    • G00 X18.4698 Y-23.6403 F70
    • G01 Z-1 F50
    •  
    • G00 Z0.5 F70
    • G00 X0 Y0 F70
    • M30

Python CNC Code Simulators Currently, there do not seem to be any viable Python options for CNC Code simulators. There is a project on pypi.org that has been under development since 2017. Seems like there might be an opportunity to create something no one else has ever created with Python for CNC. I am not 100% sure why there is not a Python CNC simulator however it would seem that the simulator would have to simulate 2 to 5 axis and consider cutting tool geometry which would need an additional input besides the G code this seems like it would be a lot for one person to figure out.  If there were no new challenges, there would be nothing left to code it’s likely just a matter of time before Python CNC Code Simulator is developed.