Commit e18cb883 authored by sumpfralle's avatar sumpfralle

added automatic conversion from svg to dxf


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@550 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent d1794e0c
...@@ -57,7 +57,8 @@ GTKMENU_FILE = "menubar.xml" ...@@ -57,7 +57,8 @@ GTKMENU_FILE = "menubar.xml"
HELP_WIKI_URL = "http://sourceforge.net/apps/mediawiki/pycam/index.php?title=%s" HELP_WIKI_URL = "http://sourceforge.net/apps/mediawiki/pycam/index.php?title=%s"
FILTER_GCODE = ("GCode files", ("*.ngc", "*.nc", "*.gc", "*.gcode")) FILTER_GCODE = ("GCode files", ("*.ngc", "*.nc", "*.gc", "*.gcode"))
FILTER_MODEL = (("STL models", "*.stl"), ("DXF contours", "*.dxf")) FILTER_MODEL = (("STL models", "*.stl"), ("DXF contours", "*.dxf"),
("SVG files", "*.svg"))
FILTER_CONFIG = ("Config files", "*.conf") FILTER_CONFIG = ("Config files", "*.conf")
FILTER_EMC_TOOL = ("EMC tool files", "*.tbl") FILTER_EMC_TOOL = ("EMC tool files", "*.tbl")
...@@ -1766,7 +1767,8 @@ class ProjectGui: ...@@ -1766,7 +1767,8 @@ class ProjectGui:
file_type, importer = pycam.Importers.detect_file_type(filename) file_type, importer = pycam.Importers.detect_file_type(filename)
if file_type and callable(importer): if file_type and callable(importer):
self.last_model_file = filename self.last_model_file = filename
self.load_model(importer(filename)) # TODO: get the "program_locations"
self.load_model(importer(filename, program_locations=None))
self.update_save_actions() self.update_save_actions()
else: else:
log.error("Failed to detect filetype!") log.error("Failed to detect filetype!")
......
...@@ -204,7 +204,7 @@ class DXFParser: ...@@ -204,7 +204,7 @@ class DXFParser:
return None return None
def import_model(filename): def import_model(filename, program_locations=None):
try: try:
infile = open(filename,"rb") infile = open(filename,"rb")
except IOError, err_msg: except IOError, err_msg:
......
...@@ -74,7 +74,7 @@ def UniqueEdge(p1, p2): ...@@ -74,7 +74,7 @@ def UniqueEdge(p1, p2):
return e return e
def ImportModel(filename, use_kdtree=True): def ImportModel(filename, use_kdtree=True, program_locations=None):
global vertices, edges, kdtree global vertices, edges, kdtree
vertices = 0 vertices = 0
edges = 0 edges = 0
......
# -*- 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-eps", eps_filename, svg_filename])
except OSError, err_msg:
log.warning("SVGImporter: failed to execute 'inkscape': %s" % 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):
if location is None:
location = "pstoedit"
try:
process = subprocess.Popen(stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
args = [location,
"-dt",
"-f", "dxf:-polyaslines",
eps_filename, dxf_filename])
except OSError, err_msg:
log.warning("SVGImporter: failed to execute 'pstoedit': %s" % err_msg)
return False
returncode = process.wait()
if returncode == 0:
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):
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 = "inkscape"
if program_locations and "pstoedit" in program_locations:
pstoedit_path = program_locations["pstoedit"]
else:
pstoedit_path = "pstoedit"
# 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
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, location=pstoedit_path)
# we don't need the eps file anymore
remove_temp_file(eps_file_name)
if not success:
result = None
else:
log.info("Successfully converted EPS file to DXF file")
result = pycam.Importers.DXFImporter.import_model(dxf_file_name)
# always remove the dxf file
remove_temp_file(dxf_file_name)
return result
...@@ -21,11 +21,12 @@ You should have received a copy of the GNU General Public License ...@@ -21,11 +21,12 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>. along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
__all__ = ["STLImporter", "DXFImporter", "TestModel"] __all__ = ["STLImporter", "DXFImporter", "SVGImporter", "TestModel"]
import pycam.Utils.log import pycam.Utils.log
import pycam.Importers.DXFImporter import pycam.Importers.DXFImporter
import pycam.Importers.STLImporter import pycam.Importers.STLImporter
import pycam.Importers.SVGImporter
import os import os
...@@ -39,12 +40,14 @@ def detect_file_type(filename): ...@@ -39,12 +40,14 @@ def detect_file_type(filename):
else: else:
# check all listed importers # check all listed importers
# TODO: this should be done by evaluating the header of the file # TODO: this should be done by evaluating the header of the file
if filename.endswith(".stl"): if filename.lower().endswith(".stl"):
return ("stl", pycam.Importers.STLImporter.ImportModel) return ("stl", pycam.Importers.STLImporter.ImportModel)
elif filename.endswith(".dxf"): elif filename.lower().endswith(".dxf"):
return ("dxf", pycam.Importers.DXFImporter.import_model) return ("dxf", pycam.Importers.DXFImporter.import_model)
elif filename.lower().endswith(".svg"):
return ("svg", pycam.Importers.SVGImporter.import_model)
else: else:
log.error("Importers: Failed to detect the model type of '%s'." \ log.error("Importers: Failed to detect the model type of '%s'." \
% filename + " Is the file extension (.stl/.dxf) correct?") % filename + " Is the file extension (stl/dxf/svg) correct?")
return failure return failure
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment