Page 1 of 1

GCode.js hack help needed

Posted: Fri Mar 28, 2014 2:32 pm
by cmcgrath5035
Andrew
First- I like 3.4.6-pro - CAM simulation working well 8)
I am hacking your GCode.js script to customize the output for my CNC
I want the file.nc to be self documenting a bit, as I use a few different GCode generators.

Here is a snippet:

Code: Select all

GCode.prototype.writeHeader = function() {
    //cjm add header lines to the GCode file
    this.writeLine("(GCode generated by QCAD on -date-)" );		 //Insert a comment in GCode file
    this.writeLine("(Spindle ON and Spindle Off added by this script)"); //Insert a comment in GCode file
    this.writeRapidZMove(this.getSafetyZLevel());
    this.toolPosition = GCode.ToolPosition.Clear;
};
Can you suggest some js to get -date- filled in by current system date and time?
I am just getting up to speed on js

Thanks!

Re: GCode.js hack help needed

Posted: Fri Mar 28, 2014 5:10 pm
by andrew
There are plenty of good sources for JavaScript related help and tips on the Internet. For example, google for "JavaScript date as string". A simple way is:
var dateAsString = new Date().toString();
Please note that changing the GCode.js script is not recommended. Instead, please create a new script that derives and extends the functionality of GCode.js:

File GCodeLocal.js:
include("GCode.js");

function GCodeLocal(documentInterface, newDocumentInterface) {
    GCode.call(this, documentInterface, newDocumentInterface);
    // set custom options here...
}

GCodeLocal.prototype = new GCode();

GCodeLocal.prototype.writeHeader = function() {
    // write custom header here...
};

Re: GCode.js hack help needed

Posted: Fri Mar 28, 2014 6:18 pm
by cmcgrath5035
Thanks Andrew.
I started down that path then got sort of confused as to how much of the GCode functionality needed to be replicated in my Local script, so took the hack path for the moment.
I plan to go back and do it 'right' when I have a bit more time.

Re: GCode.js hack help needed

Posted: Sat Mar 29, 2014 12:46 pm
by cmcgrath5035
If anyone is interested in how this came out, here is my completed hack:

I added this function definition (from web search) to GCode.js

Code: Select all

//cjm Pulled this function from internet
// It builds date sting -> 2013/10/04 08:51:32
 function getDateTime() {
    var now     = new Date(); 
    var year    = now.getFullYear();
    var month   = now.getMonth()+1; 
    var day     = now.getDate();
    var hour    = now.getHours();
    var minute  = now.getMinutes();
    var second  = now.getSeconds(); 
    if(month.toString().length == 1) {
        var month = '0'+month;
    }
    if(day.toString().length == 1) {
        var day = '0'+day;
    }   
    if(hour.toString().length == 1) {
        var hour = '0'+hour;
    }
    if(minute.toString().length == 1) {
        var minute = '0'+minute;
    }
    if(second.toString().length == 1) {
        var second = '0'+second;
    }   
    var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;   
    return dateTime;

}
And I then made this modification to writeHeader

Code: Select all

GCode.prototype.writeHeader = function() {
    //cjm add header lines to the GCode file
    this.writeLine("(GCode generated by QCAD on " + getDateTime() + ")");	 //Insert a comment in GCode file
    this.writeLine("(Spindle ON/OFF added by this script)"); 			//Insert a comment in GCode file
    this.writeRapidZMove(this.getSafetyZLevel());
    this.toolPosition = GCode.ToolPosition.Clear;
};
The top of my resulting GCode looks like this:

Code: Select all

N10 (GCode generated by QCAD on 2014/03/29 07:32:25)
N20 (Spindle ON/OFF added by this script)
N30 G00 Z5
N40 ......
..........
I have additional hacks that add an M03 to start the spindle before the first cut and an M05 to stop it at end.

Now I need to go back and do it correctly, rather than changing GCode.js directly.

Re: GCode.js hack help needed

Posted: Sat Mar 29, 2014 9:34 pm
by cmcgrath5035
Is there a method to access the current file name ( the file.dxf that is being processed by the GCode generator) from within GCode.js?
I would like to add that to the GCode file as well

Re: GCode.js hack help needed

Posted: Mon Mar 31, 2014 8:26 am
by andrew
this.fileName
I'd recommend to wrap this into a method (e.g. getFileName, in case this internal property changes).

Re: GCode.js hack help needed

Posted: Mon Mar 31, 2014 10:56 am
by cmcgrath5035
Thanks, Andrew

Re: GCode.js hack help needed

Posted: Fri Apr 04, 2014 3:29 am
by cmcgrath5035
this.fileName returns the full path for the GCode file.nc.

What I would like is the filename for the dxf design file that is being used as the source for the GCode.

Thanks

Re: GCode.js hack help needed

Posted: Fri Apr 04, 2014 9:51 am
by andrew
The current document (RDocument) is available as

Code: Select all

this.document
. You can get its file name with

Code: Select all

this.document.getFileName()
.

Re: GCode.js hack help needed

Posted: Fri Apr 04, 2014 12:33 pm
by cmcgrath5035
Thanks once again, Andrew.

So for anyone interested, my modified GCode.js header section is now

Code: Select all

GCode.prototype.writeHeader = function() {
    //cjm add header lines to the GCode file
    this.writeLine("(GCode generated by QCAD on " + getDateTime() + ")");	 //Insert a comment in GCode file
    this.writeLine("(GCode generated for File " + this.document.getFileName() + ")");		//Insert a comment in GCode file
    this.writeLine("(Spindle ON/OFF added by this script)"); 			//Insert a comment in GCode file
    this.writeRapidZMove(this.getSafetyZLevel());
    this.toolPosition = GCode.ToolPosition.Clear;
};
And the GCode header looks like this

Code: Select all

N10 (GCode generated by QCAD on 2014/04/04 07:24:40)
N20 (GCode generated for File /home/carl/Projects/CNC_and_CAM/GCode Samples/ShapeOko_Diagonal_Test_4_in_mm.dxf)
N30 (Spindle ON/OFF added by this script)
N40 .......
Which is what I wanted for documentation/tracking purposes.

Now time to take my hack and do it properly so it will survive version updates...