Root/dxf2x/dxf2gp

1#!/usr/bin/perl
2#
3# dxf2gp - Convert simple DXF to Gnuplot
4#
5# Written 2011 by Werner Almesberger
6# Copyright 2011 Werner Almesberger
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13
14#
15# Currently, only the directives LINE, CIRCLE, and ARC are supported.
16# Adjacent line segments are not merged. Arcs and circles are translated
17# to (multi-segment) lines.
18#
19# The main purpose of this script is currently to convert the M1 case
20# design:
21# http://projects.qi-hardware.com/index.php/p/m1/source/tree/master/cad/protocase_v7_laser.dxf
22#
23
24$S_SCAN = 0; # wait for LINE, CIRCLE, or ARC
25$S_ByLayer = 1; # next is ByLayer
26$S_INDEX = 2; # next is index
27$S_DATA = 3; # next is indexed data
28$S_MARKER = 4; # next is subclass marker
29
30
31sub line
32{
33    print "$_[10] $_[20]\n$_[11] $_[21]\n\n";
34}
35
36sub circle
37{
38    local ($x, $y, $r) = ($_[10], $_[20], $_[40]);
39
40    for (my $i = 0; $i != 100; $i++) {
41        local $v = 3.14159265*2/100*$i;
42        print $x+$r*sin($v), " ", $y+$r*cos($v), "\n";
43    }
44    print $x, " ", $y+$r, "\n\n";
45}
46
47sub arc
48{
49    local ($x, $y, $r) = ($_[10], $_[20], $_[40]);
50    local ($a0, $a1) = ($_[50], $_[51]);
51
52    $a1 += 360 if $a1 < $a0;
53    for (my $a = $a0; $a <= $a1; $a += 2) {
54        local $v = 3.14159265/180*$a;
55        print $x+$r*cos($v), " ", $y+$r*sin($v), "\n";
56    }
57    print "\n";
58}
59
60
61$s = $S_SCAN;
62while (<>) {
63    if ($s == $S_SCAN) {
64        if (/^LINE\b/) {
65            *fn = *line;
66        } elsif (/^CIRCLE/) {
67            *fn = *circle;
68        } elsif (/^ARC/) {
69            *fn = *arc;
70        } else {
71            next;
72        }
73        $s = $S_ByLayer;
74        next;
75    } elsif ($s == $S_ByLayer) {
76        if (/^ByLayer/) {
77            $s = $S_INDEX;
78        }
79        next;
80    } elsif ($s == $S_INDEX) {
81        if ($_ == 0) {
82            &fn(@a);
83            @a = ();
84            $s = $S_SCAN;
85        } elsif ($_ == 100) {
86            $s = $S_MARKER;
87        } else {
88            $index = $_;
89            $s = $S_DATA;
90        }
91    } elsif ($s == $S_DATA) {
92        $a[$index] = $_+0;
93        $s = $S_INDEX;
94    } elsif ($s == $S_MARKER) {
95        $s = $S_INDEX;
96    } else {
97        die;
98    }
99}
100

Archive Download this file

Branches:
master



interactive