View Single Post
  #4   Ban this user!
Old 06-08-2005, 01:18 AM
galacticroot galacticroot is offline
 
Join Date: Nov 2004
Location: US
Posts: 23
galacticroot is on a distinguished road

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.
Reply With Quote

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361