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
# -*- coding: utf-8 -*-
"""
$Id$
Copyright 2010 Lars Kruse <devel@sumpfralle.de>
This file is part of PyCAM.
PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import pycam.Importers.DXFImporter
import tempfile
import subprocess
import os
log = pycam.Utils.log.get_logger()
def convert_svg2eps(svg_filename, eps_filename, location=None):
if location is None:
location = "inkscape"
try:
process = subprocess.Popen(stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
args = [location, "--export-area-page", "--export-eps",
eps_filename, svg_filename])
except OSError, err_msg:
log.error("SVGImporter: failed to execute 'inkscape' (%s): %s" \
% (location, err_msg))
return False
returncode = process.wait()
if returncode == 0:
return True
else:
log.warn(("SVGImporter: failed to convert SVG file (%s) to EPS file " \
+ "(%s): %s") % (svg_filename, eps_filename,
process.stderr.read()))
return False
def convert_eps2dxf(eps_filename, dxf_filename, location=None, unit="mm"):
if location is None:
location = "pstoedit"
args = [location, "-dt", "-nc", "-f", "dxf:-polyaslines"]
if unit == "mm":
# eps uses inch by default - we need to scale
args.extend(("-xscale", "25.4", "-yscale", "25.4"))
args.append(eps_filename)
args.append(dxf_filename)
try:
process = subprocess.Popen(stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, args=args)
except OSError, err_msg:
log.error("SVGImporter: failed to execute 'pstoedit' (%s): %s" \
% (location, err_msg))
return False
returncode = process.wait()
if returncode == 0:
return True
elif returncode == -11:
log.warn(("SVGImporter: maybe there was a problem with the " \
+ "conversion from EPS (%s) to DXF\n Users of Ubuntu 'lucid' should install " \
+ "the package 'libpstoedit0c2a' from the 'maverick' " \
+ "repository to avoid this warning.") % str(eps_filename))
return True
else:
log.warn(("SVGImporter: failed to convert EPS file (%s) to DXF file " \
+ "(%s): %s") % (eps_filename, dxf_filename,
process.stderr.read()))
return False
def import_model(filename, program_locations=None, unit="mm", callback=None):
if not os.path.isfile(filename):
log.error("SVGImporter: file (%s) does not exist" % filename)
return None
if program_locations and "inkscape" in program_locations:
inkscape_path = program_locations["inkscape"]
else:
inkscape_path = None
if program_locations and "pstoedit" in program_locations:
pstoedit_path = program_locations["pstoedit"]
else:
pstoedit_path = None
# the "right" way would be:
# inkscape --print='| pstoedit -dt -f dxf:-polyaslines - -' input.svg
# Sadly a bug in v0.47 breaks this: https://bugs.launchpad.net/inkscape/+bug/511361
# convert svg to eps via inkscape
eps_file_handle, eps_file_name = tempfile.mkstemp(suffix=".eps")
os.close(eps_file_handle)
success = convert_svg2eps(filename, eps_file_name, location=inkscape_path)
def remove_temp_file(filename):
if os.path.isfile(filename):
try:
os.remove(filename)
except OSError, err_msg:
log.warn("SVGImporter: failed to remove temporary file " \
+ "(%s): %s" % (filename, err_msg))
# remove the temporary file
if not success:
remove_temp_file(eps_file_name)
return None
if callback and callback():
remove_temp_file(eps_file_name)
log.warn("SVGImporter: load model operation was cancelled")
return None
log.info("Successfully converted SVG file to EPS file")
# convert eps to dxf via pstoedit
dxf_file_handle, dxf_file_name = tempfile.mkstemp(suffix=".dxf")
os.close(dxf_file_handle)
success = convert_eps2dxf(eps_file_name, dxf_file_name, unit=unit,
location=pstoedit_path)
# we don't need the eps file anymore
remove_temp_file(eps_file_name)
if not success:
result = None
elif callback and callback():
log.warn("SVGImporter: load model operation was cancelled")
result = None
else:
log.info("Successfully converted EPS file to DXF file")
result = pycam.Importers.DXFImporter.import_model(dxf_file_name,
unit=unit, color_as_height=True, callback=callback)
# always remove the dxf file
remove_temp_file(dxf_file_name)
return result