the_paco, yeah, you can help with it if you want. I'm not sure how far I'll get with it though. I don't have a lot of time to work on it.
ynneb, it is written for Linux mostly because that's what EMC runs on and it is the most popular for open source software. However, it should also run on anything with a Unix-like environment and Perl, including MacOSX and Windows with Cygwin, although I don't have those to test it on.
So far, it is a fairly useful program for engraving with SVG paths. I'm probably going to add text support next.
I've decided to use a frontend/backend setup for this program. It won't have any built in user interface and will be just a processor. The interface will be provided by a seperate program. This increases the flexibility of it tremendously.
Here is a gallery of the engravings I have done with it so far on my MaxNC10:
http://ldwp.com/hosted/CNC/Acrylic%20engravings/
A couple earlier ones had some errors. Several have lines that don't quite connect because the program was skipping the first segment of each line. The predator has a line that shouldn't be there because of a problem in handling broken paths(ignoring the break). Both problems have now been fixed.
Here is a chart showing the planned processing steps:
If I can find documentation about LWO, DXF, etc... file formats, those would be nice to import too, but I haven't been able to find enough information to write a parser in Perl.
I have the SVG file input done, and the path processor complete enough to be useful. The Frontend, 2D plotter, and post processor functions are currently provided by a temporary test program that works, but is very limited.
The job assembler is something that I haven't seen very often in other CAM software. It combines multiple programs and handles offsets for making multiple items out of a single piece of material.
The process macro has the same function as a user interface, except with less interactivity. It is user written and would describe the overall process and required features.
The feature macros describe machining operations. They are used to generate lengthy operations, like pocket cuts, with a single command.
The macros and templates should allow this to be very versatile and work on all sorts of machine setups.
The job/process assembler is a module that would allow almost fully automated mass production of parts when combined with an appropriate process macro. It could be used to cut out many sheet metal parts on a laser cutter and place each in a container with a pick and place manipulator. I haven't seen this in many CAM packages, and I haven't decided on the details of it yet. It might be better to have this as a seperate and more powerful program.
Here is the perl code for the SVG module. It supports paths with the M,L,C, and z commands so far(which is what I usually get with SodiPodi). These are translated into line segments and move to commands. The data structure it produces needs some reworking. Also, the XML::Parser tree output handling needs to be rewritten. It only looks for paths on one level. There are other things that should be done better but don't really cause any serious problems.
Here is parser/svg.pm in its current state.
Code:
#SVG parser and processor
package parser::svg;
use XML::Parser;
sub new {
my $self = {};
bless $self;
return $self;
}
#Parse an XML SVG file
#(self,fname)
sub parsefile {
my $self = shift if @_;
my $fname = shift if @_;
my $parser = new XML::Parser(Style => 'Tree');
$self->{svgtree} = $parser->parsefile($fname);
return 1;
}
#Extract path coord lists
#(self)->[pathcoordlists]
sub extractpathcoords {
my $self = shift if @_;
my @coordlists;
#Search through the tree
while(my $attrib = shift(@{@{$self->{svgtree}}[1]})) {
if($attrib eq 'path') {
print "path\n";
my $params = shift(@{@{$self->{svgtree}}[1]});
my $pathdata = @{$params}[0]->{d};
print "DATA: $pathdata \n";
my $coordlist = $self->plotpath($pathdata,.05);
push(@coordlists,$coordlist);
}
}
return \@coordlists;
}
#Handle plotting of SVG paths
#(self,curvedata[,curvestep])->coordlist
sub plotpath {
my $self = shift if @_;
my $data = shift if @_;
my $curvestep = .1;
$curvestep = shift if @_;
my @commands = split(/ /,$data);
my $points = {};
my @coordlist;
while(my $command = shift(@commands)) {
if($command eq 'M') { #Moveto
my $x = shift(@commands);
my $y = shift(@commands);
$points->{current} = [$x,$y];
$points->{start} = [$x,$y] unless defined $points->{start};
push (@coordlist,["M",$x,$y]);
} elsif($command eq 'L') { #Line to
my $x = shift(@commands);
my $y = shift(@commands);
$points->{current} = [$x,$y];
$points->{start} = [$x,$y] unless defined $points->{start};
push(@coordlist,["L",$x,$y]);
} elsif($command eq 'z') { #Return to start
$points->{current} = $points->{start};
my @point = ("L",@{$points->{start}});
push (@coordlist,\@point);
} elsif($command eq 'C') { #Cubic bezier curve, absolute
my $x0 = @{$points->{current}}[0];
my $y0 = @{$points->{current}}[1];
my $x1 = shift(@commands);
my $y1 = shift(@commands);
my $x2 = shift(@commands);
my $y2 = shift(@commands);
my $x3 = shift(@commands);
my $y3 = shift(@commands);
print "CBCurve:$x0,$y0,$x1,$y1,$x2,$y2,$x3,$y3\n";
my @newcoords = $self->plotbeziercubic($x0,$y0,$x1,$y1,$x2,$y2,$x3,$y3,$curvestep);
push(@coordlist,@newcoords);
$points->{current} = [@{$newcoords[scalar(@newcoords)-1]}[1],@{$newcoords[scalar(@newcoords)-1]}[2]];
} else {
die "ERROR: Unknown command \"$command\" in path.\n";
}
}
return \@coordlist;
}
#Plot a cubic bezier curve
#(self,x0,y0,x1,y1,x2,y2,x3,y3,step)->coordlist
sub plotbeziercubic {
my $self = shift if @_;
my $x0 = shift if @_;
my $y0 = shift if @_;
my $x1 = shift if @_;
my $y1 = shift if @_;
my $x2 = shift if @_;
my $y2 = shift if @_;
my $x3 = shift if @_;
my $y3 = shift if @_;
my $step = shift if @_;
#Coefficients
my $cx = (3*$x1) - (3*$x0);
my $bx = (3*$x2) - (3*$x1) - $cx;
my $ax = $x3 - $x0 - $cx - $bx;
my $cy = (3*$y1) - (3*$y0);
my $by = (3*$y2) - (3*$y1) - $cy;
my $ay = $y3 - $y0 - $cy - $by;
#Compute coordinates
my $t = 0;
my @coordlist;
while($t <= 1) {
my $x = ($ax*($t**3))+($bx*($t**2))+($cx*$t)+$x0;
my $y = ($ay*($t**3))+($by*($t**2))+($cy*$t)+$y0;
push(@coordlist,["L",$x,$y]);
$t += $step;
}
return @coordlist;
}
1;
This is the small test program I mentioned. It generates G-code with the above module:
Code:
#!/usr/bin/perl -w
use parser::svg;
my $svg = new parser::svg;
$svg->parsefile('something.svg'); #Process this file
my $coordlistsref = $svg->extractpathcoords();
#Generate G-Code
my @gcode;
my $d0 = "0";
my $d1 = "-.1";
my $feed = "20";
my $sfactor = ".011"; #Coords are mult by this to convert to inches
my $maxx=0;
my $maxy=0;
my $retract = 0;
my $lcounter = 0;
open(TESTFILE,">test.curve");
foreach my $coordlist (@{$coordlistsref}) {
if(@{$coordlist}[1]) {
#Begin cut profile
while(my $coord = shift(@{$coordlist})) {
my $mode = shift(@{$coord});
if($mode eq 'M') { #Go to
if(!$retract) { #Ignore if already retracted
print "LINE: $lcounter RETRACT to $d0\n";
push(@gcode,"N" . $lcounter . "G00Z" . $d0); #Retract blade
$lcounter += 1;
$retract = 1; #Retracted
}
my $x = smult(@{$coord}[0],$sfactor);
$maxx = $x if $x > $maxx;
my $y = smult(@{$coord}[1],$sfactor);
$maxy = $y if $y > $maxy;
print "LINE: $lcounter RAPID to $x,$y\n";
push(@gcode,"N" . $lcounter . "G00X" . $x . "Y" . $y); #Go to coord
$lcounter += 1;
print TESTFILE "M $x $y ";
} elsif($mode eq 'L') { #Cut a line
if($retract) { #Insert blade if retracted
print "LINE: $lcounter INSERT to $d1\n";
push(@gcode,"N" . $lcounter . "G01Z" . $d1 . "F" . $feed);
$lcounter += 1;
$retract = 0; #Inserted
}
my $x = smult(@{$coord}[0],$sfactor);
$maxx = $x if $x > $maxx;
my $y = smult(@{$coord}[1],$sfactor);
$maxy = $y if $y > $maxy;
print "LINE: $lcounter CUT to $x,$y\n";
push(@gcode,"N" . $lcounter . "G01X" . $x . "Y" . $y . "F" . $feed);
$lcounter += 1;
print TESTFILE "L $x $y ";
} else {
die "ERROR, unknown mode.\n";
}
}
print "LINE: $lcounter RETRACT to $d0\n";
push(@gcode,"N" . $lcounter . "G00Z" . $d0); #Retract blade
$lcounter += 1;
} else {
print "NOTICE: Blank path detected. Omitting.\n";
}
}
close(TESTFILE);
#Write Gcode
open(GCODE,">out.t");
foreach my $line (@gcode) {
print GCODE $line . "\n";
}
close(GCODE);
print "Max X: $maxx\nMax Y: $maxy\n";
sub smult {
my $one = shift;
my $two = shift;
if($one && $two) {
return $one*$two;
} else {
return 0;
}
}
This code works with plain SVG code produced by SodiPodi on Linux as long as all points are corners. It hasn't been tested with other editors. The $sfactor is used to convert the internal SVG coordinated into inches for the CNC machine. It isn't quite .011, but that worked for me. It will tell you the highest X and Y values seen when it processes a file, so you can use those to adjust it a bit. The variables above it are for configuration of feed and depths. The ones below it initialize those counters to 0, so don't change them.
You should probably use a g-code preview program to make sure there aren't any errors before making anything with it.