forked from samyk/samytools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdxf2svg
executable file
·122 lines (101 loc) · 2.86 KB
/
dxf2svg
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
#!/usr/bin/perl
# automatically convert .dxf's to .svg's (typically Fusion 360 DXFs to SVGs for use in Glowforge)
#
# -samy kamkar 2018/05/17
# Path to 3D files dir
my $DEFAULT_DIR = "$ENV{HOME}/Code/3D";
# Possible paths to Inkscape binary, as well as $PATH
my @inkscape = (qw|
/Applications/Design/Inkscape.app/Contents/Resources/bin/inkscape
/Applications/Design/Inkscape.app/Contents/Resources/bin/inkscape-bin
/Applications/Design/Inkscape.app/Contents/MacOS/Inkscape
/opt/local/bin/inkscape
/Applications/Inkscape.app/Contents/Resources/bin/inkscape
/Applications/Inkscape.app/Contents/Resources/bin/inkscape-bin
/Applications/Inkscape.app/Contents/MacOS/Inkscape
|, map { "$_/inkscape" } split(/:/, $ENV{PATH}));
# options to pass to inkscape - order matters
my @opts = qw/--convert-dpi-method=none -z -l/;
=cut
to run on startup on macOS, make sure your paths (perl, dxf2svg, and dxf directory are correct):
cat > ~/Library/LaunchAgents/pl.samy.dxf2svg.plist
<?xml version=1.0 encoding=UTF-8?>
<!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version=1.0>
<dict>
<key>Label</key>
<string>pl.samy.dxf2svg</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/perl</string>
<string>/b/dxf2svg</string>
<string>-a</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
=cut
use Cwd;
use strict;
# Find path to inkscape
my ($INKSCAPE) = grep -e, @inkscape;
die "You must have inkscape installed or discoverable from your \$PATH" unless $INKSCAPE;
die "usage: $0 <from dxf> [optional svg] | <-a [optional path to watch for dxfs] (auto-convert in background)>\n" unless $ARGV[0] eq "-a" || grep -e, @ARGV;
# run in background
$ARGV[0] eq "-a" ? bg($ARGV[1]) : convert(@ARGV);
sub bg
{
my $dir = shift || $DEFAULT_DIR;
if (my $p = fork)
{
die "Running in bg! (pid $p)\n";
}
close(STDERR);
close(STDIN);
close(STDOUT);
my %files = map { $_ => mtime($_) } <$dir/*.dxf>;
while (1)
{
foreach my $file (<$dir/*.dxf>)
{
my $mtime = mtime($file);
# new file or newer mod time
if (!$files{$file})
{
#print "New file: $file\n";
$files{$file} = $mtime;
dxf2svg($file);
}
elsif ($mtime > $files{$file})
{
#print "Newer mod time: $file\n";
$files{$file} = $mtime;
dxf2svg($file);
}
}
sleep 1;
}
}
sub convert
{
my ($from, $to) = @_;
# need absolute paths
my $cwd = cwd();
$from = "$cwd/$from" unless $from =~ m|^/|;
# out filename
($to = $from) =~ s/\.\w+$/.svg/ unless defined($to);
print "Converting $from -> $to!\n";
print STDERR "$INKSCAPE @opts $to $from\n";
system($INKSCAPE, @opts, $to, $from);
}
sub mtime
{
return (stat($_[0]))[9];
}
sub dxf2svg
{
my $out = $_[0];
$out =~ s/dxf$/svg/;
convert($_[0], $out);
}