summaryrefslogtreecommitdiff
path: root/xml2dxf.pl
blob: 19e9f4c90eccc02e5c5828162affff442807762a (plain)
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
#!/usr/bin/perl

# converts a DXF in XML format back to DXF.

## Copyright (c) 2018-2020 by Thomas Kremer
## License: GPL ver. 2 or 3

# usage:
#   xml2dxf.pl infile.xml > outfile.dxf
#   xml2dxf.pl < infile.xml > outfile.dxf

use strict;
use warnings;

use DXF;
use XML::DOM;
use IO::Handle;

sub xml2lol {
  my $node = shift;
  my $name = $node->getTagName;
  $name =~ s/^_/\$/;
  my @xmlattrs = $node->getAttributes->getValues;
  my %attrs;
  for (@xmlattrs) {
    my $aname = $_->getName;
    my $aval = $_->getValue;
    if ($aname =~ s/-array$//) {
      $aval = [split / /,$aval];
    }
    $attrs{$aname} = $aval;
  }
  my @children;
  for ($node->getChildNodes) {
    if ($_->getNodeType == XML::DOM::ELEMENT_NODE) {
      push @children, xml2lol($_);
    }
  }
  return DXF::lol($name => \%attrs,\@children);
}

my $file = shift;
my $f;
if (defined $file) {
  open($f, "<", $file) or die "cannot open file";
} else {
  $f = \*STDIN;
}

$/ = undef;
my $content = <$f>;

my $xmldoc = XML::DOM::Parser->new->parse($content);
my $lol = xml2lol($xmldoc->getDocumentElement);
$xmldoc->dispose;
DXF::lol2dxf($lol,sub {print @_;});