def readfile(filename: str, version: str = None, audit=False) -> Optional[Drawing]: """ Use an installed `ODA File Converter`_ to convert a DWG/DXB/DXF file into a temporary DXF file and load this file by `ezdxf`. Args: filename: file to load by ODA File Converter version: load file as specific DXF version, by default the same version as the source file or if not detectable the latest by `ezdxf` supported version. audit: audit source file before loading """ infile = Path(filename).absolute() if not infile.exists(): raise FileNotFoundError(f"No such file or directory: '{infile}'") in_folder = infile.parent name = infile.name ext = infile.suffix.lower() tmp_folder = Path(temp_path).absolute() if not tmp_folder.exists(): tmp_folder.mkdir() dxf_temp_file = (tmp_folder / name).with_suffix('.dxf') _version = 'ACAD2018' if ext == '.dxf': if is_binary_dxf_file(str(infile)): pass elif is_dxf_file(str(infile)): with open(filename, 'rt') as fp: info = dxf_info(fp) _version = VERSION_MAP[info.version] elif ext == '.dwg': _version = dwg_version(str(infile)) if _version is None: raise ValueError('Unknown or unsupported DWG version.') else: raise ValueError(f"Unsupported file format: '{ext}'") if version is None: version = _version version = map_version(version) cmd = _odafc_cmd(name, str(in_folder), str(tmp_folder), fmt='DXF', version=version, audit=audit) _execute_odafc(cmd) if dxf_temp_file.exists(): doc = ezdxf.readfile(str(dxf_temp_file)) dxf_temp_file.unlink() doc.filename = infile.with_suffix('.dxf') return doc return None
def dxf_stream_info(stream: TextIO) -> 'DXFInfo': """ Reads basic DXF information from a text stream: DXF version, encoding and handle seed. """ from ezdxf.lldxf.validator import dxf_info info = dxf_info(stream) # R2007 files and later are always encoded as UTF-8 if info.version >= 'AC1021': info.encoding = 'utf-8' return info
def dxf_stream_info(stream: TextIO) -> 'DXFInfo': """ Reads basic DXF information from a text stream: DXF version, encoding and handle seed. Returns: DXF info object with attributes: version, release, handseed, encoding """ from ezdxf.lldxf.validator import dxf_info info = dxf_info(stream) if info.version >= 'AC1021': # R2007 files and later are always encoded as UTF-8 info.encoding = 'utf-8' return info
def checkhandles(stream): info = dxf_info(stream) stream.seek(0) handles = [] iterator = low_level_tagger(stream) for tag in iterator: if tag.code in (5, 105): try: handle = int(tag.value, 16) except ValueError: print('invalid handle at line number %d' % iterator.lineno) else: handles.append(handle) printhandles(handles, info)
def checkhandles(stream): info = dxf_info(stream) stream.seek(0) handles = [] lineno = 1 iterator = ascii_tags_loader(stream, skip_comments=False) for tag in iterator: if tag.code in (5, 105): try: handle = int(tag.value, 16) except ValueError: print('invalid handle at line number %d' % lineno) else: handles.append(handle) lineno += 2 printhandles(handles, info)
def _detect_version(path: str) -> str: version = 'ACAD2018' ext = os.path.splitext(path)[1].lower() if ext == '.dxf': if is_binary_dxf_file(path): pass elif is_dxf_file(path): with open(path, 'rt') as fp: info = dxf_info(fp) version = VERSION_MAP[info.version] elif ext == '.dwg': version = dwg_version(path) if version is None: raise ValueError('Unknown or unsupported DWG version.') else: raise ValueError(f"Unsupported file format: '{ext}'") return map_version(version)
def _detect_version(path: str) -> str: version = "ACAD2018" ext = os.path.splitext(path)[1].lower() if ext == ".dxf": if is_binary_dxf_file(path): pass elif is_dxf_file(path): with open(path, "rt") as fp: info = dxf_info(fp) version = VERSION_MAP[info.version] elif ext == ".dwg": version = dwg_version(path) # type: ignore if version is None: raise ValueError("Unknown or unsupported DWG version.") else: raise ValueError(f"Unsupported file format: '{ext}'") return map_version(version)
def get_dxf_info(self) -> None: info = dxf_info(cast(TextIO, self)) # since DXF R2007 (AC1021) file encoding is always 'utf-8' self.encoding = info.encoding if info.version < 'AC1021' else 'utf-8' self.dxfversion = info.version