Hello Guest it is December 11, 2019, 09:23:19 AM

Author Topic: Plugin or Script to Modify Gcode  (Read 17057 times)

0 Members and 1 Guest are viewing this topic.

Plugin or Script to Modify Gcode
« on: October 17, 2012, 10:15:47 AM »
I'm trying to determine the best way to handle this. I'm looking to write a fairly simple plugin (or macro script?) that makes modifications to a gcode file whenever it is opened in Mach. Basically the plugin/script would, after a gcode file is opened, parse the gcode file to make certain modifications to the code and then save the changes to the gcode window for execution.

The purpose for wanting to do this is that I've added a second Z axis and second router to my machine. I assigned the second Z axis as the "A" axis in Mach. The second Z axis is at a known offset on the X-Y plane, so I want Mach to parse all loaded gcode files and search for all toolchanges which ask for Tool #2. It will then change all "Z" axis moves to "A" axis moves within that section of Gcode. It will also insert the proper offset code, etc.

I realize I could accomplish the same thing with the swapaxis() command via a macro, but this seems dangerous in that the change can be permanent to the pin designations if mach exits with swapaxis() still active.

I also realize I could simple modify the post-processor in my CAM package to handle all this, but I want a more universal solution.

One way that I might accomplish this would be to create a wizard that brings up a File-Open dialog and thereafter parsed the gcode, saving the result to the "teach file" and posting the gcode. This might work (I think) but I'd prefer to use Mach's actual gcode load routine if possible. It would also be nice if I could save the result to an actual gcode file prior to posting to the gcode window. I'm not sure if any of the above is possible, however.

Ideally, this would be a plugin (I think) with its own plugin options dialog, but I'm having a hard time finding the documentation needed to execute these types of functions. I've watched the tutorial videos and done a fair bit of searching but I can't find information on a plugin's ability to access a loaded gcode file and to modify it.

I would welcome any comment/advice/direction! Thanks in advance.

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: Plugin or Script to Modify Gcode
« Reply #1 on: October 17, 2012, 01:07:43 PM »
Just have MACH3 do an Unswapaxis() on Initiation. That way when you active MACH3 it auto unswapps any swapped drives AND on any Reset it will do the same.

NOW can your routine be done ? Probably but NOT by a mere MORTAL (;-)

(;-) TP

Re: Plugin or Script to Modify Gcode
« Reply #2 on: October 17, 2012, 01:15:13 PM »
Thanks for the reply! My understanding of SwapAxis(), however, is that it essentially acts the same as actually changing/reversing the ports and pins for the two Axes. You can use ResetAxisSwap() to undo it, but if you close Mach while SwapAxis() is active, it will write the swapped pin designations to the user XML file, making the change permanent. Therefore, when Mach is thereafter loaded, ResetAxisSwap() will not work because the pins are then setup wrong.

That is why I'd prefer not to use SwapAxis() if possible.

Luckily, I'm not mortal (but I'm not immortal either, so I'm not sure what I am)!  :)

The biggest problem I'm having is documentation of what is available in plugins. For macros, there appears to be more documentation, but it is tough to find as much on the plugin side of things!

Any ideas?

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: Plugin or Script to Modify Gcode
« Reply #3 on: October 17, 2012, 03:53:10 PM »
I had to go back and check and YES you are correct it WILL rewrite the XML on close.  The original version (long ago) only kept the swap locally and for the current session of mach3 only.

I'll check around to see IF anyone is up to the task. I assume you want this for old code that was created BEFORE your axis was added?

(;-) TP

Offline ger21

*
  • *
  •  6,291 6,291
    • View Profile
    • The CNC Woodworker
Re: Plugin or Script to Modify Gcode
« Reply #4 on: October 17, 2012, 05:32:03 PM »
There's an LED to indicate when the axis are swapped. Does it get reset when Mach3 is closed? If not, you can check it's status and determine what you need to do when starting.
Gerry

2010 Screenset
http://www.thecncwoodworker.com/2010.html

JointCAM Dovetail and Box Joint software
http://www.g-forcecnc.com/jointcam.html

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: Plugin or Script to Modify Gcode
« Reply #5 on: October 17, 2012, 09:32:12 PM »
With Mach3 writing the XML changes instead of just keeping the change local I can see where it COULD create a HUGE mess that the Operator would have to KNOW HOW to clean up.

Can't image why it is that way BUT it is what it is. (;-)

I think you could do what is needed with a VB style macro OR  better yet a PLUGIN in C++.   I can see what you need and understand what has to happen but that is about the end of my plugin writing(;-) 

Hopefully one of the heavy coders will show up soon and take a look at the project.

(;-) TP
Re: Plugin or Script to Modify Gcode
« Reply #6 on: October 17, 2012, 10:16:09 PM »
Thanks everyone for the replies. I've been thinking about it and a plugin woudl definitely be the way to go. however, I'm thinking it would technically be possible using a macro, which would work as follows:

