Esempio n. 1
0
    def resolve_color(self,
                      entity: "DXFGraphic",
                      *,
                      resolved_layer: Optional[str] = None) -> Color:
        """Resolve the rgb-color of `entity` as hex color string:
        "#RRGGBB" or "#RRGGBBAA".
        """
        if entity.dxf.hasattr("true_color"):
            # An existing true color value always overrides ACI color!
            # Do not default to BYLAYER or BYBLOCK, this ACI value is ignored!
            aci = 7
        else:
            aci = entity.dxf.color  # defaults to BYLAYER

        entity_layer = resolved_layer or layer_key(self.resolve_layer(entity))
        layer_properties = self.layers.get(entity_layer,
                                           DEFAULT_LAYER_PROPERTIES)

        if aci == const.BYLAYER:
            color = layer_properties.get_entity_color_from_layer(
                self.current_layout_properties.default_color)
        elif aci == const.BYBLOCK:
            if not self.inside_block_reference:
                color = self.current_layout_properties.default_color
            else:
                color = self.current_block_reference_properties.color  # type: ignore
        else:  # BYOBJECT
            color = self._true_entity_color(entity.rgb, aci)
        alpha = self._entity_alpha_str(entity.dxf.get("transparency"),
                                       layer_properties.color)
        return color[:7] + alpha
Esempio n. 2
0
    def add_layer(self, layer: "Layer") -> None:
        """Setup layer properties."""
        properties = LayerProperties()
        name = layer_key(layer.dxf.name)
        # Store real layer name (mixed case):
        properties.layer = layer.dxf.name
        properties.color = self._true_layer_color(layer)

        # set layer transparency
        alpha = transparency_to_alpha(layer.transparency)
        if alpha < 255:
            properties.color = set_color_alpha(properties.color, alpha)

        # Depend layer ACI color from layout background color?
        # True color overrides ACI color and layers with only true color set
        # have default ACI color 7!
        if not layer.has_dxf_attrib("true_color"):
            properties.has_aci_color_7 = layer.dxf.color == 7

        # Normalize linetype names to UPPERCASE:
        properties.linetype_name = str(layer.dxf.linetype).upper()
        properties.linetype_pattern = self.line_pattern.get(
            properties.linetype_name, CONTINUOUS_PATTERN)
        properties.lineweight = self._true_layer_lineweight(
            layer.dxf.lineweight)
        properties.is_visible = layer.is_on() and not layer.is_frozen()
        if self.export_mode:
            properties.is_visible &= bool(layer.dxf.plot)
        self.layers[name] = properties
Esempio n. 3
0
    def set_layers_state(self, layers: Set[str], state=True):
        """Set layer state of `layers` to on/off.

        Args:
             layers: set of layer names
             state: `True` turn this `layers` on and others off,
                    `False` turn this `layers` off and others on
        """
        layers = {layer_key(name) for name in layers}
        for name, layer in self.layers.items():
            if name in layers:
                layer.is_visible = state
            else:
                layer.is_visible = not state
Esempio n. 4
0
 def resolve_aci_color(self, aci: int, resolved_layer: str) -> Color:
     """Resolve the `aci` color as hex color string: "#RRGGBB" """
     if aci == const.BYLAYER:
         layer = self.layers.get(layer_key(resolved_layer),
                                 DEFAULT_LAYER_PROPERTIES)
         color = layer.get_entity_color_from_layer(
             self.current_layout_properties.default_color)
     elif aci == const.BYBLOCK:
         if not self.inside_block_reference:
             color = self.current_layout_properties.default_color
         else:
             color = self.current_block_reference_properties.color  # type: ignore
     else:  # BYOBJECT
         color = self._true_entity_color(None, aci)
     return color
