#!/usr/bin/perl

if(@ARGV == 0){
    print("usage: sprit.pl WRLFILE\n");
    exit(0);
}

$wrlfilename=$ARGV[0];
open(WRLFILE,"<".$ARGV[0]);
@lines=<WRLFILE>;
close(WRLFILE);
$inpoint=0;
$nline=0;
@rgb={0,0,0};
$x=0;
$y=0;
$z=0;
$px=-99999;
$py=-99999;
$pz=-99999;
open(RUBYFILE,">lines.rb");

foreach (@lines){
    if(/point \[/){
	$inpoint=1;
	$nline ++;
    }
    elsif($inpoint==1 && /\]/){
	$inpoint=0;
	$px=-99999;
	$py=-99999;
	$pz=-99999;
    }
    elsif($inpoint==1 && /,/){
	s/,//;
	/(-*\d+\.*\d*) (-*\d+\.*\d*) (-*\d+\.*\d*)/;
	$x = $1*0.01;
	$y = $2*0.01;
	$z = $3*0.01;
#	print $_;
#	@p=split(/\ /,$_);
	if($px!=-99999 && $py!=-99999 && $pz!=-99999 && ($px!=$x || $py!=$y || $pz!=$z)){
	    print RUBYFILE "line = Sketchup.active_model.entities.add_line([".$px.", ".$py.", ".$pz."], [".$x.", ".$y.", ".$z."])\n";
	    print RUBYFILE "line ? line.material = [".$rgb[0].", ".$rgb[1].", ".$rgb[2]."] : nil\n";
#	    print RUBYFILE "line ? line.material = \"red\" : nil\n";
	}
	$px=$x; $py=$y; $pz=$z;
    }
    elsif($inpoint==0 && /diffuseColor/){
	s/diffuseColor//;
	/(\d+\.*\d*) (\d+\.*\d*) (\d+\.*\d*)/;
	$rgb[0] = 255*$1;
	$rgb[1] = 255*$2;
	$rgb[2] = 255*$3;
    }

}
close(RUBYFILE); 
    
