Esempio n. 1
0
def raw_pretty_print(filename, nocompile=True, legacy_mode=False):
    try:
        info = dxf_file_info(str(filename))
    except IOError:
        print("Unable to read DXF file '{}'.".format(filename))
        sys.exit(1)
    except DXFError as e:
        print(str(e))
        sys.exit(2)

    with io.open(filename, mode='rt', encoding=info.encoding, errors='ignore') as dxf:
        tagger = low_level_tagger(dxf)
        if legacy_mode:
            tagger = tag_reorder_layer(tagger)
        if nocompile is False:
            tagger = tag_compiler(tagger)
        html_filename = filename.parent / (filename.stem + '.html')
        try:
            with io.open(html_filename, mode='wt', encoding='utf-8') as html:
                html.write(rawpp(tagger, str(filename)))
        except IOError:
            print("IOError: can not write file '{}'.".format(html_filename))
        except DXFStructureError as e:
            print("DXFStructureError: {}".format(str(e)))
    return html_filename
Esempio n. 2
0
 def __init__(self, name: str):
     self.name = str(name)
     if not is_dxf_file(name):
         raise DXFStructureError(f'File {name} is not a DXF file')
     info = dxf_file_info(name)
     self.encoding = info.encoding
     self.dxfversion = info.version
     self.file = open(name, mode='rt', encoding=self.encoding)
     self._fp_entities_section = None
     self._fp_objects_section = None
Esempio n. 3
0
def get_tagger(filename):
    from ezdxf.lldxf.validator import is_dxf_file
    from ezdxf.filemanagement import dxf_file_info
    if not is_dxf_file(filename):
        raise IOError("File '{}' is not a DXF file.".format(filename))

    info = dxf_file_info(filename)
    with open(filename, mode='rt', encoding=info.encoding, errors='ignore') as fp:
        tagger = read(fp)
    return tagger
Esempio n. 4
0
def get_tag_loader(filename: Union[str, Path],
                   errors: str = "ignore") -> Iterable[DXFTag]:

    filename = str(filename)
    if not is_dxf_file(filename):
        raise IOError(f"File '{filename}' is not an ASCII DXF file.")

    info = dxf_file_info(filename)
    with open(filename, mode="rt", encoding=info.encoding,
              errors=errors) as fp:  # type: ignore
        return list(ascii_tags_loader(fp, skip_comments=True))  # type: ignore
Esempio n. 5
0
def modelspace(filename: str,
               types: Iterable[str] = None) -> Iterable[DXFGraphic]:
    """
    Iterate over all modelspace entities as :class:`DXFGraphic` objects of a seekable file.

    Use this function to 'quick' iterate over modelspace entities of a DXF file,
    filtering DXF types may speed up things if many entity types will be skipped.

    Args:
        filename: filename of a seekable DXF file
        types: DXF types like ``['LINE', '3DFACE']`` which should be returned, ``None`` returns all supported types.

    """
    info = dxf_file_info(filename)
    prev_code: int = -1
    prev_value: str = ''
    entities = False
    requested_types = _requested_types(types)

    with open(filename, mode='rt', encoding=info.encoding) as fp:
        tagger = low_level_tagger(fp)
        queued: Optional[DXFEntity] = None
        tags: List[DXFTag] = []
        factory = EntityFactory()
        linked_entity = entity_linker()
        for tag in tag_compiler(tagger):
            code = tag.code
            value = tag.value
            if entities:
                if code == 0 and value == 'ENDSEC':
                    if queued:
                        yield queued
                    return
                if code == 0:
                    if len(tags) and tags[0].value in requested_types:
                        entity = factory.entity(ExtendedTags(tags))
                        if not linked_entity(
                                entity) and entity.dxf.paperspace == 0:
                            if queued:  # queue one entity for collecting linked entities (VERTEX, ATTRIB)
                                yield queued
                            queued = entity
                    tags = [tag]
                else:
                    tags.append(tag)
                continue  # if entities - nothing else matters
            elif code == 2 and prev_code == 0 and prev_value == 'SECTION':
                entities = (value == 'ENTITIES')

            prev_code = code
            prev_value = value