Esempio n. 5
0
 def resolve_all(self, entity: "DXFGraphic") -> Properties:
     """Resolve all properties of `entity`."""
     p = Properties()
     p.layer = self.resolve_layer(entity)
     resolved_layer = layer_key(p.layer)
     p.units = self.resolve_units()
     p.color = self.resolve_color(entity, resolved_layer=resolved_layer)
     p.linetype_name, p.linetype_pattern = self.resolve_linetype(
         entity, resolved_layer=resolved_layer)
     p.lineweight = self.resolve_lineweight(entity,
                                            resolved_layer=resolved_layer)
     p.linetype_scale = self.resolve_linetype_scale(entity)
     p.is_visible = self.resolve_visible(entity,
                                         resolved_layer=resolved_layer)
     if entity.is_supported_dxf_attrib("style"):
         p.font = self.resolve_font(entity)
     if isinstance(entity, DXFPolygon):
         p.filling = self.resolve_filling(entity)
     return p
Esempio n. 6
0
    def resolve_visible(self,
                        entity: "DXFGraphic",
                        *,
                        resolved_layer: Optional[str] = None) -> bool:
        """Resolve the visibility state of `entity`. Returns ``True`` if
        `entity` is visible.
        """
        if isinstance(entity, Insert):
            # depends only on the invisible flag, the layer state has no effect!
            return not bool(entity.dxf.invisible)
        elif isinstance(entity, Face3d):
            return any(entity.get_edges_visibility())

        entity_layer = resolved_layer or layer_key(self.resolve_layer(entity))
        layer_properties = self.layers.get(entity_layer)
        if layer_properties and not layer_properties.is_visible:
            return False
        elif isinstance(entity, Attrib):
            return not bool(entity.dxf.invisible) and not entity.is_invisible
        else:
            return not bool(entity.dxf.invisible)
Esempio n. 7
0
    def resolve_linetype(
            self,
            entity: "DXFGraphic",
            *,
            resolved_layer: str = None) -> Tuple[str, Sequence[float]]:
        """Resolve the linetype of `entity`. Returns a tuple of the linetype
        name as upper-case string and the simplified linetype pattern as tuple
        of floats.
        """
        aci = entity.dxf.color
        # Not sure if plotstyle table overrides actual entity setting?
        if (0 < aci < 256
            ) and self.plot_styles[aci].linetype != acadctb.OBJECT_LINETYPE:
            # todo: return special line types - overriding linetypes by
            #  plotstyle table
            pass
        name = entity.dxf.linetype.upper()  # default is 'BYLAYER'
        if name == "BYLAYER":
            entity_layer = resolved_layer or layer_key(
                self.resolve_layer(entity))
            layer = self.layers.get(entity_layer, DEFAULT_LAYER_PROPERTIES)
            name = layer.linetype_name
            pattern = layer.linetype_pattern

        elif name == "BYBLOCK":
            if self.inside_block_reference:
                name = self.current_block_reference_properties.linetype_name  # type: ignore
                pattern = (
                    self.current_block_reference_properties.
                    linetype_pattern  # type: ignore
                )
            else:
                # There is no default layout linetype
                name = "STANDARD"
                pattern = CONTINUOUS_PATTERN
        else:
            pattern = self.line_pattern.get(name, CONTINUOUS_PATTERN)
        return name, pattern
Esempio n. 8
0
        def lineweight():
            aci = entity.dxf.color
            # Not sure if plotstyle table overrides actual entity setting?
            if (0 < aci < 256) and self.plot_styles[
                    aci].lineweight != acadctb.OBJECT_LINEWEIGHT:
                # overriding lineweight by plotstyle table
                return self.plot_styles.get_lineweight(aci)
            lineweight = entity.dxf.lineweight  # default is BYLAYER
            if lineweight == const.LINEWEIGHT_BYLAYER:
                entity_layer = resolved_layer or layer_key(
                    self.resolve_layer(entity))
                return self.layers.get(entity_layer,
                                       DEFAULT_LAYER_PROPERTIES).lineweight

            elif lineweight == const.LINEWEIGHT_BYBLOCK:
                if self.inside_block_reference:
                    return self.current_block_reference_properties.lineweight
                else:
                    # There is no default layout lineweight
                    return self.default_lineweight()
            elif lineweight == const.LINEWEIGHT_DEFAULT:
                return self.default_lineweight()
            else:
                return float(lineweight) / 100.0