GCode.js hack help needed

Discussions around the CAM Add-On of QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Indicate the post processor used.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
cmcgrath5035
Full Member
Posts: 58
Joined: Thu Oct 13, 2011 7:38 pm

GCode.js hack help needed

Post by cmcgrath5035 » Fri Mar 28, 2014 2:32 pm

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!

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: GCode.js hack help needed

Post by andrew » Fri Mar 28, 2014 5:10 pm

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...
};

cmcgrath5035
Full Member
Posts: 58
Joined: Thu Oct 13, 2011 7:38 pm

Re: GCode.js hack help needed

Post by cmcgrath5035 » Fri Mar 28, 2014 6:18 pm

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.

cmcgrath5035
Full Member
Posts: 58
Joined: Thu Oct 13, 2011 7:38 pm

Re: GCode.js hack help needed

Post by cmcgrath5035 » Sat Mar 29, 2014 12:46 pm

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.

cmcgrath5035
Full Member
Posts: 58
Joined: Thu Oct 13, 2011 7:38 pm

Re: GCode.js hack help needed

Post by cmcgrath5035 » Sat Mar 29, 2014 9:34 pm

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

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: GCode.js hack help needed

Post by andrew » Mon Mar 31, 2014 8:26 am

this.fileName
I'd recommend to wrap this into a method (e.g. getFileName, in case this internal property changes).

cmcgrath5035
Full Member
Posts: 58
Joined: Thu Oct 13, 2011 7:38 pm

Re: GCode.js hack help needed

Post by cmcgrath5035 » Mon Mar 31, 2014 10:56 am

Thanks, Andrew

cmcgrath5035
Full Member
Posts: 58
Joined: Thu Oct 13, 2011 7:38 pm

Re: GCode.js hack help needed

Post by cmcgrath5035 » Fri Apr 04, 2014 3:29 am

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

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

Re: GCode.js hack help needed

Post by andrew » Fri Apr 04, 2014 9:51 am

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()
.

cmcgrath5035
Full Member
Posts: 58
Joined: Thu Oct 13, 2011 7:38 pm

Re: GCode.js hack help needed

Post by cmcgrath5035 » Fri Apr 04, 2014 12:33 pm

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...

Post Reply

Return to “QCAD/CAM”