Ejemplo n.º 1
0
    def __setattr__(self, key: str, value: Any) -> None:
        """ Set DXF attribute `key` to `value`.

        Raises:
            DXFAttributeError: attribute `key` is not supported

        """
        def check(value):
            value = cast_value(attrib_def.code, value)
            if not attrib_def.is_valid_value(value):
                if attrib_def.fixer:
                    value = attrib_def.fixer(value)
                    logger.debug(f'Fixed invalid attribute "{key}" in entity'
                                 f' {str(self._entity)} to "{str(value)}".')
                else:
                    raise DXFValueError(
                        f'Invalid value {str(value)} for attribute "{key}" in '
                        f'entity {str(self._entity)}.')
            return value

        attrib_def: Optional['DXFAttr'] = self.dxfattribs.get(key)
        if attrib_def:
            if attrib_def.xtype == XType.callback:
                attrib_def.set_callback_value(self._entity, value)
            else:
                self.__dict__[key] = check(value)
        else:
            raise DXFAttributeError(
                ERR_INVALID_DXF_ATTRIB.format(key, self.dxftype))

        if key in SETTER_EVENTS:
            handler = getattr(self._entity, SETTER_EVENTS[key], None)
            if handler:
                handler(value)
Ejemplo n.º 2
0
    def _export_dxf_attribute_optional(self, tagwriter: 'TagWriter',
                                       name: str) -> None:
        """
        Exports DXF attribute `name` by `tagwriter`. Optional tags are only written if different to default value.

        Args:
            tagwriter: tag writer object
            name: DXF attribute name

        """
        export_dxf_version = tagwriter.dxfversion
        not_force_optional = not tagwriter.force_optional
        attrib = self.dxfattribs.get(name, None)

        if attrib:
            optional = attrib.optional
            default = attrib.default
            value = self.get(name, None)
            if value is None and not optional:  # force default value e.g. layer
                value = default  # default value could be None

            if (value is not None) and (export_dxf_version >= attrib.dxfversion
                                        ):  # do not export None
                # check optional value == default value
                if optional and not_force_optional and default is not None and (
                        default == value):
                    return  # do not write explicit optional attribs if equal to default value
                # just use x, y for 2d points if value is a 3d point (Vector, tuple)
                if attrib.xtype == XType.point2d and len(value) > 2:
                    value = value[:2]
                tag = dxftag(attrib.code, value)
                tagwriter.write_tag(tag)
        else:
            raise DXFAttributeError(
                ERR_INVALID_DXF_ATTRIB.format(name, self.dxftype))
Ejemplo n.º 3
0
 def _update_owner_handles(self) -> None:
     if self._drawing.dxfversion <= 'AC1009':
         return  # no owner handles
     owner_handle = self._table_header.get_handle()
     for entry in iter(self):
         if not entry.supports_dxf_attrib('owner'):
             raise DXFAttributeError(repr(entry))
         entry.dxf.owner = owner_handle
Ejemplo n.º 4
0
    def get_frozen_layer_entities(self) -> Iterable['Layer']:
        """
        Returns generator of Layer() objects.

        """
        if self.drawing is None:
            raise DXFAttributeError("'drawing attribute is None, can not build DXF entities.")
        wrapper = self.dxffactory.wrap_handle
        return (wrapper(handle) for handle in self.get_frozen_layer_handles())
Ejemplo n.º 5
0
    def __delattr__(self, key: str) -> None:
        """ Delete DXF attribute `key`.

        Raises:
            DXFAttributeError: attribute `key` does not exist

        """
        if self.hasattr(key):
            del self.__dict__[key]
        else:
            raise DXFAttributeError(ERR_DXF_ATTRIB_NOT_EXITS.format(key))
Ejemplo n.º 6
0
 def __getattr__(self, key: str) -> Any:
     """ called if key does not exist, returns default value or None for unset default values
     """
     attrib_def = self.dxfattribs.get(key, None)  # type: DXFAttr
     if attrib_def:
         if attrib_def.xtype == XType.callback:
             return attrib_def.get_callback_value(self._entity)
         else:
             return attrib_def.default  # returns None for attributes without default value
     else:
         raise DXFAttributeError(ERR_INVALID_DXF_ATTRIB.format(key, self.dxftype))
