Beispiel #1
0
    def write(self, stream: Union[TextIO, BinaryIO], fmt: str = 'asc') -> None:
        """
        Write drawing as ASCII DXF to a text stream or as Binary DXF to a binary stream.
        For DXF R2004 (AC1018) and prior open stream with drawing :attr:`encoding` and :code:`mode='wt'`.
        For DXF R2007 (AC1021) and later use :code:`encoding='utf-8'`, or better use the later added :class:`Drawing`
        property :attr:`output_encoding` which returns the correct encoding automatically.

        Args:
            stream: output text stream or binary stream
            fmt: ``'asc'`` for ASCII DXF (default) or ``'bin'`` for binary DXF
        """
        dxfversion = self.dxfversion
        if dxfversion == DXF12:
            handles = bool(self.header.get('$HANDLING', 0))
        else:
            handles = True
        if dxfversion > DXF12:
            self.classes.add_required_classes(dxfversion)

        self._create_appids()
        self._update_header_vars()
        self._update_metadata()

        if fmt.startswith('asc'):
            tagwriter = TagWriter(stream, write_handles=handles, dxfversion=dxfversion)
        elif fmt.startswith('bin'):
            tagwriter = BinaryTagWriter(
                stream, write_handles=handles, dxfversion=dxfversion, encoding=self.output_encoding,
            )
            tagwriter.write_signature()
        else:
            raise ValueError(f"Unknown output format: '{fmt}'.")

        self.export_sections(tagwriter)
Beispiel #2
0
    def write(self, stream: Union[TextIO, BinaryIO], fmt: str = "asc") -> None:
        """Write drawing as ASCII DXF to a text stream or as Binary DXF to a
        binary stream. For DXF R2004 (AC1018) and prior open stream with
        drawing :attr:`encoding` and :code:`mode='wt'`. For DXF R2007 (AC1021)
        and later use :code:`encoding='utf-8'`, or better use the later added
        :class:`Drawing` property :attr:`output_encoding` which returns the
        correct encoding automatically. The correct and required error handler
        is :code:`errors='dxfreplace'`!

        If writing to a :class:`StringIO` stream, use :meth:`Drawing.encode` to
        encode the result string from :meth:`StringIO.get_value`::

            binary = doc.encode(stream.get_value())

        Args:
            stream: output text stream or binary stream
            fmt: ``'asc'`` for ASCII DXF (default) or ``'bin'`` for binary DXF

        """
        dxfversion = self.dxfversion
        if dxfversion == DXF12:
            handles = bool(self.header.get("$HANDLING", 0))
        else:
            handles = True
        if dxfversion > DXF12:
            self.classes.add_required_classes(dxfversion)

        self._create_appids()
        self._update_header_vars()
        self.update_extents()
        self.update_limits()
        self._update_metadata()

        if fmt.startswith("asc"):
            tagwriter = TagWriter(
                stream,  # type: ignore
                write_handles=handles,
                dxfversion=dxfversion,
            )
        elif fmt.startswith("bin"):
            tagwriter = BinaryTagWriter(  # type: ignore
                stream,  # type: ignore
                write_handles=handles,
                dxfversion=dxfversion,
                encoding=self.output_encoding,
            )
            tagwriter.write_signature()  # type: ignore
        else:
            raise ValueError(f"Unknown output format: '{fmt}'.")

        self.export_sections(tagwriter)