G-codes/offsets/tools with .Net

Moderators: TomKerekes, dynomotion

Post Reply
Moray
Posts: 121
Joined: Thu Apr 26, 2018 10:16 pm

G-codes/offsets/tools with .Net

Post by Moray » Mon Oct 21, 2019 11:48 pm

I'm currently looking as the feasibility of making a custom C#/WPF version of KMCNC, as for a couple of upcoming projects I'm looking to expand on the tool handling functionality within KMCNC.

Anyway I'm trying to work out how G-codes, tools, and offsets (both tool, and work coordinate offsets) are handled within KMCNC, but I'm completely failing at trying to understand how things are done via MVC, or find any kind of simple guide as to how MVC works.
I suspect my biggest issue is as MVC has been deprecated, VS2017 isn't showing everything, or I'm missing something blatantly obvious.


Are these all handled internally within KMCNC, or are some of them done via the .Net interpreter?

If I was to write my own program, how much could I rely on the .Net interpreter?
I.e. would I be restricted by having to use the EMC style tool table, or can I use my own tool tables and only pass the required offsets?

How are G/M codes handled?
Could I add/edit existing G codes?

User avatar
TomKerekes
Posts: 615
Joined: Mon Dec 04, 2017 1:49 am

Re: G-codes/offsets/tools with .Net

Post by TomKerekes » Tue Oct 22, 2019 6:59 pm

Hi Moray,
I'm currently looking as the feasibility of making a custom C#/WPF version of KMCNC, as for a couple of upcoming projects I'm looking to expand on the tool handling functionality within KMCNC.
That would be great. But it might be a significant effort. KMotionCNC involves about 45,000 lines of C++ code. However using C# and .NET should be far less.

Note there is a C#/WPF example SimpleGCodeWPF but it is extremely primitive:
(oops just noticed the title should say WPF not Forms)
SimpleGCodeWPF.png


Anyway I'm trying to work out how G-codes, tools, and offsets (both tool, and work coordinate offsets) are handled within KMCNC, but I'm completely failing at trying to understand how things are done via MVC, or find any kind of simple guide as to how MVC works.
These are handled and maintained by the Interpreter. KMotionCNC just provides the GUI to allow you to view and change them. You might look at the file \PC VC Examples\KMotionCNC\EditToolFile.cpp to see how KMotionCNC reads/writes/displays the Interpreter's Tool Table file.

For Tool offsets the Interpreter reads the EMC style Tool Table File that supports up to 99 Tools. Actually it supports two file format Versions. The original EMC style and a newer format that includes more info such as the image file for the Tool. See the Interpreter Code in \GCodeInterpreter\driver.cpp that reads the Tool File

int read_tool_file( /* ARGUMENT VALUES */
char * tool_file, /* name of tool file */
setup_pointer settings) /* pointer to machine settings */

If I was to write my own program, how much could I rely on the .Net interpreter?
You could rely on the Interpreter to handle any Tool Offsets.


How are G/M codes handled?
Could I add/edit existing G codes?
The Interpreter handles these. To add/edit G or M Codes you would need to modify the C++ Interpreter Code. See the tables const int _gees[] and const int _ems[] in file: \GCodeInterpreter\rs274ngc.cpp

Besides allowing the codes in the tables the code to handle them must also be added. For example see the code in static int convert_g in the same file which then does something like:

if (block->g_modes[0] == G_4) {
CHP(convert_dwell(block->p_number));

etc.


HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

Moray
Posts: 121
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Tue Oct 22, 2019 10:51 pm

Thanks for that info Tom.

I'm well aware it could be quite a big undertaking, which is why I've been trying to understand how the key things interact, and try and workout a general overview of how I'd go about implementing them.

I think I've been through all the examples now, and have already copied that WPF example to do some initial coding/testing.

I'll go and do some more code reading.

Moray
Posts: 121
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Sun Oct 27, 2019 8:14 am

I've made a start on this, but have hit my first problem.

I'm trying to implement User Buttons with similar functionality to KMCNC, however I'm struggling to understand how best to implement it.
I see KMCNC stores all the relevant information in an MCODEACTIONS array, and that is accessible to the interpreter (or is at least set within KMotionCNCDlg.cpp).

Is there something similar available using C#, or will I have to use SetMcodeAction for each M code?

User avatar
TomKerekes
Posts: 615
Joined: Mon Dec 04, 2017 1:49 am

Re: G-codes/offsets/tools with .Net

Post by TomKerekes » Sun Oct 27, 2019 4:55 pm

Hi Moray,

Since you are writing your own C# code you could simply write C# code to do whatever you want directly. Set a bit, run a C Program, set an Interpreter offset, etc.

But if you wish to use the Interpreter's Action mechanism that is used by the Interpreter for MCodes and that KMotionCNC uses for User Buttons you may do this also. Yes you will need to fill in the parameters in the Action Array for any Action you will be using with SetMCodeAction. The Action Array layout is described here. Note array indexes 11-20 are available for use by an Application. You will also need to setup any MCode Action that you use.

After an Action is configured and you want the Action to be performed (ie. Operator pushed the button) you can use InvokeAction.

The KFLOPWebNC C# example does this in:
\PC VCS Examples\KFlopWebNC\KFlopWebNC\Model\Device Interop (Scripting Object)\DeviceInteropHandler Interpreter.cs

BTW to facilitate KMotionCNC Screen Scripting the Interpreter was modified to allow Actions to be performed dynamically. An action can be created and then invoked without using the Action Array by passing an Action to InvokeAction instead of the Array Index. This allows an Application to invoke unlimited Actions. But that method has not yet been exported to .NET. It would be easy to export if necessary.

HTH
Regards,

Tom Kerekes
Dynomotion, Inc.

Moray
Posts: 121
Joined: Thu Apr 26, 2018 10:16 pm

Re: G-codes/offsets/tools with .Net

Post by Moray » Sun Oct 27, 2019 8:17 pm

Thanks for the information.

I'm still not sure how I ultimately want to implement user buttons, so I'm currently trying to replicate how KMCNC does things to get a functional base to work from. I've only added two user buttons so far anyway, as I'm still working on how best to achieve handling the required variables to avoid having to copy/paste code for every button.
I think ultimately I'd like to be able to dynamically create user buttons, so having that functionality you mention added to .Net would be helpful, but I'm happy working with the current options for now. I need to be able to configure the default G/M code actions anyway.

My aim is to have a machine running g-code this week, then gradually add the required functionality.

Post Reply