1. Call the Mach File -> Open command (DoOEMButton(216))
2. Read the file name and path of the currently open file
3. Close the file in Mach
4. Open the file using VB commands to read the text of the file
5. Parse the text (change Z movements to A (or whatever axis is chosen) in the proper sections, etc.
6. Write the changes to the text file and close it
7. Have Mach open the file again using DoOEMButton(216) which opens the file ready for execution.

A very roundabout way of going about things but it should be technically possible...
 

Offline ger21

*
  • *
  •  6,291 6,291
    • View Profile
    • The CNC Woodworker
Re: Plugin or Script to Modify Gcode
« Reply #7 on: October 17, 2012, 10:33:14 PM »
Wouldn't it be easier to write a standalone file converter?

I'm currently building a dual Z axis router, and plan on using SwapAxis() in a macro, calling SawpAxis() based on tool #'s.

If your main concern is closing Mach3 with the axis swapped, there are a coupe things you can do.
1) write a macro to ResetAXisSwap(), give it an M code, and add it to the end of all your g-code. So every time you finish running a program, the SwapAxis() will be reset.
2) add a simple script to the close g-code button to make sure any swaps are reset when you close your g-code file.

These should go a long way towards making it a little safer.

One other thing. I've had my .xml become corrupt a few times now, so I have a backup copy on my desktop. If Mach3 does somehow start with the axis swapped, it's simple to just close it, copy the backup .xml in place, and restart. Not ideal, and maybe not viable at all in your situation, but it's an easy fix.
Gerry

2010 Screenset
http://www.thecncwoodworker.com/2010.html

JointCAM Dovetail and Box Joint software
http://www.g-forcecnc.com/jointcam.html

Offline ger21

*
  • *
  •  6,291 6,291
    • View Profile
    • The CNC Woodworker
Re: Plugin or Script to Modify Gcode
« Reply #8 on: October 17, 2012, 11:00:14 PM »
Just did a quick test of another idea.
I did some searching, and didn't see a SwapAxis() LED.
So I created a UserLED, and had the macro turn it on when it Swapped the Axis. And turned it off when resetswapaxis() was called.
If you close Mach3 with the axis swapped, they do remain swapped, but the LED remains on.
Have your initialization string swap them back, turn off the led, and pop up a message box telling you to restart Mach3. You should then be good to go.

Kind of a hack, but it seems like it would work fine.
Gerry

2010 Screenset
http://www.thecncwoodworker.com/2010.html

JointCAM Dovetail and Box Joint software
http://www.g-forcecnc.com/jointcam.html
Re: Plugin or Script to Modify Gcode
« Reply #9 on: October 18, 2012, 08:52:35 AM »
I think SwapAxis() will work fine 99.9% of the time if you take the proper precautions, such as inserting ResetAxisSwap() in a macro that somehow gets run prior to shutting down Mach, and/or at the end of a gcode file, etc.

I've been discussing the issue over on Joe's forum where a user has been running dual Z axes for a year or two now. He uses a modified post-processor in Aspire to handle most of the leg work, but it works very well for him. My only problems with a modified post-processor are that (1) it only works for one tool at a time - when you want to run toolpaths for the second Z, you have to save a new file and then combine the files by had afterward, and (2) it only works with one CAM package unless you write a new post processor or for all your CAM software.

SwapAxis() is a good and simple way to do it and given the ideas to make sure it is reset I don't see much of a chance for problems, especially if you use the LED Gerry is talking about - that is a good idea. But in my opinion, SwapAxis() doesn't really fully capture the essence of what is going on with a dual Z setup. If you use SwapAxis() exclusively, you are really turning your dual Z (two separate axes) into a single Z axis that can by swapped out and offset. I like the ability to jog both Z's independently, and I like the idea of gcode telling the machine exactly what to do rather than tricking the machine into changing pin assignments in the middle of a job. Again, that is really just personal preference when it comes down to it.

That said, there is apparently one more additional benefit to SwapAxis() that hasn't been mentioned yet, although I have not yet begun looking into this issue: Apparently Mach does not let you probe on an A, B, or C axis - that is, you can't use a touch plate to set Z-Zero on the A, B or C Axis (this is according to the user on Joe's forum - again I haven't started looking at this yet). My inclination is that there must be a way to make this work. However, his solution was simply to use SwapAxis() prior to probing, which solves the problem. He's now able to use the touchplate for both Z axes.

Gerry I've already modified your screenset heavily to account for other changes, but it sounds like your touchprobe scripts will need to be modified as well. Having worked on those and given your knowledge of how they work do you think they could be modified to use a different Axis or will SwapAxis() be necessary on those scripts?