def readfile(filename: str, encoding: str = None, legacy_mode: bool = False, filter_stack=None) -> 'Drawing': """ Read DXF document specified by `filename` from file-system. This is the preferred method to open existing ASCII or Binary DXF files. Read the DXF drawing from the file-system with auto-detection of encoding for ASCII DXF files. Decoding errors will be ignored. Override encoding detection by setting argument `encoding` to the estimated encoding. (use Python encoding names like in the :func:`open` function). If argument `legacy_mode` is ``True``, `ezdxf` tries to reorder the coordinates of the LINE entity in files from CAD applications which wrote the coordinates in the order: x1, x2, y1, y2. Additional fixes may be added later. The legacy mode has a speed penalty of around 5%. .. hint:: Try argument :code:`legacy_mode=True` if error ``'Missing required y coordinate near line: ...'`` occurs. Args: filename: filename of ASCII or Binary DXF document encoding: use ``None`` for auto detect (default), or set a specific encoding like ``'utf-8'``, ignored for Binary DXF files legacy_mode: adds an extra trouble shooting import layer if ``True`` filter_stack: interface to put filters between reading layers Raises: IOError: File `filename` is not a DXF file or does not exist. DXFStructureError: for invalid DXF structure """ # for argument filter_stack see :class:`~ezdxf.drawing.Drawing.read` for more information from ezdxf.lldxf.validator import is_dxf_file, is_binary_dxf_file from ezdxf.tools.codepage import is_supported_encoding from ezdxf.lldxf.tagger import binary_tags_loader if is_binary_dxf_file(filename): with open(filename, 'rb') as fp: data = fp.read() loader = binary_tags_loader(data) return Drawing.load(loader, legacy_mode, filter_stack) if not is_dxf_file(filename): raise IOError("File '{}' is not a DXF file.".format(filename)) info = dxf_file_info(filename) if encoding is not None: # override default encodings if absolute necessary info.encoding = encoding with open(filename, mode='rt', encoding=info.encoding, errors='ignore') as fp: doc = read(fp, legacy_mode=legacy_mode, filter_stack=filter_stack) doc.filename = filename if encoding is not None and is_supported_encoding(encoding): # store overridden encoding if supported by AutoCAD, else default encoding stored in $DWGENCODING is used # as document encoding or 'cp1252' if $DWGENCODING is unset. doc.encoding = encoding return doc
def readfile(filename, encoding=None, legacy_mode=False): """ Read DXF drawing specified by *filename* from file system. Supported DXF versions: - pre AC1009 DXF versions will be upgraded to AC1009 - AC1009: AutoCAD R12 (DXF R12) - AC1012: AutoCAD R13 upgraded to AC1015 - AC1014: AutoCAD R14 upgraded to AC1015 - AC1015: AutoCAD 2000 - AC1018: AutoCAD 2004 - AC1021: AutoCAD 2007, fixates encoding='utf-8' - AC1024: AutoCAD 2010, fixates encoding='utf-8' - AC1027: AutoCAD 2013, fixates encoding='utf-8' - AC1032: AutoCAD 2018, fixates encoding='utf-8' Args: filename: DXF filename encoding: use None for auto detect, or set a specific encoding like 'utf-8' legacy_mode: True - adds an extra trouble shooting import layer; False - requires DXF file from modern CAD apps """ if not is_dxf_file(filename): raise IOError("File '{}' is not a DXF file.".format(filename)) info = dxf_file_info(filename) with io.open(filename, mode='rt', encoding=info.encoding, errors='ignore') as fp: dwg = read(fp, legacy_mode=legacy_mode, dxfversion=info.version) dwg.filename = filename if encoding is not None and is_supported_encoding(encoding): dwg.encoding = encoding return dwg
def readfile(filename: Union[str, 'Path'], encoding: str = None, errors: str = 'surrogateescape') -> 'Drawing': """ Read the DXF document `filename` from the file-system. This is the preferred method to load existing ASCII or Binary DXF files, the required text encoding will be detected automatically and decoding errors will be ignored. Override encoding detection by setting argument `encoding` to the estimated encoding. (use Python encoding names like in the :func:`open` function). If this function struggles to load the DXF document and raises a :class:`DXFStructureError` exception, try the :func:`ezdxf.recover.readfile` function to load this corrupt DXF document. Args: filename: filename of the ASCII- or Binary DXF document encoding: use ``None`` for auto detect (default), or set a specific encoding like "utf-8", argument is ignored for Binary DXF files errors: specify decoding error handler - "surrogateescape" to preserve possible binary data (default) - "ignore" to use the replacement char U+FFFD "\ufffd" for invalid data - "strict" to raise an :class:`UnicodeDecodeError` exception for invalid data Raises: IOError: not a DXF file or file does not exist DXFStructureError: for invalid or corrupted DXF structures UnicodeDecodeError: if `errors` is "strict" and a decoding error occurs .. deprecated:: v0.14 argument `legacy_mode`, use module :mod:`ezdxf.recover` to load DXF documents with structural flaws. """ from ezdxf.lldxf.validator import is_dxf_file, is_binary_dxf_file from ezdxf.tools.codepage import is_supported_encoding from ezdxf.lldxf.tagger import binary_tags_loader filename = str(filename) if is_binary_dxf_file(filename): with open(filename, 'rb') as fp: data = fp.read() loader = binary_tags_loader(data, errors=errors) return Drawing.load(loader) if not is_dxf_file(filename): raise IOError(f"File '{filename}' is not a DXF file.") info = dxf_file_info(filename) if encoding is not None: # override default encodings if absolute necessary info.encoding = encoding with open(filename, mode='rt', encoding=info.encoding, errors=errors) as fp: doc = read(fp) doc.filename = filename if encoding is not None and is_supported_encoding(encoding): # store overridden encoding if supported by AutoCAD, else default # encoding stored in $DWGENCODING is used as document encoding or # 'cp1252' if $DWGENCODING is unset. doc.encoding = encoding return doc
def readfile(filename: str, encoding: str = None, legacy_mode: bool = False) -> 'Drawing': """ Read the DXF document `filename` from the file-system. This is the preferred method to load existing ASCII or Binary DXF files, the required text encoding will be detected automatically and decoding errors will be ignored. Override encoding detection by setting argument `encoding` to the estimated encoding. (use Python encoding names like in the :func:`open` function). If this function struggles to load the DXF document and raises a :class:`DXFStructureError` exception, try the :func:`ezdxf.recover.readfile` function to load this corrupt DXF document. Args: filename: filename of the ASCII- or Binary DXF document encoding: use ``None`` for auto detect (default), or set a specific encoding like "utf-8", argument is ignored for Binary DXF files legacy_mode: adds an extra trouble shooting import layer if ``True`` (deprecated) Raises: IOError: File `filename` is not a DXF file or does not exist. DXFStructureError: for invalid DXF structure .. deprecated:: v0.14 argument `legacy_mode`, use module :mod:`ezdxf.recover` to load DXF documents with structural flaws. """ from ezdxf.lldxf.validator import is_dxf_file, is_binary_dxf_file from ezdxf.tools.codepage import is_supported_encoding from ezdxf.lldxf.tagger import binary_tags_loader if legacy_mode: warnings.warn( '"legacy_mode" is deprecated (removed in v0.16), replace call by ' 'ezdxf.recover.read().', DeprecationWarning) filename = str(filename) if is_binary_dxf_file(filename): with open(filename, 'rb') as fp: data = fp.read() loader = binary_tags_loader(data) return Drawing.load(loader, legacy_mode) if not is_dxf_file(filename): raise IOError("File '{}' is not a DXF file.".format(filename)) info = dxf_file_info(filename) if encoding is not None: # override default encodings if absolute necessary info.encoding = encoding with open(filename, mode='rt', encoding=info.encoding, errors='ignore') as fp: doc = read(fp, legacy_mode=legacy_mode) doc.filename = filename if encoding is not None and is_supported_encoding(encoding): # store overridden encoding if supported by AutoCAD, else default # encoding stored in $DWGENCODING is used as document encoding or # 'cp1252' if $DWGENCODING is unset. doc.encoding = encoding return doc