示例#1
0
def main(in_path, out_path):
    try:
        _, out_suffix = os.path.splitext(out_path)
        
        with util.TemporaryDirectory() as temp_dir:
            absolute_in_path = os.path.abspath(in_path)
            temp_out_path = os.path.join(temp_dir, 'out.pdf')

            # Asymptote creates A LOT of temp files (presumably when invoking
            #  LaTeX) and leaves some of them behind. Thus we run asymptote
            # in a temporary directory.
            loaded_files = _asymptote(
                absolute_in_path,
                'out',
                os.path.dirname(absolute_in_path),
                temp_dir)
            
            if not os.path.exists(temp_out_path):
                raise util.UserError('Asymptote did not generate a PDF file.', in_path)
            
            # All dependencies as paths relative to the project root.
            dependencies = set(map(os.path.relpath, loaded_files))
            
            # Write output files.
            make.write_dependencies(out_path + '.d', out_path, dependencies - {in_path})
            shutil.copyfile(temp_out_path, out_path)
    except util.UserError as e:
        raise util.UserError('While processing {}: {}', in_path, e)
示例#2
0
    def check_document_units(cls, path):
        with open(path, 'r') as file:
            p = etree.XMLParser(huge_tree=True)
            document = etree.parse(file, parser=p)

        height_attr = document.getroot().get('height')

        if height_attr is None:
            raise util.UserError(
                'SVG document has no height attribute. See '
                'https://github.com/Feuermurmel/openscad-template/wiki/Absolute-Measurements'
            )

        _, height_unit = cls._parse_measure(height_attr)

        if height_unit is None or height_unit == 'px':
            raise util.UserError(
                'Height of SVG document is not an absolute measure. See '
                'https://github.com/Feuermurmel/openscad-template/wiki/Absolute-Measurements'
            )

        if document.getroot().get('viewBox') is None:
            raise util.UserError(
                'SVG document has no viewBox attribute. See '
                'https://github.com/Feuermurmel/openscad-template/wiki/Absolute-Measurements'
            )
示例#3
0
def main(in_path, out_path):
    try:
        _, out_suffix = os.path.splitext(out_path)

        effect.ExportEffect.check_document_units(in_path)

        with util.TemporaryDirectory() as temp_dir:
            temp_svg_path = os.path.join(temp_dir, os.path.basename(in_path))

            shutil.copyfile(in_path, temp_svg_path)

            _unfuck_svg_document(temp_svg_path)

            export_effect = effect.ExportEffect()
            export_effect.affect(args=[temp_svg_path], output=False)

        with open(out_path, 'w') as file:
            if out_suffix == '.dxf':
                export_effect.write_dxf(file)
            elif out_suffix == '.asy':
                export_effect.write_asy(file)
            else:
                raise Exception('Unknown file type: {}'.format(out_suffix))
    except util.UserError as e:
        raise util.UserError('While processing {}: {}', in_path, e)