Ejemplo n.º 7
0
 def get(self, key: str, default: Any = None) -> Any:
     """ Returns given `default` value not DXF default value for unset attributes. """
     # callback values should not exist as attribute in __dict__
     if self.hasattr(key):
         # do not return the DXF default value
         return self.__dict__[key]
     attrib_def = self.dxfattribs.get(key, None)  # type: DXFAttr
     if attrib_def:
         if attrib_def.xtype == XType.callback:
             return attrib_def.get_callback_value(self._entity)
         else:
             return default  # return give default
     else:
         raise DXFAttributeError(ERR_INVALID_DXF_ATTRIB.format(key, self.dxftype))
Ejemplo n.º 8
0
    def __setattr__(self, key: str, value: Any) -> None:
        attrib_def = self.dxfattribs.get(key, None)  # type: DXFAttr
        if attrib_def:
            if attrib_def.xtype == XType.callback:
                attrib_def.set_callback_value(self._entity, value)
            else:
                self.__dict__[key] = cast_value(attrib_def.code, value)
        else:
            raise DXFAttributeError(ERR_INVALID_DXF_ATTRIB.format(key, self.dxftype))

        if key in SETTER_EVENTS:
            handler = getattr(self._entity, SETTER_EVENTS[key], None)
            if handler:
                handler(value)
Ejemplo n.º 9
0
    def __getattr__(self, key: str) -> Any:
        """ Called if DXF attribute `key` does not exist, returns the DXF
        default value or ``None``.

        Raises:
            DXFAttributeError: attribute `key` is not supported

        """
        attrib_def: Optional['DXFAttr'] = self.dxfattribs.get(key)
        if attrib_def:
            if attrib_def.xtype == XType.callback:
                return attrib_def.get_callback_value(self._entity)
            else:
                return attrib_def.default
        else:
            raise DXFAttributeError(
                ERR_INVALID_DXF_ATTRIB.format(key, self.dxftype))
Ejemplo n.º 10
0
    def _export_dxf_attribute_optional(self, tagwriter: 'TagWriter',
                                       name: str) -> None:
        """
        Exports DXF attribute `name` by `tagwriter`. Optional tags are only
        written if different to default value.

        Args:
            tagwriter: tag writer object
            name: DXF attribute name

        """
        export_dxf_version = tagwriter.dxfversion
        not_force_optional = not tagwriter.force_optional
        attrib: Optional['DXFAttr'] = self.dxfattribs.get(name)

        if attrib:
            optional = attrib.optional
            default = attrib.default
            value = self.get(name, None)
            # Force default value e.g. layer
            if value is None and not optional:
                # Default value could be None
                value = default

                # Do not export None values
            if ((value is not None)
                    and (export_dxf_version >= attrib.dxfversion)):
                # Do not write explicit optional attribs if equal to default
                # value
                if (optional and not_force_optional and default is not None
                        and default == value):
                    return
                    # Just export x, y for 2D points, if value is a 3D point
                if attrib.xtype == XType.point2d and len(value) > 2:
                    value = value[:2]
                if isinstance(value, str):
                    assert '\n' not in value, "line break '\\n' not allowed"
                    assert '\r' not in value, "line break '\\r' not allowed"
                tag = dxftag(attrib.code, value)
                tagwriter.write_tag(tag)
        else:
            raise DXFAttributeError(
                ERR_INVALID_DXF_ATTRIB.format(name, self.dxftype))
Ejemplo n.º 11
0
    def get(self, key: str, default: Any = None) -> Any:
        """ Returns value of DXF attribute `key` or the given `default` value
        not DXF default value for unset attributes.

        Raises:
            DXFAttributeError: attribute `key` is not supported

        """
        # callback values should not exist as attribute in __dict__
        if self.hasattr(key):
            # do not return the DXF default value
            return self.__dict__[key]
        attrib_def: Optional['DXFAttr'] = self.dxfattribs.get(key)
        if attrib_def:
            if attrib_def.xtype == XType.callback:
                return attrib_def.get_callback_value(self._entity)
            else:
                return default  # return give default
        else:
            raise DXFAttributeError(
                ERR_INVALID_DXF_ATTRIB.format(key, self.dxftype))
Ejemplo n.º 12
0
 def __getattr__(self, key):
     key = self.key(key)
     try:
         return self._tables[key]
     except KeyError:  # internal exception
         raise DXFAttributeError(key)
Ejemplo n.º 13
0
 def __delattr__(self, key: str) -> None:
     if self.hasattr(key):
         del self.__dict__[key]
     else:
         raise DXFAttributeError(ERR_DXF_ATTRIB_NOT_EXITS.format(key))
Ejemplo n.º 14
0
 def _get_dxfattr_definition(self, key: str) -> 'DXFAttr':
     try:
         return self.DXFATTRIBS[key]
     except KeyError:
         raise DXFAttributeError(key)