Esempio n. 6
0
def readfile(filename: str, legacy_mode: bool = False, compile_tags=True) -> Iterable[DXFTag]:
    from ezdxf.lldxf.validator import is_dxf_file

    if not is_dxf_file(filename):
        raise IOError("File '{}' is not a DXF file.".format(filename))

    info = dxf_file_info(filename)
    fp = open(filename, mode='rt', encoding=info.encoding, errors='ignore')
    tagger = low_level_tagger(fp)
    if legacy_mode:
        tagger = tag_reorder_layer(tagger)
    if compile_tags:
        tagger = tag_compiler(tagger)
    return tagger
Esempio n. 7
0
def from_file(filename: str, codes: Set[int] = None) -> Iterable['DXFTag']:
    """
    Yields comment tags from file `filename` as :class:`~ezdxf.lldxf.types.DXFTag` objects.

    Args:
        filename: filename as string
        codes: yields also additional tags with specified group codes e.g. {5, 0} to also yield handle and
               structure tags

    """
    if is_dxf_file(filename):
        info = dxf_file_info(filename)
        with open(filename, mode='rt', encoding=info.encoding) as fp:
            yield from from_stream(fp, codes=codes)
    else:
        raise IOError('File "{}" is not a DXF file.'.format(filename))
Esempio n. 8
0
def readfile(filename: str,
             legacy_mode: bool = False,
             compile_tags=True,
             is_binary_dxf=False) -> Iterable[DXFTag]:
    if is_binary_dxf:
        with open(filename, mode='rb') as fp:
            data = fp.read()
            tagger = binary_tags_loader(data)
    else:
        info = dxf_file_info(filename)
        fp = open(filename, mode='rt', encoding=info.encoding, errors='ignore')
        tagger = ascii_tags_loader(fp)

    if legacy_mode:
        tagger = tag_reorder_layer(tagger)
    if compile_tags:
        tagger = tag_compiler(iter(tagger))
    return tagger
Esempio n. 9
0
def modelspace(
    filename: Filename,
    types: Iterable[str] = None,
    errors: str = "surrogateescape",
) -> Iterable[DXFGraphic]:
    """Iterate over all modelspace entities as :class:`DXFGraphic` objects of
    a seekable file.

    Use this function to iterate "quick" over modelspace entities of a DXF file,
    filtering DXF types may speed up things if many entity types will be skipped.

    Args:
        filename: filename of a seekable DXF file
        types: DXF types like ``['LINE', '3DFACE']`` which should be returned,
            ``None`` returns all supported types.
        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:
        DXFStructureError: invalid or incomplete DXF file
        UnicodeDecodeError: if `errors` is "strict" and a decoding error occurs

    """
    info = dxf_file_info(str(filename))
    prev_code: int = -1
    prev_value: Any = ""
    entities = False
    requested_types = _requested_types(types)

    with open(filename, mode="rt", encoding=info.encoding,
              errors=errors) as fp:
        tagger = ascii_tags_loader(fp)
        queued: Optional[DXFEntity] = None
        tags: List[DXFTag] = []
        linked_entity = entity_linker()

        for tag in tag_compiler(tagger):
            code = tag.code
            value = tag.value
            if entities:
                if code == 0:
                    if len(tags) and tags[0].value in requested_types:
                        entity = factory.load(ExtendedTags(tags))
                        if (not linked_entity(entity)
                                and entity.dxf.paperspace == 0):
                            # queue one entity for collecting linked entities:
                            # VERTEX, ATTRIB
                            if queued:
                                yield queued  # type: ignore
                            queued = entity
                    tags = [tag]
                else:
                    tags.append(tag)
                if code == 0 and value == "ENDSEC":
                    if queued:
                        yield queued  # type: ignore
                    return
                continue  # if entities - nothing else matters
            elif code == 2 and prev_code == 0 and prev_value == "SECTION":
                entities = value == "ENTITIES"

            prev_code = code
            prev_value = value